13#ifndef CXXUTILS_CONCURRENTMAP_H
14#define CXXUTILS_CONCURRENTMAP_H
22#include "boost/iterator/iterator_facade.hpp"
89template <
class KEY,
class VALUE,
template <
class>
class UPDATER,
90 class HASHER = std::hash<KEY>,
91 class MATCHER = std::equal_to<KEY>,
111 return m_h (keyAsKey (k));
118 return m_m (keyAsKey (
a), keyAsKey (b));
133 using key_type = KEY;
134 using mapped_type =
VALUE;
135 using size_type = size_t;
137 using Updater_t =
typename Impl_t::Updater_t;
139 using Context_t =
typename Updater_t::Context_t;
141 using Lock_t =
typename Impl_t::Lock_t;
144 static_assert(
sizeof(
typename Impl_t::val_t) >=
sizeof(key_type) );
145 static_assert(
sizeof(
typename Impl_t::val_t) >=
sizeof(mapped_type) );
158 const Context_t& ctx = Updater_t::defaultContext());
173 const Context_t& ctx = Updater_t::defaultContext());
188 template <
class InputIterator>
193 const Context_t& ctx = Updater_t::defaultContext());
212 size_type
size()
const;
249 :
public boost::iterator_facade<const_iterator,
250 const const_iterator_value,
251 std::bidirectional_iterator_tag,
252 const const_iterator_value>
351 size_type
count (key_type key)
const;
370 mapped_type
at (key_type key)
const;
380 std::pair<const_iterator, const_iterator>
406 std::pair<const_iterator, bool>
408 const Context_t& ctx = Updater_t::defaultContext());
423 std::pair<const_iterator, bool>
425 key_type key, mapped_type val,
426 const Context_t& ctx = Updater_t::defaultContext());
440 std::pair<const_iterator, bool>
442 const Context_t& ctx = Updater_t::defaultContext());
458 std::pair<const_iterator, bool>
460 key_type key, mapped_type val,
461 const Context_t& ctx = Updater_t::defaultContext());
477 template <
class PAIR>
478 std::pair<const_iterator, bool>
insert (
const PAIR& p);
489 template <
class InputIterator>
490 void insert (InputIterator first, InputIterator last);
533 const Context_t& ctx = Updater_t::defaultContext());
552 const Context_t& ctx = Updater_t::defaultContext());
559 void clear (
const Context_t& ctx = Updater_t::defaultContext());
637 typename Impl_t::const_iterator
get (key_type key)
const;
651 std::pair<const_iterator, bool>
654 bool overwrite =
true,
655 const Context_t& ctx = Updater_t::defaultContext());
670 std::pair<const_iterator, bool>
674 bool overwrite =
true,
675 const Context_t& ctx = Updater_t::defaultContext());
Hash table allowing concurrent, lockless reads.
Concept check for Updater class used by concurrent classes.
Helpers for converting between uintptr_t and a pointer or integer.
Simple range from a pair of iterators.
Concept for a value that can be saved in a concurrent hash map.
Concept check for Updater class used by concurrent classes.
A couple standard-library related concepts.
Simple range from a pair of iterators.
typename Updater_t::Context_t Context_t
uintptr_t ConcurrentHashmapVal_t
Type used for keys and values — an unsigned big enough to hold a pointer.
ConcurrentHashmapImpl(Updater_t &&updater, size_t capacity_in, const Hasher_t &hasher, const Matcher_t &matcher, const typename Updater_t::Context_t &ctx)
Constructor.
void forceClear()
Erase the table in-place.
~ConcurrentMap()=default
Destructor.
CxxUtils::iterator_range< const_iterator > const_iterator_range
A range defined by two iterators.
size_t erased() const
The number of erased elements in the current table.
const_iterator cend() const
Iterator at the end of the map.
Updater_t & updater()
Access the Updater instance.
const_iterator begin() const
Return a begin iterator.
std::pair< const key_type, mapped_type > const_iterator_value
Value structure for iterators.
std::pair< const_iterator, const_iterator > equal_range(key_type key) const
Return a range of iterators with entries matching key.
const_iterator find(bit_t bit) const
If bit bit is set, return an iterator pointing to it.
std::pair< const_iterator, bool > put(key_type key, mapped_type val, bool overwrite=true, const Context_t &ctx=Updater_t::defaultContext())
Insert / overwrite an entry in the table.
bool empty() const
Return true if there are no 1 bits in the set.
void quiescent(const Context_t &ctx)
Called when this thread is no longer referencing anything from this container.
void rehash(size_type capacity)
Increase the table capacity.
bit_t size() const
Count the number of 1 bits in the set.
bool contains(key_type key) const
Test if a key is in the container.
ConcurrentBitset & erase(bit_t bit)
Turn off one bit.
bit_t capacity() const
The number of bits that this container can hold.
ConcurrentBitset & operator=(const ConcurrentBitset &other)
Assignment.
const_iterator_range range() const
Return an iterator range covering the entire map.
static mapped_type mappedAsMapped(val_t val)
Convert an underlying mapped value to this type's mapped value.
void swap(ClassName &other)
Swap this expression with another one.
std::pair< const_iterator, bool > insert_or_assign(key_type key, mapped_type val, const Context_t &ctx=Updater_t::defaultContext())
Add an element to the map, or overwrite an existing one.
UPDATER< Impl > Updater_t
ConcurrentBitset & clear()
Clear all bits in the set.
const_iterator end() const
Return an end iterator.
mapped_type at(key_type key) const
Look up an element in the map.
ConcurrentMap(Updater_t &&updater, size_type capacity=64, const Context_t &ctx=Updater_t::defaultContext())
Hash map from integers/pointers allowing concurrent, lockless reads.
ConcurrentBitset & insert(bit_t bit, bit_t new_nbits=0)
Set a bit to 1.
static key_type keyAsKey(val_t val)
Convert an underlying key value to this type's key value.
static val_t keyAsVal(key_type k)
Convert this type's key value to an underlying key value.
std::atomic< Impl * > m_impl
The current implementation object.
const_iterator cbegin() const
Iterator at the start of the map.
bit_t count() const
Count the number of 1 bits in the set.
std::pair< const_iterator, bool > emplace(key_type key, mapped_type val, const Context_t &ctx=Updater_t::defaultContext())
Add an element to the map.
void reserve(size_type capacity, const Context_t &ctx=Updater_t::defaultContext())
Increase the table capacity.
static val_t mappedAsVal(mapped_type val)
Convert this type's mapped value to an underlying mapped value.
Impl_t::const_iterator get(key_type key) const
Do a lookup in the table.
Lock_t lock()
Take a lock on the container.
Iterator over all 1 bits in the set.
bool valid() const
Test if this iterator is valid.
void decrement()
iterator_facade requirement: Decrement the iterator.
void increment()
iterator_facade requirement: Increment the iterator.
const_iterator(Block_t cache, bit_t bit, const std::atomic< Block_t > *data, const std::atomic< Block_t > *end)
Constructor.
bool equal(const const_iterator &other) const
iterator_facade requirement: Equality test.
friend class boost::iterator_core_access
Required by iterator_facade.
const const_iterator_value dereference() const
iterator_facade requirement: Dereference the iterator.
const_iterator(typename Impl_t::const_iterator it)
Constructor.
Impl_t::const_iterator m_impl
The iterator on the underlying table.