ra_map.hpp

This file defines a Random Access Map template.

A random access map allows for simple traversal by index and a guarantee that a value at a given index will always be at that index unless any map element is deleted. This allows storage of indices for maps with a fixed layout, resulting in easy access.

Note

Status: ALPHA

template<typename KEY_T, typename T, typename Hash = std::hash<KEY_T>, typename KeyEqual = std::equal_to<KEY_T>, typename Allocator = std::allocator<std::pair<const KEY_T, T>>>
class ra_map
#include <ra_map.hpp>

This class uses a combination of a hashtable (std::unordered_map) and vector to lookup insert, lookup, and delete values in constant time, while still being able to step through all values (albeit in an arbitrary order).

Note

The arbitrary order of values may change if any values are deleted.

Public Types

using key_type = KEY_T
using mapped_type = T
using value_type = std::pair<const KEY_T, T>
using size_type = std::size_t
using difference_type = std::ptrdiff_t
using hasher = Hash
using key_equal = KeyEqual
using allocator_type = Allocator
using reference = value_type&
using const_reference = const value_type&
using pointer = typename std::allocator_traits<Allocator>::pointer
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer
using layout_t = unordered_map<KEY_T, size_t, Hash, KeyEqual>

Public Functions

ra_map() = default
ra_map(const ra_map&) = default
ra_map(ra_map&&) = default
this_t &operator=(const ra_map&) = default
this_t &operator=(ra_map&&) = default
inline auto begin()
inline auto cbegin() const
inline auto end()
inline auto cend() const
inline size_t insert(const value_type &v)

Insert a new value into container by copy; return position.

inline size_t insert(value_type &&v)

Insert a new value into container by move; return position.

template<typename ...Ts>
inline size_t emplace(Ts&&... args)

Construct a new value in place in a container container; return position.

inline bool erase(const KEY_T &key)

Erase a specific value from the container.

inline size_t count(const KEY_T &key) const
inline T &operator[](key_type key)

Is value included? (0 or 1).

Index into the ra_map by key.

inline const layout_t &GetLayout() const
inline T &NewEntry(key_type key)
inline bool Has(key_type key) const
inline size_t GetID(key_type key) const
inline key_type &KeyAtID(size_t id)
inline T &AtID(size_t id)
inline const T &AtID(size_t id) const

Private Types

using this_t = ra_map<KEY_T, T, Hash, KeyEqual, Allocator>

Private Members

layout_t id_map

Map to find keys in vector.

vector<value_type> vals

Vector of all values.