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 : 15471569 : BasicRange() :
50 : : mnMinimum(Traits::maxVal()),
51 : 15471569 : mnMaximum(Traits::minVal())
52 : : {
53 : 15471569 : }
54 : :
55 : 38966108 : explicit BasicRange( T nValue ) :
56 : : mnMinimum(nValue),
57 : 38966108 : mnMaximum(nValue)
58 : : {
59 : 38966108 : }
60 : :
61 : 350001 : void reset()
62 : : {
63 : 350001 : mnMinimum = Traits::maxVal();
64 : 350001 : mnMaximum = Traits::minVal();
65 : 350001 : }
66 : :
67 : 80285822 : bool isEmpty() const
68 : : {
69 : 80285822 : return Traits::maxVal() == mnMinimum;
70 : : }
71 : :
72 : 71675008 : T getMinimum() const { return mnMinimum; }
73 : 61537787 : T getMaximum() const { return mnMaximum; }
74 : :
75 : 168 : double getCenter() const
76 : : {
77 [ + + ][ + + ]: 168 : if(isEmpty())
78 : : {
79 : 10 : return 0.0;
80 : : }
81 : : else
82 : : {
83 : 168 : return ((mnMaximum + mnMinimum) / 2.0);
84 : : }
85 : : }
86 : :
87 : 95 : bool isInside(T nValue) const
88 : : {
89 [ - + ][ - + ]: 95 : if(isEmpty())
90 : : {
91 : 0 : return false;
92 : : }
93 : : else
94 : : {
95 [ + - ][ + + ]: 95 : return (nValue >= mnMinimum) && (nValue <= mnMaximum);
[ + - ][ + - ]
96 : : }
97 : : }
98 : :
99 : 1087 : bool isInside(const BasicRange& rRange) const
100 : : {
101 [ - + ]: 1087 : if(isEmpty())
102 : : {
103 : 0 : return false;
104 : : }
105 : : else
106 : : {
107 [ - + ]: 1087 : if(rRange.isEmpty())
108 : : {
109 : 0 : return false;
110 : : }
111 : : else
112 : : {
113 [ + + ][ + + ]: 1087 : return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
114 : : }
115 : : }
116 : : }
117 : :
118 : 12138434 : bool overlaps(const BasicRange& rRange) const
119 : : {
120 [ - + ][ - + ]: 12138434 : if(isEmpty())
121 : : {
122 : 0 : return false;
123 : : }
124 : : else
125 : : {
126 [ + + ][ - + ]: 12138434 : if(rRange.isEmpty())
127 : : {
128 : 540 : return false;
129 : : }
130 : : else
131 : : {
132 [ + + ][ + + ]: 12138434 : return !((rRange.mnMaximum < mnMinimum) || (rRange.mnMinimum > mnMaximum));
[ + - ][ + + ]
133 : : }
134 : : }
135 : : }
136 : :
137 : 20 : bool overlapsMore(const BasicRange& rRange) const
138 : : {
139 [ + - ][ - + ]: 20 : if(isEmpty() || rRange.isEmpty())
[ - + ][ + - ]
[ - + ][ - + ]
140 : 0 : return false;
141 : : // returns true if the overlap is more than just a touching at the limits
142 [ + + ][ + - ]: 20 : return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
[ + + ][ + - ]
143 : : }
144 : :
145 : 3620 : bool operator==( const BasicRange& rRange ) const
146 : : {
147 [ + + ][ + + ]: 3620 : return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
[ + - ][ + - ]
148 : : }
149 : :
150 : 204 : bool operator!=( const BasicRange& rRange ) const
151 : : {
152 [ + + ][ + + ]: 204 : return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
153 : : }
154 : :
155 : 0 : bool equal(const BasicRange& rRange) const
156 : : {
157 : : return (
158 : : fTools::equal(mnMinimum, rRange.mnMinimum) &&
159 [ # # ][ # # ]: 0 : fTools::equal(mnMaximum, rRange.mnMaximum));
160 : : }
161 : :
162 : 39287335 : void expand(T nValue)
163 : : {
164 [ + + ][ + + ]: 39287335 : if(isEmpty())
165 : : {
166 : 32189 : mnMinimum = mnMaximum = nValue;
167 : : }
168 : : else
169 : : {
170 [ + + ][ + + ]: 39255146 : if(nValue < mnMinimum)
171 : : {
172 : 4033670 : mnMinimum = nValue;
173 : : }
174 : :
175 [ + + ][ + + ]: 39255146 : if(nValue > mnMaximum)
176 : : {
177 : 31998433 : mnMaximum = nValue;
178 : : }
179 : : }
180 : 39287335 : }
181 : :
182 : 9328899 : void expand(const BasicRange& rRange)
183 : : {
184 [ + + ]: 9328899 : if(isEmpty())
185 : : {
186 : 7681820 : mnMinimum = rRange.mnMinimum;
187 : 7681820 : mnMaximum = rRange.mnMaximum;
188 : : }
189 : : else
190 : : {
191 [ + + ]: 1647079 : if(!rRange.isEmpty())
192 : : {
193 [ + + ]: 1636546 : if(rRange.mnMinimum < mnMinimum)
194 : : {
195 : 213132 : mnMinimum = rRange.mnMinimum;
196 : : }
197 : :
198 [ + + ]: 1636546 : if(rRange.mnMaximum > mnMaximum)
199 : : {
200 : 290112 : mnMaximum = rRange.mnMaximum;
201 : : }
202 : : }
203 : : }
204 : 9328899 : }
205 : :
206 : 11937866 : void intersect(const BasicRange& rRange)
207 : : {
208 : : // here, overlaps also tests all isEmpty() conditions already.
209 [ + + ][ + + ]: 11937866 : if( !overlaps( rRange ) )
210 : : {
211 : 224007 : reset();
212 : : }
213 : : else
214 : : {
215 [ + + ][ + - ]: 11713859 : if(rRange.mnMinimum > mnMinimum)
216 : : {
217 : 704893 : mnMinimum = rRange.mnMinimum;
218 : : }
219 : :
220 [ + + ][ - + ]: 11713859 : if(rRange.mnMaximum < mnMaximum)
221 : : {
222 : 889234 : mnMaximum = rRange.mnMaximum;
223 : : }
224 : : }
225 : 11937866 : }
226 : :
227 : 746176 : void grow(T nValue)
228 : : {
229 [ + - ]: 746176 : if(!isEmpty())
230 : : {
231 : 746176 : bool bLessThanZero(nValue < 0);
232 : :
233 [ - + ][ # # ]: 746176 : if(nValue > 0 || bLessThanZero)
234 : : {
235 : 746176 : mnMinimum -= nValue;
236 : 746176 : mnMaximum += nValue;
237 : :
238 [ - + ]: 746176 : if(bLessThanZero)
239 : : {
240 : : // test if range did collapse
241 [ # # ]: 0 : if(mnMinimum > mnMaximum)
242 : : {
243 : : // if yes, collapse to center
244 : 0 : mnMinimum = mnMaximum = (mnMinimum + mnMaximum) / 2;
245 : : }
246 : : }
247 : : }
248 : : }
249 : 746176 : }
250 : :
251 : 3486747 : typename Traits::DifferenceType getRange() const
252 : : {
253 [ + + ][ - + ]: 3486747 : if(isEmpty())
254 : : {
255 : 12 : return Traits::neutral();
256 : : }
257 : : else
258 : : {
259 : 3486747 : return (mnMaximum - mnMinimum);
260 : : }
261 : : }
262 : : };
263 : :
264 : : // some pre-fabricated traits
265 : : struct DoubleTraits
266 : : {
267 : 9261582 : static double minVal() { return DBL_MIN; };
268 : 26895887 : 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 : 6559988 : static sal_Int32 minVal() { return SAL_MIN_INT32; };
277 : 69211505 : static sal_Int32 maxVal() { return SAL_MAX_INT32; };
278 : 12 : 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: */
|