December 26, 2008 | Filed Under C# | 1 Comment
public void Foreach(T[] array, Action action)
{
int cpus = 2 * Environment.ProcessorCount;
int nitems = array.Length;
int chunk = nitems / cpus;
int counter = cpus;
using (AutoResetEvent signal = new AutoResetEvent(false))
{
for (int i = 1; i <= cpus; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
int unit = (int)o;
for (int j = (unit - 1) * chunk;
j < (unit == cpus ? nitems : unit * chunk);
j++)
{
action(array[j]);
}
if (Interlocked.Decrement(ref counter) == 0)
signal.Set();
}, i);
}
signal.WaitOne();
}
}
public void ForeachWithResults(T[] array, T1[] results, Action action)
{
int cpus = 2 * Environment.ProcessorCount;
int nitems = array.Length;
int chunk = nitems / cpus;
int counter = cpus;
using (AutoResetEvent signal = new AutoResetEvent(false))
{
for (int i = 1; i <= cpus; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object o)
{
int unit = (int)o;
for (int j = (unit - 1) * chunk;
j < (unit == cpus ? nitems : unit * chunk);
j++)
{
action(j, array[j], results);
}
if (Interlocked.Decrement(ref counter) == 0)
signal.Set();
}, i);
}
signal.WaitOne();
}
}
December 20, 2008 | Filed Under C# | 1 Comment
This morning I’ve written a simple Infix Notation Evaluator in C, and this afternoon I’ve rewritten it in C#. I Think that C# is more easy to understand than C, so I’ve posted the C# Example. (If you want the C example, ask me).
In few words, I take the Input in “Infix Notation” and I convert it in Reverse Polish Notation (RPN), and then the RPN string is evaluated.
First Example: Evaluating a simple Math Expression.
string expr = "10 + 5 - (2 + (3 * 4) + (5 * 3)) - 4 ^ 2 << 8";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());
Second Example: Evaluating a simple Math Expression with variables.
Dictionary vars = new Dictionary();
vars.Add("A", "1"); vars.Add("B", "2");
vars.Add("C", "3"); vars.Add("D", "4");
vars.Add("K", "5"); vars.Add("L", "6");
string expr = "(A + B) + C + (D * (K + (L - 1)))";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());
Third example: Evaluating simple String expression.
string expr = "'Hello' + ' ' * 3 + \"World\"";
Operation operation = new MyTokener().Parser(expr);
Console.WriteLine("{0} = {1}", expr, operation.Evaluate());
After the Example you can find the Source Code Here (You can compile it under Linux with gmcs).
That's all folks! I'm really busy with the Real Work, I'm waiting the 25th for taking a break.
December 6, 2008 | Filed Under Cocoa | No Comments
Today I’ve played a bit with WebKit. I Want do something like Cocoa VBox View and NSScrollView but with more flexibility. Below you can see the example result.

The interesting part of this example is How to manage WebKit content, and how to add something dynamically.
I use webView:decidePolicyForNavigationAction:request:frame:decisionListener: of WebPolicyDelegate Protocol to handle my custom requests, in this way…
- (void)webView:(WebView *)sender
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request frame:(WebFrame *)frame
decisionListener:(id)listener
{
if ([[[request URL] path] isEqualToString:@"PATH to Handle"]) {
[listener ignore];
// Handle Request
} else {
[listener use];
}
}
…Then with a little bit of JavaScript I’ll set my dynamic content loaded from Database or somewhere else.
NSArray *args = [NSArray arrayWithObjects:@"Item1Content",
@"Hi, I'm Test 1 Content", nil];
[[webKit windowScriptObject] callWebScriptMethod:@"fillElement"
withArguments:args];
The fillElement method is a simple JS function like this document.getElementById(elem).innerHTML = data;
Here you can find the Source Code.
December 2, 2008 | Filed Under Cocoa | 8 Comments
Yesterday, I’ve written a simple Sidebar controller that can saves you some time and trouble with Cocoa Source List. Nodes and Folders can be drag and dropped into other folders or near other items and you can easily set badges on nodes.

Cocoa Sidebar
You can find the example Source Code Here.
And below you can see hot to add folders, nodes and set badges.
- (void)populateOutlineContents:(id)inObject {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[sidebar addRootFolder:@"1" name:@"DEVICES"];
[sidebar addChild:@"1.1" name:@"Machintosh HD"
icon:[NSImage imageNamed:NSImageNameComputer]
action:@selector(buttonPres:) target:self];
[sidebar addRootFolder:@"2" name:@"PLACES"];
[sidebar addChild:@"2.1"
url:NSHomeDirectory()
action:@selector(buttonPres:) target:self];
[sidebar addRootFolder:@"3" name:@"OTHER."];
[sidebar addChild:@"3.1" name:@"Bonjour"
icon:[NSImage imageNamed:NSImageNameBonjour]
action:@selector(buttonPres:) target:self];
[sidebar addChild:@"3.2" name:@"Users"
icon:[NSImage imageNamed:NSImageNameUserGroup]
action:@selector(buttonPres:) target:self];
[sidebar clearSelection];
// Add Badge to Node with Key
[sidebar setBadge:@"2.1" count:5];
[sidebar setBadge:@"3.2" count:7];
[pool release];
}
- (void)buttonPres:(id)sender {
NSLog(@"Button Press %@", [sender nodeTitle]);
}