Jul 21
In the spirit of the Nerd ABCs, I would like to present the ABCs of programming languages:
There are far too many programming languages which start with A, P, and S and not enough which start with G, K, N, U, W, X, Y, or Z.
Thanks to Dave, Keith, Sid, Andrew, and Adam.
Jul 20
Let’s say you have a string and you want to test if it contains the substrings foo, bar, and baz in any order. To do this using regular expressions, you have the following options:
- Test for
/foo/, /bar/, and /baz/ independently and serially in your code. This is the easiest solution, but it is kind of cheating.
- Write a single regular expression that has all possible orderings of the substrings, i.e.
/foo.*bar.*baz|foo.*baz.*bar|bar.*foo.*baz|.../. This doesn’t scale, as you have to write n! combinations.
- Use lookahead assertions, i.e.
/(?=.*foo)(?=.*bar)(?=.*baz)/. This may not be supported by all regular expression engines, and you may run into issues of overlap.
Jul 19
I was intending to revisit and possibly rewrite my Win32 LD_PRELOAD application when I discovered Microsoft Research has written something which does far, far more: Detours.
Jul 17
I was about to write a tutorial on how to create a custom Win32 control in C when I found that someone had beat me to it: Creating Custom Controls From Scratch. Overall it is quite good, and I learned a few new things myself. (By the way, the entire Catch22 Tutorials site is filled with fascinating material for Win32 developers.)
One thing to note: If you separate your custom control into a DLL, you will need to create an initialization function which registers the window classes (similar to InitCommonControls()). Be sure to pass the CS_GLOBALCLASS flag to RegisterClass() to register it as application global. If you don’t, you will only be able to instantiate your control manually by passing the DLL’s HMODULE as a parameter to CreateWindow().
By the way, MFC seems to support customizing controls primarily through window subclassing, which is a related but slightly different mechanism.
Jul 12
Let’s say you have two STL std::maps with identical types, and you want to copy all the elements from one to the other. The easiest way to do this is to use map::insert():
typedef std::map<key_type, value_type> map_t;
map_t map1;
map_t map2;
// Copy all elements from map1 to map2
map2.insert(map1.begin(), map1.end());
Alternatively, you could use the STL std::copy algorithm:
// Copy all elements from map1 to map2.
std::copy(map1.begin(), map1.end(), std::inserter(map2, map2.begin()));
Both methods’ performance should be an amortized O(n) because they insert records in sorted order and use the hinting form of map::insert.
Note that because both methods ultimately call map::insert they will not overwrite a preexisting key’s associated value. In other words, if map1 has the value V1 associated with key K and map2 the value V2 associated with the same key K, V2 will remain in map2 after the copy operation.
Let’s say you want to perform the copy but have map1’s values overwrite map2’s for identical keys. The first way to solve this problem that entered my mind was to write my own OutputIterator which performs an overwriting assignment and pass it to std::copy. However, there’s a far simpler approach. You can copy map2’s values into map1, relying on the fact that map2’s values won’t overwrite map1’s, and then swap the results:
map1.insert(map2.begin(), map2.end());
map2.swap(map1);
Thanks, Sam, for helping me figure all this out.
Jul 10
In the near future developers will have to focus almost exclusively on concurrency to tease out further performance gains. Given this, here is some interesting material on the topic:
Jul 04
On Saturday, July 1, the Wall Street Journal ran the article Will You Be Able To Retire? which tries to answer the question “Is my retirement savings on the right track, or not?” The most interesting part of the article is a graph which provides savings and debt targets based on age, reproduced here:
In some cases, the debt targets appear overly optimistic. Using their figure of a $100,000/year salary, this means that one’s total debt should be around $200,000 at age 30. Considering that in April, 2006 the median price of a house in Orange County was $628,0001, I don’t see too many young, recent home buyers from that area hitting that target.
Regardless, this graph gives a useful metric to see whether you are on track for saving for retirement.
Recent Comments