src/generics/range.h

Sat, 17 Mar 2018 20:47:35 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 17 Mar 2018 20:47:35 +0200
changeset 1295
ac8f0297885b
parent 1273
900f1dfae46b
child 1326
69a90bd2dba2
permissions
-rw-r--r--

the header is now also saved

1273
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
1 /*
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
4 *
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
8 * (at your option) any later version.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
9 *
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
13 * GNU General Public License for more details.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
14 *
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
17 */
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
18
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
19
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
20 #pragma once
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
21
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
22 /*
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
23 * Range
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
24 *
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
25 * This class models a range of values (by default integers but anything with a
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
26 * total order qualifies) The value type must be constructible with 0 and 1 for
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
27 * the sake of default values.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
28 *
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
29 * The range may be iterated, in which case the first value yielded will be the
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
30 * lower bound. Then, the iterator's value is incremented by a certain step
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
31 * value, yielding the next value. This is continued until the iterator would
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
32 * yield a value larger than upper bound.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
33 *
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
34 * A range can be constructed in a number of ways:
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
35 * - Range<>()
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
36 * default-construction, lower and upper bounds will be set to 0.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
37 * - Range<>(first, end)
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
38 * the range contains the values [first, end - 1].
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
39 * Iteration of this range yields: first, first + 1, ..., end - 1.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
40 * - Range<>(first, second, end)
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
41 * the range contains the values [first, end - 1].
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
42 * Iteration of this range yields:
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
43 * - first
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
44 * - first + (second - first)
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
45 * - first + 2*(second - first)
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
46 * - end - 1 (assuming that (end - 1) can be represented with
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
47 * (first + n*(second - first)) with some n
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
48 *
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
49 * The function range() is provided to avoid the angle brackets for the common
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
50 * use case where T = int.
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
51 */
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
52 template<typename T = int>
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
53 class Range
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
54 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
55 struct Iterator
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
56 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
57 const T baseValue;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
58 const T stepValue;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
59 int stepCount = 0;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
60
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
61 Iterator() :
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
62 baseValue {0},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
63 stepValue {1},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
64 stepCount {0} {}
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
65
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
66 Iterator(T value, T stepValue, T stepCount) :
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
67 baseValue {value},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
68 stepValue {stepValue},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
69 stepCount {stepCount} {}
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
70
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
71 T operator*() const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
72 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
73 return baseValue + stepCount * stepValue;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
74 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
75
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
76 bool operator!=(const Iterator& other) const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
77 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
78 return stepCount != other.stepCount;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
79 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
80
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
81 Iterator& operator++()
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
82 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
83 stepCount += 1;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
84 return *this;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
85 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
86
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
87 Iterator& operator--()
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
88 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
89 stepCount -= 1;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
90 return *this;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
91 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
92 };
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
93
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
94 public:
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
95 Range() :
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
96 beginValue {0},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
97 endValue {0},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
98 step {1} {}
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
99
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
100 Range(T first, T second, T last) :
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
101 beginValue {first},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
102 endValue {last + (second - first)},
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
103 step {second - first} {}
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
104
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
105 Iterator begin() const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
106 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
107 return {beginValue, step, 0};
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
108 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
109
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
110 bool contains(T value) const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
111 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
112 return value >= beginValue and value < endValue;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
113 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
114
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
115 Iterator end() const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
116 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
117 return {beginValue, step, (endValue - beginValue) / step};
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
118 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
119
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
120 bool overlaps(const Range<T>& other) const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
121 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
122 return contains(other.beginValue) or contains(other.endValue - 1);
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
123 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
124
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
125 bool operator==(const Range<T>& other) const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
126 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
127 return beginValue == other.beginValue
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
128 and endValue == other.endValue
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
129 and step == other.step;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
130 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
131
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
132 bool operator!=(Range<T> const& other) const
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
133 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
134 return not operator==(other);
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
135 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
136
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
137 private:
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
138 T beginValue;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
139 T endValue;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
140 T step;
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
141 };
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
142
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
143 /*
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
144 * Returns a range from [first, second, ..., last]
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
145 */
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
146 template<typename T = int>
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
147 Range<T> range(T first, T second, T last)
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
148 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
149 return {first, second, last};
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
150 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
151
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
152 /*
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
153 * Returns a range from [first, first + 1, ..., end - 1]
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
154 */
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
155 /*
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
156 template<typename T = int>
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
157 Range<T> range(T end)
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
158 {
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
159 return range<T>(0, 1, end - 1);
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
160 }
900f1dfae46b Implemented row moving in the model and replaced swapping with it
Santeri Piippo
parents:
diff changeset
161 */

mercurial