carb::container::RHUnorderedMultimap

Defined in carb/container/RHUnorderedMultimap.h

template<class Key, class Value, class Hasher = std::hash<Key>, class Equals = std::equal_to<Key>, size_t LoadFactorMax100 = 80>
class RHUnorderedMultimap : public detail::RobinHood<80, Key, std::pair<const Key, Value>, detail::Select1st<Key, std::pair<const Key, Value>>, std::hash<Key>, std::equal_to<Key>>

Implements an Unordered Multimap, that is: a container that contains a mapping of keys to values where keys may be inserted multiple times, each creating a new element.

There is no defined order to the set of keys.

In an open-addressing (“OA”) hash table, the contained items are stored in the buckets directly. Contrast this with traditional hash tables that typically have a level of indirection: buckets point to the head of a linked-list that contains every item that hashes to that bucket. Open-addressing hash tables are great for using contiguous memory, whereas traditional hash tables have a separate allocation per node and fragment memory. However, OA hash tables have a couple downsides: if a collision occurs on insertion, probing must happen until an open spot is found where the item can be placed. For a find operation, probing must continue until an empty spot is reached to make sure that all keys have been checked. When erasing an item, a “deleted” marker must be put in its place so that probing past the key can continue. This system also gives advantage to earlier insertions and penalizes later collisions.

The Robin Hood algorithm for open-addressing hashing was first postulated by Pedro Celis in 1986: https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf. Simply put, it applies a level of fairness to locality of items within the OA hash table. This is done by tracking the distance from an items ideal insertion point. Similarly the distance-from-ideal can be easily computed for existing locations that are probed. Once a probed location for a new item will cause the new item to be worse off (farther from ideal insertion) than the existing item, the new item can “steal” the location from the existing item, which must then probe until it finds a location where it is worse off than the existing item, and so on. This balancing of locality has beneficial side effects for finding and erasing too: when searching for an item, once a location is reached where the item would be worse off than the existing item, probing can cease with the knowledge that the item is not contained.

OA hash tables cannot be direct drop-in replacements for closed-addressing hash containers such as std::unordered_map as nearly every modification to the table can potentially invalidate any other iterator.

Open-addressing hash tables may not be a good replacement for std unordered containers in cases where the key and/or value is very large (though this may be mitigated somewhat by using indirection through std::unique_ptr). Since OA hash tables must carry the size of each value_type, having a low load factor (or a high capacity() to size() ratio) wastes a lot of memory, especially if the key/value pair is very large.

It is important to keep OA hash tables as compact as possible, as operations like clear() and iterating over the hash table are O(n) over capacity(), not size(). You can always ensure that the hash table is as compact as possible by calling rehash(0).

Because of the nature of how elements are stored in this hash table, there are two iterator types: iterator and find_iterator (both with const versions). These types can be compared with each other, but incrementing these objects works differently. iterator and const_iterator traverse to the next item in the container, while find_iterator and const_find_iterator will only traverse to the next item with the same key. In multi-key containers, items with the same key may not necessarily be stored adjacently, so incrementing iterator may not encounter the next item with the same key as the previous. For unique-key containers, incrementing a find_iterator will always produce end() since keys are guaranteed to be unique.

Iterator/reference/pointer invalidation (note differences from std::unordered_multimap):

Operation

Invalidates

All read operations

Never

clear, rehash, reserve, operator=, insert, emplace

Always

erase

Only the element removed

swap

All iterators, no pointers/references

Warning

This container is similar to, but not a drop-in replacement for std::unordered_multimap due to differences in iterator invalidation and memory layout.

Template Parameters
  • Key – The key type

  • Value – The mapped type to be associated with Key

  • Hasher – A functor to use as a hashing function for Key

  • Equals – A functor to use to compare two Key values for equality

  • LoadFactorMax100 – The load factor to use for the table. This value must be in the range [10, 100] and represents the percentage of entries in the hash table that will be filled before resizing. Open-addressing hash maps with 100% usage have better memory usage but worse performance since they need “gaps” in the hash table to terminate runs.

Public Types

using key_type = typename Base::key_type

The key type.

using mapped_type = Value

The mapped value type.

using value_type = typename Base::value_type

The value type (effectively std::pair<const key_type, mapped_type>)

using size_type = typename Base::size_type

Unsigned integer type (typically size_t)

using difference_type = typename Base::difference_type

Signed integer type (typically ptrdiff_t)

using hasher = typename Base::hasher

The hash function.

using key_equal = typename Base::key_equal

The key-equals function.

using reference = typename Base::reference

value_type&

using const_reference = typename Base::const_reference

const value_type&

using pointer = typename Base::pointer

value_type*

using const_pointer = typename Base::const_pointer

const value_type*

using iterator = typename Base::iterator

A LegacyForwardIterator to value_type.

using const_iterator = typename Base::const_iterator

A LegacyForwardIterator to const value_type

using find_iterator = typename Base::find_iterator

A LegacyForwardIterator to value_type that proceeds to the next matching key when incremented.

using const_find_iterator = typename Base::const_find_iterator

A LegacyForwardIterator to const value_type that proceeds to the next matching key when incremented.

Public Functions

constexpr RHUnorderedMultimap() noexcept = default

Constructs empty container.

inline RHUnorderedMultimap(const RHUnorderedMultimap &other)

Copy constructor.

Copies elements from another container.

Note

*this may have a different carb::container::detail::RobinHood::capacity() than other.

Parameters

other – The other container to copy entries from.

inline RHUnorderedMultimap(RHUnorderedMultimap &&other)

Move constructor.

Moves elements from another container.

Note

No move constructors on contained elements are invoked. other will be carb::container::detail::RobinHood::empty() after this operation.

Parameters

other – The other container to move entries from.

~RHUnorderedMultimap() = default

Destructor.

Destroys all contained elements and frees memory.

inline RHUnorderedMultimap &operator=(const RHUnorderedMultimap &other)

Copy-assign operator.

Destroys all currently stored elements and copies elements from another container.

Parameters

other – The other container to copy entries from.

Returns

*this

inline RHUnorderedMultimap &operator=(RHUnorderedMultimap &&other)

Move-assign operator.

Effectively swaps with another container.

Parameters

other – The other container to copy entries from.

Returns

*this

inline iterator insert(const value_type &value)

Inserts an element into the container.

All iterators, references and pointers are invalidated.

Parameters

value – The value to insert by copying.

Returns

an iterator to the inserted element.

inline iterator insert(value_type &&value)

Inserts an element into the container.

All iterators, references and pointers are invalidated.

Parameters

value – The value to insert by moving.

Returns

an iterator to the inserted element.

template<class P>
inline iterator insert(std::enable_if_t<std::is_constructible<value_type, P&&>::value, P&&> value)

Inserts an element into the container.

Only participates in overload resolution if std::is_constructible_v<value_type, P&&> is true.

All iterators, references and pointers are invalidated.

Parameters

value – The value to insert by constructing via std::forward<P>(value).

Returns

an iterator to the inserted element.

template<class ...Args>
inline iterator emplace(Args&&... args)

Constructs an element in-place.

All iterators, references and pointers are invalidated.

Parameters

args – The arguments to pass to the value_type constructor.

Returns

an iterator to the inserted element.

inline size_type erase(const key_type &key)

Removes elements with the given key.

References, pointers and iterators to the erase element are invalidated. All other iterators, pointers and references remain valid.

Parameters

key – the key value of elements to remove

Returns

the number of elements removed.

inline size_t count(const key_type &key) const

Returns the number of elements matching the specified key.

Parameters

key – The key to check for.

Returns

The number of elements with the given key.