ATLAS Offline Software
Loading...
Searching...
No Matches
ConcurrentToValMap.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
4 */
12
13
14#ifndef CXXUTILS_CONCURRENTTOVALMAP_H
15#define CXXUTILS_CONCURRENTTOVALMAP_H
16
17
19#include "CxxUtils/UIntConv.h"
20#include "CxxUtils/concepts.h"
21#include "CxxUtils/IsUpdater.h"
23#include "boost/iterator/iterator_facade.hpp"
24#include <memory>
25#include <type_traits>
26#include <stdexcept>
27
28
29namespace CxxUtils {
30
31
94template <class KEY, class VALUE, template <class> class UPDATER,
95 class HASHER = std::hash<KEY>,
96 class MATCHER = std::equal_to<KEY>,
103{
104private:
107
108 // Translate between the Hasher/Matcher provided (which take KEY arguments)
109 // and what we need to give to the underlying implementation
110 // (which takes a uintptr_t).
111 struct Hasher
112 {
113 size_t operator() (val_t k) const {
114 return m_h (keyAsKey (k));
115 }
116 HASHER m_h;
117 };
118 struct Matcher
119 {
120 bool operator() (val_t a, val_t b) const {
121 return m_m (keyAsKey (a), keyAsKey (b));
122 }
123 MATCHER m_m;
124 };
125
127 using Impl_t = typename detail::ConcurrentHashmapImpl<UPDATER,
128 Hasher,
129 Matcher,
130 NULLVAL>;
131
132
133public:
135 using key_type = KEY;
137 using size_type = size_t;
139 using Updater_t = typename Impl_t::Updater_t;
141 using Context_t = typename Updater_t::Context_t;
142
143
153 size_type capacity = 64,
154 const Context_t& ctx = Updater_t::defaultContext());
155
156
169 size_t capacity = 64,
170 const Context_t& ctx = Updater_t::defaultContext());
171
172
185 template <class InputIterator>
186 ConcurrentToValMap (InputIterator f,
187 InputIterator l,
189 size_type capacity = 64,
190 const Context_t& ctx = Updater_t::defaultContext());
191
192
194 ConcurrentToValMap (const ConcurrentToValMap& other) = delete;
196 ConcurrentToValMap& operator= (const ConcurrentToValMap& other) = delete;
197 ConcurrentToValMap& operator= (ConcurrentToValMap&& other) = delete;
198
199
204
205
210
211
215 bool empty() const;
216
217
221 size_t capacity() const;
222
223
236 using const_iterator_value = std::pair<const key_type, const mapped_type&>;
237 using iterator_value = std::pair<const key_type, mapped_type&>;
238
239 class const_iterator;
240 class iterator;
241
242
253 : public boost::iterator_facade<const_iterator,
254 const const_iterator_value,
255 std::bidirectional_iterator_tag,
256 const const_iterator_value>
257 {
258 public:
263 const_iterator (typename Impl_t::const_iterator it);
264
265
270 const_iterator (const iterator& other);
271
272
278 bool valid() const;
279
280
281 private:
283 friend class boost::iterator_core_access;
284 friend class iterator;
285
286
290 void increment();
291
292
296 void decrement();
297
298
302 bool equal (const const_iterator& other) const;
303
304
308 bool equal (const iterator& other) const;
309
310
315
316
318 typename Impl_t::const_iterator m_impl;
319 };
320
321
336 : public boost::iterator_facade<iterator,
337 const iterator_value,
338 std::bidirectional_iterator_tag,
339 const iterator_value>
340 {
341 public:
346 iterator (typename Impl_t::const_iterator it);
347
348
354 bool valid() const;
355
356
357 private:
359 friend class boost::iterator_core_access;
360 friend class const_iterator;
361
362
366 void increment();
367
368
372 void decrement();
373
374
378 bool equal (const iterator& other) const;
379
380
384 bool equal (const const_iterator& other) const;
385
386
391
392
394 typename Impl_t::const_iterator m_impl;
395 };
396
397
401
402
407
408
416
417
421 const_iterator begin() const;
422
423
427 const_iterator end() const;
428
429
436 iterator begin();
437
438
445 iterator end();
446
447
451 const_iterator cbegin() const;
452
453
457 const_iterator cend() const;
458
459
464 bool contains (const key_type key) const;
465
466
473 size_type count (const key_type key) const;
474
475
482 const_iterator find (const key_type key) const;
483
484
494 iterator find (const key_type key);
495
496
504 const mapped_type& at (const key_type key) const;
505
506
518 mapped_type& at (const key_type key);
519
520
528 std::pair<const_iterator, const_iterator>
529 equal_range (const key_type key) const;
530
531
542 std::pair<iterator, iterator>
544
545
557 std::pair<const_iterator, bool>
558 emplace (key_type key, const mapped_type& val,
559 const Context_t& ctx = Updater_t::defaultContext());
560
561
573 std::pair<const_iterator, bool>
575 const Context_t& ctx = Updater_t::defaultContext());
576
577
589 std::pair<const_iterator, bool>
590 emplace (key_type key, std::unique_ptr<mapped_type> val,
591 const Context_t& ctx = Updater_t::defaultContext());
592
593
606 template <class PAIR>
607 std::pair<const_iterator, bool> insert (const PAIR& p,
608 const Context_t& ctx = Updater_t::defaultContext());
609
610
623 template <class PAIR>
624 std::pair<const_iterator, bool> insert (PAIR&& p,
625 const Context_t& ctx = Updater_t::defaultContext());
626
627
637 template <class InputIterator>
638 void insert (InputIterator first, InputIterator last,
639 const Context_t& ctx = Updater_t::defaultContext());
640
641
651 const Context_t& ctx = Updater_t::defaultContext());
652
653
662
663
669 void quiescent (const Context_t& ctx);
670
671
683
684
689
690
691private:
696 static key_type keyAsKey (val_t val);
697
698
704
705
712
713
719
720
728 typename Impl_t::const_iterator get (const key_type key) const;
729
730
741 std::pair<const_iterator, bool>
742 put (const key_type key,
743 std::unique_ptr<mapped_type> val,
744 const Context_t& ctx = Updater_t::defaultContext());
745
746
749};
750
751
752} // namespace CxxUtils
753
754
756
757
758#endif // not CXXUTILS_CONCURRENTTOVALMAP_H
Hash table allowing concurrent, lockless reads.
Concept check for Updater class used by concurrent classes.
static Double_t a
Helpers for converting between uintptr_t and a pointer or integer.
const const_iterator_value dereference() const
iterator_facade requirement: Dereference the iterator.
Impl_t::const_iterator m_impl
The iterator on the underlying table.
void increment()
iterator_facade requirement: Increment the iterator.
bool equal(const const_iterator &other) const
iterator_facade requirement: Equality test.
bool valid() const
Test if this iterator is valid.
void decrement()
iterator_facade requirement: Decrement the iterator.
const_iterator(typename Impl_t::const_iterator it)
Constructor.
const_iterator(const iterator &other)
Conversion from non-const iterator (for interoperability).
bool equal(const iterator &other) const
iterator_facade requirement: Equality test.
const iterator_value dereference() const
iterator_facade requirement: Dereference the iterator.
void increment()
iterator_facade requirement: Increment the iterator.
bool equal(const const_iterator &other) const
iterator_facade requirement: Equality test.
bool equal(const iterator &other) const
iterator_facade requirement: Equality test.
bool valid() const
Test if this iterator is valid.
void decrement()
iterator_facade requirement: Decrement the iterator.
Impl_t::const_iterator m_impl
The iterator on the underlying table.
iterator(typename Impl_t::const_iterator it)
Constructor.
ConcurrentToValMap(InputIterator f, InputIterator l, Updater_t &&updater, size_type capacity=64, const Context_t &ctx=Updater_t::defaultContext())
Constructor from a range.
iterator begin()
Iterator at the start of the map.
Impl_t::const_iterator get(const key_type key) const
Do a lookup in the table.
const_iterator begin() const
Iterator at the start of the map.
ConcurrentToValMap(ConcurrentToValMap &&other)=delete
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.
std::pair< iterator, iterator > equal_range(const key_type key)
Return a range of iterators with entries matching key.
const mapped_type & at(const key_type key) const
Look up an element in the map.
const_iterator cend() const
Iterator at the end of the map.
iterator find(const key_type key)
Look up an element in the map.
iterator end()
Iterator at the end of the map.
const_iterator_range range() const
Return an iterator range covering the entire map.
CxxUtils::iterator_range< const_iterator > const_iterator_range
A range defined by two iterators.
size_type size() const
Return the number of items currently in the map.
void quiescent(const Context_t &ctx)
Called when this thread is no longer referencing anything from this container.
std::pair< const_iterator, bool > insert(const PAIR &p, const Context_t &ctx=Updater_t::defaultContext())
Add an element to the map.
std::pair< const key_type, const mapped_type & > const_iterator_value
Value structures for iterators.
typename detail::ConcurrentHashmapImpl< UPDATER, Hasher, Matcher, NULLVAL > Impl_t
The underlying uint->uint hash table.
std::pair< const_iterator, bool > emplace(key_type key, std::unique_ptr< mapped_type > val, const Context_t &ctx=Updater_t::defaultContext())
Add an element to the map.
std::pair< const_iterator, bool > emplace(key_type key, const 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.
ConcurrentToValMap(Updater_t &&updater, size_type capacity=64, const Context_t &ctx=Updater_t::defaultContext())
Constructor.
iterator_range range()
Return an iterator range covering the entire map.
std::pair< const_iterator, bool > insert(PAIR &&p, const Context_t &ctx=Updater_t::defaultContext())
Add an element to the map.
CxxUtils::iterator_range< iterator > iterator_range
const_iterator find(const key_type key) const
Look up an element in the map.
std::pair< const key_type, mapped_type & > iterator_value
const_iterator end() const
Iterator at the end of the map.
std::pair< const_iterator, const_iterator > equal_range(const key_type key) const
Return a range of iterators with entries matching key.
void swap(ConcurrentToValMap &other)
Swap this container with another.
static val_t mappedAsVal(mapped_type *val)
Convert this type's mapped value to an underlying mapped value.
ConcurrentToValMap(const ConcurrentToValMap &other, Updater_t &&updater, size_t capacity=64, const Context_t &ctx=Updater_t::defaultContext())
Constructor from another map.
static val_t keyAsVal(key_type k)
Convert this type's key value to an underlying key value.
ConcurrentToValMap(const ConcurrentToValMap &other)=delete
Copy / move / assign not supported.
typename Impl_t::Updater_t Updater_t
Updater object.
static key_type keyAsKey(val_t val)
Convert an underlying key value to this type's key value.
bool empty() const
Test if the map is currently empty.
static mapped_type * mappedAsMapped(val_t val)
Convert an underlying mapped value a pointer to this type's mapped value.
typename Updater_t::Context_t Context_t
Context type.
CxxUtils::detail::ConcurrentHashmapVal_t val_t
Representation type in the underlying map.
bool contains(const key_type key) const
Test if a key is in the container.
const_iterator cbegin() const
Iterator at the start of the map.
void rehash(size_type capacity)
Increase the table capacity.
size_type count(const key_type key) const
Return the number of times a given key is in the container.
std::pair< const_iterator, bool > put(const key_type key, std::unique_ptr< mapped_type > val, const Context_t &ctx=Updater_t::defaultContext())
Insert an entry in the table.
mapped_type & at(const key_type key)
Look up an element in the map.
KEY key_type
Standard STL types.
void insert(InputIterator first, InputIterator last, const Context_t &ctx=Updater_t::defaultContext())
Insert a range of elements to the map.
Hash table allowing concurrent, lockless reads.
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.
Definition IsUpdater.h:51
A couple standard-library related concepts.
#define VALUE(TESTED)
Definition expect.h:59
Simple range from a pair of iterators.
uintptr_t ConcurrentHashmapVal_t
Type used for keys and values — an unsigned big enough to hold a pointer.