SPH
ArrayView.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 
11 
16 template <typename T, typename TCounter = Size>
17 class ArrayView {
18 private:
19  using StorageType = typename WrapReferenceType<T>::Type;
20 
21  StorageType* data;
22  TCounter actSize = 0;
23 
24 public:
25  using Type = T;
26  using Counter = TCounter;
27 
29  : data(nullptr) {}
30 
31  INLINE ArrayView(StorageType* data, TCounter size)
32  : data(data)
33  , actSize(size) {}
34 
35  INLINE ArrayView(const ArrayView& other)
36  : data(other.data)
37  , actSize(other.actSize) {}
38 
40  INLINE ArrayView(std::nullptr_t)
41  : data(nullptr) {}
42 
45  this->data = other.data;
46  this->actSize = other.actSize;
47  return *this;
48  }
49 
52  return ArrayView<const T, TCounter>(data, actSize);
53  }
54 
56  return Iterator<StorageType>(data, data, data + actSize);
57  }
58 
60  return Iterator<const StorageType>(data, data, data + actSize);
61  }
62 
64  return Iterator<StorageType>(data + actSize, data, data + actSize);
65  }
66 
68  return Iterator<const StorageType>(data + actSize, data, data + actSize);
69  }
70 
71  INLINE T& operator[](const TCounter idx) {
72  SPH_ASSERT(unsigned(idx) < unsigned(actSize), idx, actSize);
73  return data[idx];
74  }
75 
76  INLINE const T& operator[](const TCounter idx) const {
77  SPH_ASSERT(unsigned(idx) < unsigned(actSize), idx, actSize);
78  return data[idx];
79  }
80 
81  INLINE T& front() {
82  SPH_ASSERT(actSize > 0);
83  return data[0];
84  }
85 
86  INLINE const T& front() const {
87  SPH_ASSERT(actSize > 0);
88  return data[0];
89  }
90 
91  INLINE T& back() {
92  SPH_ASSERT(actSize > 0);
93  return data[actSize - 1];
94  }
95 
96  INLINE const T& back() const {
97  SPH_ASSERT(actSize > 0);
98  return data[actSize - 1];
99  }
100 
101  INLINE TCounter size() const {
102  return this->actSize;
103  }
104 
105  INLINE bool empty() const {
106  return this->actSize == 0;
107  }
108 
110  INLINE ArrayView subset(const TCounter start, const TCounter length) const {
111  SPH_ASSERT(start <= size() && start + length <= size());
112  return ArrayView(data + start, length);
113  }
114 
115  INLINE bool operator!() const {
116  return data == nullptr;
117  }
118 
119  INLINE explicit operator bool() const {
120  return data != nullptr;
121  }
122 
124  bool operator==(const ArrayView& other) const {
125  if (actSize != other.actSize) {
126  return false;
127  }
128  for (TCounter i = 0; i < actSize; ++i) {
129  if (data[i] != other[i]) {
130  return false;
131  }
132  }
133  return true;
134  }
135 
137  bool operator!=(const ArrayView& other) const {
138  return !(*this == other);
139  }
140 
144  friend std::ostream& operator<<(std::ostream& stream, const ArrayView& array) {
145  for (const T& t : array) {
146  stream << t << std::endl;
147  }
148  return stream;
149  }
150 };
151 
152 template <typename T>
153 bool almostEqual(const ArrayView<T> v1, const ArrayView<T> v2, const Float& eps) {
154  if (v1.size() != v2.size()) {
155  return false;
156  }
157 
158  for (Size i = 0; i < v1.size(); ++i) {
159  if (!Sph::almostEqual(v1[i], v2[i], eps)) {
160  return false;
161  }
162  }
163  return true;
164 }
165 
166 template <typename T>
168  return ArrayView<T>(&value, 1);
169 }
170 
INLINE ArrayView< T > getSingleValueView(T &value)
Definition: ArrayView.h:167
bool almostEqual(const ArrayView< T > v1, const ArrayView< T > v2, const Float &eps)
Definition: ArrayView.h:153
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Ordinary iterator for custom containers.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE Iterator< const StorageType > begin() const
Definition: ArrayView.h:59
INLINE const T & front() const
Definition: ArrayView.h:86
bool operator!=(const ArrayView &other) const
Inequality operator.
Definition: ArrayView.h:137
INLINE const T & back() const
Definition: ArrayView.h:96
INLINE ArrayView(const ArrayView &other)
Definition: ArrayView.h:35
INLINE T & front()
Definition: ArrayView.h:81
friend std::ostream & operator<<(std::ostream &stream, const ArrayView &array)
Prints content of arrayview to stream.
Definition: ArrayView.h:144
bool operator==(const ArrayView &other) const
Comparison operator, comparing arrayviews element-by-element.
Definition: ArrayView.h:124
INLINE bool empty() const
Definition: ArrayView.h:105
INLINE bool operator!() const
Definition: ArrayView.h:115
INLINE const T & operator[](const TCounter idx) const
Definition: ArrayView.h:76
INLINE TCounter size() const
Definition: ArrayView.h:101
TCounter Counter
Definition: ArrayView.h:26
INLINE ArrayView(StorageType *data, TCounter size)
Definition: ArrayView.h:31
INLINE T & back()
Definition: ArrayView.h:91
INLINE ArrayView subset(const TCounter start, const TCounter length) const
Returns a subset of the arrayview.
Definition: ArrayView.h:110
INLINE Iterator< const StorageType > end() const
Definition: ArrayView.h:67
INLINE T & operator[](const TCounter idx)
Definition: ArrayView.h:71
INLINE Iterator< StorageType > begin()
Definition: ArrayView.h:55
INLINE ArrayView()
Definition: ArrayView.h:28
INLINE ArrayView & operator=(const ArrayView &other)
Copy operator.
Definition: ArrayView.h:44
INLINE Iterator< StorageType > end()
Definition: ArrayView.h:63
INLINE ArrayView(std::nullptr_t)
Explicitly initialize to nullptr.
Definition: ArrayView.h:40
Simple (forward) iterator over continuous array of objects of type T.
Definition: Iterator.h:18