-
Notifications
You must be signed in to change notification settings - Fork 0
Map
Sven Bieg edited this page Feb 26, 2023
·
21 revisions
The map is an Index with key-value-pairs.
Items can be added, removed and looked-up in constant low time.
It is comparable to the map-class in the standard-library.
Clusters::map<_key_t, _value_t, _size_t=uint32_t, _group_size=10>
To create a map You need to specify the key- and the value-type.
With standard-settings Your map has 32 bit and a group-size of 10.
Clusters::map<int, int> map; // Integer-map with integer-values
Clusters::map<std::string, std::string> index; // String-map with string-values
Clusters::map<int, int, uint64_t, 100> index; // Integer-map with integer-values, 64 bit and a group-size of 100
You can access items by position or by look-up:
auto item=map.get_at(0); // Returns the item at position 0
bool found=map.try_get(0, &i); // Returns if the key exists and copies the value if present
bool found=map.contains(5); // Returns if the map contains an item with a key of 5
auto it=map.find(5); // Returns an iterator at the item with a key of 5 if present
auto it=map.find(5, find_func::below_or_equal); // Returns an iterator at the item with a key of 5 or below
auto count=map.get_count(); // Returns the number of items in the map
When adding an item it gets inserted at the right position automatically.
You can overwrite an existing item by using the set-function:
bool added=map.add(5, 0); // True
added=map.add(6, 0); // True
added=map.add(5, 1); // False - the item exists already
map.set(5, 0); // Overwrites the item or adds it if doesn't exist
map[5]=0; // Same as above
bool removed=map.remove(5); // Remove the item with a key of 5 (true)
removed=map.remove(4); // False - the item didn't exist
removed=map.remove(5, &item); // Remove the item with a key of 5 and return item
map.remove_at(0); // Remove item at position 0
map.clear(); // Remove all items
for(auto &item: map)
std::cout<<item.get_key()<<": "<<item.get_value()<<"\n"; // Prints every item to screen in correct order
for(auto it=map.rbegin(); it.has_current(); it.move_previous())
std::cout<<it->get_key()": "<<it->get_value()<<"\n"; // Prints items to the screen in correct reverse order
for(auto it=map.begin(); it.has_current(); )
{
if(it->get_key()%2==0)
{
it.remove_current(); // Remove every even number
continue;
}
it.move_next();
}
This is a comparison to the map-class in the standard-library.
I added 10'000'000 items in a release-build with full optimization:
int count=10'000'000;
Clusters::map<int, int> clusters_map;
for(int i=0; i<count; i++)
clusters_map.add(i, i);
std::map<int, int> std_map;
for(int i=0; i<count; i++)
std_map.insert(std::pair<int, int>(i, i));
The map has been 5 times faster and needed only about 40% memory!
Copyright 2024, Sven Bieg (svenbieg@web.de)