Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / basegfx / range / basicrange.hxx
blobbea40dd3ded2fdb5337e8d48ee99971ba2ec2f3b
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 INCLUDED_BASEGFX_RANGE_BASICRANGE_HXX
21 #define INCLUDED_BASEGFX_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 // Silence over-eager warning emitted at least by GCC 4.9.2 in certain
162 // instantiations:
163 #if defined __GNUC__ && !defined __clang__
164 #pragma GCC diagnostic push
165 #pragma GCC diagnostic ignored "-Wstrict-overflow"
166 #endif
167 if(nValue < mnMinimum)
168 #if defined __GNUC__ && !defined __clang__
169 #pragma GCC diagnostic pop
170 #endif
172 mnMinimum = nValue;
175 if(nValue > mnMaximum)
177 mnMaximum = nValue;
182 void expand(const BasicRange& rRange)
184 if(isEmpty())
186 mnMinimum = rRange.mnMinimum;
187 mnMaximum = rRange.mnMaximum;
189 else
191 if(!rRange.isEmpty())
193 if(rRange.mnMinimum < mnMinimum)
195 mnMinimum = rRange.mnMinimum;
198 if(rRange.mnMaximum > mnMaximum)
200 mnMaximum = rRange.mnMaximum;
206 void intersect(const BasicRange& rRange)
208 // here, overlaps also tests all isEmpty() conditions already.
209 if( !overlaps( rRange ) )
211 reset();
213 else
215 if(rRange.mnMinimum > mnMinimum)
217 mnMinimum = rRange.mnMinimum;
220 if(rRange.mnMaximum < mnMaximum)
222 mnMaximum = rRange.mnMaximum;
227 void grow(T nValue)
229 if(!isEmpty())
231 bool bLessThanZero(nValue < 0);
233 if(nValue > 0 || bLessThanZero)
235 mnMinimum -= nValue;
236 mnMaximum += nValue;
238 if(bLessThanZero)
240 // test if range did collapse
241 if(mnMinimum > mnMaximum)
243 // if yes, collapse to center
244 mnMinimum = mnMaximum = (mnMinimum + mnMaximum) / 2;
251 typename Traits::DifferenceType getRange() const
253 if(isEmpty())
255 return Traits::neutral();
257 else
259 return (mnMaximum - mnMinimum);
264 // some pre-fabricated traits
265 struct DoubleTraits
267 static double minVal() { return DBL_MIN; };
268 static double maxVal() { return DBL_MAX; };
269 static double neutral() { return 0.0; };
271 typedef double DifferenceType;
274 struct Int32Traits
276 static sal_Int32 minVal() { return SAL_MIN_INT32; };
277 static sal_Int32 maxVal() { return SAL_MAX_INT32; };
278 static sal_Int32 neutral() { return 0L; };
280 typedef sal_Int64 DifferenceType;
283 } // end of namespace basegfx
285 #endif // INCLUDED_BASEGFX_RANGE_BASICRANGE_HXX
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */