MyraMath
Index.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_EXPRESSION_INDEX_H
7 #define MYRAMATH_EXPRESSION_INDEX_H
8 
14 #include <myramath/MYRAMATH_EXPORT.h>
15 
16 #ifdef MYRAMATH_ENABLE_CPP11
17 #include <initializer_list>
18 #endif
19 
20 namespace myra {
21 
23 template<int N> class Index
24  {
25  public:
26 
27  // Default constructor, fills with zeros.
28  Index()
29  {
30  for (int n = 0; n < N; ++n)
31  contents[n] = 0;
32  }
33 
34  // Fill constructor, copies N int's from pointer p.
35  Index(const int* p)
36  {
37  for (int n = 0; n < N; ++n)
38  contents[n] = *p++;
39  }
40 
41 #ifdef MYRAMATH_ENABLE_CPP11
42 
43  // Fill constructor, from std::initializer_list<int>
44  Index(std::initializer_list<int> list)
45  {
46  for (int n = 0; n < N; ++n)
47  contents[n] = *(list.begin()+n);
48  }
49 
50 #endif
51 
52  // Accessing/mutating contents.
53  const int& operator [](int n) const
54  { return contents[n]; }
55  int& operator [](int n)
56  { return contents[n]; }
57 
58  // Raw access to contiguous data.
59  const int* data() const
60  { return contents; }
61  int* data()
62  { return contents; }
63 
64  // Const range over contents.
65  const int* begin() const
66  { return contents+0; }
67  const int* end() const
68  { return contents+N; }
69 
70  // Mutable range over contents.
71  int* begin()
72  { return contents+0; }
73  int* end()
74  { return contents+N; }
75 
76  // Returns first F values. Example usage: index.first<2>() returns the 0'th and 1'st values, etc.
77  template<int F> Index<F> first() const
78  { return Index<F>(this->begin()); }
79 
80  // Returns last L values. Example usage: index.last<2>() returns the N-2'th and N-1'th values, etc.
81  template<int L> Index<L> last() const
82  { return Index<L>(this->begin()+N-L); }
83 
84  private:
85 
86  // Internal contents.
87  int contents[N];
88 
89  };
90 
92 template<int N> bool operator == (const Index<N>& a, const Index<N>& b)
93  {
94  for (int n = 0; n < N; ++n)
95  if (a[n] != b[n])
96  return false;
97  return true;
98  }
99 
101 template<int N> bool operator != (const Index<N>& a, const Index<N>& b)
102  { return !(a==b); }
103 
105 template<int N1, int N2> Index<N1+N2> concatenate(const Index<N1>& i1, const Index<N2>& i2)
106  {
107  Index<N1+N2> i;
108  for (int n = 0; n < N1; ++n)
109  i[n] = i1[n];
110  for (int n = 0; n < N2; ++n)
111  i[n+N1] = i2[n];
112  return i;
113  }
114 
116 MYRAMATH_EXPORT Index<1> make_Index(int i);
117 MYRAMATH_EXPORT Index<2> make_Index(int i, int j);
118 MYRAMATH_EXPORT Index<3> make_Index(int i, int j, int k);
119 
120 } // namespace myra
121 
122 #endif
Definition: syntax.dox:1
std::vector< T > concatenate(const std::vector< T > &v1, const std::vector< T > &v2)
Returns [v1 v2].
Definition: concatenate.h:19
Essentially a std::array<int,N>, provided for backwards compatibility.
Definition: Index.h:23