LSST Applications g180d380827+0f66a164bb,g2079a07aa2+86d27d4dc4,g2305ad1205+7d304bc7a0,g29320951ab+500695df56,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+e42ea45bea,g48712c4677+36a86eeaa5,g487adcacf7+2dd8f347ac,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+c70619cc9d,g5a732f18d5+53520f316c,g5ea96fc03c+341ea1ce94,g64a986408d+f7cd9c7162,g858d7b2824+f7cd9c7162,g8a8a8dda67+585e252eca,g99cad8db69+469ab8c039,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+c92fc63c7e,gbd866b1f37+f7cd9c7162,gc120e1dc64+02c66aa596,gc28159a63d+0e5473021a,gc3e9b769f7+b0068a2d9f,gcf0d15dbbd+e42ea45bea,gdaeeff99f8+f9a426f77a,ge6526c86ff+84383d05b3,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+f7cd9c7162,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
PackedIndex.h
Go to the documentation of this file.
1// -*- LSST-C++ -*-
2/*
3 * Developed for the LSST Data Management System.
4 * This product includes software developed by the LSST Project
5 * (https://www.lsst.org).
6 * See the COPYRIGHT file at the top-level directory of this distribution
7 * for details of code ownership.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22#ifndef LSST_AFW_MATH_POLYNOMIALS_PackedIndex_h_INCLUDED
23#define LSST_AFW_MATH_POLYNOMIALS_PackedIndex_h_INCLUDED
24
25namespace lsst { namespace geom { namespace polynomials {
26
28enum class PackingOrder {
29
43 YX,
44
58 XY
59};
60
69struct Index2d {
70
72 constexpr Index2d() noexcept : flat(0), nx(0), ny(0) {}
73
75 constexpr Index2d(std::size_t flat_, std::size_t nx_, std::size_t ny_) noexcept :
76 flat(flat_), nx(nx_), ny(ny_)
77 {}
78
80 constexpr bool operator==(Index2d const & other) const noexcept {
81 return flat == other.flat && nx == other.nx && ny == other.ny;
82 }
83
85 constexpr bool operator!=(Index2d const & other) const noexcept {
86 return !(*this == other);
87 }
88
92};
93
94namespace detail {
95
96// Specialization of all PackedIndexIterator/PackedIndexRange logic
97// that isn't common across PackingOrders.
98template <PackingOrder packing> struct PackingOrderTraits;
99
100template <>
102
103 // Return the offset of the given coefficient after the (nx + ny) offset is subtracted off.
105
106 static void increment(Index2d & index) {
107 if (index.ny == 0) {
108 index.ny = index.nx + 1;
109 index.nx = 0;
110 } else {
111 --index.ny;
112 ++index.nx;
113 }
114 }
115
116 // Return the nx and ny values appropriate for the end iterator of a range of the given order.
117 static std::size_t getEndX(std::size_t order) { return 0; }
118 static std::size_t getEndY(std::size_t order) { return order + 1; }
119
120};
121
122template <>
124
125 // Return the offset of the given coefficient after the (nx + ny) offset is subtracted off.
127
128 static void increment(Index2d & index) {
129 if (index.nx == 0) {
130 index.nx = index.ny + 1;
131 index.ny = 0;
132 } else {
133 --index.nx;
134 ++index.ny;
135 }
136 }
137
138 // Return the nx and ny values appropriate for the end iterator of a range of the given order.
139 static std::size_t getEndX(std::size_t order) { return order + 1; }
140 static std::size_t getEndY(std::size_t order) { return 0; }
141
142};
143
144} // namespace detail
145
163template <PackingOrder packing>
166public:
167
170 using pointer = Index2d const *;
171 using reference = Index2d const &;
173
175 static constexpr std::size_t computeOffset(std::size_t order) noexcept {
176 return order*(order + 1)/2;
177 }
178
180 static constexpr std::size_t computeSize(std::size_t order) noexcept {
181 return computeOffset(order + 1);
182 }
183
185 static constexpr std::size_t computeIndex(std::size_t nx, std::size_t ny) noexcept {
186 return computeOffset(nx + ny) + Traits::computeInnerIndex(nx, ny);
187 }
188
190 static constexpr PackedIndexIterator makeEnd(std::size_t order) noexcept {
192 }
193
195 constexpr PackedIndexIterator() noexcept : _index() {}
196
198 constexpr PackedIndexIterator(std::size_t nx, std::size_t ny) noexcept :
199 _index(computeIndex(nx, ny), nx, ny)
200 {}
201
203 constexpr reference operator*() const noexcept { return _index; }
204
206 constexpr pointer operator->() const noexcept { return &_index; }
207
210 ++_index.flat;
211 Traits::increment(_index);
212 return *this;
213 }
214
217 PackedIndexIterator r(*this);
218 ++(*this);
219 return r;
220 }
221
223 constexpr bool operator==(PackedIndexIterator const & other) const noexcept {
224 return _index == other._index;
225 }
226
228 constexpr bool operator!=(PackedIndexIterator const & other) const noexcept {
229 return !(*this == other);
230 }
231
232private:
233
234 constexpr PackedIndexIterator(std::size_t order) noexcept :
235 _index(computeOffset(order + 1), Traits::getEndX(order), Traits::getEndY(order))
236 {}
237
238 Index2d _index;
239};
240
247template <PackingOrder packing>
249public:
250
255 using pointer = typename iterator::pointer;
258
260 static constexpr std::size_t computeOffset(std::size_t order) noexcept {
262 }
263
265 static constexpr std::size_t computeSize(std::size_t order) noexcept {
267 }
268
270 static constexpr std::size_t computeIndex(std::size_t nx, std::size_t ny) noexcept {
271 return iterator::computeIndex(nx, ny);
272 }
273
275 constexpr PackedIndexRange(iterator first, iterator last) noexcept :
276 _begin(first),
277 _end(last)
278 {}
279
281 constexpr iterator begin() const noexcept { return _begin; }
282
284 constexpr iterator cbegin() const noexcept { return _begin; }
285
287 constexpr iterator end() const noexcept { return _end; }
288
290 constexpr iterator cend() const noexcept { return _end; }
291
293 constexpr std::size_t size() const noexcept { return _end->flat - _begin->flat; }
294
296 constexpr bool empty() const noexcept { return size() == 0u; }
297
299 constexpr bool operator==(PackedIndexRange const & other) const noexcept {
300 return _begin == other._begin && _end == other._end;
301 }
302
304 constexpr bool operator!=(PackedIndexRange const & other) const noexcept {
305 return !(*this == other);
306 }
307
308private:
309 iterator _begin;
310 iterator _end;
311};
312
313}}} // namespace lsst::geom::polynomials
314
315#endif // !LSST_AFW_MATH_POLYNOMIALS_PackedIndex_h_INCLUDED
An iterator for traversing "packed" triangular 2-d series expansions, in which two 1-d expansions are...
PackedIndexIterator operator++(int) noexcept
Move to the next element in the packed array and return a copy of the iterator before the move.
constexpr pointer operator->() const noexcept
Dereference the iterator, yielding a Index2d const pointer.
constexpr bool operator!=(PackedIndexIterator const &other) const noexcept
Inequality comparison.
constexpr PackedIndexIterator(std::size_t nx, std::size_t ny) noexcept
Construct an iterator pointing to the element with the given x and y orders.
static constexpr std::size_t computeIndex(std::size_t nx, std::size_t ny) noexcept
Return the flattened index for the element with the given x and y orders.
constexpr bool operator==(PackedIndexIterator const &other) const noexcept
Equality comparison.
PackedIndexIterator & operator++() noexcept
Move to the next element in the packed array and return the iterator.
static constexpr PackedIndexIterator makeEnd(std::size_t order) noexcept
Construct an iterator one past the end of an expansion with the given order.
static constexpr std::size_t computeSize(std::size_t order) noexcept
Return the flattened size of an expansion with the given maximum order (inclusive).
constexpr PackedIndexIterator() noexcept
Construct an iterator at the beginning of an expansion of any order.
static constexpr std::size_t computeOffset(std::size_t order) noexcept
Return the flattened offset to the start of the given order.
constexpr reference operator*() const noexcept
Dereference the iterator, yielding a Index2d const reference.
A specialized iterator range class for PackedIndexIterator, providing size calculation,...
constexpr bool empty() const noexcept
Return true if the number of elements in the flattened expansion is zero.
constexpr iterator begin() const noexcept
Return an iterator to the start of the range.
PackedIndexIterator< packing > iterator
typename iterator::reference reference
constexpr PackedIndexRange(iterator first, iterator last) noexcept
Construct from begin and end iterators.
typename iterator::difference_type difference_type
constexpr bool operator==(PackedIndexRange const &other) const noexcept
Equality comparison.
constexpr bool operator!=(PackedIndexRange const &other) const noexcept
Inequality comparison.
typename iterator::pointer pointer
constexpr iterator cbegin() const noexcept
Return an iterator to the start of the range.
static constexpr std::size_t computeSize(std::size_t order) noexcept
Return the flattened size of an expansion with the given maximum order (inclusive).
constexpr iterator end() const noexcept
Return an iterator to one past the end of the range.
typename iterator::value_type value_type
static constexpr std::size_t computeIndex(std::size_t nx, std::size_t ny) noexcept
Return the flattened index for the element with the given x and y orders.
constexpr iterator cend() const noexcept
Return an iterator to one past the end of the range.
constexpr std::size_t size() const noexcept
Return the number of elements in the flattened expansion.
static constexpr std::size_t computeOffset(std::size_t order) noexcept
Return the flattened offset to the start of the given order.
PackingOrder
Enum defining the packing orders used to order 2-d polynomial coefficients.
Definition PackedIndex.h:28
@ XY
A pair of indices is mapped to the flattened position , which yields the (nx, ny) ordering.
@ YX
A pair of indices is mapped to the flattened position , which yields the (nx, ny) ordering.
A custom tuple that relates the indices of two 1-d functions for x and y to the flattened index for t...
Definition PackedIndex.h:69
constexpr bool operator==(Index2d const &other) const noexcept
Equality comparison.
Definition PackedIndex.h:80
std::size_t nx
Index into the 1-d function for nx.
Definition PackedIndex.h:90
constexpr bool operator!=(Index2d const &other) const noexcept
Inequality comparison.
Definition PackedIndex.h:85
std::size_t ny
Index into the 1-d functoin for ny.
Definition PackedIndex.h:91
std::size_t flat
Index into the flattened 2-d function.
Definition PackedIndex.h:89
constexpr Index2d(std::size_t flat_, std::size_t nx_, std::size_t ny_) noexcept
Construct with the provided values.
Definition PackedIndex.h:75
constexpr Index2d() noexcept
Construct an index with zero entries.
Definition PackedIndex.h:72
static std::size_t computeInnerIndex(std::size_t nx, std::size_t ny)
static std::size_t computeInnerIndex(std::size_t nx, std::size_t ny)
table::Key< int > order