ATLAS Offline Software
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-2023 CERN for the benefit of the ATLAS collaboration.
4  */
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"
22 #include "boost/iterator/iterator_facade.hpp"
23 #include "boost/range/iterator_range.hpp"
24 #include <memory>
25 #include <type_traits>
26 #include <stdexcept>
27 
28 
29 namespace CxxUtils {
30 
31 
94 template <class KEY, class VALUE, template <class> class UPDATER,
95  class HASHER = std::hash<KEY>,
96  class MATCHER = std::equal_to<KEY>,
98 ATH_REQUIRES (detail::IsConcurrentHashmapPayload<KEY> &&
99  detail::IsUpdater<UPDATER> &&
100  detail::IsHash<HASHER, KEY> &&
101  detail::IsBinaryPredicate<MATCHER, KEY>)
102 class ConcurrentToValMap
103 {
104 private:
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 
133 public:
135  using key_type = KEY;
136  using mapped_type = VALUE;
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 
152  ConcurrentToValMap (Updater_t&& updater,
153  size_type capacity = 64,
154  const Context_t& ctx = Updater_t::defaultContext());
155 
156 
167  ConcurrentToValMap (const ConcurrentToValMap& other,
168  Updater_t&& updater,
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,
188  Updater_t&& updater,
189  size_type capacity = 64,
190  const Context_t& ctx = Updater_t::defaultContext());
191 
192 
194  ConcurrentToValMap (const ConcurrentToValMap& other) = delete;
195  ConcurrentToValMap (ConcurrentToValMap&& other) = delete;
196  ConcurrentToValMap& operator= (const ConcurrentToValMap& other) = delete;
197  ConcurrentToValMap& operator= (ConcurrentToValMap&& other) = delete;
198 
199 
203  ~ConcurrentToValMap();
204 
205 
209  size_type size() const;
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 
252  class const_iterator
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 
314  const const_iterator_value dereference() const;
315 
316 
318  typename Impl_t::const_iterator m_impl;
319  };
320 
321 
335  class iterator
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 
390  const iterator_value dereference() const;
391 
392 
394  typename Impl_t::const_iterator m_impl;
395  };
396 
397 
399  typedef boost::iterator_range<const_iterator> const_iterator_range;
400  typedef boost::iterator_range<iterator> iterator_range;
401 
402 
406  const_iterator_range range() const;
407 
408 
415  iterator_range range();
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>
543  equal_range (const key_type key);
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>
574  emplace (key_type key, mapped_type&& val,
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 
650  void reserve (size_type capacity,
651  const Context_t& ctx = Updater_t::defaultContext());
652 
653 
661  void rehash (size_type capacity);
662 
663 
669  void quiescent (const Context_t& ctx);
670 
671 
682  void swap (ConcurrentToValMap& other);
683 
684 
688  Updater_t& updater();
689 
690 
691 private:
696  static key_type keyAsKey (val_t val);
697 
698 
703  static val_t keyAsVal (key_type k);
704 
705 
711  static mapped_type* mappedAsMapped (val_t val);
712 
713 
718  static val_t mappedAsVal (mapped_type* val);
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 
748  Impl_t m_impl;
749 };
750 
751 
752 } // namespace CxxUtils
753 
754 
756 
757 
758 #endif // not CXXUTILS_CONCURRENTTOVALMAP_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:41
IsUpdater.h
Concept check for Updater class used by concurrent classes.
ConcurrentToValMap.icc
skel.it
it
Definition: skel.GENtoEVGEN.py:396
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
CxxUtils::detail::ConcurrentHashmapImpl
Hash table allowing concurrent, lockless reads.
Definition: ConcurrentHashmapImpl.h:208
empty
bool empty(TH1 *h)
Definition: computils.cxx:295
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
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
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
hist_file_dump.f
f
Definition: hist_file_dump.py:135
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:536
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
fitman.k
k
Definition: fitman.py:528
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.