v0.13.2
Loading...
Searching...
No Matches
Tensor1_pointer.hpp
Go to the documentation of this file.
1/* A version for pointers, useful for previously
2 constructed arrays. */
3
4#pragma once
5
6namespace FTensor
7{
8
9template <class T, int Tensor_Dim> class Tensor1<T *, Tensor_Dim> {
10protected:
11
12 const int inc;
13
14 /* Note that the T *'s are mutable, so the pointer can change,
15 allowing iterating over a array. */
16
17 mutable T *restrict data[Tensor_Dim];
18
19public:
20 /* Initializations for varying numbers of elements, with each one
21 defined for a particular Tensor_Dim. To initialize a different
22 dimension, just add the appropriate constructor and call to
23 the Tensor1_constructor constructor. */
24 Tensor1(T *d0, const int i = 1) : inc(i) {
26 }
27 Tensor1(T *d0, T *d1, const int i = 1) : inc(i) {
29 }
30 Tensor1(T *d0, T *d1, T *d2, const int i = 1) : inc(i) {
32 }
33 Tensor1(T *d0, T *d1, T *d2, T *d3, const int i = 1) : inc(i) {
35 }
36 Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, const int i = 1) : inc(i) {
38 }
39 /* Initializations for varying numbers of elements. */
40 template <class... U> Tensor1(U *... d) : data(d...), inc(1) {}
41
42 /* Initialization from array */
43 template <class U>
44 Tensor1(std::array<U *, Tensor_Dim> &a, const int i = 1) : inc(i) {
45 std::copy(a.begin(), a.end(), data);
46 }
47
48 Tensor1(const int i = 1) : inc(i) {}
49
50 /* There are two operator(int)'s, one for non-consts that lets you
51 change the value, and one for consts that doesn't. */
52
53 T &operator()(const int N) {
54#ifdef FTENSOR_DEBUG
55 if(N >= Tensor_Dim || N < 0)
56 {
57 std::stringstream s;
58 s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.operator(" << N
59 << ")" << std::endl;
60 throw std::out_of_range(s.str());
61 }
62#endif
63 return *data[N];
64 }
65 T operator()(const int N) const
66 {
67#ifdef FTENSOR_DEBUG
68 if(N >= Tensor_Dim || N < 0)
69 {
70 std::stringstream s;
71 s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.operator(" << N
72 << ") const" << std::endl;
73 throw std::out_of_range(s.str());
74 }
75#endif
76 return *data[N];
77 }
78 T *ptr(const int N) const
79 {
80#ifdef FTENSOR_DEBUG
81 if(N >= Tensor_Dim || N < 0)
82 {
83 std::stringstream s;
84 s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.ptr(" << N << ")"
85 << std::endl;
86 throw std::out_of_range(s.str());
87 }
88#endif
89 return data[N];
90 }
91
92 T *&ptr(const int N) {
93#ifdef FTENSOR_DEBUG
94 if (N >= Tensor_Dim || N < 0) {
95 std::stringstream s;
96 s << "Bad index in Tensor1<T*," << Tensor_Dim << ">.ptr(" << N << ")"
97 << std::endl;
98 throw std::out_of_range(s.str());
99 }
100#endif
101 return data[N];
102 }
103
104 /* These operator()'s are the first part in constructing template
105 expressions. They can be used to slice off lower dimensional
106 parts. They are not entirely safe, since you can accidentaly use a
107 higher dimension than what is really allowed (like Dim=5). */
108
109 template <char i, int Dim>
112 {
113 return Tensor1_Expr<Tensor1<T *, Tensor_Dim>, T, Dim, i>(*this);
114 }
115
116 template <char i, int Dim>
118 operator()(const Index<i, Dim> &index) const
119 {
120 return Tensor1_Expr<const Tensor1<T *, Tensor_Dim>, T, Dim, i>(*this);
121 }
122
123 /* The ++ operator increments the pointer, not the number that the
124 pointer points to. This allows iterating over a grid. */
125
126 const Tensor1 &operator++() const
127 {
128 for(int i = 0; i < Tensor_Dim; ++i)
129 data[i] += inc;
130 return *this;
131 }
132
133 private:
134
135 /**
136 * @brief Preventing casting on derived class
137 *
138 * That can be source of errors
139 *
140 */
141 template <int I>
142 Tensor1(const Tensor1<PackPtr<T *, I>, Tensor_Dim> &) = delete;
143 };
144
145 template <class T, int Tensor_Dim, int I>
146 class Tensor1<PackPtr<T *, I>, Tensor_Dim> : public Tensor1<T *, Tensor_Dim> {
147
148 public:
149
150 /* Initializations for varying numbers of elements. */
151 template <class... U> Tensor1(U *... d) : Tensor1<T *, Tensor_Dim>(d...) {}
152
153 template <class U>
154 Tensor1(std::array<U *, Tensor_Dim> &a) : Tensor1<T *, Tensor_Dim>(a) {}
155
156 Tensor1(): Tensor1<T *, Tensor_Dim>() {}
157
158 /* The ++ operator increments the pointer, not the number that the
159 pointer points to. This allows iterating over a grid. */
160
161 const Tensor1 &operator++() const
162 {
163 for(int i = 0; i < Tensor_Dim; ++i)
165 return *this;
166 }
167 };
168
169
170}
constexpr double a
Tensor1(T *d0, T *d1, T *d2, T *d3, T *d4, T *d5, const int i=1)
Tensor1(T *d0, T *d1, const int i=1)
Tensor1(std::array< U *, Tensor_Dim > &a, const int i=1)
Tensor1(T *d0, T *d1, T *d2, T *d3, const int i=1)
Tensor1(T *d0, T *d1, T *d2, const int i=1)
Tensor1_Expr< Tensor1< T *, Tensor_Dim >, T, Dim, i > operator()(const Index< i, Dim > &index)
Tensor1(const Tensor1< PackPtr< T *, I >, Tensor_Dim > &)=delete
Preventing casting on derived class.
Tensor1_Expr< const Tensor1< T *, Tensor_Dim >, T, Dim, i > operator()(const Index< i, Dim > &index) const
FTensor::Index< 'i', SPACE_DIM > i
const double T
Tensors class implemented by Walter Landry.
Definition: FTensor.hpp:51
const Tensor1_Expr< const dTensor0< T, Dim, i >, typename promote< T, double >::V, Dim, i > d(const Tensor0< T * > &a, const Index< i, Dim > index, const Tensor1< int, Dim > &d_ijk, const Tensor1< double, Dim > &d_xyz)
Definition: dTensor0.hpp:27
constexpr IntegrationType I
static constexpr std::array< double, 3 > d4
static constexpr std::array< double, 3 > d3
const int N
Definition: speed_test.cpp:3