Why is std::map implemented as a red-black tree?

Probably the two most common self balancing tree algorithms are Red-Black trees and AVL trees. To balance the tree after an insertion/update both algorithms use the notion of rotations where the nodes of the tree are rotated to perform the re-balancing. While in both algorithms the insert/delete operations are O(log n), in the case of … Read more

Create a list with initial capacity in Python

Warning: This answer is contested. See comments. def doAppend( size=10000 ): result = [] for i in range(size): message= “some unique object %d” % ( i, ) result.append(message) return result def doAllocate( size=10000 ): result=size*[None] for i in range(size): message= “some unique object %d” % ( i, ) result[i]= message return result Results. (evaluate each … Read more

Literal notation for Dictionary in C#?

You use the collection initializer syntax, but you still need to make a new Dictionary<string, string> object first as the shortcut syntax is translated to a bunch of Add() calls (like your code): var data = new Dictionary<string, string> { { “test”, “val” }, { “test2”, “val2” } }; In C# 6, you now have … Read more

In Python, how do I iterate over a dictionary in sorted key order?

Haven’t tested this very extensively, but works in Python 2.5.2. >>> d = {“x”:2, “h”:15, “a”:2222} >>> it = iter(sorted(d.iteritems())) >>> it.next() (‘a’, 2222) >>> it.next() (‘h’, 15) >>> it.next() (‘x’, 2) >>> If you are used to doing for key, value in d.iteritems(): … instead of iterators, this will still work with the solution … Read more

How to convert List to Map in Kotlin?

You have two choices: The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map: val map = friends.associateBy({it.facebookId}, {it.points}) The second, less performant, is to use the standard map function to create a list of Pair which can … Read more

What is the best way to implement nested dictionaries?

What is the best way to implement nested dictionaries in Python? This is a bad idea, don’t do it. Instead, use a regular dictionary and use dict.setdefault where apropos, so when keys are missing under normal usage you get the expected KeyError. If you insist on getting this behavior, here’s how to shoot yourself in … Read more

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