Loading...
Searching...
No Matches
SBCContainerHashMap< KeyType, ValueType, HashMapFunctor, KeyComparator > Class Template Reference

This template class describes a hashtable-based map. More...

The hash table

unsigned int numberOfEntries
 The number of entries in the hash map.
 
SBCContainerVector< SBCContainerHashMapEntry< KeyType, ValueType > * > * hashMap
 The hash table.
 

Constructors and destructors

 SBCContainerHashMap (unsigned int nElements=0)
 Creates a new hash map.
 
 SBCContainerHashMap (const SBCContainerHashMap &hashMap)
 Copy constructor.
 
 SBCContainerHashMap (SBCContainerHashMap &&hashMap)
 Move constructor.
 
virtual ~SBCContainerHashMap ()
 Deletes the hash map.
 

Operators

SBCContainerHashMapoperator= (const SBCContainerHashMap &hashMap)
 Copy assignment.
 
SBCContainerHashMapoperator= (SBCContainerHashMap &&hashMap)
 Move assignment.
 

Insertion and look-up

void clear ()
 Erases all entries in the hash map.
 
SBCContainerHashMapEntry< KeyType, ValueType > * insert (const KeyType &key, const ValueType &value)
 Inserts an element in the hash map.
 
bool erase (const KeyType &key)
 Attempts to erase an entry with a specific key and returns true if successful.
 
bool hasValue (const KeyType &key) const
 Returns true if the key has an associated value.
 
bool getValue (const KeyType &key, ValueType &value) const
 Returns true if the key has an associated value and puts it in the value, else returns false.
 
std::optional< ValueType > getValue (const KeyType &key) const
 Returns the value associated to the key if found.
 
unsigned int size () const
 The number of entries in the hash map.
 
bool empty () const
 Returns true when the hash map contains no entry.
 

Iterators

iterator begin ()
 Returns an iterator that points to the beginning of the hash map.
 
const_iterator begin () const
 Returns an iterator that points to the beginning of the hash map.
 
ValueType & operator[] (const KeyType &key)
 Returns a reference to the value mapped to the key, which is inserted if necessary.
 
iterator find (const KeyType &key)
 Finds a key in the hash map.
 
const_iterator find (const KeyType &key) const
 Finds a key in the hash map.
 
iterator end ()
 Returns an iterator that points to the end of the hash map.
 
const_iterator end () const
 Returns an iterator that points to the end of the hash map.
 
std::map< KeyType, ValueType > toStdMap () const
 Returns a std map containing the same (key, value) pairs.
 

Debugging

unsigned int getMemoryFootprint () const
 Returns the memory footprint of the hash map.
 

Detailed Description

template<class KeyType, class ValueType, class HashMapFunctor = SBCContainerHashMapFunctor<KeyType>, class KeyComparator = SBCContainerHashMapKeyComparator<KeyType>>
class SBCContainerHashMap< KeyType, ValueType, HashMapFunctor, KeyComparator >
Parameters
KeyTypeThe type of keys
ValueTypeThe type of values
HashMapFunctorThe hash map functor, used to hash keys (by default, this is a SBCContainerHashMapFunctor<KeyType>)
KeyComparatorThe key comparator, used to compare keys (by default, this is a SBCContainerHashMapKeyComparator<KeyType>)

This template class describes a hashtable-based map. This container is used for example to index objects (see SBCContainerIndexer), and may be used to efficiently map keys to values:

SBVector<SBAtom*> v;
SBHashMap<SBAtom*, unsigned int> h;
// create atoms and map them to integers
for (unsigned int i = 0; i<10; i++) {
v.push_back(new SBAtom(SBElement::Carbon));
h.insert(v[i], 6 * i + 17)
}
// print integers associated to atoms
unsigned int j = 0;
for (unsigned int i = 0; i < 10; i++)
if (h.getValue(v[i], j)) std::cout << j << '\n';
SBMStructuralModelNodeAtom SBAtom
The short name of SBMStructuralModelNodeAtom.
Definition: SBMStructuralModelNodeAtom.hpp:666
@ Carbon
The type of the Carbon element.
Definition: SBMElement.hpp:48

Short name: SBHashMap

See also
SB_FOR