Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _BGFX_RANGE_BASICRANGE_HXX
30 : : #define _BGFX_RANGE_BASICRANGE_HXX
31 : :
32 : : #include <sal/types.h>
33 : : #include <float.h>
34 : : #include <basegfx/numeric/ftools.hxx>
35 : :
36 : :
37 : : namespace basegfx
38 : : {
39 : : template< typename T, typename Traits > class BasicRange
40 : : {
41 : : protected:
42 : : T mnMinimum;
43 : : T mnMaximum;
44 : :
45 : : public:
46 : : typedef T ValueType;
47 : : typedef Traits TraitsType;
48 : :
49 : 16474536 : BasicRange() :
50 : : mnMinimum(Traits::maxVal()),
51 : 16474536 : mnMaximum(Traits::minVal())
52 : : {
53 : 16474536 : }
54 : :
55 : 253966 : explicit BasicRange( T nValue ) :
56 : : mnMinimum(nValue),
57 : 253966 : mnMaximum(nValue)
58 : : {
59 : 253966 : }
60 : :
61 : 520933 : void reset()
62 : : {
63 : 520933 : mnMinimum = Traits::maxVal();
64 : 520933 : mnMaximum = Traits::minVal();
65 : 520933 : }
66 : :
67 : 47954034 : bool isEmpty() const
68 : : {
69 : 47954034 : return Traits::maxVal() == mnMinimum;
70 : : }
71 : :
72 : 5221731 : T getMinimum() const { return mnMinimum; }
73 : 5239090 : T getMaximum() const { return mnMaximum; }
74 : :
75 : 47882 : double getCenter() const
76 : : {
77 [ - + ]: 47882 : if(isEmpty())
78 : : {
79 : 0 : return 0.0;
80 : : }
81 : : else
82 : : {
83 : 47882 : return ((mnMaximum + mnMinimum) / 2.0);
84 : : }
85 : : }
86 : :
87 : 172717 : bool isInside(T nValue) const
88 : : {
89 [ - + ]: 172717 : if(isEmpty())
90 : : {
91 : 0 : return false;
92 : : }
93 : : else
94 : : {
95 [ + + ][ + + ]: 172717 : return (nValue >= mnMinimum) && (nValue <= mnMaximum);
96 : : }
97 : : }
98 : :
99 : 15026 : bool isInside(const BasicRange& rRange) const
100 : : {
101 [ - + ]: 15026 : if(isEmpty())
102 : : {
103 : 0 : return false;
104 : : }
105 : : else
106 : : {
107 [ - + ]: 15026 : if(rRange.isEmpty())
108 : : {
109 : 0 : return false;
110 : : }
111 : : else
112 : : {
113 [ + + ][ + + ]: 15026 : return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
114 : : }
115 : : }
116 : : }
117 : :
118 : 73224 : bool overlaps(const BasicRange& rRange) const
119 : : {
120 [ - + ]: 73224 : if(isEmpty())
121 : : {
122 : 0 : return false;
123 : : }
124 : : else
125 : : {
126 [ - + ]: 73224 : if(rRange.isEmpty())
127 : : {
128 : 0 : return false;
129 : : }
130 : : else
131 : : {
132 [ + + ][ + + ]: 73224 : return !((rRange.mnMaximum < mnMinimum) || (rRange.mnMinimum > mnMaximum));
133 : : }
134 : : }
135 : : }
136 : :
137 : 41222 : bool overlapsMore(const BasicRange& rRange) const
138 : : {
139 [ + - ][ - + ]: 41222 : if(isEmpty() || rRange.isEmpty())
[ - + ]
140 : 0 : return false;
141 : : // returns true if the overlap is more than just a touching at the limits
142 [ + + ][ + + ]: 41222 : return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
143 : : }
144 : :
145 : 0 : bool operator==( const BasicRange& rRange ) const
146 : : {
147 [ # # ][ # # ]: 0 : return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
148 : : }
149 : :
150 : : bool operator!=( const BasicRange& rRange ) const
151 : : {
152 : : return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
153 : : }
154 : :
155 : : bool equal(const BasicRange& rRange) const
156 : : {
157 : : return (
158 : : fTools::equal(mnMinimum, rRange.mnMinimum) &&
159 : : fTools::equal(mnMaximum, rRange.mnMaximum));
160 : : }
161 : :
162 : 37742106 : void expand(T nValue)
163 : : {
164 [ + + ][ # # ]: 37742106 : if(isEmpty())
165 : : {
166 : 8403230 : mnMinimum = mnMaximum = nValue;
167 : : }
168 : : else
169 : : {
170 [ + + ][ # # ]: 29338876 : if(nValue < mnMinimum)
171 : : {
172 : 1344910 : mnMinimum = nValue;
173 : : }
174 : :
175 [ + + ][ # # ]: 29338876 : if(nValue > mnMaximum)
176 : : {
177 : 8218160 : mnMaximum = nValue;
178 : : }
179 : : }
180 : 37742106 : }
181 : :
182 : 8859367 : void expand(const BasicRange& rRange)
183 : : {
184 [ + + ]: 8859367 : if(isEmpty())
185 : : {
186 : 8579963 : mnMinimum = rRange.mnMinimum;
187 : 8579963 : mnMaximum = rRange.mnMaximum;
188 : : }
189 : : else
190 : : {
191 [ + - ]: 279404 : if(!rRange.isEmpty())
192 : : {
193 [ + + ]: 279404 : if(rRange.mnMinimum < mnMinimum)
194 : : {
195 : 47501 : mnMinimum = rRange.mnMinimum;
196 : : }
197 : :
198 [ + + ]: 279404 : if(rRange.mnMaximum > mnMaximum)
199 : : {
200 : 26532 : mnMaximum = rRange.mnMaximum;
201 : : }
202 : : }
203 : : }
204 : 8859367 : }
205 : :
206 : : void intersect(const BasicRange& rRange)
207 : : {
208 : : // here, overlaps also tests all isEmpty() conditions already.
209 : : if( !overlaps( rRange ) )
210 : : {
211 : : reset();
212 : : }
213 : : else
214 : : {
215 : : if(rRange.mnMinimum > mnMinimum)
216 : : {
217 : : mnMinimum = rRange.mnMinimum;
218 : : }
219 : :
220 : : if(rRange.mnMaximum < mnMaximum)
221 : : {
222 : : mnMaximum = rRange.mnMaximum;
223 : : }
224 : : }
225 : : }
226 : :
227 : : void grow(T nValue)
228 : : {
229 : : if(!isEmpty())
230 : : {
231 : : bool bLessThanZero(nValue < 0);
232 : :
233 : : if(nValue > 0 || bLessThanZero)
234 : : {
235 : : mnMinimum -= nValue;
236 : : mnMaximum += nValue;
237 : :
238 : : if(bLessThanZero)
239 : : {
240 : : // test if range did collapse
241 : : if(mnMinimum > mnMaximum)
242 : : {
243 : : // if yes, collapse to center
244 : : mnMinimum = mnMaximum = (mnMinimum + mnMaximum) / 2;
245 : : }
246 : : }
247 : : }
248 : : }
249 : : }
250 : :
251 : 4738 : typename Traits::DifferenceType getRange() const
252 : : {
253 [ - + ][ # # ]: 4738 : if(isEmpty())
254 : : {
255 : 0 : return Traits::neutral();
256 : : }
257 : : else
258 : : {
259 : 4738 : return (mnMaximum - mnMinimum);
260 : : }
261 : : }
262 : : };
263 : :
264 : : // some pre-fabricated traits
265 : : struct DoubleTraits
266 : : {
267 : 16995469 : static double minVal() { return DBL_MIN; };
268 : 64949493 : static double maxVal() { return DBL_MAX; };
269 : 0 : static double neutral() { return 0.0; };
270 : :
271 : : typedef double DifferenceType;
272 : : };
273 : :
274 : : struct Int32Traits
275 : : {
276 : 0 : static sal_Int32 minVal() { return SAL_MIN_INT32; };
277 : 10 : static sal_Int32 maxVal() { return SAL_MAX_INT32; };
278 : 0 : static sal_Int32 neutral() { return 0L; };
279 : :
280 : : typedef sal_Int64 DifferenceType;
281 : : };
282 : :
283 : : } // end of namespace basegfx
284 : :
285 : : #endif /* _BGFX_RANGE_BASICRANGE_HXX */
286 : :
287 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|