Sat, 15 Sep 2018 15:57:56 +0300
refactor
1319 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
1326 | 3 | * Copyright (C) 2013 - 2018 Teemu Piippo |
1319 | 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 | #include "../basics.h" | |
21 | ||
22 | /* | |
23 | * Implements a ring adapter over T. This class corrects indices given to the element-operator so that they're within bounds. | |
24 | * The maximum amount can be specified manually. | |
25 | * | |
26 | * Example: | |
27 | * | |
28 | * int A[] = {10,20,30,40}; | |
29 | * ring(A)[0] == A[0 % 4] == A[0] | |
30 | * ring(A)[5] == A[5 % 4] == A[1] | |
31 | * ring(A)[-1] == ring(A)[-1 + 4] == A[3] | |
32 | */ | |
33 | template<typename T> | |
34 | class RingAdapter | |
35 | { | |
36 | private: | |
37 | // The private section must come first because _collection is used in decltype() below. | |
38 | T& _collection; | |
39 | const int _count; | |
40 | ||
41 | public: | |
42 | RingAdapter(T& collection, int count) : | |
43 | _collection {collection}, | |
44 | _count {count} {} | |
45 | ||
46 | template<typename IndexType> | |
47 | decltype(_collection[IndexType()]) operator[](IndexType index) | |
48 | { | |
49 | if (_count == 0) | |
50 | { | |
51 | // Argh! ...let the collection deal with this case. | |
52 | return _collection[0]; | |
53 | } | |
54 | else | |
55 | { | |
56 | index %= _count; | |
57 | ||
58 | // Fix negative modulus... | |
59 | if (index < 0) | |
60 | index += _count; | |
61 | ||
62 | return _collection[index]; | |
63 | } | |
64 | } | |
65 | ||
66 | int size() const | |
67 | { | |
68 | return _count; | |
69 | } | |
70 | }; | |
71 | ||
72 | /* | |
73 | * Convenience function for RingAdapter so that the template parameter does not have to be provided. The ring amount is assumed | |
74 | * to be the amount of elements in the collection. | |
75 | */ | |
76 | template<typename T> | |
77 | RingAdapter<T> ring(T& collection) | |
78 | { | |
79 | return {collection, countof(collection)}; | |
80 | } | |
81 | ||
82 | /* | |
83 | * Version of ring() that allows manual specification of the count. | |
84 | */ | |
85 | template<typename T> | |
86 | RingAdapter<T> ring(T& collection, int count) | |
87 | { | |
88 | return {collection, count}; | |
89 | } | |
90 | ||
91 | template<typename T> | |
92 | int countof(const RingAdapter<T>& ring) | |
93 | { | |
94 | return ring.size(); | |
95 | } |