ATLAS Offline Software
ConcurrentStrMap.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-2023 CERN for the benefit of the ATLAS collaboration.
4  */
13 #ifndef CXXUTILS_CONCURRENTSTRMAP_H
14 #define CXXUTILS_CONCURRENTSTRMAP_H
15 
16 
18 #include "CxxUtils/UIntConv.h"
19 #include "CxxUtils/concepts.h"
20 #include "CxxUtils/IsUpdater.h"
21 #include "boost/iterator/iterator_facade.hpp"
22 #include "boost/range/iterator_range.hpp"
23 #include <type_traits>
24 #include <stdexcept>
25 
26 
27 namespace CxxUtils {
28 
29 
74 template <class VALUE, template <class> class UPDATER>
75 ATH_REQUIRES (detail::IsConcurrentHashmapPayload<VALUE> &&
76  detail::IsUpdater<UPDATER>)
77 class ConcurrentStrMap
78 {
79 private:
81  struct Hasher;
82  struct Matcher;
86 
87 
88 public:
90  using key_type = std::string;
91  using mapped_type = VALUE;
92  using size_type = size_t;
94  using Updater_t = typename Impl_t::Updater_t;
96  using Context_t = typename Updater_t::Context_t;
98  using Lock_t = typename Impl_t::Lock_t;
99 
101  static_assert( sizeof(typename Impl_t::val_t) >= sizeof(mapped_type) );
102 
103 
112  ConcurrentStrMap (Updater_t&& updater,
113  size_type capacity = 64,
114  const Context_t& ctx = Updater_t::defaultContext());
115 
126  ConcurrentStrMap (const ConcurrentStrMap& other,
127  Updater_t&& updater,
128  size_t capacity = 64,
129  const Context_t& ctx = Updater_t::defaultContext());
130 
131 
144  template <class InputIterator>
145  ConcurrentStrMap (InputIterator f,
146  InputIterator l,
147  Updater_t&& updater,
148  size_type capacity = 64,
149  const Context_t& ctx = Updater_t::defaultContext());
150 
151 
153  ConcurrentStrMap (const ConcurrentStrMap& other) = delete;
154  ConcurrentStrMap (ConcurrentStrMap&& other) = delete;
155  ConcurrentStrMap& operator= (const ConcurrentStrMap& other) = delete;
156  ConcurrentStrMap& operator= (ConcurrentStrMap&& other) = delete;
157 
158 
162  ~ConcurrentStrMap();
163 
164 
168  size_type size() const;
169 
170 
174  bool empty() const;
175 
176 
180  size_t capacity() const;
181 
182 
195  using const_iterator_value = std::pair<const key_type&, mapped_type>;
196 
197 
207  class const_iterator
208  : public boost::iterator_facade<const_iterator,
209  const const_iterator_value,
210  std::bidirectional_iterator_tag,
211  const const_iterator_value>
212  {
213  public:
218  const_iterator (typename Impl_t::const_iterator it);
219 
220 
226  bool valid() const;
227 
228 
229  private:
231  friend class boost::iterator_core_access;
232 
233 
237  void increment();
238 
239 
243  void decrement();
244 
245 
249  bool equal (const const_iterator& other) const;
250 
251 
255  const const_iterator_value dereference() const;
256 
257 
259  typename Impl_t::const_iterator m_impl;
260  };
261 
262 
264  typedef boost::iterator_range<const_iterator> const_iterator_range;
265 
266 
270  const_iterator_range range() const;
271 
272 
276  const_iterator begin() const;
277 
278 
282  const_iterator end() const;
283 
284 
288  const_iterator cbegin() const;
289 
290 
294  const_iterator cend() const;
295 
296 
301  bool contains (const key_type& key) const;
302 
303 
310  size_type count (const key_type& key) const;
311 
312 
319  const_iterator find (const key_type& key) const;
320 
321 
329  mapped_type at (const std::string& key) const;
330 
331 
339  std::pair<const_iterator, const_iterator>
340  equal_range (const key_type& key) const;
341 
342 
351  Lock_t lock();
352 
353 
365  std::pair<const_iterator, bool>
366  emplace (const key_type& key, mapped_type val,
367  const Context_t& ctx = Updater_t::defaultContext());
368 
369 
382  std::pair<const_iterator, bool>
383  emplace (const Lock_t& lock,
384  const key_type& key, mapped_type val,
385  const Context_t& ctx = Updater_t::defaultContext());
386 
387 
399  std::pair<const_iterator, bool>
400  emplace (key_type&& key, mapped_type val,
401  const Context_t& ctx = Updater_t::defaultContext());
402 
403 
416  std::pair<const_iterator, bool>
417  emplace (const Lock_t& lock,
418  key_type&& key, mapped_type val,
419  const Context_t& ctx = Updater_t::defaultContext());
420 
421 
433  std::pair<const_iterator, bool>
434  insert_or_assign (const key_type& key, mapped_type val,
435  const Context_t& ctx = Updater_t::defaultContext());
436 
437 
451  std::pair<const_iterator, bool>
452  insert_or_assign (const Lock_t& lock,
453  const key_type& key, mapped_type val,
454  const Context_t& ctx = Updater_t::defaultContext());
455 
456 
468  std::pair<const_iterator, bool>
469  insert_or_assign (key_type&& key, mapped_type val,
470  const Context_t& ctx = Updater_t::defaultContext());
471 
472 
486  std::pair<const_iterator, bool>
487  insert_or_assign (const Lock_t& lock,
488  key_type&& key, mapped_type val,
489  const Context_t& ctx = Updater_t::defaultContext());
490 
491 
506  template <class PAIR>
507  std::pair<const_iterator, bool> insert (const PAIR& p,
508  const Context_t& ctx = Updater_t::defaultContext());
509 
510 
523  template <class PAIR>
524  std::pair<const_iterator, bool> insert (PAIR&& p,
525  const Context_t& ctx = Updater_t::defaultContext());
526 
527 
537  template <class InputIterator>
538  void insert (InputIterator first, InputIterator last,
539  const Context_t& ctx = Updater_t::defaultContext());
540 
541 
550  void reserve (size_type capacity,
551  const Context_t& ctx = Updater_t::defaultContext());
552 
553 
561  void rehash (size_type capacity);
562 
563 
569  void quiescent (const Context_t& ctx);
570 
571 
582  void swap (ConcurrentStrMap& other);
583 
584 
588  Updater_t& updater();
589 
590 
591 private:
596  static const std::string* keyAsString (val_t val);
597 
598 
603  static val_t keyAsVal (const std::string* s);
604 
605 
610  static mapped_type mappedAsMapped (val_t val);
611 
612 
617  static val_t mappedAsVal (mapped_type val);
618 
619 
627  typename Impl_t::const_iterator get (const key_type& key) const;
628 
629 
641  std::pair<const_iterator, bool>
642  put (std::unique_ptr<key_type> key,
643  mapped_type val,
644  bool overwrite = true,
645  const Context_t& ctx = Updater_t::defaultContext());
646 
647 
659  std::pair<const_iterator, bool>
660  put (const Lock_t& lock,
661  std::unique_ptr<key_type> key,
662  mapped_type val,
663  bool overwrite = true,
664  const Context_t& ctx = Updater_t::defaultContext());
665 
666 
673  struct Hasher
674  {
676  size_t operator() (const val_t p) const;
678  size_t operator() (const std::string& s) const;
680  std::hash<std::string> m_hash;
681  };
682 
683 
687  struct Matcher
688  {
690  bool operator() (const val_t a, const val_t b) const;
691  };
692 
693 
695  Impl_t m_impl;
696 };
697 
698 
699 } // namespace CxxUtils
700 
701 
703 
704 
705 #endif // not CXXUTILS_CONCURRENTSTRMAP_H
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:41
IsUpdater.h
Concept check for Updater class used by concurrent classes.
skel.it
it
Definition: skel.GENtoEVGEN.py:423
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
CxxUtils::detail::ConcurrentHashmapImpl< UPDATER, Hasher, Matcher >
ConcurrentStrMap.icc
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
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:45
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:195
VALUE
#define VALUE(TESTED)
Definition: expect.h:59
UIntConv.h
Helpers for converting between uintptr_t and a pointer or integer.
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:77
concepts.h
Compatibility helpers for using some pieces of C++20 concepts with older compilers.
a
TList * a
Definition: liststreamerinfos.cxx:10
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
CxxUtils::ATH_REQUIRES
ATH_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
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.