7 #include <initializer_list>
10 #include <type_traits>
17 template<std::
size_t Dim,
typename T>
19 static_assert(std::is_arithmetic<T>::value,
"ndcoord component type must be arithmetic");
22 std::array<T, Dim> components_;
28 using iterator =
typename std::array<T, Dim>::iterator;
33 ndcoord(T value = 0) noexcept { components_.fill(value); }
37 auto out = components_.begin();
38 for(
auto in = begin; in !=
end; ++in, ++out)
39 *out = static_cast<T>(*in);
42 ndcoord(std::initializer_list<T> l) noexcept :
52 assert(i >= 0 && i < Dim);
53 return components_[i];
56 assert(i >= 0 && i < Dim);
57 return components_[i];
68 template<
typename Unary>
70 for(T& c : *
this) c = fct(c);
74 template<
typename Binary>
76 for(std::ptrdiff_t i = 0; i < Dim; ++i)
77 components_[i] = fct(components_[i], c[i]);
93 {
return transform(a, b, std::plus<T>()); }
95 {
return transform(a, b, std::minus<T>()); }
97 {
return transform(a, b, std::multiplies<T>()); }
99 {
return transform(a, b, std::divides<T>()); }
107 {
return a.components_ == b.components_; }
109 {
return a.components_ != b.components_; }
113 for(T c : components_) prod *= c;
117 const T&
front() const noexcept {
return components_.front(); }
118 T&
front() noexcept {
return components_.front(); }
119 const T&
back() const noexcept {
return components_.back(); }
120 T&
back() noexcept {
return components_.back(); }
122 template<std::size_t Section_dim = Dim - 1>
128 template<std::size_t Section_dim = Dim - 1>
136 for(std::ptrdiff_t j = 0; j < i; ++j) result[j] = components_[j];
137 for(std::ptrdiff_t j = i + 1; j < Dim; ++j) result[j - 1] = components_[j];
143 template<std::
size_t Dim,
typename T,
typename Unary>
146 for(std::ptrdiff_t i = 0; i < Dim; ++i) o[i] = fct(a[i]);
151 template<std::
size_t Dim,
typename T,
typename Binary>
154 for(std::ptrdiff_t i = 0; i < Dim; ++i) o[i] = fct(a[i], b[i]);
159 template<std::
size_t Dim,
typename T>
160 std::ostream& operator<<(std::ostream& str, const ndcoord<Dim, T>& coord) {
162 for(std::ptrdiff_t i = 0; i < Dim - 1; i++) str << coord[i] <<
", ";
163 str << coord.
back() <<
')';
169 std::ostream& operator<<(std::ostream& str, const ndcoord<0, T>& coord) {
174 template<std::
size_t Dim>
177 template<std::
size_t Dim>
181 template<
typename T,
typename... Components>
183 return ndcoord<
sizeof...(Components), T>({
static_cast<T
>(c)... });
186 template<
typename... Components>
188 return make_ndcoord<std::size_t>(c...);
191 template<
typename... Components>
193 return make_ndcoord<std::ptrdiff_t>(c...);
197 template<std::
size_t Dim1, std::
size_t Dim2,
typename T>
200 if(Dim1 + Dim2 == 0)
return coord;
201 auto it = coord.
begin();
202 if(Dim1 > 0)
for(T c : coord1) *(it++) = c;
203 if(Dim2 > 0)
for(T c : coord2) *(it++) = c;
208 template<std::
size_t Dim1,
typename T,
typename Int>
214 template<std::
size_t Dim2,
typename T,
typename Int>
222 return {coord[1], coord[0]};
friend ndcoord operator/(const ndcoord &a, T val) noexcept
Definition: ndcoord.h:103
ndcoord & operator/=(T val) noexcept
Definition: ndcoord.h:87
const_iterator begin() const noexcept
Definition: ndcoord.h:61
ndcoord< Dim, T > transform(const ndcoord< Dim, T > &a, Unary fct)
Definition: ndcoord.h:144
const time_unit & const_reference
Definition: ndcoord.h:27
ndcoord< Dim-1, T > erase(std::ptrdiff_t i) const noexcept
Definition: ndcoord.h:134
ndcoord(const ndcoord< Dim, T2 > &coord)
Definition: ndcoord.h:48
friend ndcoord operator+(const ndcoord &a, const ndcoord &b) noexcept
Definition: ndcoord.h:92
auto tail() const noexcept
Definition: ndcoord.h:123
ndcoord & operator+=(const ndcoord &c) noexcept
Definition: ndcoord.h:81
auto make_ndcoord(Components...c)
Definition: ndcoord.h:182
ndcoord & operator*=(const ndcoord &c) noexcept
Definition: ndcoord.h:83
ndcoord & transform_inplace(Unary fct)
Definition: ndcoord.h:69
typename std::array< time_unit, Dim >::iterator iterator
Definition: ndcoord.h:28
friend bool operator!=(const ndcoord &a, const ndcoord &b) noexcept
Definition: ndcoord.h:108
ndcoord & operator/=(const ndcoord &c) noexcept
Definition: ndcoord.h:84
friend ndcoord operator*(const ndcoord &a, T val) noexcept
Definition: ndcoord.h:101
auto head() const noexcept
Definition: ndcoord.h:129
const_iterator end() const noexcept
Definition: ndcoord.h:64
T & front() noexcept
Definition: ndcoord.h:118
iterator begin() noexcept
Definition: ndcoord.h:60
ndcoord(It begin, It end)
Definition: ndcoord.h:36
auto make_ndsize(Components...c)
Definition: ndcoord.h:187
Vector of n-dimensional coordinates.
Definition: ndcoord.h:18
friend ndcoord operator/(const ndcoord &a, const ndcoord &b) noexcept
Definition: ndcoord.h:98
T product() const noexcept
Definition: ndcoord.h:111
iterator end() noexcept
Definition: ndcoord.h:63
ndcoord operator-() noexcept
Definition: ndcoord.h:90
ndcoord< Dim1+Dim2, T > ndcoord_cat(const ndcoord< Dim1, T > &coord1, const ndcoord< Dim2, T > &coord2)
Definition: ndcoord.h:198
time_unit & reference
Definition: ndcoord.h:26
ndcoord & transform_inplace(const ndcoord &c, Binary fct)
Definition: ndcoord.h:75
time_unit value_type
Definition: ndcoord.h:25
auto make_ndptrdiff(Components...c)
Definition: ndcoord.h:192
std::ptrdiff_t difference_type
Definition: ndcoord.h:31
const T & front() const noexcept
Definition: ndcoord.h:117
size_type size() const noexcept
Definition: ndcoord.h:66
const T & back() const noexcept
Definition: ndcoord.h:119
const_iterator cend() const noexcept
Definition: ndcoord.h:65
const_iterator cbegin() const noexcept
Definition: ndcoord.h:62
T & operator[](std::ptrdiff_t i) noexcept
Definition: ndcoord.h:51
friend ndcoord operator-(const ndcoord &a, const ndcoord &b) noexcept
Definition: ndcoord.h:94
ndcoord & operator*=(T val) noexcept
Definition: ndcoord.h:86
ndarray_view< 2, T > flip(const ndarray_view< 2, T > &vw)
Definition: ndarray_view.h:216
ndcoord(T value=0) noexcept
Definition: ndcoord.h:33
ndcoord & operator-=(const ndcoord &c) noexcept
Definition: ndcoord.h:82
T & back() noexcept
Definition: ndcoord.h:120
const T & operator[](std::ptrdiff_t i) const noexcept
Definition: ndcoord.h:55
ndcoord operator+() noexcept
Definition: ndcoord.h:89
ndcoord(std::initializer_list< T > l) noexcept
Definition: ndcoord.h:42
std::size_t size_type
Definition: ndcoord.h:30
typename std::array< time_unit, Dim >::const_iterator const_iterator
Definition: ndcoord.h:29
friend ndcoord operator*(const ndcoord &a, const ndcoord &b) noexcept
Definition: ndcoord.h:96
friend bool operator==(const ndcoord &a, const ndcoord &b) noexcept
Definition: ndcoord.h:106