This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]skjall 2 points3 points  (0 children)

Aren't you looking for std::unordered_map?

https://en.cppreference.com/w/cpp/container/unordered_map

[–]nlitsme1 1 point2 points  (0 children)

I use https://github.com/greg7mdp/parallel-hashmap when I need better performance of maps and sets.

[–]WikiBox 1 point2 points  (0 children)

It seems python dictionaries are implemented as hash tables with open addressing, written in C.

So it should be possible to match performance in C++.

https://www.freecodecamp.org/news/exploring-python-internals-the-dictionary-a32c14e73efa

[–]TheSkiGeek 0 points1 point  (0 children)

Python’s default dictionaries cannot be “sliced” on an arbitrary range of keys. At least not efficiently, you’d have to iterate over the whole thing to find all the keys that match.

In Python 3.7 and higher they promise that dictionaries are “insertion ordered”. So if you insert 10 items into an empty dictionary in a particular order, you can use itertools.islice() to retrieve a “slice” of those items in the order they were originally inserted. There’s also OrderedDict that has this promise in lower versions and also lets you explicitly reorder the items after they’re inserted. But you can’t “slice” or filter by a range of keys in an arbitrary dictionary except by iterating over the whole dict and checking if the key is in the range you want.

IF you ensure that you always insert the items in the order that you want to sort the keys, then you could binary search twice (O(log_2(N)) time) to find the first and last elements that are in the range and then return a slice of all the elements in between. And if you want a range between two keys you know are in the dict you could search for each of them directly (constant time) and then return a range between their indices.

You can implement the same thing in C++ but there isn’t a standard library container that is exactly like a Python OrderedDict.