update credits
[LibreOffice.git] / include / basegfx / range / basicrange.hxx
blob51b5593ae61d7e5ecdd349aba9ab9bbcbbee0e24
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef _BGFX_RANGE_BASICRANGE_HXX
21 #define _BGFX_RANGE_BASICRANGE_HXX
23 #include <sal/types.h>
24 #include <float.h>
25 #include <basegfx/numeric/ftools.hxx>
28 namespace basegfx
30 template< typename T, typename Traits > class BasicRange
32 protected:
33 T mnMinimum;
34 T mnMaximum;
36 public:
37 typedef T ValueType;
38 typedef Traits TraitsType;
40 BasicRange() :
41 mnMinimum(Traits::maxVal()),
42 mnMaximum(Traits::minVal())
46 explicit BasicRange( T nValue ) :
47 mnMinimum(nValue),
48 mnMaximum(nValue)
52 void reset()
54 mnMinimum = Traits::maxVal();
55 mnMaximum = Traits::minVal();
58 bool isEmpty() const
60 return Traits::maxVal() == mnMinimum;
63 T getMinimum() const { return mnMinimum; }
64 T getMaximum() const { return mnMaximum; }
66 double getCenter() const
68 if(isEmpty())
70 return 0.0;
72 else
74 return ((mnMaximum + mnMinimum) / 2.0);
78 bool isInside(T nValue) const
80 if(isEmpty())
82 return false;
84 else
86 return (nValue >= mnMinimum) && (nValue <= mnMaximum);
90 bool isInside(const BasicRange& rRange) const
92 if(isEmpty())
94 return false;
96 else
98 if(rRange.isEmpty())
100 return false;
102 else
104 return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
109 bool overlaps(const BasicRange& rRange) const
111 if(isEmpty())
113 return false;
115 else
117 if(rRange.isEmpty())
119 return false;
121 else
123 return !((rRange.mnMaximum < mnMinimum) || (rRange.mnMinimum > mnMaximum));
128 bool overlapsMore(const BasicRange& rRange) const
130 if(isEmpty() || rRange.isEmpty())
131 return false;
132 // returns true if the overlap is more than just a touching at the limits
133 return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
136 bool operator==( const BasicRange& rRange ) const
138 return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
141 bool operator!=( const BasicRange& rRange ) const
143 return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
146 bool equal(const BasicRange& rRange) const
148 return (
149 fTools::equal(mnMinimum, rRange.mnMinimum) &&
150 fTools::equal(mnMaximum, rRange.mnMaximum));
153 void expand(T nValue)
155 if(isEmpty())
157 mnMinimum = mnMaximum = nValue;
159 else
161 if(nValue < mnMinimum)
163 mnMinimum = nValue;
166 if(nValue > mnMaximum)
168 mnMaximum = nValue;
173 void expand(const BasicRange& rRange)
175 if(isEmpty())
177 mnMinimum = rRange.mnMinimum;
178 mnMaximum = rRange.mnMaximum;
180 else
182 if(!rRange.isEmpty())
184 if(rRange.mnMinimum < mnMinimum)
186 mnMinimum = rRange.mnMinimum;
189 if(rRange.mnMaximum > mnMaximum)
191 mnMaximum = rRange.mnMaximum;
197 void intersect(const BasicRange& rRange)
199 // here, overlaps also tests all isEmpty() conditions already.
200 if( !overlaps( rRange ) )
202 reset();
204 else
206 if(rRange.mnMinimum > mnMinimum)
208 mnMinimum = rRange.mnMinimum;
211 if(rRange.mnMaximum < mnMaximum)
213 mnMaximum = rRange.mnMaximum;
218 void grow(T nValue)
220 if(!isEmpty())
222 bool bLessThanZero(nValue < 0);
224 if(nValue > 0 || bLessThanZero)
226 mnMinimum -= nValue;
227 mnMaximum += nValue;
229 if(bLessThanZero)
231 // test if range did collapse
232 if(mnMinimum > mnMaximum)
234 // if yes, collapse to center
235 mnMinimum = mnMaximum = (mnMinimum + mnMaximum) / 2;
242 typename Traits::DifferenceType getRange() const
244 if(isEmpty())
246 return Traits::neutral();
248 else
250 return (mnMaximum - mnMinimum);
255 // some pre-fabricated traits
256 struct DoubleTraits
258 static double minVal() { return DBL_MIN; };
259 static double maxVal() { return DBL_MAX; };
260 static double neutral() { return 0.0; };
262 typedef double DifferenceType;
265 struct Int32Traits
267 static sal_Int32 minVal() { return SAL_MIN_INT32; };
268 static sal_Int32 maxVal() { return SAL_MAX_INT32; };
269 static sal_Int32 neutral() { return 0L; };
271 typedef sal_Int64 DifferenceType;
274 } // end of namespace basegfx
276 #endif /* _BGFX_RANGE_BASICRANGE_HXX */
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */