ndArray.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2020 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9#include <inttypes.h>
10#include <vector>
11
12// psrchive/Util/units/ndArray.h
13
14#ifndef __ndArray_H
15#define __ndArray_H
16
18
20
21template<unsigned N, typename T> class ndArray
22{
23 ndArray<N-1,T> next;
24 uint64_t ndat;
25
26 uint64_t multiplicity;
27 mutable uint64_t offset;
28
29 friend class ndArray<N+1,T>;
30
31public:
32
33 ndArray () { offset = 0; multiplicity = 1; }
34
36 ndArray<N-1,T>& operator* (uint64_t sz)
37 { ndat = sz; next.multiplicity = multiplicity * ndat; return next; }
38
39 const ndArray<N-1,T>& operator[] (uint64_t idat) const
40 { next.offset = offset + idat * next.stride(); return next; }
41
42 ndArray<N-1,T>& operator[] (uint64_t idat)
43 { next.offset = offset + idat * next.stride(); return next; }
44
45 uint64_t stride () const { return ndat * next.stride(); }
46 uint64_t size () const { return ndat; }
47};
48
49template<typename T>
50class ndArray<1,T>
51{
52 uint64_t ndat;
53
54 uint64_t multiplicity;
55 mutable uint64_t offset;
56
57 friend class ndArray<2,T>;
58
59 std::vector<T> data;
60
61public:
62 ndArray () { offset = 0; multiplicity = 1; }
63
64 void operator* (uint64_t sz) { ndat = sz; data.resize (ndat * multiplicity); }
65 const T& operator[] (uint64_t idat) const { return data[offset + idat]; }
66 T& operator[] (uint64_t idat) { return data[offset + idat]; }
67
68 uint64_t stride () const { return ndat; }
69 uint64_t size () const { return ndat; }
70};
71
72#endif
73
An N-dimensional array of elements of type T, defined recursively.
Definition ndArray.h:22
ndArray< N-1, T > & operator*(uint64_t sz)
Set the dimension along this index.
Definition ndArray.h:36

Generated using doxygen 1.14.0