ATLAS Offline Software
Loading...
Searching...
No Matches
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 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4*/
33
34
35#ifndef CXXUTILS_ARRAY_H
36#define CXXUTILS_ARRAY_H
37
38
39#include "CxxUtils/Arrayrep.h"
40#include <iterator>
41
42
43namespace CxxUtils {
44
45
46// Forward declarations.
47template <unsigned int N>
48class ArrayIterator;
49template <unsigned int N>
50class WritableArray;
51
52
53//**********************************************************************
54
55
70template <unsigned int N>
72{
73public:
76
77
85 static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
86};
87
88
97template <>
99{
100public:
102 typedef const Arrayelt* const_iterator;
103
104
112 static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
113};
114
115
116//**********************************************************************
117
118
133template <unsigned int N>
134class Array
135{
136public:
138 enum { NDIM = N };
139
142
143
154
163 Array (const Arrayrep& rep);
164
170 bool valid() const;
171
179 std::vector<unsigned int> shape() const;
180
187 unsigned int size (unsigned int dim = 0) const;
188
197 Array<N-1> operator[] (unsigned int i) const;
198
205 const Arrayelt* ptr () const;
206
207
213
214
220
221
229 void write_array (std::ostream& stream) const;
230
231
232 // Protected, not private, so that they can be accessed by @c WritableArray.
233protected:
243 Array (const Arrayrep& rep, unsigned int offs);
244
245 // These classes need to call the above protected constructor.
246 friend class Array<N+1>;
248 friend class ArrayIterator<N+1>;
249
253
255 unsigned int m_offs;
256};
257
258
268template <>
269class Array<0>
270{
271public:
273 enum { NDIM = 0 };
274
284
293 Array (const Arrayrep& rep);
294
300 bool valid() const;
301
310 std::vector<unsigned int> shape() const;
311
321 unsigned int size (unsigned int dim=0) const;
322
327 operator Arrayelt() const;
328
333 int asint () const;
334
335
343 void write_array (std::ostream& stream) const;
344
345
346 // Protected, not private, so that they can be accessed by @c WritableArray.
347protected:
357 Array (const Arrayrep& rep, unsigned int offs);
358
359 // This class needs to call the above protected constructor.
360 friend class Array<1>;
361 friend class WritableArray<1>;
362
366};
367
368
369template <unsigned int N>
370std::ostream& operator<< (std::ostream& s, const Array<N>& a);
371
372
373//**********************************************************************
374
375
390template <unsigned int N>
392{
393public:
394 using iterator_category = std::random_access_iterator_tag;
395 using value_type = const Array<N-1>;
396 using difference_type = std::ptrdiff_t;
398
399
415 {
416 public:
422
423
429
430
437 const value_type* operator-> () const;
438
439
440 private:
443 };
444
445
451
452
459 ArrayIterator (const Arrayrep* rep, unsigned int offs);
460
461 // Use default copy ctor and assignment.
462
468 bool operator== (const ArrayIterator& other) const;
469
470
476 bool operator!= (const ArrayIterator& other) const;
477
478
486 bool operator< (const ArrayIterator& other) const;
487
488
496 bool operator> (const ArrayIterator& other) const;
497
498
506 bool operator<= (const ArrayIterator& other) const;
507
508
516 bool operator>= (const ArrayIterator& other) const;
517
518
527
528
539
540
546
547
553
554
560
561
567
568
579
580
587
588
595
596
603
604
611
612
622
623
624private:
627
630 unsigned int m_offs;
631};
632
633
634//**********************************************************************
635
636
644template <unsigned int N>
646 : public Array<N>
647{
648public:
658
667 WritableArray<N-1> operator[] (unsigned int i);
668
677 Array<N-1> operator[] (unsigned int i) const;
678
686
687
688private:
698 WritableArray (Arrayrep& rep, unsigned int offs);
699
700 // This class needs to call the above protected constructor.
701 friend class WritableArray<N+1>;
702
705};
706
707
715template <>
717 : public Array<0>
718{
719public:
729
737 WritableArray<0>& operator= (Arrayelt elt);
738
739
740private:
750 WritableArray (Arrayrep& rep, unsigned int offs);
751
752 // This class needs to call the above protected constructor.
753 friend class WritableArray<1>;
754
758};
759
760
761//**********************************************************************
762
763
771template <unsigned int N>
773 : private Arrayrep,
774 public WritableArray<N>
775{
776public:
784 WritableArrayData (const unsigned int shape[]);
785
793 WritableArrayData (const std::vector<unsigned int>& shape);
794};
795
796
802template <class T>
803 requires std::assignable_from<T&, float>
804void fromArrayrep (const CaloRec::Arrayrep& rep, T& x);
805
806
812template <unsigned int N>
814
815
819template <class T>
821 requires (T t, const CxxUtils::Arrayrep& rep)
822 {
823 { CxxUtils::fromArrayrep (rep, t) };
824 };
825
826
827} // namespace CxxUtils
828
829
830#include "CxxUtils/Array.icc"
831
832
833// Backwards compatibility.
834namespace CaloRec {
835 using CxxUtils::Array;
838}
839
840
841#endif // not CXXUTILS_ARRAY_H
842
Representation class for Array's.
static Double_t a
#define x
Array()
Default constructor.
WritableArrayData(const unsigned int shape[])
Constructor.
WritableArray(Arrayrep &rep)
Constructor.
static const_iterator make_iterator(const Arrayrep *rep, unsigned int offs)
Construct an Array<1>::const_iterator.
const Arrayelt * const_iterator
Iterator type for an Array<1>.
Helper for defining iterators over Array's.
static const_iterator make_iterator(const Arrayrep *rep, unsigned int offs)
Construct an Array<N>::const_iterator.
ArrayIterator< N > const_iterator
Iterator type for an Array<N>.
const value_type * operator->() const
Dereference the proxy.
pointer(const ArrayIterator &i)
Proxy constructor.
value_type m_a
The contained Array proxy instance.
value_type operator*() const
Dereference the proxy.
bool operator==(const ArrayIterator &other) const
Equality comparison.
const Arrayrep * m_rep
The underlying array representation.
value_type operator[](difference_type n) const
Array indexing relative to the iterator.
bool operator<(const ArrayIterator &other) const
Less-than comparison.
value_type operator*() const
Dereference the iterator.
ArrayIterator< N > & operator+=(difference_type n)
Advance the iterator.
bool operator<=(const ArrayIterator &other) const
Less-than-or-equal comparison.
ArrayIterator(const Arrayrep *rep, unsigned int offs)
Constructor from Arrayrep and offset.
ArrayIterator()
Default constructor.
bool operator>(const ArrayIterator &other) const
Greater-than comparison.
ArrayIterator< N > operator-(difference_type n) const
Return a new iterator pointing n steps behind.
std::random_access_iterator_tag iterator_category
ArrayIterator< N > & operator--()
Back up the iterator.
ArrayIterator< N > operator+(difference_type n) const
Return a new iterator pointing n steps ahead.
pointer operator->() const
Dereference the iterator.
bool operator>=(const ArrayIterator &other) const
Greater-than-or-equal comparison.
bool operator!=(const ArrayIterator &other) const
Inequality comparison.
ArrayIterator< N > & operator-=(difference_type n)
Back up the iterator.
unsigned int m_offs
Offset into the representation's data array of the first element referred to by this iterator.
ArrayIterator< N > & operator++()
Advance the iterator.
const Arrayelt * m_elt
Pointer to this array's single element.
std::vector< unsigned int > shape() const
Return the array shape.
Array()
Default constructor.
void write_array(std::ostream &stream) const
Creates a text representation of the array content.
unsigned int size(unsigned int dim=0) const
Return the size of the array along one dimension.
Array(const Arrayrep &rep, unsigned int offs)
Private constructor for array indexing.
Array(const Arrayrep &rep)
Constructor.
bool valid() const
Test for validity.
int asint() const
Convert to an integer.
Read-only multidimensional array.
Array(const Arrayrep &rep, unsigned int offs)
Private constructor for array indexing.
Array()
Default constructor.
Array(const Arrayrep &rep)
Constructor.
Array< N-1 > operator[](unsigned int i) const
Array indexing.
void write_array(std::ostream &stream) const
Creates a text representation of the array content.
const Arrayelt * ptr() const
Return a direct pointer to array elements.
std::vector< unsigned int > shape() const
Return the array shape.
const_iterator end() const
Return an iterator pointing past the end of the container.
const_iterator begin() const
Return an iterator pointing at the beginning of the container.
bool valid() const
Test for validity.
unsigned int size(unsigned int dim=0) const
Return the size of the array along one dimension.
const Arrayrep * m_rep
Pointer to the representation.
ArrayIteratorChooser< N >::const_iterator const_iterator
The iterator for this container.
unsigned int m_offs
Offset in the Arrayrep's data of the first element of this array.
WritableArrayData(const std::vector< unsigned int > &shape)
Constructor.
WritableArrayData(const unsigned int shape[])
Constructor.
WritableArray(Arrayrep &rep, unsigned int offs)
Private constructor for array indexing.
WritableArray(Arrayrep &rep)
Constructor.
Arrayelt * m_elt_nc
Pointer to this array's single element, non-const copy.
Read-write multidimensional array.
Arrayelt * ptr()
Return a direct pointer to array elements.
WritableArray(Arrayrep &rep, unsigned int offs)
Private constructor for array indexing.
WritableArray< N-1 > operator[](unsigned int i)
Array indexing.
WritableArray(Arrayrep &rep)
Constructor.
Arrayrep * m_rep_nc
The underlying array representation, non-const copy.
Concept testing whether a type may be used with FromArrayrep.
Namespace for helper functions.
float Arrayelt
The type of an element of an Array.
std::ostream & operator<<(std::ostream &s, const Array< N > &a)
void fromArrayrep(const CaloRec::Arrayrep &rep, T &x)
Helper to convert from an @x Arrayrep to a scalar type.
Representation class for Array's.