template<class TKey, class TValue, class THash = CompareHash<TKey>, class TSize = SizeT>
class evo::MapHash< TKey, TValue, THash, TSize >
Map implemented as a hash table.
- Template Parameters
-
| TKey | Map key type |
| TValue | Map value type |
| THash | Hash type to use – default: CompareHash |
| TSize | Size type to use for size values (must be unsigned integer) – default: SizeT |
- Features
- Similar to STL
unordered_map, implemented with a hash table
- Values are always unique (no duplicate keys)
- Items are each stored as a Pair
- This uses PtrList internally
- No memory allocated by new empty map
- Sharing makes copying efficient
C++11:
- Range-based for loop – see STL Compatibility
MapHash<int,int> map;
for (auto& item : map.asconst()) {
item.key();
item.value();
}
- Initialization lists
MapHash<int,int> map = {{3, 300}, {1, 100}, {2, 200}};
MapHash<String,String> strmap = {{"key1", "foo"}, {"key2", "bar"}};
- Move semantics
- Comparison/Hash
This type is used for both hashing and comparing keys. You can leave the default comparison/hash type (CompareHash) or specify an alternative.
See: Primitives & Containers
- Iterators
Caution: Modifying or resizing a map will shift or invalidate existing iterators using it.
- Constructor
- Read Access
- Modifiers
- Helpers
- Example
This example works with any Map type.
Example using a map of numbers
int main() {
MyMap map;
map[1] = 10;
map.addsplit("3=30,4=40");
bool has1 = map.contains(1);
bool has5 = map.contains(5);
bool has1_10 = map.contains(1, 10);
bool has1_20 = map.contains(1, 20);
bool has5_50 = map.contains(5, 50);
int* value = map.findM(1);
if (value != NULL)
*value = 100;
return 0;
}
Output:
2=20,3=30,1=10,4=40
2=20
3=30
1=100
4=40
Note: Item ordering will vary
|
| | MapHash () |
| | Constructor. More...
|
| |
| | MapHash (const MapBaseType &src) |
| | Copy constructor. More...
|
| |
| | MapHash (const ThisType &src) |
| | Copy constructor. More...
|
| |
| | MapHash (const std::initializer_list< InitPair > &init) |
| | < Used with initializer_list constructor (C++11) More...
|
| |
| | MapHash (ThisType &&src) |
| | Move constructor (C++11). More...
|
| |
| virtual MapBaseType & | add (const MapBaseType &map, bool update=true)=0 |
| | Add items from given map. More...
|
| |
| Item & | add (const Key &key, const Value &value, bool update=true) |
| | Add or update using given key and value. More...
|
| |
| Item & | add (const Item &item, bool update=true) |
| | Add or update using given item. More...
|
| |
| ThisType & | add (const MapBaseType &map, bool update=true) |
| |
| template<class T > |
| Size | addsplit (const T &str, char delim=',', char kvdelim='=') |
| | Split delimited string into map key/value items. More...
|
| |
| const ThisType & | asconst () const |
| | Explicitly use a const reference to this. More...
|
| |
| IterM | begin () |
| | Get iterator at first item (mutable). More...
|
| |
| Iter | begin () const |
| | Get iterator at first item (const). More...
|
| |
| Size | capacity () const |
| | Get map capacity. More...
|
| |
| ThisType & | capacity (Size size) |
| | Set hash map size (capacity). More...
|
| |
| ThisType & | capacitymin (Size min) |
| | Set map capacity to at least given minimum. More...
|
| |
| Iter | cbegin () const |
| | Get iterator at first item (const). More...
|
| |
| Iter | cend () const |
| | Get iterator at end (const). More...
|
| |
| ThisType & | clear () |
| | Clear by removing all items. More...
|
| |
| virtual MapBaseType & | compact () |
| | Reduce capacity to fit current size (modifier). More...
|
| |
| bool | contains (const Key &key) const |
| | Get whether map contains the given key. More...
|
| |
| bool | empty () const |
| | Get whether map is empty (size is 0). More...
|
| |
| IterM | end () |
| | Get iterator at end. More...
|
| |
| Iter | end () const |
| | Get iterator at end (const). More...
|
| |
| const Value * | find (const Key &key) const |
| | Find (lookup) value for given key (const). More...
|
| |
| Value * | findM (const Key &key) |
| | Find (lookup) value for given key (mutable). More...
|
| |
| Value & | get (const Key &key, bool *created=NULL) |
| | Get item value for key (mutable). More...
|
| |
| Item & | getitem (const Key &key, bool *created=NULL) |
| | Get map item for key (mutable). More...
|
| |
| Iter | iter (const Key &key) const |
| | Find (lookup) iterator for given key (const). More...
|
| |
| IterM | iterM (const Key &key) |
| | Find (lookup) iterator for given key (mutable). More...
|
| |
| bool | null () const |
| | Get whether map is null. More...
|
| |
| bool | operator!= (const MapBaseType &map) const |
| | Inequality operator. More...
|
| |
| ThisType & | operator= (ThisType &&src) |
| | Move assignment operator (C++11). More...
|
| |
| ThisType & | operator= (const MapBaseType &src) |
| | Assignment operator. More...
|
| |
| ThisType & | operator= (const ThisType &src) |
| | Assignment operator. More...
|
| |
| bool | operator== (const MapBaseType &map) const |
| | Equality operator. More...
|
| |
| Value & | operator[] (const Key &key) |
| | Get item value for key (mutable). More...
|
| |
| bool | ordered () const |
| | Get whether map is ordered. More...
|
| |
| bool | remove (const Key &key) |
| | Find and remove item with given key. More...
|
| |
| bool | remove (typename MapBaseType::IterM &iter, IteratorDir dir=iterNONE) |
| | Remove item using given iterator. More...
|
| |
| bool | remove (IterM &iter, IteratorDir dir=iterNONE) |
| | Remove item using given iterator. More...
|
| |
| ThisType & | reserve (Size size) |
| | Reserve space for new items. More...
|
| |
| void | resize (Size size) |
| | Set hash map size (capacity) directly. More...
|
| |
| ThisType & | set () |
| | Set as null and empty. More...
|
| |
| ThisType & | set (const MapBaseType &src) |
| |
| ThisType & | set (const ThisType &src) |
| | Set as copy of given map. More...
|
| |
| ThisType & | setempty () |
| | Set as empty but not null. More...
|
| |
| bool | shared () const |
| | Get whether shared. More...
|
| |
| Size | size () const |
| | Get map size (number of items). More...
|
| |
| ThisType & | unshare () |
| | Make data unique by allocating new buffer, if needed (modifier). More...
|
| |