00001
00002
00033 #ifndef CXXUTILS_ARRAY_H
00034 #define CXXUTILS_ARRAY_H
00035
00036
00037 #include "CxxUtils/Arrayrep.h"
00038 #include <iterator>
00039
00040
00041 namespace CxxUtils {
00042
00043
00044
00045 template <unsigned int N>
00046 class ArrayIterator;
00047
00048
00049
00050
00051
00066 template <unsigned int N>
00067 class ArrayIteratorChooser
00068 {
00069 public:
00071 typedef ArrayIterator<N> const_iterator;
00072
00073
00081 static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
00082 };
00083
00084
00093 template <>
00094 class ArrayIteratorChooser<1>
00095 {
00096 public:
00098 typedef const Arrayelt* const_iterator;
00099
00100
00108 static const_iterator make_iterator (const Arrayrep* rep, unsigned int offs);
00109 };
00110
00111
00112
00113
00114
00129 template <unsigned int N>
00130 class Array
00131 {
00132 public:
00134 enum { NDIM = N };
00135
00137 typedef typename ArrayIteratorChooser<N>::const_iterator const_iterator;
00138
00139
00149 Array ();
00150
00159 Array (const Arrayrep& rep);
00160
00166 bool valid() const;
00167
00175 std::vector<unsigned int> shape() const;
00176
00183 unsigned int size (unsigned int dim = 0) const;
00184
00193 Array<N-1> operator[] (unsigned int i) const;
00194
00201 const Arrayelt* ptr () const;
00202
00203
00208 const_iterator begin () const;
00209
00210
00215 const_iterator end () const;
00216
00217
00218
00219 protected:
00229 Array (const Arrayrep& rep, unsigned int offs);
00230
00231
00232 friend class Array<N+1>;
00233 friend class ArrayIterator<N+1>;
00234
00237 const Arrayrep* m_rep;
00238
00240 unsigned int m_offs;
00241 };
00242
00243
00253 template <>
00254 class Array<0>
00255 {
00256 public:
00258 enum { NDIM = 0 };
00259
00268 Array ();
00269
00278 Array (const Arrayrep& rep);
00279
00285 bool valid() const;
00286
00295 std::vector<unsigned int> shape() const;
00296
00306 unsigned int size (unsigned int dim=0) const;
00307
00312 operator Arrayelt() const;
00313
00318 int asint () const;
00319
00320
00321
00322 protected:
00332 Array (const Arrayrep& rep, unsigned int offs);
00333
00334
00335 friend class Array<1>;
00336
00339 const Arrayelt* m_elt;
00340 };
00341
00342
00343
00344
00345
00360 template <unsigned int N>
00361 class ArrayIterator
00362 : public std::iterator<std::random_access_iterator_tag, const Array<N-1> >
00363 {
00364 public:
00365
00367 typedef std::iterator<std::random_access_iterator_tag, const Array<N-1> >
00368 base_iterator;
00369
00371 typedef typename base_iterator::iterator_category iterator_category;
00372
00374 typedef typename base_iterator::value_type value_type;
00375
00377 typedef typename base_iterator::difference_type difference_type;
00378
00380 typedef typename base_iterator::reference reference;
00381
00382
00397 class pointer
00398 {
00399 public:
00404 pointer (const ArrayIterator& i);
00405
00406
00411 value_type operator* () const;
00412
00413
00420 const value_type* operator-> () const;
00421
00422
00423 private:
00425 value_type m_a;
00426 };
00427
00428
00433 ArrayIterator ();
00434
00435
00442 ArrayIterator (const Arrayrep* rep, unsigned int offs);
00443
00444
00445
00451 bool operator== (const ArrayIterator& other) const;
00452
00453
00459 bool operator!= (const ArrayIterator& other) const;
00460
00461
00469 bool operator< (const ArrayIterator& other) const;
00470
00471
00479 bool operator> (const ArrayIterator& other) const;
00480
00481
00489 bool operator<= (const ArrayIterator& other) const;
00490
00491
00499 bool operator>= (const ArrayIterator& other) const;
00500
00501
00509 value_type operator* () const;
00510
00511
00521 pointer operator-> () const;
00522
00523
00528 ArrayIterator<N>& operator++ ();
00529
00530
00535 ArrayIterator<N> operator++ (int);
00536
00537
00542 ArrayIterator<N>& operator-- ();
00543
00544
00549 ArrayIterator<N> operator-- (int);
00550
00551
00561 value_type operator[] (difference_type n) const;
00562
00563
00569 ArrayIterator<N>& operator+= (difference_type n);
00570
00571
00577 ArrayIterator<N> operator+ (difference_type n) const;
00578
00579
00585 ArrayIterator<N>& operator-= (difference_type n);
00586
00587
00593 ArrayIterator<N> operator- (difference_type n) const;
00594
00595
00604 difference_type operator- (const ArrayIterator& other) const;
00605
00606
00607 private:
00609 const Arrayrep* m_rep;
00610
00613 unsigned int m_offs;
00614 };
00615
00616
00617
00618
00619
00627 template <unsigned int N>
00628 class WritableArray
00629 : public Array<N>
00630 {
00631 public:
00640 WritableArray (Arrayrep& rep);
00641
00650 WritableArray<N-1> operator[] (unsigned int i) const;
00651
00658 Arrayelt* ptr ();
00659
00660
00661 private:
00671 WritableArray (Arrayrep& rep, unsigned int offs);
00672
00673
00674 friend class WritableArray<N+1>;
00675 };
00676
00677
00685 template <>
00686 class WritableArray<0>
00687 : public Array<0>
00688 {
00689 public:
00698 WritableArray (Arrayrep& rep);
00699
00707 WritableArray<0>& operator= (Arrayelt elt);
00708
00709
00710 private:
00720 WritableArray (Arrayrep& rep, unsigned int offs);
00721
00722
00723 friend class WritableArray<1>;
00724 };
00725
00726
00727
00728
00729
00737 template <unsigned int N>
00738 class WritableArrayData
00739 : private Arrayrep,
00740 public WritableArray<N>
00741 {
00742 public:
00750 WritableArrayData (const unsigned int shape[]);
00751
00759 WritableArrayData (const std::vector<unsigned int>& shape);
00760 };
00761
00762
00763 }
00764
00765
00766 #include "CxxUtils/Array.icc"
00767
00768
00769
00770 namespace CaloRec {
00771 using CxxUtils::Array;
00772 using CxxUtils::WritableArray;
00773 using CxxUtils::WritableArrayData;
00774 }
00775
00776
00777 #endif // not CXXUTILS_ARRAY_H
00778