Why should hash functions use a prime number modulus?

Usually a simple hash function works by taking the “component parts” of the input (characters in the case of a string), and multiplying them by the powers of some constant, and adding them together in some integer type. So for example a typical (although not especially good) hash of a string might be: (first char) … Read more

Preserving order with LINQ

I examined the methods of System.Linq.Enumerable, discarding any that returned non-IEnumerable results. I checked the remarks of each to determine how the order of the result would differ from order of the source. Preserves Order Absolutely. You can map a source element by index to a result element AsEnumerable Cast Concat Select ToArray ToList Preserves … Read more

How are Python’s Built In Dictionaries Implemented?

Here is everything about Python dicts that I was able to put together (probably more than anyone would like to know; but the answer is comprehensive). Python dictionaries are implemented as hash tables. Hash tables must allow for hash collisions i.e. even if two distinct keys have the same hash value, the table’s implementation must … Read more

JavaScript hashmap equivalent

Hash your objects yourself manually, and use the resulting strings as keys for a regular JavaScript dictionary. After all, you are in the best position to know what makes your objects unique. That’s what I do. Example: var key = function(obj){ // Some unique object-dependent key return obj.totallyUniqueEmployeeIdKey; // Just an example }; var dict … Read more

Why does the C++ STL not provide any “tree” containers?

There are two reasons you could want to use a tree: You want to mirror the problem using a tree-like structure: For this we have boost graph library Or you want a container that has tree like access characteristics For this we have std::map (and std::multimap) std::set (and std::multiset) Basically the characteristics of these two … Read more

Split List into Sublists with LINQ

Try the following code. public static List<List<T>> Split<T>(IList<T> source) { return source .Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / 3) .Select(x => x.Select(v => v.Value).ToList()) .ToList(); } The idea is to first group the elements by indexes. Dividing by three has the effect of grouping them … Read more

When should I use a List vs a LinkedList

In most cases, List<T> is more useful. LinkedList<T> will have less cost when adding/removing items in the middle of the list, whereas List<T> can only cheaply add/remove at the end of the list. LinkedList<T> is only at it’s most efficient if you are accessing sequential data (either forwards or backwards) – random access is relatively … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)