Thu, 27 Feb 2020 12:23:42 +0200
use glm::project instead of figuring out the conversion manually...
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
24 | 3 | * Copyright (C) 2013 - 2020 Teemu Piippo |
19 | 4 | * |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #pragma once | |
20 | ||
21 | namespace iter | |
22 | { | |
23 | namespace _imp | |
24 | { | |
26 | 25 | template<typename T, typename Tint> |
19 | 26 | class RingAdapter; |
27 | } | |
28 | ||
29 | template<typename T> | |
26 | 30 | _imp::RingAdapter<T, int> ring(T&& collection); |
19 | 31 | |
26 | 32 | template<typename T, typename Tint> |
33 | _imp::RingAdapter<T, Tint> ring(T&& collection, Tint count); | |
19 | 34 | } |
35 | ||
36 | /* | |
37 | * Implements a ring adapter over T. This class corrects indices given to the element-operator so that they're within bounds. | |
38 | * The maximum amount can be specified manually. | |
39 | * | |
40 | * Example: | |
41 | * | |
42 | * int A[] = {10,20,30,40}; | |
43 | * ring(A)[0] == A[0 % 4] == A[0] | |
44 | * ring(A)[5] == A[5 % 4] == A[1] | |
45 | * ring(A)[-1] == ring(A)[-1 + 4] == A[3] | |
46 | */ | |
26 | 47 | template<typename T, typename Tint> |
19 | 48 | class iter::_imp::RingAdapter |
49 | { | |
50 | private: | |
51 | // The private section must come first because _collection is used in decltype() below. | |
26 | 52 | T&& collection; |
53 | const Tint count; | |
19 | 54 | |
55 | public: | |
26 | 56 | RingAdapter(T&& collection, Tint count) : |
19 | 57 | collection {collection}, |
58 | count {count} {} | |
59 | ||
26 | 60 | decltype(collection[Tint{}]) operator[](Tint index) |
19 | 61 | { |
62 | if (count == 0) | |
63 | { | |
64 | // Argh! ...let the collection deal with this case. | |
65 | return this->collection[index]; | |
66 | } | |
67 | else | |
68 | { | |
69 | index %= this->count; | |
70 | ||
71 | // Fix negative modulus... | |
72 | if (index < 0) | |
73 | index += this->count; | |
74 | ||
75 | return this->collection[index]; | |
76 | } | |
77 | } | |
78 | ||
26 | 79 | Tint size() const |
19 | 80 | { |
81 | return this->count; | |
82 | } | |
83 | }; | |
84 | ||
85 | /* | |
86 | * Convenience function for RingAdapter so that the template parameter does not have to be provided. The ring amount is assumed | |
87 | * to be the amount of elements in the collection. | |
88 | */ | |
89 | template<typename T> | |
26 | 90 | iter::_imp::RingAdapter<T, int> iter::ring(T&& collection) |
19 | 91 | { |
92 | return {collection, countof(collection)}; | |
93 | } | |
94 | ||
95 | /* | |
96 | * Version of ring() that allows manual specification of the count. | |
97 | */ | |
26 | 98 | template<typename T, typename Tint> |
99 | iter::_imp::RingAdapter<T, Tint> iter::ring(T&& collection, Tint count) | |
19 | 100 | { |
101 | return {collection, count}; | |
102 | } | |
103 | ||
26 | 104 | template<typename T, typename Tint> |
105 | int countof(const iter::_imp::RingAdapter<T, Tint>& ring) | |
19 | 106 | { |
107 | return ring.size(); | |
108 | } |