WARPXM v1.10.0
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#ifndef WMARRAY_H
2#define WMARRAY_H
3
4// Wx includes
5#include "lib/wxrange.h"
6
7// Wm includes
8#include "warpxm/warpxm_config.h"
9
10// STL includes
11#include <cstring>
12#include <map>
13#include <memory>
14
15namespace wxm
16{
17namespace array
18{
19
20template<typename T> class array_allocation_t
21{
22public:
24 array_allocation_t() = default;
25
31 {
32 allocate();
33 }
34
36 array_allocation_t(const array_allocation_t& other) = default;
37
40 {
41 terminate();
42 }
43
50 {
51 if (&other != this)
52 {
53 link(other);
54 }
55 return *this;
56 }
57
62 void fill(const T& value)
63 {
64 std::fill_n(data(), _range.size(), value);
65 }
66
71 T* data()
72 {
73 return _data->data();
74 }
75
76 const T* data() const
77 {
78 return _data->data();
79 }
80
81 virtual T& operator[](int index)
82 {
83 return (*_data)[index];
84 }
85
86 virtual const T& operator[](int index) const
87 {
88 return (*_data)[index];
89 }
90
94 T& operator()(size_t i, size_t j)
95 {
96 return operator[](i* _range.length(1) + j);
97 }
98
102 const T& operator()(size_t i, size_t j) const
103 {
104 return operator[](i* _range.length(1) + j);
105 }
106
110 const T& operator()(size_t i, size_t j, size_t k) const
111 {
112 return operator[]((i * _range.length(1) + j) * _range.length(2) + k);
113 }
114
118 T& operator()(size_t i, size_t j, size_t k)
119 {
120 return operator[]((i * _range.length(1) + j) * _range.length(2) + k);
121 }
122
123 const WxRange& range() const
124 {
125 return _range;
126 }
127
128 friend std::ostream& operator<<(std::ostream& stream, const array_allocation_t& array)
129 {
130 int num_pts = array.range().size();
131 stream << "[";
132 for (int i = 0; i < num_pts; i++)
133 {
134 stream << array[i];
135 if (i != num_pts - 1)
136 {
137 stream << ", ";
138 }
139 }
140 stream << "]";
141 return stream;
142 }
143
144protected:
145 void copy(const array_allocation_t& other)
146 {
147 // This is a deep copy - all data in _data is copied to new
148 // class
149 terminate();
150 _range = other._range;
151 allocate();
152 std::memcpy(data(), other.data(), _range.size() * sizeof(T));
153 }
154
155 void link(const array_allocation_t& other)
156 {
157 // This is a shallow copy, only the reference to the data is
158 // really stored
159 terminate();
160 _range = other._range;
161 _data = other._data;
162 }
163
164 void allocate()
165 {
166 // Allocate a new dataset
167 _data.reset();
168 _data = std::make_shared<std::vector<T>>(std::vector<T>());
169 _data->resize(_range.size());
170 }
171
173 {
174 // Delete the array - if array is linked, then the core data may
175 // be saved depending on
176 // the existance of other linked arrays
177 _data.reset();
178 }
179
182
184 std::shared_ptr<std::vector<T>> _data;
185
186private:
187};
188
190{
191public:
193 patch_array_t() = default;
194
201 {
202 }
203
205 patch_array_t(const patch_array_t& other) = default;
206
209 {
210 terminate();
211 }
212
219 {
220 if (&other != this)
221 {
222 link(static_cast<const array_allocation_t<real>&>(other));
223 }
224 return *this;
225 }
226
237 const WxRange& interior_range() const
238 {
239 return _interior_range;
240 }
241
242protected:
245};
246} // namespace array
247} // namespace wxm
248
249#endif // WMARRAY_H
WxRange represents a hyper-rectangular domain of an n-dimensional space of integers.
Definition: wxrange.h:23
int length(unsigned dim) const
Length of edge along dimension 'dim'.
Definition: wxrange.h:294
int size() const
Number of elements in range.
Definition: wxrange.h:304
Definition: array.h:21
std::shared_ptr< std::vector< T > > _data
Shared pointer containing data.
Definition: array.h:184
void link(const array_allocation_t &other)
Definition: array.h:155
virtual const T & operator[](int index) const
Definition: array.h:86
void allocate()
Definition: array.h:164
array_allocation_t(const array_allocation_t &other)=default
Copy Constructor.
array_allocation_t()=default
Default Constructor.
const T & operator()(size_t i, size_t j, size_t k) const
3D array access
Definition: array.h:110
void terminate()
Definition: array.h:172
virtual ~array_allocation_t()
Destructor.
Definition: array.h:39
void fill(const T &value)
Fill array with a value.
Definition: array.h:62
friend std::ostream & operator<<(std::ostream &stream, const array_allocation_t &array)
Definition: array.h:128
const T & operator()(size_t i, size_t j) const
2D array access
Definition: array.h:102
WxRange _range
Range of data.
Definition: array.h:181
array_allocation_t & operator=(const array_allocation_t &other)
Assignment operator - makes a shallow copy.
Definition: array.h:49
T * data()
Access the underlying data array.
Definition: array.h:71
array_allocation_t(const WxRange &range)
Allocate the array for a given range.
Definition: array.h:30
const T * data() const
Definition: array.h:76
void copy(const array_allocation_t &other)
Definition: array.h:145
const WxRange & range() const
Definition: array.h:123
T & operator()(size_t i, size_t j, size_t k)
3D array access
Definition: array.h:118
virtual T & operator[](int index)
Definition: array.h:81
T & operator()(size_t i, size_t j)
2D array access
Definition: array.h:94
Definition: array.h:190
patch_array_t()=default
Default Constructor.
patch_array_t & operator=(const patch_array_t &other)
Assignment operator - makes a shallow copy.
Definition: array.h:218
patch_array_t(const patch_array_t &other)=default
Copy Constructor.
const WxRange & interior_range() const
Returns range of interior of dataset.
Definition: array.h:237
virtual ~patch_array_t()
Destructor.
Definition: array.h:208
WxRange _interior_range
Range describing interior range of dataset.
Definition: array.h:244
patch_array_t(const WxRange &range)
Allocate the array for a given range.
Definition: array.h:199
Base namespace for everything not included in the global namespace.
Definition: field_source.h:8
#define real
Definition: wmoclunstructuredreconstruction.h:11