BSplinebasis
Loading...
Searching...
No Matches
Grid.h
1/*
2 * ########################################################################
3 * The contents of this file is free and unencumbered software released into the
4 * public domain. For more information, please refer to <http://unlicense.org/>
5 * ########################################################################
6 */
7
8#ifndef BSPLINE_SUPPORT_GRID_H
9#define BSPLINE_SUPPORT_GRID_H
10
11#include <bspline/exceptions/BSplineException.h>
12#include <bspline/internal/misc.h>
13#include <bspline/internal/test_checks.h>
14
15#include <algorithm>
16#include <memory>
17#include <vector>
18
20using namespace bspline::exceptions;
21
26template <typename T>
27class Grid final {
28 private:
30 std::shared_ptr<const std::vector<T>> _data;
31
36 bool isSteadilyIncreasing() const {
37 for (size_t i = 1; i < _data->size(); i++) {
38 if ((*_data)[i - 1] >= (*_data)[i]) {
39 return false;
40 }
41 }
42 return true;
43 }
44
52 void checkValidity() const {
53 // Check the validity of the provided data.
54 if (!_data || _data->size() < 2) {
55 throw BSplineException(ErrorCode::MISSING_DATA);
56 } else if (!isSteadilyIncreasing()) {
57 throw BSplineException(ErrorCode::INCONSISTENT_DATA,
58 "The grid points are not steadily increasing.");
59 }
60 }
61
62 public:
66 using const_iterator = typename std::vector<T>::const_iterator;
67
68 Grid() = delete;
69
80 template <typename Iter>
83
90 explicit Grid(std::vector<T> v)
91 : Grid(std::make_shared<const std::vector<T>>(std::move(v))){};
92
99 explicit Grid(const std::initializer_list<T> &v) : Grid(v.begin(), v.end()){};
100
107 explicit Grid(std::shared_ptr<const std::vector<T>> data)
108 : _data(std::move(data)) {
109 checkValidity();
110 };
111
116 Grid(const Grid &g) noexcept = default;
117
122 Grid &operator=(const Grid &g) noexcept = default;
123
127 ~Grid() = default;
128
133 Grid(Grid &&g) = delete;
134
139 Grid &operator=(Grid &&g) = delete;
140
146 bool operator==(const Grid &g) const {
149 if (_data == g._data) {
150 // Fast path: Compare pointers.
151 return true;
152 }
153
154 // Slow path: Compare logically.
155 return (*_data) == (*g._data);
156 }
157
163 bool operator!=(const Grid &g) const { return !(*this == g); }
164
169 size_t size() const {
171 return _data->size();
172 };
173
180 std::shared_ptr<const std::vector<T>> getData() const {
182 return _data;
183 };
184
192 bool empty() const {
194 return _data->empty();
195 };
196
205 const T &operator[](size_t i) const {
207 return (*_data)[i];
208 };
209
218 const T &at(size_t i) const {
220 if (i >= size()) {
221 throw BSplineException(ErrorCode::INVALID_ACCESS);
222 }
223 return (*_data)[i];
224 };
225
231 const T &front() const {
233 if (empty()) {
234 throw BSplineException(ErrorCode::INVALID_ACCESS);
235 }
236 return _data->front();
237 };
238
244 const T &back() const {
246 if (empty()) {
247 throw BSplineException(ErrorCode::INVALID_ACCESS);
248 }
249 return _data->back();
250 };
251
258 return _data->begin();
259 };
260
267 return _data->end();
268 };
269
275 size_t findElement(const T &x) const {
277 const auto beginIt = begin();
278 const auto endIt = end();
279 const auto elementIt = std::lower_bound(beginIt, endIt, x);
280
281 if (elementIt == endIt || *elementIt != x) {
282 // Element was not found
283 throw BSplineException(ErrorCode::INCONSISTENT_DATA);
284 } else {
285 return std::distance(beginIt, elementIt);
286 }
287 };
288};
289} // namespace bspline::support
290#endif // BSPLINE_SUPPORT_GRID_H
The main exception class.
Definition BSplineException.h:84
Represents a global Grid.
Definition Grid.h:27
const T & at(size_t i) const
Gives access to the i-th element of the Grid.
Definition Grid.h:218
typename std::vector< T >::const_iterator const_iterator
Iterator type.
Definition Grid.h:66
Grid(Iter begin, Iter end)
Constructs a Grid from a set of begin and end iterators.
Definition Grid.h:81
Grid(const std::initializer_list< T > &v)
Constructs a Grid from a std::initializer_list.
Definition Grid.h:99
const_iterator end() const
Returns the end iterator of the Grid..
Definition Grid.h:265
Grid & operator=(Grid &&g)=delete
Explicitly deleted move assignment operator.
bool operator!=(const Grid &g) const
Negated comparison operator.
Definition Grid.h:163
const T & operator[](size_t i) const
Gives access to the i-th element of the Grid.
Definition Grid.h:205
size_t size() const
Returns the number of elements of the Grid.
Definition Grid.h:169
Grid(std::shared_ptr< const std::vector< T > > data)
Constructs a Grid from a std::shared_ptr<const std::vector<T>>.
Definition Grid.h:107
bool empty() const
Checks whether the spline is empty.
Definition Grid.h:192
std::shared_ptr< const std::vector< T > > getData() const
Gives access to the underlying data.
Definition Grid.h:180
const_iterator begin() const
Returns the begin iterator of the Grid.
Definition Grid.h:256
Grid(Grid &&g)=delete
Explicitly deleted move constructor.
Grid(const Grid &g) noexcept=default
Default copy constructor.
size_t findElement(const T &x) const
Returns the index corresponding to the element x.
Definition Grid.h:275
Grid(std::vector< T > v)
Constructs a Grid from a std::vector.
Definition Grid.h:90
Grid & operator=(const Grid &g) noexcept=default
Default copy assignment operator.
bool operator==(const Grid &g) const
Comparison operator.
Definition Grid.h:146
const T & back() const
Returns a reference to the last element of the Grid.
Definition Grid.h:244
~Grid()=default
Default destructor.
const T & front() const
Returns a reference to the first element of the Grid.
Definition Grid.h:231
Exceptions and error codes.
Definition BSplineException.h:19
Namespace for the Spline's Grid and Support.
Definition Grid.h:19