Aug 31, 2009

Posting Code into Blogger Posts

A concise article describes how to use SyntaxHighLighter to insert program code snippets into Blogger posts.

Bloom Filter

The Bloom filter is a space-efficient probabilistic data structure that is used to test whether an element is a member of a set. False positives are possible, but false negatives are not. Elements can be added to the set, but not removed (though this can be addressed with a counting filter). The more elements that are added to the set, the larger the probability of false positives.

Practical applicaitons of Bloom filter including fast test that whether a request could be handled by a server instance, whether a data element is in a replicate in a redundent system.

Aug 12, 2009

Be Careful with stl::accumulate

If we are to accumulate a vector of doubles, use the following code snippet:

accumulate(timbre_topic_dist.begin(), timbre_topic_dist.end(), 0.0);

You do not want to be lazy and write 0.0 as 0, which will be interpreted by the compiler as an integer, which is used to infer the type of intermediate and final result of accumulate. For your reference, here attaches one of the multiple prototypes of accumulate:
      template < typename _InputIterator, typename _Tp >
_Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) {
for (; __first != __last; ++__first)
__init = __init + *__first;
return __init;
}
Note that the partial result is stored in _Tp __init, which means even we explicitly use plus as the accumulator, the result will still be truncated.

accumulate(timbre_topic_dist.begin(), timbre_topic_dist.end(), 0, // Wrong
plus()); // No effect to correct the mistake.