1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2020 Teemu Piippo |
|
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 { |
|
25 template<typename T, typename Tint> |
|
26 class RingAdapter; |
|
27 } |
|
28 |
|
29 template<typename T> |
|
30 _imp::RingAdapter<T, int> ring(T&& collection); |
|
31 |
|
32 template<typename T, typename Tint> |
|
33 _imp::RingAdapter<T, Tint> ring(T&& collection, Tint count); |
|
34 } |
|
35 |
|
36 /* |
|
37 * Implements a ring adapter over T. This class corrects indices given to the element-operator so that they're within |
|
38 * bounds. 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 */ |
|
47 template<typename T, typename Tint> |
|
48 class iter::_imp::RingAdapter |
|
49 { |
|
50 private: |
|
51 // The private section must come first because _collection is used in decltype() below. |
|
52 T&& collection; |
|
53 const Tint count; |
|
54 |
|
55 public: |
|
56 RingAdapter(T&& collection, Tint count) : |
|
57 collection {collection}, |
|
58 count {count} {} |
|
59 |
|
60 decltype(collection[Tint{}]) operator[](Tint index) |
|
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 |
|
79 Tint size() const |
|
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 |
|
87 * amount is assumed to be the amount of elements in the collection. |
|
88 */ |
|
89 template<typename T> |
|
90 iter::_imp::RingAdapter<T, int> iter::ring(T&& collection) |
|
91 { |
|
92 return {collection, countof(collection)}; |
|
93 } |
|
94 |
|
95 /* |
|
96 * Version of ring() that allows manual specification of the count. |
|
97 */ |
|
98 template<typename T, typename Tint> |
|
99 iter::_imp::RingAdapter<T, Tint> iter::ring(T&& collection, Tint count) |
|
100 { |
|
101 return {collection, count}; |
|
102 } |
|
103 |
|
104 template<typename T, typename Tint> |
|
105 int countof(const iter::_imp::RingAdapter<T, Tint>& ring) |
|
106 { |
|
107 return ring.size(); |
|
108 } |
|