ATLAS Offline Software
Control/CxxUtils/CxxUtils/Array.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 /*
4  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
5 */
6 
37 #ifndef CXXUTILS_ARRAY_H
38 #define CXXUTILS_ARRAY_H
39 
40 
41 #include "CxxUtils/Arrayrep.h"
42 #include "CxxUtils/concepts.h"
43 #include <iterator>
44 
45 
46 namespace CxxUtils {
47 
48 
49 // Forward declarations.
50 template <unsigned int N>
52 template <unsigned int N>
54 
55 
56 //**********************************************************************
57 
58 
73 template <unsigned int N>
75 {
76 public:
79 
80 
88  static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
89 };
90 
91 
100 template <>
102 {
103 public:
105  typedef const Arrayelt* const_iterator;
106 
107 
115  static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
116 };
117 
118 
119 //**********************************************************************
120 
121 
136 template <unsigned int N>
137 class Array
138 {
139 public:
141  enum { NDIM = N };
142 
145 
146 
156  Array ();
157 
166  Array (const Arrayrep& rep);
167 
173  bool valid() const;
174 
182  std::vector<unsigned int> shape() const;
183 
190  unsigned int size (unsigned int dim = 0) const;
191 
200  Array<N-1> operator[] (unsigned int i) const;
201 
208  const Arrayelt* ptr () const;
209 
210 
216 
217 
222  const_iterator end () const;
223 
224 
232  void write_array (std::ostream& stream) const;
233 
234 
235  // Protected, not private, so that they can be accessed by @c WritableArray.
236 protected:
246  Array (const Arrayrep& rep, unsigned int offs);
247 
248  // These classes need to call the above protected constructor.
249  friend class Array<N+1>;
251  friend class ArrayIterator<N+1>;
252 
255  const Arrayrep* m_rep;
256 
258  unsigned int m_offs;
259 };
260 
261 
271 template <>
272 class Array<0>
273 {
274 public:
276  enum { NDIM = 0 };
277 
286  Array ();
287 
296  Array (const Arrayrep& rep);
297 
303  bool valid() const;
304 
313  std::vector<unsigned int> shape() const;
314 
324  unsigned int size (unsigned int dim=0) const;
325 
330  operator Arrayelt() const;
331 
336  int asint () const;
337 
338 
346  void write_array (std::ostream& stream) const;
347 
348 
349  // Protected, not private, so that they can be accessed by @c WritableArray.
350 protected:
360  Array (const Arrayrep& rep, unsigned int offs);
361 
362  // This class needs to call the above protected constructor.
363  friend class Array<1>;
364  friend class WritableArray<1>;
365 
368  const Arrayelt* m_elt;
369 };
370 
371 
372 template <unsigned int N>
373 std::ostream& operator<< (std::ostream& s, const Array<N>& a);
374 
375 
376 //**********************************************************************
377 
378 
393 template <unsigned int N>
394 class ArrayIterator
395 {
396 public:
397  using iterator_category = std::random_access_iterator_tag;
398  using value_type = const Array<N-1>;
399  using difference_type = std::ptrdiff_t;
401 
402 
417  class pointer
418  {
419  public:
425 
426 
432 
433 
440  const value_type* operator-> () const;
441 
442 
443  private:
446  };
447 
448 
454 
455 
462  ArrayIterator (const Arrayrep* rep, unsigned int offs);
463 
464  // Use default copy ctor and assignment.
465 
471  bool operator== (const ArrayIterator& other) const;
472 
473 
479  bool operator!= (const ArrayIterator& other) const;
480 
481 
489  bool operator< (const ArrayIterator& other) const;
490 
491 
499  bool operator> (const ArrayIterator& other) const;
500 
501 
509  bool operator<= (const ArrayIterator& other) const;
510 
511 
519  bool operator>= (const ArrayIterator& other) const;
520 
521 
530 
531 
542 
543 
549 
550 
556 
557 
563 
564 
570 
571 
582 
583 
590 
591 
598 
599 
606 
607 
614 
615 
625 
626 
627 private:
629  const Arrayrep* m_rep;
630 
633  unsigned int m_offs;
634 };
635 
636 
637 //**********************************************************************
638 
639 
647 template <unsigned int N>
648 class WritableArray
649  : public Array<N>
650 {
651 public:
661 
670  WritableArray<N-1> operator[] (unsigned int i);
671 
680  Array<N-1> operator[] (unsigned int i) const;
681 
689 
690 
691 private:
701  WritableArray (Arrayrep& rep, unsigned int offs);
702 
703  // This class needs to call the above protected constructor.
704  friend class WritableArray<N+1>;
705 
708 };
709 
710 
718 template <>
720  : public Array<0>
721 {
722 public:
732 
740  WritableArray<0>& operator= (Arrayelt elt);
741 
742 
743 private:
753  WritableArray (Arrayrep& rep, unsigned int offs);
754 
755  // This class needs to call the above protected constructor.
756  friend class WritableArray<1>;
757 
761 };
762 
763 
764 //**********************************************************************
765 
766 
774 template <unsigned int N>
776  : private Arrayrep,
777  public WritableArray<N>
778 {
779 public:
787  WritableArrayData (const unsigned int shape[]);
788 
796  WritableArrayData (const std::vector<unsigned int>& shape);
797 };
798 
799 
805 template <class T>
806  ATH_REQUIRES(std::assignable_from<T&, float>)
808 
809 
815 template <unsigned int N>
817 
818 
819 } // namespace CxxUtils
820 
821 
822 #include "CxxUtils/Array.icc"
823 
824 
825 // Backwards compatibility.
826 namespace CaloRec {
827  using CxxUtils::Array;
830 }
831 
832 
833 #endif // not CXXUTILS_ARRAY_H
834 
CxxUtils::WritableArray< 0 >::m_elt_nc
Arrayelt * m_elt_nc
Pointer to this array's single element, non-const copy.
Definition: Control/CxxUtils/CxxUtils/Array.h:760
CxxUtils::WritableArray< 0 >
Definition: Control/CxxUtils/CxxUtils/Array.h:721
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
yodamerge_tmp.dim
dim
Definition: yodamerge_tmp.py:239
CxxUtils::ArrayIteratorChooser
Helper for defining iterators over Array's.
Definition: Control/CxxUtils/CxxUtils/Array.h:75
CxxUtils::WritableArray
Read-write multidimensional array.
Definition: Control/CxxUtils/CxxUtils/Array.h:53
CxxUtils::ArrayIteratorChooser::make_iterator
static const_iterator make_iterator(const Arrayrep *rep, unsigned int offs)
Construct an Array<N>::const_iterator.
make_hlt_rep.rep
rep
Definition: make_hlt_rep.py:32
CxxUtils::WritableArray::m_rep_nc
Arrayrep * m_rep_nc
The underlying array representation, non-const copy.
Definition: Control/CxxUtils/CxxUtils/Array.h:707
CxxUtils::Array::const_iterator
ArrayIteratorChooser< N >::const_iterator const_iterator
The iterator for this container.
Definition: Control/CxxUtils/CxxUtils/Array.h:144
CxxUtils::Array::size
unsigned int size(unsigned int dim=0) const
Return the size of the array along one dimension.
CxxUtils::Array::valid
bool valid() const
Test for validity.
CxxUtils::Array::operator[]
Array< N-1 > operator[](unsigned int i) const
Array indexing.
taskman.template
dictionary template
Definition: taskman.py:317
CxxUtils::Array< 0 >::shape
std::vector< unsigned int > shape() const
Return the array shape.
CxxUtils::operator<<
std::ostream & operator<<(std::ostream &s, const Array< N > &a)
CxxUtils::ArrayIterator::pointer::m_a
value_type m_a
The contained Array proxy instance.
Definition: Control/CxxUtils/CxxUtils/Array.h:445
CxxUtils::Array< 0 >::Array
Array(const Arrayrep &rep)
Constructor.
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:562
CxxUtils::Array::m_rep
const Arrayrep * m_rep
Pointer to the representation.
Definition: Control/CxxUtils/CxxUtils/Array.h:255
x
#define x
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
CxxUtils::ArrayIterator::operator->
pointer operator->() const
Dereference the iterator.
CxxUtils::ArrayIterator::operator--
ArrayIterator< N > & operator--()
Back up the iterator.
CxxUtils::ArrayIterator::operator[]
value_type operator[](difference_type n) const
Array indexing relative to the iterator.
CxxUtils::WritableArrayData::WritableArrayData
WritableArrayData(const unsigned int shape[])
Constructor.
CxxUtils::Array::end
const_iterator end() const
Return an iterator pointing past the end of the container.
CxxUtils::ArrayIterator::difference_type
std::ptrdiff_t difference_type
Definition: Control/CxxUtils/CxxUtils/Array.h:399
CxxUtils::ArrayIteratorChooser< 1 >::make_iterator
static const_iterator make_iterator(const Arrayrep *rep, unsigned int offs)
Construct an Array<1>::const_iterator.
CxxUtils::ArrayIteratorChooser::const_iterator
ArrayIterator< N > const_iterator
Iterator type for an Array<N>.
Definition: Control/CxxUtils/CxxUtils/Array.h:78
CxxUtils::ArrayIterator::pointer
Proxy to return from operator>.
Definition: Control/CxxUtils/CxxUtils/Array.h:418
CxxUtils::Array::NDIM
@ NDIM
Definition: Control/CxxUtils/CxxUtils/Array.h:141
CxxUtils::ArrayIterator::operator<=
bool operator<=(const ArrayIterator &other) const
Less-than-or-equal comparison.
CxxUtils::Array< 0 >::size
unsigned int size(unsigned int dim=0) const
Return the size of the array along one dimension.
CxxUtils::WritableArray< 0 >::WritableArray
WritableArray(Arrayrep &rep)
Constructor.
lumiFormat.i
int i
Definition: lumiFormat.py:92
CxxUtils::Array::ptr
const Arrayelt * ptr() const
Return a direct pointer to array elements.
Arrayrep.h
Representation class for Array's.
beamspotman.n
n
Definition: beamspotman.py:731
CxxUtils::ArrayIterator::operator!=
bool operator!=(const ArrayIterator &other) const
Inequality comparison.
CxxUtils::ArrayIterator::pointer::operator->
const value_type * operator->() const
Dereference the proxy.
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::Array
Read-only multidimensional array.
Definition: Control/CxxUtils/CxxUtils/Array.h:138
CxxUtils::WritableArrayData::WritableArrayData
WritableArrayData(const std::vector< unsigned int > &shape)
Constructor.
CxxUtils::WritableArrayData
Definition: Control/CxxUtils/CxxUtils/Array.h:778
CxxUtils::Array< 0 >::m_elt
const Arrayelt * m_elt
Pointer to this array's single element.
Definition: Control/CxxUtils/CxxUtils/Array.h:368
CxxUtils::ArrayIterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: Control/CxxUtils/CxxUtils/Array.h:397
CxxUtils::WritableArray::operator[]
WritableArray< N-1 > operator[](unsigned int i)
Array indexing.
CxxUtils::ArrayIterator::operator-
ArrayIterator< N > operator-(difference_type n) const
Return a new iterator pointing n steps behind.
CxxUtils::Array< 0 >::valid
bool valid() const
Test for validity.
CaloRec::Arrayelt
float Arrayelt
The type of an element of an Array.
Definition: Control/CxxUtils/CxxUtils/Arrayrep.h:26
CxxUtils::ArrayIteratorChooser< 1 >::const_iterator
const Arrayelt * const_iterator
Iterator type for an Array<1>.
Definition: Control/CxxUtils/CxxUtils/Array.h:105
CaloRec
Namespace for helper functions.
Definition: CaloCellFactor.h:22
CxxUtils::Array< 0 >::write_array
void write_array(std::ostream &stream) const
Creates a text representation of the array content.
CxxUtils::ArrayIterator::ArrayIterator
ArrayIterator(const Arrayrep *rep, unsigned int offs)
Constructor from Arrayrep and offset.
CxxUtils::ArrayIterator::m_offs
unsigned int m_offs
Offset into the representation's data array of the first element referred to by this iterator.
Definition: Control/CxxUtils/CxxUtils/Array.h:633
CxxUtils::ArrayIterator::operator+=
ArrayIterator< N > & operator+=(difference_type n)
Advance the iterator.
CxxUtils::ArrayIterator::operator==
bool operator==(const ArrayIterator &other) const
Equality comparison.
CxxUtils::ArrayIterator::pointer::pointer
pointer(const ArrayIterator &i)
Proxy constructor.
CxxUtils::Array::Array
Array()
Default constructor.
CxxUtils::ArrayIterator::operator+
ArrayIterator< N > operator+(difference_type n) const
Return a new iterator pointing n steps ahead.
concepts.h
Compatibility helpers for using some pieces of C++20 concepts with older compilers.
a
TList * a
Definition: liststreamerinfos.cxx:10
CxxUtils::ArrayIterator::m_rep
const Arrayrep * m_rep
The underlying array representation.
Definition: Control/CxxUtils/CxxUtils/Array.h:629
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
CxxUtils::Array::m_offs
unsigned int m_offs
Offset in the Arrayrep's data of the first element of this array.
Definition: Control/CxxUtils/CxxUtils/Array.h:258
CxxUtils::ArrayIterator::ArrayIterator
ArrayIterator()
Default constructor.
CxxUtils::WritableArray::ptr
Arrayelt * ptr()
Return a direct pointer to array elements.
CxxUtils::ArrayIterator::operator<
bool operator<(const ArrayIterator &other) const
Less-than comparison.
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
CaloRec::Arrayrep
Representation class for Array's.
Definition: Control/CxxUtils/CxxUtils/Arrayrep.h:62
CxxUtils::ArrayIterator::operator-=
ArrayIterator< N > & operator-=(difference_type n)
Back up the iterator.
CxxUtils::Array::write_array
void write_array(std::ostream &stream) const
Creates a text representation of the array content.
CxxUtils::ArrayIterator::operator++
ArrayIterator< N > & operator++()
Advance the iterator.
CxxUtils::ArrayIterator::operator*
value_type operator*() const
Dereference the iterator.
CxxUtils::Array::begin
const_iterator begin() const
Return an iterator pointing at the beginning of the container.
CxxUtils::Array< 0 >::Array
Array()
Default constructor.
CxxUtils::ArrayIterator
Iterator class for Array<N>.
Definition: Control/CxxUtils/CxxUtils/Array.h:51
CxxUtils::WritableArray::WritableArray
WritableArray(Arrayrep &rep)
Constructor.
CxxUtils::ArrayIterator::operator>=
bool operator>=(const ArrayIterator &other) const
Greater-than-or-equal comparison.
CxxUtils::Array::shape
std::vector< unsigned int > shape() const
Return the array shape.
CxxUtils::ArrayIterator::pointer::operator*
value_type operator*() const
Dereference the proxy.
CxxUtils::Array< 0 >::asint
int asint() const
Convert to an integer.
CxxUtils::Array::Array
Array(const Arrayrep &rep)
Constructor.
CxxUtils::fromArrayrep
void fromArrayrep(const CaloRec::Arrayrep &rep, T &x)
Helper to convert from an @x Arrayrep to a scalar type.
CxxUtils::ArrayIterator::operator>
bool operator>(const ArrayIterator &other) const
Greater-than comparison.