ATLAS Offline Software
Loading...
Searching...
No Matches
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
17
18template<class T>
20
21public:
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
68private:
69 std::vector<std::atomic<T>> m_vec;
70 const T m_emptyValue;
71};
72
73template<class T>
74std::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
84template<class T>
86 for(auto &x : m_vec) x.store(emptyValue(), std::memory_order_relaxed);
87}
88
89template<class T>
90bool IdentifiableValueCache<T>::setOrDrop(size_t i, const T &value){
91 T val = emptyValue();
92 return m_vec.at(i).compare_exchange_strong(val, value);
93}
94
95#endif
#define x
IdentifiableValueCache(const IdentifiableValueCache &)=delete
~IdentifiableValueCache()=default
const std::vector< std::atomic< T > > & rawReadAccess() const
T retrieve(size_t i)
Retrieve the Value stored in that hash.
const T & emptyValue() const
Return the empty value that is interpreted as an empty entry.
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...
std::vector< std::atomic< IDCInDetBSErrContainer::ErrorCode > > m_vec
T retrieve(size_t i) const
As above, but no cache extension.
bool present(size_t i) const
As above, but no cache extension.
std::vector< std::pair< size_t, T > > getAll() const
Make a vector of hashes and values, convenient for iteration and other uses.
bool present(size_t i)
Returns true if the value is set to anything but the emptyValue.
void forceReset()
Forceable empty the container, DO NOT USE THIS IN MT ENVIRONMENT.
bool setOrDrop(size_t i, const T &value)
Set the given hash to the value.
STL namespace.