ATLAS Offline Software
Loading...
Searching...
No Matches
ConcurrentPtrSet.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 */
11
12
14#include "CxxUtils/IsUpdater.h"
16#include "boost/iterator/iterator_facade.hpp"
17#include <type_traits>
18#include <ranges>
19
20
21#ifndef CXXUTILS_CONCURRENTPTRSET_H
22#define CXXUTILS_CONCURRENTPTRSET_H
23
24
25namespace CxxUtils {
26
27
74template <class VALUE, template <class> class UPDATER>
77{
78private:
80 struct Hasher;
81 struct Matcher;
85
86
87public:
89 using key_type = VALUE*;
90 using const_key_type = const VALUE*;
91 using size_type = size_t;
93 using Updater_t = typename Impl_t::Updater_t;
95 using Context_t = typename Updater_t::Context_t;
97 using Lock_t = typename Impl_t::Lock_t;
98
99
109 size_type capacity = 64,
110 const Context_t& ctx = Updater_t::defaultContext());
111
112
125 size_t capacity = 64,
126 const Context_t& ctx = Updater_t::defaultContext());
127
128
139 template <class InputIterator>
140 ConcurrentPtrSet (InputIterator f,
141 InputIterator l,
143 size_type capacity = 64,
144 const Context_t& ctx = Updater_t::defaultContext());
145
146
148 ConcurrentPtrSet (const ConcurrentPtrSet& other) = delete;
150 ConcurrentPtrSet& operator= (const ConcurrentPtrSet& other) = delete;
151 ConcurrentPtrSet& operator= (ConcurrentPtrSet&& other) = delete;
152
153
158
159
164
165
169 bool empty() const;
170
171
175 size_t capacity() const;
176
177
182
183
194 : public boost::iterator_facade<const_iterator,
195 const const_iterator_value,
196 std::bidirectional_iterator_tag,
197 const const_iterator_value>
198 {
199 public:
204 const_iterator (typename Impl_t::const_iterator it);
205
206
212 bool valid() const;
213
214
215 private:
217 friend class boost::iterator_core_access;
218
219
223 void increment();
224
225
229 void decrement();
230
231
235 bool equal (const const_iterator& other) const;
236
237
242
243
245 typename Impl_t::const_iterator m_impl;
246 };
247
248
251
252
257
258
262 const_iterator begin() const;
263
264
268 const_iterator end() const;
269
270
274 const_iterator cbegin() const;
275
276
280 const_iterator cend() const;
281
282
287 bool contains (const const_key_type key) const;
288
289
296 size_type count (const const_key_type key) const;
297
298
305 const_iterator find (const const_key_type key) const;
306
307
315 std::pair<const_iterator, const_iterator>
316 equal_range (const const_key_type key) const;
317
318
328
329
340 std::pair<const_iterator, bool>
341 emplace (const key_type key,
342 const Context_t& ctx = Updater_t::defaultContext());
343
344
356 std::pair<const_iterator, bool>
358 const key_type key,
359 const Context_t& ctx = Updater_t::defaultContext());
360
361
373 std::pair<const_iterator, bool> insert (const key_type p);
374
375
381 template <class InputIterator>
382 void insert (InputIterator first, InputIterator last);
383
384
394 const Context_t& ctx = Updater_t::defaultContext());
395
396
405
406
412 void clear (size_t capacity,
413 const Context_t& ctx = Updater_t::defaultContext());
414
415
420 void clear (const Context_t& ctx = Updater_t::defaultContext());
421
422
428 void quiescent (const Context_t& ctx);
429
430
441 void swap (ConcurrentPtrSet& other);
442
443
448
449
450private:
455 static key_type keyAsPtr (val_t val);
456
457
462 static val_t keyAsVal (const const_key_type p);
463
464
472 typename Impl_t::const_iterator get (const const_key_type key) const;
473
474
484 std::pair<const_iterator, bool>
485 put (const key_type key,
486 const Context_t& ctx = Updater_t::defaultContext());
487
488
499 std::pair<const_iterator, bool>
500 put (const Lock_t& lock,
501 const key_type key,
502 const Context_t& ctx = Updater_t::defaultContext());
503
504
511 struct Hasher
512 {
514 size_t operator() (const val_t p) const;
516 size_t operator() (const const_key_type p) const;
518 std::hash<const_key_type> m_hash;
519 };
520
521
525 struct Matcher
526 {
528 bool operator() (const val_t a, const val_t b) const;
529 };
530
531
534};
535
536
537} // namespace CxxUtils
538
539
541
542
543#endif // not CXXUTILS_CONCURRENTPTRSET_H
Hash table allowing concurrent, lockless reads.
Concept check for Updater class used by concurrent classes.
static Double_t a
bool equal(const const_iterator &other) const
iterator_facade requirement: Equality test.
Impl_t::const_iterator m_impl
The iterator on the underlying table.
key_type dereference() const
iterator_facade requirement: Dereference the iterator.
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.
void increment()
iterator_facade requirement: Increment the iterator.
std::pair< const_iterator, bool > put(const key_type key, const Context_t &ctx=Updater_t::defaultContext())
Insert an entry in the table.
size_type count(const const_key_type key) const
Return the number of times a given key is in the container.
ConcurrentPtrSet(const ConcurrentPtrSet &other)=delete
Copy / move / assign not supported.
std::pair< const_iterator, const_iterator > equal_range(const const_key_type key) const
Return a range of iterators with entries matching key.
std::pair< const_iterator, bool > emplace(const Lock_t &lock, const key_type key, const Context_t &ctx=Updater_t::defaultContext())
Add an element to the set, with external locking.
void swap(ConcurrentPtrSet &other)
Swap this container with another.
Lock_t lock()
Take a lock on the container.
CxxUtils::iterator_range< const_iterator > const_iterator_range
ConcurrentPtrSet(const ConcurrentPtrSet &other, Updater_t &&updater, size_t capacity=64, const Context_t &ctx=Updater_t::defaultContext())
Constructor from another set.
const_iterator cbegin() const
Iterator at the start of the set.
const_iterator find(const const_key_type key) const
Look up an element in the set.
size_type size() const
Return the number of items currently in the set.
void reserve(size_type capacity, const Context_t &ctx=Updater_t::defaultContext())
Increase the table capacity.
std::pair< const_iterator, bool > put(const Lock_t &lock, const key_type key, const Context_t &ctx=Updater_t::defaultContext())
Insert an entry in the table, with external locking.
const_iterator end() const
Iterator at the end of the set.
ConcurrentPtrSet(ConcurrentPtrSet &&other)=delete
typename detail::ConcurrentHashmapImpl< CxxUtils::SimpleUpdater, Hasher, Matcher > Impl_t
void rehash(size_type capacity)
Increase the table capacity.
static val_t keyAsVal(const const_key_type p)
Convert a pointer to an underlying key value.
bool contains(const const_key_type key) const
Test if a key is in the container.
Impl_t::const_iterator get(const const_key_type key) const
Do a lookup in the table.
bool empty() const
Test if the set is currently empty.
void clear(const Context_t &ctx=Updater_t::defaultContext())
Erase the table (don't change the capacity).
ConcurrentPtrSet(Updater_t &&updater, size_type capacity=64, const Context_t &ctx=Updater_t::defaultContext())
Constructor.
const_iterator_range range() const
Return an iterator range covering the entire set.
~ConcurrentPtrSet()
Destructor.
std::pair< const_iterator, bool > insert(const key_type p)
Add an element to the set.
std::pair< const_iterator, bool > emplace(const key_type key, const Context_t &ctx=Updater_t::defaultContext())
Add an element to the set.
ConcurrentPtrSet(InputIterator f, InputIterator l, Updater_t &&updater, size_type capacity=64, const Context_t &ctx=Updater_t::defaultContext())
Constructor from a range.
void quiescent(const Context_t &ctx)
Called when this thread is no longer referencing anything from this container.
const_iterator cend() const
Iterator at the end of the set.
const_iterator begin() const
Iterator at the start of the set.
void insert(InputIterator first, InputIterator last)
Insert a range of elements to the set.
void clear(size_t capacity, const Context_t &ctx=Updater_t::defaultContext())
Erase the table and change the capacity.
static key_type keyAsPtr(val_t val)
Convert an underlying key value to a pointer.
Hash table allowing concurrent, lockless reads.
Simple range from a pair of iterators.
Concept check for Updater class used by concurrent classes.
Definition IsUpdater.h:51
#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.
std::hash< const_key_type > m_hash
Hash functional.
Matching functional for keys.