ATLAS Offline Software
MultiDimArray.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef MUONCALIBIDENTIFIER_MULTIDIMARRAY_H
6 #define MUONCALIBIDENTIFIER_MULTIDIMARRAY_H
7 
8 /***************************************************************************
9  * Variable Multidimensional Array
10  * -----------------------------------------
11  *
12  * Author : Martin Woudstra
13  * Creation Date: 20 April 2004
14  * Last Update : 26 April 2005
15  ***************************************************************************/
16 
17 #include <iostream>
18 #include <sstream>
19 #include <limits.h>
20 #include <float.h>
21 
22 // general default value calls default constructor
23 
24 template <class T, unsigned int N> class MultiDimArray;
25 
29 template <class T, unsigned int I>
31 public:
35  typedef MultiDimArray<T,I-1> SubType;
37  static ThisType defaultThisType();
38 };
39 
43 template<class T>
45 public:
47  typedef typename T::ValueType ThisType;
49  static ThisType defaultThisType() { return T::defaultValue(); }
50  // no subtype
51 };
52 
56 template<class T>
58 public:
64  static ThisType defaultThisType();
65 };
66 
72 template <class T, unsigned int N>
73 class MultiDimArray {
74 public:
75  //
76  // several needed type definitions
77  //
80  typedef typename T::ValueType ValueType;
81  // default (invalid) values for subtypes
84  static const SubType& invalidSubType();
89  void clear();
90  /* Read-write access to subtype at index. If subtype at index does not exist,
91  it will be created. */
93  /* Read-only access to subtype at index. If subtype at index does not exist,
94  an invalid subtype is returned. */
95  const SubType& operator[]( int index ) const;
96  /* Minimum index of this field */
97  int minIndex() const;
98  /* Maximum index of this field */
99  int maxIndex() const;
101  unsigned int size() const;
104  unsigned int depth() const;
106  bool isInRange( int index ) const;
109  unsigned int totalSize() const;
111  unsigned int validSize() const;
113  void dump( std::ostream& os = std::cout ) const;
115  void dump( const std::string& prefix, std::ostream& os = std::cout ) const;
117  std::string dumpToString() const;
119  std::string dumpToString( const std::string& prefix ) const;
121  template< class K >
122  void dumpOneEntry( const K& indices, std::ostream& os = std::cout ) const;
124  template< class K >
125  std::string dumpOneEntryToString( const K& indices ) const;
126 private:
129  unsigned int m_size;
131 #ifdef MULTIDIMARRAY_DEBUG
132  static unsigned int s_objectCount;
133 #endif
134 };
135 
136 template <class T, unsigned int I>
138  return ThisType();
139 }
140 
141 template <class T>
143  return ThisType();
144 }
145 
146 template <class T, unsigned int N>
149 }
150 
151 template <class T, unsigned int N>
154 }
155 
156 template <class T, unsigned int N>
158  return s_invalidSubType;
159 }
160 
161 template <class T, unsigned int N>
163  : m_data(0), m_minIndex(0), m_size(0)
164 {
165 #ifdef MULTIDIMARRAY_DEBUG
166  ++s_objectCount;
167  MsgStream log(Athena::getMessageSvc(),"MultiDimArray<T,N>");
168  log<<MSG::DEBUG<<"Creating MultiDimArray<" << N << ">" << " at " << this << " #objects: " << s_objectCount<<endmsg;
169 #endif
170 }
171 
172 template <class T, unsigned int N>
174 #ifdef MULTIDIMARRAY_DEBUG
175  --s_objectCount;
176  MsgStream log(Athena::getMessageSvc(),"MultiDimArray<T,N>");
177  log<<MSG::DEBUG<<"Deleting MultiDimArray<" << N << ">" << " at " << this << " #objects: " << s_objectCount<<endmsg;
178 #endif
179  delete[] m_data;
180 }
181 
182 template <class T, unsigned int N>
184  : m_data(0), m_minIndex(0), m_size(0)
185 {
186 #ifdef MULTIDIMARRAY_DEBUG
187  ++s_objectCount;
188  MsgStream log(Athena::getMessageSvc(),"MultiDimArray<T,N>");
189  log<<MSG::DEBUG<<"Copying MultiDimArray<" << N << ">" << " at " << this << " #objects: " << s_objectCount<<endmsg;
190 #endif
191  operator=( rhs );
192 }
193 
194 template <class T, unsigned int N>
196  delete[] m_data;
197  m_data = 0;
198  m_size = 0;
199  m_minIndex = 0;
200 }
201 
202 template <class T, unsigned int N>
203 inline int MultiDimArray<T,N>::minIndex() const {
204  return m_minIndex;
205 }
206 
207 template <class T, unsigned int N>
208 inline int MultiDimArray<T,N>::maxIndex() const {
209  return m_minIndex + m_size - 1;
210 }
211 
212 template <class T, unsigned int N>
213 inline unsigned int MultiDimArray<T,N>::size() const {
214  return m_size;
215 }
216 
217 template <class T, unsigned int N>
218 inline unsigned int MultiDimArray<T,N>::depth() const {
219  return N;
220 }
221 
222 template <class T, unsigned int N>
223 inline bool MultiDimArray<T,N>::isInRange( int index ) const {
224  return index >= minIndex() && index <= maxIndex();
225 }
226 
227 template <class T, unsigned int N>
229 #ifdef MULTIDIMARRAY_DEBUG
230  MsgStream log(Athena::getMessageSvc(),"MultiDimArray<T,N>");
231  log<<MSG::DEBUG<<"Assigning MultiDimArray<" << N << ">" << " to " << this << " from " << &rhs<<endmsg;
232 #endif
233  // ensure equal data size
234  if ( m_size != rhs.m_size ) {
235  delete[] m_data;
236  m_size = rhs.m_size;
237  if ( m_size ) {
238  m_data = new SubType[m_size];
239  } else {
240  m_data = 0;
241  }
242  }
243  // copy the data
244  for ( unsigned int i = 0; i < m_size; ++i ) m_data[i] = rhs.m_data[i];
245  // set others
246  m_minIndex = rhs.m_minIndex;
247 
248  return *this;
249 }
250 
251 template <class T, unsigned int N>
253  if ( !isInRange( index ) ) {
254  MsgStream log(Athena::getMessageSvc(),"MultiDimArray<T,N>");
255  log<<MSG::WARNING<<"MultiDimArray<" << N << ">::operator["<< index << "]" << " index out of range (" << minIndex() << "," << maxIndex() << ")"<<endmsg;
256  return s_invalidSubType;
257  }
258  return m_data[index - m_minIndex];
259 }
260 
261 template <class T, unsigned int N>
263 #ifdef MULTIDIMARRAY_DEBUG
264  MsgStream log(Athena::getMessageSvc(),"MultiDimArray<T,N>");
265 #endif if ( !m_data ) {
266  m_minIndex = index;
267  m_size = 1;
268  m_data = new SubType[1];
269  m_data[0] = s_invalidSubType;
270 #ifdef MULTIDIMARRAY_DEBUG
271  log<<MSG::DEBUG<<"MultiDimArray<" << N << ">::operator["<< index << "]" << " at " << this << ":" << " new data array. size=" << m_size << " range=(" << minIndex() << "," << maxIndex() << ")"<<endmsg;
272 #endif
273  } else if ( index > maxIndex() ) {
274  // add to vector at back
275  unsigned int oldSize = m_size;
276  int nAdd = index - maxIndex();
277  m_size += nAdd;
278  SubType* newData = new SubType[m_size];
279  // copy old data and set new data to invalid
280  unsigned int i = 0;
281  for ( ; i < oldSize; ++i ) newData[i] = m_data[i];
282  for ( ; i < m_size ; ++i ) newData[i] = s_invalidSubType;
283  delete[] m_data;
284  m_data = newData;
285 #ifdef MULTIDIMARRAY_DEBUG
286  log<<MSG::DEBUG<<"MultiDimArray<" << N << ">::operator["<< index << "]" << " at " << this << ":" << " added " << nAdd << " at back. size=" << m_size << " range=(" << minIndex() << "," << maxIndex() << ")"<<endmsg;
287 #endif
288  } else if ( index < minIndex() ) {
289  // add to vector at front
290  unsigned int nAdd = minIndex() - index;
291  m_size += nAdd;
292  SubType* newData = new SubType[m_size];
293  // copy old data and set new data to invalid
294  unsigned int i = 0;
295  for ( ; i < nAdd ; ++i ) newData[i] = s_invalidSubType;
296  for ( ; i < m_size; ++i ) newData[i] = m_data[i-nAdd];
297  delete[] m_data;
298  m_data = newData;
299  m_minIndex = index;
300 #ifdef MULTIDIMARRAY_DEBUG
301  log<<MSG::DEBUG<<"MultiDimArray<" << N << ">::operator["<< index << "]" << " at " << this << ":" << " added " << nAdd << " at front. size=" << m_size << " range=(" << minIndex() << "," << maxIndex() << ")"<<endmsg;
302 #endif
303  }
304  // return ref to indexed value
305  return m_data[index - m_minIndex];
306 }
307 
308 
309 template <class T, unsigned int N>
310 static unsigned int totalSize( const MultiDimArray<T,N>& ht ) {
311  unsigned int nTotal = 0;
312  int idxMin = ht.minIndex();
313  int idxMax = ht.maxIndex();
314  for ( int idx = idxMin; idx <= idxMax; ++idx ) {
315  nTotal += totalSize( ht[idx] );
316  }
317  return nTotal;
318 }
319 
320 template <class T, unsigned int N>
321 static unsigned int validSize( const MultiDimArray<T,N>& ht ) {
322  unsigned int nTotal = 0;
323  int idxMin = ht.minIndex();
324  int idxMax = ht.maxIndex();
325  for ( int idx = idxMin; idx <= idxMax; ++idx ) {
326  nTotal += validSize( ht[idx] );
327  }
328  return nTotal;
329 }
330 
331 template<class T>
332 static unsigned int totalSize( const MultiDimArray<T,1>& ht ) {
333  return ht.size();
334 }
335 
336 template<class T>
337 static unsigned int validSize( const MultiDimArray<T,1>& ht ) {
338  unsigned int cnt = 0;
339  int idxMin = ht.minIndex();
340  int idxMax = ht.maxIndex();
341  for ( int idx = idxMin; idx <= idxMax; ++idx ) {
343  }
344  return cnt;
345 }
346 
347 template <class T, unsigned int N>
348 inline unsigned int MultiDimArray<T,N>::totalSize() const {
349  return ::totalSize( *this );
350 }
351 
352 template <class T, unsigned int N>
353 inline unsigned int MultiDimArray<T,N>::validSize() const {
354  return ::validSize( *this );
355 }
356 
357 template <class T, unsigned int N>
358 void dump( const MultiDimArray<T,N>& idh, std::ostream& os = std::cout,
359  const std::string& prefix = "(" ) {
360  int idxMin = idh.minIndex();
361  int idxMax = idh.maxIndex();
362  for ( int idx = idxMin; idx <= idxMax; ++idx ) {
363  std::ostringstream oss;
364  oss << idx << ",";
365  dump( idh[idx], os, prefix + oss.str() );
366  }
367 }
368 
369 template <class T>
370 void dump( const MultiDimArray<T,1>& idh, std::ostream& os = std::cout,
371  const std::string& prefix = "(" ) {
372  int idxMin = idh.minIndex();
373  int idxMax = idh.maxIndex();
374  for ( int idx = idxMin; idx <= idxMax; ++idx ) {
375  os << prefix << idx << "): hash=" << idh[idx] << "\n";
376  }
377 }
378 
379 template <class T, unsigned int N>
380 inline void MultiDimArray<T,N>::dump( std::ostream& os ) const {
381  ::dump( *this, os );
382  os.flush();
383 }
384 
385 template <class T, unsigned int N>
386 inline void MultiDimArray<T,N>::dump( const std::string& prefix, std::ostream& os ) const {
387  ::dump( *this, os, prefix + "(" );
388  os.flush();
389 }
390 
391 template <class T, unsigned int N>
392 inline std::string MultiDimArray<T,N>::dumpToString() const {
393  std::ostringstream oss;
394  dump( oss );
395  return oss.str();
396 }
397 
398 template <class T, unsigned int N>
399 inline std::string MultiDimArray<T,N>::dumpToString( const std::string& prefix ) const {
400  std::ostringstream oss;
401  dump( prefix, oss );
402  return oss.str();
403 }
404 
405 template <class T, unsigned int N, class K>
406 void dumpOneEntry( const MultiDimArray<T,N>& idh, const K& indices,
407  std::ostream& os = std::cout, const std::string& prefix = "(" ) {
408  int idx = indices[N-1];
409  std::ostringstream oss;
410  oss << idx << ",";
411  dumpOneEntry( idh[idx], indices, os, prefix + oss.str() );
412 }
413 
414 template<class T, class K>
415 void dumpOneEntry( const MultiDimArray<T,1>& idh, const K& indices,
416  std::ostream& os = std::cout, const std::string& prefix = "(" ) {
417  int idx = indices[0];
418  os << prefix << idx << ")=" << idh[idx];
419 }
420 
421 template <class T, unsigned int N>
422 template <class K>
423 inline void MultiDimArray<T,N>::dumpOneEntry( const K& indices, std::ostream& os ) const {
424  ::dumpOneEntry( *this, indices, os );
425 }
426 
427 template <class T, unsigned int N>
428 template< class K >
429 std::string MultiDimArray<T,N>::dumpOneEntryToString( const K& indices ) const {
430  std::ostringstream oss;
431  dumpOneEntry( indices, oss );
432  return oss.str();
433 }
434 
435 // to ease output
436 template <class T, unsigned int N>
437 std::ostream& operator<<( std::ostream& os, const MultiDimArray<T,N>& idh ) {
438  dump( idh, os );
439  return os;
440 }
441 
442 //
443 //static members
444 //
445 template <class T, unsigned int N>
447 
448 #ifdef MULTIDIMARRAY_DEBUG
449 template <class T, unsigned int N>
450 unsigned int MultiDimArray<T,N>::s_objectCount = 0;
451 #endif
452 
453 
454 
455 
456 #endif // MUONCALIBIDENTIFIER_MULTIDIMARRAY_H
MultiDimArray::invalidSubType
static const SubType & invalidSubType()
Definition: MultiDimArray.h:157
MultiDimArray::minIndex
int minIndex() const
Definition: MultiDimArray.h:203
MultiDimArray::size
unsigned int size() const
Size of this field.
Definition: MultiDimArray.h:213
MultiDimArray::m_minIndex
int m_minIndex
Definition: MultiDimArray.h:128
MultiDimArrayTypes< T, 0 >::defaultThisType
static ThisType defaultThisType()
Definition: MultiDimArray.h:49
MultiDimArrayTypes< T, 1 >::ThisType
MultiDimArray< T, 1 > ThisType
define ThisType as a MultiDimArray<T,N> with N=1
Definition: MultiDimArray.h:60
MultiDimArray::dump
void dump(const std::string &prefix, std::ostream &os=std::cout) const
Dump the complete table to an output stream, with an additional prefix before each line.
Definition: MultiDimArray.h:386
MultiDimArray::clear
void clear()
Definition: MultiDimArray.h:195
MultiDimArray::depth
unsigned int depth() const
Depth of this field, i.e.
Definition: MultiDimArray.h:218
index
Definition: index.py:1
m_data
std::vector< T > m_data
Definition: TrackTruthMatchingBaseAlg.cxx:660
Trk::indices
std::pair< long int, long int > indices
Definition: AlSymMatBase.h:24
MultiDimArray::isInRange
bool isInRange(int index) const
Check that an index is in the range of the this field.
Definition: MultiDimArray.h:223
MultiDimArray::operator[]
SubType & operator[](int index)
Definition: MultiDimArray.h:262
MultiDimArray::m_data
SubType * m_data
Definition: MultiDimArray.h:127
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
MultiDimArray::dumpToString
std::string dumpToString(const std::string &prefix) const
Dump the complete table into a string, with an additional prefix before each line.
Definition: MultiDimArray.h:399
MultiDimArray::ValueType
T::ValueType ValueType
Definition: MultiDimArray.h:80
MultiDimArray::~MultiDimArray
~MultiDimArray()
Definition: MultiDimArray.h:173
MultiDimArray::m_size
unsigned int m_size
Definition: MultiDimArray.h:129
MultiDimArray::validSize
unsigned int validSize() const
The total number of valid (=non-default) elements from this field downwards.
Definition: MultiDimArray.h:353
MultiDimArrayTypes::ThisType
MultiDimArray< T, I > ThisType
define ThisType as a MultiDimArray<T,I>
Definition: MultiDimArray.h:33
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
MultiDimArray::MultiDimArray
MultiDimArray()
Definition: MultiDimArray.h:162
MultiDimArray::dumpOneEntryToString
std::string dumpOneEntryToString(const K &indices) const
Dump one entry with given indices into a string.
Definition: MultiDimArray.h:429
lumiFormat.i
int i
Definition: lumiFormat.py:92
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
MultiDimArray::maxIndex
int maxIndex() const
Definition: MultiDimArray.h:208
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
MultiDimArrayTypes
General recursive subtyping trait.
Definition: MultiDimArray.h:30
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
MultiDimArray::ThisType
MultiDimArray< T, N > ThisType
Definition: MultiDimArray.h:78
MultiDimArray::operator[]
const SubType & operator[](int index) const
Definition: MultiDimArray.h:252
MultiDimArray::totalSize
unsigned int totalSize() const
The total number of elements, including invalid (=default) holes, from this field downwards.
Definition: MultiDimArray.h:348
MultiDimArray::dumpOneEntry
void dumpOneEntry(const K &indices, std::ostream &os=std::cout) const
Dump one entry with given indices to an output stream.
Definition: MultiDimArray.h:423
MultiDimArray::defaultValueType
static ValueType defaultValueType()
Definition: MultiDimArray.h:152
MultiDimArrayTypes< T, 1 >::SubType
MultiDimArrayTypes< T, 0 >::ThisType SubType
define the SubType as MultiDimArrayTypes<T,0>::ThisType
Definition: MultiDimArray.h:62
dumpOneEntry
void dumpOneEntry(const MultiDimArray< T, N > &idh, const K &indices, std::ostream &os=std::cout, const std::string &prefix="(")
Definition: MultiDimArray.h:406
MultiDimArray::dump
void dump(std::ostream &os=std::cout) const
Dump the complete table to an output stream.
Definition: MultiDimArray.h:380
MultiDimArrayTypes< T, 0 >::ThisType
T::ValueType ThisType
define ThisType as T::ValueType
Definition: MultiDimArray.h:47
dump
void dump(const MultiDimArray< T, N > &idh, std::ostream &os=std::cout, const std::string &prefix="(")
Definition: MultiDimArray.h:358
DeMoScan.index
string index
Definition: DeMoScan.py:362
MultiDimArray::s_invalidSubType
static SubType s_invalidSubType
Definition: MultiDimArray.h:130
trigbs_pickEvents.cnt
cnt
Definition: trigbs_pickEvents.py:71
MultiDimArray::dumpToString
std::string dumpToString() const
Dump the complete table into a string.
Definition: MultiDimArray.h:392
DEBUG
#define DEBUG
Definition: page_access.h:11
MultiDimArrayTypes::SubType
MultiDimArray< T, I-1 > SubType
define the SubType as MultiDimArrayTypes<T,I-1>
Definition: MultiDimArray.h:35
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
MultiDimArray::MultiDimArray
MultiDimArray(const MultiDimArray &)
Definition: MultiDimArray.h:183
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
python.copyTCTOutput.totalSize
totalSize
Definition: copyTCTOutput.py:93
operator<<
std::ostream & operator<<(std::ostream &os, const MultiDimArray< T, N > &idh)
Definition: MultiDimArray.h:437
I
#define I(x, y, z)
Definition: MD5.cxx:116
top::ht
double ht(const top::Event &event)
HT calculation.
Definition: EventTools.cxx:33
MultiDimArray::defaultSubType
static SubType defaultSubType()
Definition: MultiDimArray.h:147
MultiDimArray::operator=
MultiDimArray & operator=(const MultiDimArray &)
Definition: MultiDimArray.h:228
MultiDimArray
Multi-dimensional array with a compile-time number of dimensions, and a run-time complete freedom ove...
Definition: MultiDimArray.h:24
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
MultiDimArray::SubType
MultiDimArrayTypes< T, N >::SubType SubType
Definition: MultiDimArray.h:79
MultiDimArrayTypes::defaultThisType
static ThisType defaultThisType()
Definition: MultiDimArray.h:137