ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimBinArray.h
Go to the documentation of this file.
1// Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2#ifndef FPGATrackSimBinArray_H
3#define FPGATrackSimBinArray_H
4
30
31
32#include <vector>
33#include <string>
34#include <stdexcept>
35#include <utility>
36
38
39template <typename T>
41{
42public:
43 // constructors
45 FPGATrackSimBinArray(const std::vector<unsigned int> &dims, const T &initval)
46 {
47 setsize(dims, initval);
48 }
49
50 // resize the array (need to provide reference to initialize elements to)
51 void setsize(const std::vector<unsigned int> &dims, const T &initval)
52 {
53 m_dims = dims;
54
55 // Caclulate the number of entries and a "step"-size needed for translating
56 // N-dim indices into a 1-d index
57 m_entries = 1;
58 for (auto &dim : dims)
59 {
60 m_step.push_back(m_entries);
61 m_entries *= dim;
62 }
63
64 // Allocate 1-d array
65 m_data.resize(m_entries, initval);
66 }
67
68 // full size of the array (i.e. product of the size in each dimension)
69 unsigned int size() const { return m_entries; }
70
71 // access to internal 1-d std::vector as a const so that you can
72 // use various std::count and std::for_each on the elements
73 // note you can change the vector because it's const, but you can
74 // change the elements
75 const std::vector<T>& flatdata() const {return m_data;}
76
77 // get the array with the size in each dimension of the
78 const std::vector<unsigned int> &dims() const { return m_dims; }
79
80 // Check if index is a valid entry
81 bool isValid(const std::vector<unsigned int> &idx) const {
82 if (idx.size() != m_dims.size()) return false;
83 for (unsigned int i = 0; i < m_dims.size(); i++)
84 {
85 if (idx[i]>=m_dims[i]) return false;
86 }
87 return true;
88 }
89
90 // look up content by idx specified as a std::vector<unsigned int>
91 const T &operator[](const std::vector<unsigned int> &idx) const
92 {
93 if(idx.size() != m_step.size()) {
94 throw std::runtime_error("FPGATrackSimBinArray: index size does not match array dimensions");
95 }
96
97 // Translate N-dim index to 1-d index
98 unsigned int offset = 0;
99 for (unsigned int i = 0; i < m_step.size(); i++)
100 {
101 if (idx.at(i) >= m_dims.at(i))
102 {
103 throw std::runtime_error("FPGATrackSimBinArray: index out of range i=" + std::to_string(i) +
104 " idx[i]=" + std::to_string(idx.at(i)) +
105 " dims[i]=" + std::to_string(m_dims.at(i)) + "\n");
106 }
107 offset += idx.at(i) * m_step.at(i);
108 }
109
110 // Return data from 1-d vector
111 return m_data.at(offset);
112 }
113
114 // convient to also be able to use a signed integer vector
115 const T &operator[](const std::vector<int> &idx) const
116 {
117 std::vector<unsigned int> idx_unsigned;
118 for (auto &d : idx)
119 idx_unsigned.push_back(d);
120 return (*this)[idx_unsigned];
121 }
122
123 // make non-const version of the operator[]
124 // just call the const version and cast away the constness
125 // of the return value
126 T& operator[](const std::vector<int> &idx) {
127 T& retv ATLAS_THREAD_SAFE = const_cast<T&>(std::as_const(*this)[idx]);
128 return retv;
129 }
130 T& operator[](const std::vector<unsigned> &idx) {
131 T &retv ATLAS_THREAD_SAFE = const_cast<T &>(std::as_const(*this)[idx]);
132 return retv;
133 }
134
135 // ConstIterator implemenation
137 {
139 using iterator_category = std::forward_iterator_tag;
141 using difference_type = void;
144
145 // Constructor
146 ConstIterator(const std::vector<unsigned int> &idx,
147 const FPGATrackSimBinArray<T> &itrdata)
148 : m_idx(idx), m_itrdata(itrdata) {
149 if (m_idx.size() != m_itrdata.dims().size()) {
150 throw std::runtime_error(
151 "FPGATrackSimBinArray::Interator array size mismatch in "
152 "constructor");
153 }
154 }
155
156 ConstIterator &operator*() { return *this; }
157 ConstIterator *operator->() { return this; }
158
159 // Prefix increment
161 {
162 ++m_idx.at(0);
163 for (unsigned int i = 0; i < m_idx.size() - 1; i++)
164 {
165 if (m_idx.at(i) >= m_itrdata.dims().at(i))
166 {
167 ++m_idx.at(i + 1);
168 m_idx.at(i) = 0;
169 }
170 }
171 return *this;
172 }
173
174 const std::vector<unsigned int> &idx() const { return m_idx; }
175 const T &data() { return m_itrdata[m_idx]; }
176
177 // Postfix increment
179 {
180 ConstIterator tmp = *this;
181 ++(*this);
182 return tmp;
183 }
184
185 friend bool operator==(const ConstIterator &a, const ConstIterator &b) { return a.m_idx == b.m_idx; };
186 friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
187 {
188 return a.m_idx != b.m_idx;
189 };
190
191 private:
192 // current state of the iterator
193 std::vector<unsigned int> m_idx;
194
195 // reference back to the array being iterated over
197 };
198
199 // Iterator implemenation
200 struct Iterator : public ConstIterator
201 {
203 using iterator_category = std::forward_iterator_tag;
205 using difference_type = void;
208
209 // Constructor
210 Iterator(const std::vector<unsigned int> &idx,
212 : ConstIterator(idx,itrdata) {}
213
214 Iterator &operator*() { return *this; }
215 Iterator *operator->() { return this; }
216
217 // Prefix increment
219 {
220 ++(*(ConstIterator*)this);
221 return *this;
222 }
223
224 // Call const version and cast away const
225 T &data() {
226 T &retv ATLAS_THREAD_SAFE = const_cast<T &>(((ConstIterator*)this)->data());
227 return retv;
228 }
229
230 // Postfix increment
232 {
233 Iterator tmp = *this;
234 ++(*this);
235 return tmp;
236 }
237
238 friend bool operator==(const Iterator &a, const Iterator &b) { return (ConstIterator&)a == (ConstIterator&)b; }
239 friend bool operator!=(const Iterator &a, const Iterator &b) { return (ConstIterator&)a != (ConstIterator&)b; }
240 };
241
242 // ussual std iterator meanings of begin, end, and size
243 Iterator begin() { return Iterator(std::vector<unsigned int>(m_dims.size(), 0), *this); }
244 Iterator end()
245 {
246 std::vector<unsigned int> retv;
247 for (auto &d : m_dims)
248 retv.push_back(d - 1);
249 return ++Iterator(retv, *this);
250 }
251
252 auto begin() const { return ConstIterator(std::vector<unsigned int>(m_dims.size(), 0), *this); }
253 auto end() const
254 {
255 std::vector<unsigned int> retv;
256 for (auto &d : m_dims)
257 retv.push_back(d - 1);
258 return ++ConstIterator(retv, *this);
259 }
260
261
262private:
263 unsigned int m_entries{};
264 std::vector<unsigned int> m_dims{};
265 std::vector<unsigned int> m_step{};
266 std::vector<T> m_data{};
267};
268
269#endif // FPGATrackSimBinArray_H
static Double_t a
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
FPGATrackSimBinArray(const std::vector< unsigned int > &dims, const T &initval)
T & operator[](const std::vector< int > &idx)
unsigned int size() const
std::vector< T > m_data
FPGATrackSimBinArray()=default
std::vector< unsigned int > m_step
const std::vector< T > & flatdata() const
void setsize(const std::vector< unsigned int > &dims, const T &initval)
const std::vector< unsigned int > & dims() const
bool isValid(const std::vector< unsigned int > &idx) const
std::vector< unsigned int > m_dims
const T & operator[](const std::vector< int > &idx) const
const T & operator[](const std::vector< unsigned int > &idx) const
T & operator[](const std::vector< unsigned > &idx)
FPGATrackSimBinArray< T >::ConstIterator & reference
ConstIterator(const std::vector< unsigned int > &idx, const FPGATrackSimBinArray< T > &itrdata)
FPGATrackSimBinArray< T >::ConstIterator * pointer
std::forward_iterator_tag iterator_category
const FPGATrackSimBinArray< T > container_type
const std::vector< unsigned int > & idx() const
const FPGATrackSimBinArray< T > & m_itrdata
friend bool operator==(const ConstIterator &a, const ConstIterator &b)
friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
FPGATrackSimBinArray< T >::ConstIterator value_type
Iterator(const std::vector< unsigned int > &idx, FPGATrackSimBinArray< T > &itrdata)
FPGATrackSimBinArray< T > container_type
FPGATrackSimBinArray< T >::Iterator value_type
friend bool operator!=(const Iterator &a, const Iterator &b)
std::forward_iterator_tag iterator_category
FPGATrackSimBinArray< T >::Iterator * pointer
FPGATrackSimBinArray< T >::Iterator & reference
friend bool operator==(const Iterator &a, const Iterator &b)