The file map.h implements a container similar to std::map.
The main difference is that std::map (very likely) implements a red-black tree which guarantees logarithmic complexity for search, insertion and deletion.
The one implemented here implements a treap which has expected logarithmic complexity for search, insertion and deletion but in the worst case the complexity is linear.
In addition, this implementation uses operators < and == to compare keys. In contrast, std::map allows the usage of other function objects (the default is std::less) for comparisons and it does not use ==. Indeed, for std::map equality (or more correctly, equivalence) of x and y is expressed by
!comp(x, y) && !comp(y, x)
where comp is the comparison object. Notice that this implies two calls to comp::operator() which can be wasteful.
The implementation is incomplete (I spent just two days on it) and is not meant for production code. This is a simple exercise that I assigned to myself.
The file map.h is the implementation. The file test.cpp is the unit test (based on the assert macro, sorry!).
Compile the test with
g++ --std=c++11 test.cpp -o test -Wall -Wextra -pedantic
- Implement missing methods. The idea is providing almost the interface of std::map.
- Add support for allocators.
Copyright (C) 2014 Cassio Neri Moreira
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.