Friday, December 26, 2008

C# Parallel Foreach using Thread Pool



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();
}
}

Saturday, December 20, 2008

C#: Evaluating Infix Expression

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.

Saturday, December 6, 2008

WebKit: Dynamic Content

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.