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-2026 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#include <format>
42#include <sstream>
43
44
45namespace CxxUtils {
46
47
48// Forward declarations.
49template <unsigned int N>
50class ArrayIterator;
51template <unsigned int N>
52class WritableArray;
53
54
55//**********************************************************************
56
57
72template <unsigned int N>
74{
75public:
78
79
87 static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
88};
89
90
99template <>
101{
102public:
104 typedef const Arrayelt* const_iterator;
105
106
114 static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
115};
116
117
118//**********************************************************************
119
120
135template <unsigned int N>
136class Array
137{
138public:
140 enum { NDIM = N };
141
144
145
156
165 Array (const Arrayrep& rep);
166
172 bool valid() const;
173
181 std::vector<unsigned int> shape() const;
182
189 unsigned int size (unsigned int dim = 0) const;
190
199 Array<N-1> operator[] (unsigned int i) const;
200
207 const Arrayelt* ptr () const;
208
209
215
216
222
223
231 void write_array (std::ostream& stream) const;
232
233
234 // Protected, not private, so that they can be accessed by @c WritableArray.
235protected:
245 Array (const Arrayrep& rep, unsigned int offs);
246
247 // These classes need to call the above protected constructor.
248 friend class Array<N+1>;
250 friend class ArrayIterator<N+1>;
251
255
257 unsigned int m_offs;
258};
259
260
270template <>
271class Array<0>
272{
273public:
275 enum { NDIM = 0 };
276
286
295 Array (const Arrayrep& rep);
296
302 bool valid() const;
303
312 std::vector<unsigned int> shape() const;
313
323 unsigned int size (unsigned int dim=0) const;
324
329 operator Arrayelt() const;
330
335 int asint () const;
336
337
345 void write_array (std::ostream& stream) const;
346
347
348 // Protected, not private, so that they can be accessed by @c WritableArray.
349protected:
359 Array (const Arrayrep& rep, unsigned int offs);
360
361 // This class needs to call the above protected constructor.
362 friend class Array<1>;
363 friend class WritableArray<1>;
364
368};
369
370
371template <unsigned int N>
372std::ostream& operator<< (std::ostream& s, const Array<N>& a);
373
374
375//**********************************************************************
376
377
392template <unsigned int N>
394{
395public:
396 using iterator_category = std::random_access_iterator_tag;
397 using value_type = const Array<N-1>;
398 using difference_type = std::ptrdiff_t;
400
401
417 {
418 public:
424
425
431
432
439 const value_type* operator-> () const;
440
441
442 private:
445 };
446
447
453
454
461 ArrayIterator (const Arrayrep* rep, unsigned int offs);
462
463 // Use default copy ctor and assignment.
464
470 bool operator== (const ArrayIterator& other) const;
471
472
478 bool operator!= (const ArrayIterator& other) const;
479
480
488 bool operator< (const ArrayIterator& other) const;
489
490
498 bool operator> (const ArrayIterator& other) const;
499
500
508 bool operator<= (const ArrayIterator& other) const;
509
510
518 bool operator>= (const ArrayIterator& other) const;
519
520
529
530
541
542
548
549
555
556
562
563
569
570
581
582
589
590
597
598
605
606
613
614
624
625
626private:
629
632 unsigned int m_offs;
633};
634
635
636//**********************************************************************
637
638
646template <unsigned int N>
648 : public Array<N>
649{
650public:
660
669 WritableArray<N-1> operator[] (unsigned int i);
670
679 Array<N-1> operator[] (unsigned int i) const;
680
688
689
690private:
700 WritableArray (Arrayrep& rep, unsigned int offs);
701
702 // This class needs to call the above protected constructor.
703 friend class WritableArray<N+1>;
704
707};
708
709
717template <>
719 : public Array<0>
720{
721public:
731
740
741
742private:
752 WritableArray (Arrayrep& rep, unsigned int offs);
753
754 // This class needs to call the above protected constructor.
755 friend class WritableArray<1>;
756
760};
761
762
763//**********************************************************************
764
765
773template <unsigned int N>
775 : private Arrayrep,
776 public WritableArray<N>
777{
778public:
786 WritableArrayData (const unsigned int shape[]);
787
795 WritableArrayData (const std::vector<unsigned int>& shape);
796};
797
798
804template <class T>
805 requires std::assignable_from<T&, float>
806void fromArrayrep (const CaloRec::Arrayrep& rep, T& x);
807
808
814template <unsigned int N>
816
817
821template <class T>
823 requires (T t, const CxxUtils::Arrayrep& rep)
824 {
825 { CxxUtils::fromArrayrep (rep, t) };
826 };
827
828
829} // namespace CxxUtils
830
831
832namespace std {
833
834
838template <unsigned int N>
839struct formatter<CxxUtils::Array<N> >
840 : public formatter<string_view>
841{
842 template <class FmtContext>
843 FmtContext::iterator format(const CxxUtils::Array<N>& a, FmtContext& ctx) const
844 {
845 std::ostringstream ss;
846 a.write_array (ss);
847 return formatter<string_view>::format (ss.str(), ctx);
848 }
849};
850
851
852} // namespace std
853
854
855#include "CxxUtils/Array.icc"
856
857
858// Backwards compatibility.
859namespace CaloRec {
860 using CxxUtils::Array;
861 using CxxUtils::WritableArray;
862 using CxxUtils::WritableArrayData;
863}
864
865
866#endif // not CXXUTILS_ARRAY_H
867
Representation class for Array's.
static Double_t a
static Double_t ss
#define x
Read-only multidimensional array.
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)
ConcurrentBitset & operator=(const ConcurrentBitset &other)
Assignment.
void fromArrayrep(const CaloRec::Arrayrep &rep, T &x)
Helper to convert from an @x Arrayrep to a scalar type.
STL namespace.
Representation class for Array's.
FmtContext::iterator format(const CxxUtils::Array< N > &a, FmtContext &ctx) const