ATLAS Offline Software
IdentifiableValueCache.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef EVENTCONTAINERS_IDENTIFIABLEVALUECACHE_H
6 #define EVENTCONTAINERS_IDENTIFIABLEVALUECACHE_H
7 
8 #include <atomic>
9 #include <vector>
10 #include <cstdlib>
11 
18 template<class T>
20 
21 public:
22 
23  typedef std::true_type thread_safe;
24 
26  const T& emptyValue() const {
27  return m_emptyValue;
28  }
29 
30  //Prevent accidental copying
32 
34  IdentifiableValueCache(size_t maxSize, T emptyValuein)
35  : m_vec(maxSize), m_emptyValue(std::move(emptyValuein))
36  {
37  for(auto &x : m_vec) x.store(emptyValue(), std::memory_order_relaxed);
38  }
39 
41  void forceReset();
42 
44  size_t maxSize() const { return m_vec.size(); }
45 
47 
49  T retrieve(size_t i){ return m_vec.at(i).load(); }
50 
52  T retrieve(size_t i) const { return i < maxSize() ? static_cast<T>(m_vec[i]) : emptyValue() ; }
53 
55  bool present(size_t i){ return m_vec.at(i).load() != emptyValue(); }
56 
58  bool present(size_t i) const { return i < maxSize() and m_vec[i] != emptyValue() ? true : false; }
59 
61  bool setOrDrop(size_t i, const T &value);
62 
64  std::vector<std::pair<size_t, T>> getAll() const;
65 
66  const std::vector<std::atomic<T>>& rawReadAccess() const { return m_vec; }
67 
68 private:
69  std::vector<std::atomic<T>> m_vec;
70  const T m_emptyValue;
71 };
72 
73 template<class T>
74 std::vector<std::pair<size_t, T>>
76  std::vector<std::pair<size_t, T>> list;
77  for(size_t i =0; i<m_vec.size(); i++){
78  T item = m_vec[i].load(std::memory_order_relaxed);
79  if(item!=m_emptyValue) list.emplace_back(i, std::move(item));
80  }
81  return list;
82 }
83 
84 template<class T>
86  for(auto &x : m_vec) x.store(emptyValue(), std::memory_order_relaxed);
87 }
88 
89 template<class T>
91  T val = emptyValue();
92  return m_vec.at(i).compare_exchange_strong(val, value);
93 }
94 
95 #endif
IdentifiableValueCache::IdentifiableValueCache
IdentifiableValueCache(size_t maxSize, T emptyValuein)
Pass the maximum hash to size the cache and the defaultValue which will be interpreted as an empty va...
Definition: IdentifiableValueCache.h:34
IdentifiableValueCache::getAll
std::vector< std::pair< size_t, T > > getAll() const
Make a vector of hashes and values, convenient for iteration and other uses.
Definition: IdentifiableValueCache.h:75
IdentifiableValueCache::setOrDrop
bool setOrDrop(size_t i, const T &value)
Set the given hash to the value.
Definition: IdentifiableValueCache.h:90
athena.value
value
Definition: athena.py:122
IdentifiableValueCache::rawReadAccess
const std::vector< std::atomic< T > > & rawReadAccess() const
Definition: IdentifiableValueCache.h:66
IdentifiableValueCache::emptyValue
const T & emptyValue() const
Return the empty value that is interpreted as an empty entry.
Definition: IdentifiableValueCache.h:26
IdentifiableValueCache::m_emptyValue
const T m_emptyValue
Definition: IdentifiableValueCache.h:70
x
#define x
IdentifiableValueCache::retrieve
T retrieve(size_t i)
Retrieve the Value stored in that hash.
Definition: IdentifiableValueCache.h:49
IdentifiableValueCache::present
bool present(size_t i) const
As above, but no cache extension.
Definition: IdentifiableValueCache.h:58
IdentifiableValueCache::present
bool present(size_t i)
Returns true if the value is set to anything but the emptyValue.
Definition: IdentifiableValueCache.h:55
lumiFormat.i
int i
Definition: lumiFormat.py:92
IdentifiableValueCache::IdentifiableValueCache
IdentifiableValueCache(const IdentifiableValueCache &)=delete
IdentifiableValueCache::m_vec
std::vector< std::atomic< T > > m_vec
Definition: IdentifiableValueCache.h:69
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
IdentifiableValueCache::retrieve
T retrieve(size_t i) const
As above, but no cache extension.
Definition: IdentifiableValueCache.h:52
item
Definition: ItemListSvc.h:43
IdentifiableValueCache::thread_safe
std::true_type thread_safe
Definition: IdentifiableValueCache.h:23
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
IdentifiableValueCache::maxSize
size_t maxSize() const
Return the maxSize of the collection.
Definition: IdentifiableValueCache.h:44
IdentifiableValueCache::~IdentifiableValueCache
~IdentifiableValueCache()=default
IdentifiableValueCache
This class is to provide an event wide MT container for concurrent storing of basic types,...
Definition: IdentifiableValueCache.h:19
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
IdentifiableValueCache::forceReset
void forceReset()
Forceable empty the container, DO NOT USE THIS IN MT ENVIRONMENT.
Definition: IdentifiableValueCache.h:85