Tuesday, September 18, 2007

Links for 2007-09-18

Blog:

  1. Beautiful Code: Leading Programmers Explain How They Think: A list of pointers to the information about the authors of Beautiful Code. It's kind of who's who for me.
Sample Chapter:
  1. Hacker's Delight

Count Bits in Words

How do we count the number of 1-bit in a 32-bit unsigned integer, x? A naive approach is like this.
pop = 0;
for (i = 0; i < pop =" pop" x =" x">> 1;
}
Can we do better? Yes, according to Henry S. Warren, Jr., who wrote "The Quest for an Accelerated Population Count" of Beautiful Code. There are many ways to improve the loop. For example, if x is a small number, then the loop can be improved as followed.
pop = 0;
while (x) {
pop = pop + (x & 1);
x = x >> 1;
}
The following is looped as many times as the number of 1-bit in x.
pop = 0;
while (x) {
pop = pop + 1;
x = x & (x - 1); //turn off the rightmost bit
}
Or we can trade space for time. Look up the number of 1-bit in a table.
static char table[256] = {0, 1, 1, 2, 1, 2, 2, 3, ...,
pop = table[x & 0xFF] + table[(x >> 8) & 0xFF] + table[(x >> 16) & 0xFF] + table[x>> 24];
And several non-trivial approaches are discussed in the article. The author further applied the 'population count' functions to other problems, such as comparing population counts of two words, calculating Hamming distance, computing the number of trailing 0s in a word and allowing fast direct indexed access to a sparse array. Someone even posted performance analysis of the population count functions on the Internet. It seems Google would ask this kind of questions in an interview. Though I am not sure where I can apply the population count functions in my projects, Mr. Warren's article is definitely very informative.

Sunday, September 16, 2007

Links for 2007-09-16

Article:
  1. Cross-platform graphics with cairo - As the title suggested, Cairo is a free software vector darwing library suitable for cross-platform drawings. Thought written in C, it has bindings for other languages, such as C++, Ruby, Python, and .Net/mono, among others. Cairo looks straightforwards and the current release version is 1.4.10.
  2. Guide to Oracle Berkeley DB for SQL Developers - The author took a step-by-step approach to show you how you would create a database, insert/update/delete/query data in Berkeley DB, in contract with how you would do the same things in SQL.

Monday, September 10, 2007

Links for 2007-09-10

Sample Chapter:
  1. How I Became a Quant
    • Chapter 1 - David Leinweber - The author talked about how he started in the military industry in 70's and entered into the finance industry in the 80's. AI played an important role for him to make the switch. Thought he didn't say much about how he applied AI to the stock market, it's a fun read.
Article:
  1. A Little AI Goes a Long Way on Wall Street
    • Written by David Leinweber and Yossi Beinart and published in 1994.
    • According to the article, their product, MarketMind seems to be a rule-based system. It monitored the market according to the rules users specified and alerted users when conditions match. A rule editing tool was provided as well. Another system, Quantex, was integrated with MarketMind to provide automatic order generation and execution capability.
    • The article read like a white paper, talking what their systems did, but not how. However, 2 techniques were mentioned, 'Rete matching' and 'Earley Algorithm'. I think I need to dig deeper.

Sunday, September 9, 2007

Links for 2007-09-09

Article:
  1. Driven to ... Discovering Your Design Values(via this blog) - A good start to learn what RDD, TDD, BDD, DbC, DDD, and MDD is. According to the author, 'whenther "driven" or not, these approaches all emphasize a core set of values and principles ... A thoughtful designer should be able to pick and choose among practices without losing their essence.' Well said.
Blog:
  1. News from the Front - Paul has his point, but I don't agree. What a good degree can give you besides knowledge? I think it's the opportunity. In Paul's shoes, he might not care about people's education. If Paul were in the applicants' position, trying to raise funds from those 'angels', Paul might wish he has a good degree. The angels who don't know Paul but know the university he graduated from would give him the fund he needs.

Thursday, September 6, 2007

Links for 2007-09-06

Articles:
  1. Implementing a Publisher/Subscriber model for .NET Remoting
    • I adopted the method in the article to handcraft redundancy capability in a risk-monitoring system I developed.
  2. Memcached (Distributed Cache) ASP.net Provider
    • Recently, I came across this article and hoped Memcached would be a better solution for my system. But according to this introduction, this tool seems to be suitable is for load balance, not for redundancy. Still, it's good to know.
    • A windows implemention is available as well.
  3. ASP.NET Charting with NPlot
Blog:
  1. Calling a Windows Service from ASP.NET via Remoting & IpcChannel
    • Other than IpcChannel is not available in .Net Framework 1.1, the idea looks good.

Monday, September 3, 2007

DataRow.BeginEdit Method

This is the sample code copied from MSDN.

1 private void DemonstrateRowBeginEdit()

2 {

3 DataTable table = new DataTable("table1");

4 DataColumn column = new

5 DataColumn("col1",Type.GetType("System.Int32"));

6 table.RowChanged+=new

7 DataRowChangeEventHandler(Row_Changed);

8 table.Columns.Add(column);

9

10 // Add a UniqueConstraint to the table.

11 table.Constraints.Add(new UniqueConstraint(column));

12

13 // Add five rows.

14 DataRow newRow;

15

16 for(int i = 0;i<5; i++)

17 {

18 // RowChanged event will occur for every addition.

19 newRow= table.NewRow();

20 newRow[0]= i;

21 table.Rows.Add(newRow);

22 }

23 // AcceptChanges.

24 table.AcceptChanges();

25

26 // Invoke BeginEdit on each.

27 Console.WriteLine(

28 "\n Begin Edit and print original and proposed values \n");

29 foreach(DataRow row in table.Rows)

30 {

31

32 row.BeginEdit();

33 row[0]=(int) row[0]+10;

34 Console.Write("\table Original \table" +

35 row[0, DataRowVersion.Original]);

36 Console.Write("\table Proposed \table" +

37 row[0,DataRowVersion.Proposed] + "\n");

38 }

39 Console.WriteLine("\n");

40 // Accept changes

41 table.AcceptChanges();

42 // Change two rows to identical values after invoking BeginEdit.

43 table.Rows[0].BeginEdit();

44 table.Rows[1].BeginEdit();

45 table.Rows[0][0]= 100;

46 table.Rows[1][0]=100;

47 try

48 {

49 /* Now invoke EndEdit. This will cause the UniqueConstraint

50 to be enforced.*/

51 table.Rows[0].EndEdit();

52 table.Rows[1].EndEdit();

53 }

54 catch(Exception e)

55 {

56 // Process exception and return.

57 Console.WriteLine("Exception of type {0} occurred.",

58 e.GetType());

59 }

60 }

61

62 private void Row_Changed(object sender,

63 System.Data.DataRowChangeEventArgs e)

64 {

65 DataTable table = (DataTable) sender;

66 Console.WriteLine("RowChanged " + e.Action.ToString()

67 + "\table" + e.Row.ItemArray[0]);

68 }

The followings are the event sequences I have observed when executing DemonstrateRowBeginEdit method.
  • When line 21 is executed, for each row added, 1 RowChanged event is fired and 'Change' is passed as DataRowChangeEventArgs.Action argument.
  • When line 24 is executed, for each row added, 1 RowChanged event is fired and 'Commit' is passed as DataRowChangeEventArgs.Action argument.
  • When line 41 is executed, for each row modified, 2 RowChanged events are fired and 'Change' and 'Commit' are passed as DataRowChangeEventArgs.Action argument separately.
  • When line 51 is executed, 1 RowChanged event is fired and 'Change' is passed as DataRowChangeEventArgs.Action argument.
  • When line 52 is executed, System.Data.ConstraintException is thrown.
Some other behaviors are observed after the code is modified.
  • If line 32 is commented out, System.Data.VersionNotFoundException is thrown at line 36 for 'There is no Proposed data to access'.
  • If line 32 and line 36 are commented out, when line 33 is executed, 1 RowChanged event is fired and 'Change' is passed as DataRowChangeEventArgs.Action argument. And when line 41 is executed, for each row modified, 1 RowChanged event is fired and 'Commit' is passed as DataRowChangeEventArgs.Action argument.
  • If "Console.WriteLine(table.Rows[0][0]);" is inserted before line 51, default version of the data is accessed(via Reflector) and "100" is printed.
  • If "Console.WriteLine(table.Rows[0][0, DataRowVersion.Current]);" is inserted before line 51, "10" is printed.
  • If "Console.WriteLine(table.Rows[0][0, DataRowVersion.Current]);" is inserted after line 51, "100" is printed.
  • If "Console.WriteLine(table.Rows[0][0, DataRowVersion.Original]);" is inserted after line 51, "10" is printed.
  • If "table.Rows[0].AcceptChanges(); Console.WriteLine(table.Rows[0][0, DataRowVersion.Original]);" is inserted after line 51, "100" is printed.
The experiment confirms the following DataRow behaviors:
  1. DataRow.BeginEdit method temporarily suspends events.
  2. DataRow.EndEdit method invokes 1 RowChanged event.
  3. DataTable.AcceptChanges method invokes 2 RowChanged events for each row modified.
  4. The default version of DataRow is updated after DataRow.EndEdit method is called.
  5. The original version of DataRow is updated after DataRow.AcceptChanges method is called.
So why should I care about BeginEdit method? Without calling BeginEdit method, for each column in a row I modify, one RowChanged event is fired. If 10 columns per row are modified, 10 RowChanged events are fired for every modified row. If the DataTable is updated by a background thread, and is attached to a data-bound control, say, DataGridView, which is controlled by a UI thread. Those updates would cause too many events fired and might result in some weird behaviors of that data-bound control. DataRow.BeginEdit method allows us to suspend events temporarily. After completing modification, we call EndEdit or AcceptChanges method to notify that data-bound control to update changes.

Note: To update a data object bounded to a UI control from a background thread, be sure to read the article first.