forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcNumericRange.I
More file actions
282 lines (257 loc) · 6.31 KB
/
dcNumericRange.I
File metadata and controls
282 lines (257 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file dcNumericRange.I
* @author drose
* @date 2004-06-21
*/
/**
*
*/
template <class NUM>
INLINE DCNumericRange<NUM>::
DCNumericRange() {
}
/**
*
*/
template <class NUM>
INLINE DCNumericRange<NUM>::
DCNumericRange(Number min, Number max) {
add_range(min, max);
}
/**
*
*/
template <class NUM>
INLINE DCNumericRange<NUM>::
DCNumericRange(const DCNumericRange<NUM> ©) :
_ranges(copy._ranges)
{
}
/**
*
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
operator = (const DCNumericRange<NUM> ©) {
_ranges = copy._ranges;
}
/**
* Returns true if the indicated number is within the specified range, false
* otherwise.
*/
template <class NUM>
INLINE bool DCNumericRange<NUM>::
is_in_range(Number num) const {
if (_ranges.empty()) {
return true;
}
typename Ranges::const_iterator ri;
for (ri = _ranges.begin(); ri != _ranges.end(); ++ri) {
if (num >= (*ri)._min && num <= (*ri)._max) {
return true;
}
}
return false;
}
/**
* Convenience function to validate the indicated number. If the number is
* within the specified range, does nothing; otherwise, if it is outside the
* range, sets range_error to true.
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
validate(Number num, bool &range_error) const {
if (!is_in_range(num)) {
range_error = true;
}
}
/**
* Returns true if the numeric range specifies exactly one legal value, false
* if multiple values are legal.
*/
template <class NUM>
INLINE bool DCNumericRange<NUM>::
has_one_value() const {
return _ranges.size() == 1 && _ranges[0]._min == _ranges[0]._max;
}
/**
* If has_one_value() returns true, this returns the one legal value accepted
* by the numeric range.
*/
template <class NUM>
INLINE typename DCNumericRange<NUM>::Number DCNumericRange<NUM>::
get_one_value() const {
nassertr(has_one_value(), 0);
return _ranges[0]._min;
}
/**
*
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
generate_hash(HashGenerator &hashgen) const {
if (!_ranges.empty()) {
hashgen.add_int(_ranges.size());
typename Ranges::const_iterator ri;
for (ri = _ranges.begin(); ri != _ranges.end(); ++ri) {
// We don't account for the fractional part of floating-point ranges
// here. Shouldn't be a real issue.
hashgen.add_int((int)(*ri)._min);
hashgen.add_int((int)(*ri)._max);
}
}
}
/**
*
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
output(std::ostream &out, Number divisor) const {
if (!_ranges.empty()) {
typename Ranges::const_iterator ri;
ri = _ranges.begin();
output_minmax(out, divisor, *ri);
++ri;
while (ri != _ranges.end()) {
out << ", ";
output_minmax(out, divisor, *ri);
++ri;
}
}
}
/**
* Outputs the range, formatting the numeric values as quoted ASCII
* characters.
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
output_char(std::ostream &out, Number divisor) const {
if (divisor != 1) {
output(out, divisor);
} else {
if (!_ranges.empty()) {
typename Ranges::const_iterator ri;
ri = _ranges.begin();
output_minmax_char(out, *ri);
++ri;
while (ri != _ranges.end()) {
out << ", ";
output_minmax_char(out, *ri);
++ri;
}
}
}
}
/**
*
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
clear() {
_ranges.clear();
}
/**
* Adds a new minmax to the list of ranges. This is normally called only
* during dc file parsing. Returns true if successful, or false if the new
* minmax overlaps an existing minmax.
*/
template <class NUM>
INLINE bool DCNumericRange<NUM>::
add_range(Number min, Number max) {
// Check for an overlap. This is probably indicative of a typo and should
// be reported.
if (max < min) {
return false;
}
typename Ranges::const_iterator ri;
for (ri = _ranges.begin(); ri != _ranges.end(); ++ri) {
if ((min >= (*ri)._min && min <= (*ri)._max) ||
(max >= (*ri)._min && max <= (*ri)._max) ||
(min < (*ri)._min && max > (*ri)._max)) {
return false;
}
}
MinMax minmax;
minmax._min = min;
minmax._max = max;
_ranges.push_back(minmax);
return true;
}
/**
* Returns true if the range contains no elements (and thus allows all
* numbers), false if it contains at least one.
*/
template <class NUM>
INLINE bool DCNumericRange<NUM>::
is_empty() const {
return _ranges.empty();
}
/**
* Returns the number of minmax components in the range description.
*/
template <class NUM>
INLINE int DCNumericRange<NUM>::
get_num_ranges() const {
return _ranges.size();
}
/**
* Returns the minimum value defined by the nth component.
*/
template <class NUM>
INLINE typename DCNumericRange<NUM>::Number DCNumericRange<NUM>::
get_min(int n) const {
nassertr(n >= 0 && n < (int)_ranges.size(), 0);
return _ranges[n]._min;
}
/**
* Returns the maximum value defined by the nth component.
*/
template <class NUM>
INLINE typename DCNumericRange<NUM>::Number DCNumericRange<NUM>::
get_max(int n) const {
nassertr(n >= 0 && n < (int)_ranges.size(), 0);
return _ranges[n]._max;
}
/**
* Outputs a single element of the range description.
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
output_minmax(std::ostream &out, Number divisor, const MinMax &range) const {
if (divisor == 1) {
if (range._min == range._max) {
out << range._min;
} else {
out << range._min << "-" << range._max;
}
} else {
if (range._min == range._max) {
out << (double)range._min / (double)divisor;
} else {
out << (double)range._min / (double)divisor
<< "-"
<< (double)range._max / (double)divisor;
}
}
}
/**
* Outputs a single element of the range description.
*/
template <class NUM>
INLINE void DCNumericRange<NUM>::
output_minmax_char(std::ostream &out, const MinMax &range) const {
if (range._min == range._max) {
DCPacker::enquote_string(out, '\'', std::string(1, range._min));
} else {
DCPacker::enquote_string(out, '\'', std::string(1, range._min));
out << "-";
DCPacker::enquote_string(out, '\'', std::string(1, range._max));
}
}