ATLAS Offline Software
ConcurrentStrToValMap.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  */
14 #ifndef CXXUTILS_CONCURRENTSTRTOVALMAP_H
15 #define CXXUTILS_CONCURRENTSTRTOVALMAP_H
16 
17 
19 #include "CxxUtils/UIntConv.h"
20 #include "CxxUtils/IsUpdater.h"
21 #include "boost/iterator/iterator_facade.hpp"
22 #include "boost/range/iterator_range.hpp"
23 #include <memory>
24 #include <type_traits>
25 #include <stdexcept>
26 
27 
28 namespace CxxUtils {
29 
30 
83 template <class VALUE, template <class> class UPDATER>
84 requires (detail::IsUpdater<UPDATER>)
85 class ConcurrentStrToValMap
86 {
87 private:
89  struct Hasher;
90  struct Matcher;
91  using Impl_t = typename detail::ConcurrentHashmapImpl<UPDATER, Hasher, Matcher>;
94 
95 
96 public:
98  using key_type = std::string;
99  using mapped_type = VALUE;
100  using size_type = size_t;
102  using Updater_t = typename Impl_t::Updater_t;
104  using Context_t = typename Updater_t::Context_t;
105 
106 
115  ConcurrentStrToValMap (Updater_t&& updater,
116  size_type capacity = 64,
117  const Context_t& ctx = Updater_t::defaultContext());
118 
119 
130  ConcurrentStrToValMap (const ConcurrentStrToValMap& other,
131  Updater_t&& updater,
132  size_t capacity = 64,
133  const Context_t& ctx = Updater_t::defaultContext());
134 
135 
148  template <class InputIterator>
149  ConcurrentStrToValMap (InputIterator f,
150  InputIterator l,
151  Updater_t&& updater,
152  size_type capacity = 64,
153  const Context_t& ctx = Updater_t::defaultContext());
154 
155 
157  ConcurrentStrToValMap (const ConcurrentStrToValMap& other) = delete;
158  ConcurrentStrToValMap (ConcurrentStrToValMap&& other) = delete;
159  ConcurrentStrToValMap& operator= (const ConcurrentStrToValMap& other) = delete;
160  ConcurrentStrToValMap& operator= (ConcurrentStrToValMap&& other) = delete;
161 
162 
166  ~ConcurrentStrToValMap();
167 
168 
172  size_type size() const;
173 
174 
178  bool empty() const;
179 
180 
184  size_t capacity() const;
185 
186 
199  using const_iterator_value = std::pair<const key_type&, const mapped_type&>;
200  using iterator_value = std::pair<const key_type, mapped_type&>;
201 
202  class const_iterator;
203  class iterator;
204 
205 
215  class const_iterator
216  : public boost::iterator_facade<const_iterator,
217  const const_iterator_value,
218  std::bidirectional_iterator_tag,
219  const const_iterator_value>
220  {
221  public:
226  const_iterator (typename Impl_t::const_iterator it);
227 
228 
233  const_iterator (const iterator& other);
234 
235 
241  bool valid() const;
242 
243 
244  private:
246  friend class boost::iterator_core_access;
247  friend class iterator;
248 
249 
253  void increment();
254 
255 
259  void decrement();
260 
261 
265  bool equal (const const_iterator& other) const;
266 
267 
271  bool equal (const iterator& other) const;
272 
273 
277  const const_iterator_value dereference() const;
278 
279 
281  typename Impl_t::const_iterator m_impl;
282  };
283 
284 
298  class iterator
299  : public boost::iterator_facade<iterator,
300  const iterator_value,
301  std::bidirectional_iterator_tag,
302  const iterator_value>
303  {
304  public:
309  iterator (typename Impl_t::const_iterator it);
310 
311 
317  bool valid() const;
318 
319 
320  private:
322  friend class boost::iterator_core_access;
323  friend class const_iterator;
324 
325 
329  void increment();
330 
331 
335  void decrement();
336 
337 
341  bool equal (const iterator& other) const;
342 
343 
347  bool equal (const const_iterator& other) const;
348 
349 
353  const iterator_value dereference() const;
354 
355 
357  typename Impl_t::const_iterator m_impl;
358  };
359 
360 
362  typedef boost::iterator_range<const_iterator> const_iterator_range;
363  typedef boost::iterator_range<iterator> iterator_range;
364 
365 
369  const_iterator_range range() const;
370 
371 
378  iterator_range range();
379 
380 
384  const_iterator begin() const;
385 
386 
390  const_iterator end() const;
391 
392 
399  iterator begin();
400 
401 
408  iterator end();
409 
410 
414  const_iterator cbegin() const;
415 
416 
420  const_iterator cend() const;
421 
422 
427  bool contains (const key_type& key) const;
428 
429 
436  size_type count (const key_type& key) const;
437 
438 
445  const_iterator find (const key_type& key) const;
446 
447 
457  iterator find (const key_type& key);
458 
459 
467  const mapped_type& at (const key_type& key) const;
468 
469 
481  mapped_type& at (const key_type& key);
482 
483 
491  std::pair<const_iterator, const_iterator>
492  equal_range (const key_type& key) const;
493 
494 
505  std::pair<iterator, iterator>
506  equal_range (const key_type& key);
507 
508 
520  std::pair<const_iterator, bool>
521  emplace (const key_type& key, const mapped_type& val,
522  const Context_t& ctx = Updater_t::defaultContext());
523 
524 
536  std::pair<const_iterator, bool>
537  emplace (key_type&& key, const mapped_type& val,
538  const Context_t& ctx = Updater_t::defaultContext());
539 
540 
552  std::pair<const_iterator, bool>
553  emplace (const key_type& key, mapped_type&& val,
554  const Context_t& ctx = Updater_t::defaultContext());
555 
556 
568  std::pair<const_iterator, bool>
569  emplace (key_type&& key, mapped_type&& val,
570  const Context_t& ctx = Updater_t::defaultContext());
571 
572 
584  std::pair<const_iterator, bool>
585  emplace (const key_type& key, std::unique_ptr<mapped_type> val,
586  const Context_t& ctx = Updater_t::defaultContext());
587 
588 
600  std::pair<const_iterator, bool>
601  emplace (key_type&& key, std::unique_ptr<mapped_type> val,
602  const Context_t& ctx = Updater_t::defaultContext());
603 
604 
617  template <class PAIR>
618  std::pair<const_iterator, bool> insert (const PAIR& p,
619  const Context_t& ctx = Updater_t::defaultContext());
620 
621 
634  template <class PAIR>
635  std::pair<const_iterator, bool> insert (PAIR&& p,
636  const Context_t& ctx = Updater_t::defaultContext());
637 
638 
648  template <class InputIterator>
649  void insert (InputIterator first, InputIterator last,
650  const Context_t& ctx = Updater_t::defaultContext());
651 
652 
661  void reserve (size_type capacity,
662  const Context_t& ctx = Updater_t::defaultContext());
663 
664 
672  void rehash (size_type capacity);
673 
674 
680  void quiescent (const Context_t& ctx);
681 
682 
693  void swap (ConcurrentStrToValMap& other);
694 
695 
699  Updater_t& updater();
700 
701 
702 private:
707  static const std::string* keyAsString (val_t val);
708 
709 
714  static val_t keyAsVal (const std::string* s);
715 
716 
722  static mapped_type* mappedAsMapped (val_t val);
723 
724 
729  static val_t mappedAsVal (mapped_type* val);
730 
731 
739  typename Impl_t::const_iterator get (const key_type& key) const;
740 
741 
752  std::pair<const_iterator, bool>
753  put (std::unique_ptr<key_type> key,
754  std::unique_ptr<mapped_type> val,
755  const Context_t& ctx = Updater_t::defaultContext());
756 
757 
764  struct Hasher
765  {
767  size_t operator() (const val_t p) const;
769  size_t operator() (const std::string& s) const;
771  std::hash<std::string> m_hash;
772  };
773 
774 
778  struct Matcher
779  {
781  bool operator() (const val_t a, const val_t b) const;
782  };
783 
784 
786  Impl_t m_impl;
787 };
788 
789 
790 } // namespace CxxUtils
791 
792 
794 
795 
796 #endif // not CXXUTILS_CONCURRENTSTRTOVALMAP_H
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
CxxUtils::detail::ConcurrentHashmapVal_t
uintptr_t ConcurrentHashmapVal_t
Type used for keys and values — an unsigned big enough to hold a pointer.
Definition: ConcurrentHashmapImpl.h:40
IsUpdater.h
Concept check for Updater class used by concurrent classes.
skel.it
it
Definition: skel.GENtoEVGEN.py:407
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:157
columnar::operator=
AccessorTemplate & operator=(AccessorTemplate &&that)
Definition: VectorColumn.h:88
ConcurrentStrToValMap.icc
XMLtoHeader.count
count
Definition: XMLtoHeader.py:84
CxxUtils::fpcompare::equal
bool equal(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:114
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
calibdata.valid
list valid
Definition: calibdata.py:44
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
CxxUtils
Definition: aligned_vector.h:29
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: hcg.cxx:111
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
VALUE
#define VALUE(TESTED)
Definition: expect.h:59
hist_file_dump.f
f
Definition: hist_file_dump.py:140
UIntConv.h
Helpers for converting between uintptr_t and a pointer or integer.
CxxUtils::requires
requires(detail::IsConcurrentHashmapPayload< KEY > &&detail::IsConcurrentHashmapPayload< VALUE > &&detail::IsUpdater< UPDATER > &&detail::IsHash< HASHER, KEY > &&detail::IsBinaryPredicate< MATCHER, KEY >) class ConcurrentMap
Hash map from integers/pointers allowing concurrent, lockless reads.
Definition: ConcurrentMap.h:94
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
CxxUtils::end
auto end(range_with_at< T > &s)
Definition: range_with_at.h:68
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
a
TList * a
Definition: liststreamerinfos.cxx:10
columnar::empty
bool empty() const noexcept
Definition: ObjectRange.h:163
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
DeMoScan.first
bool first
Definition: DeMoScan.py:534
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
columnar::operator()
decltype(auto) operator()(ObjectId< CI, CM > id) const noexcept
Definition: ColumnAccessor.h:173
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
CxxUtils::begin
auto begin(range_with_at< T > &s)
Definition: range_with_at.h:64
ConcurrentHashmapImpl.h
Hash table allowing concurrent, lockless reads.