workaround segfault in compiler on macos-clang-intel
[LibreOffice.git] / include / basegfx / range / basicrange.hxx
blob68c365d0b9fbd2572e469aa7b9ce2a553a6a49dd
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 #pragma once
22 #include <sal/types.h>
23 #include <float.h>
24 #include <basegfx/numeric/ftools.hxx>
27 namespace basegfx
29 template< typename T, typename Traits > class BasicRange
31 protected:
32 T mnMinimum;
33 T mnMaximum;
35 public:
36 typedef T ValueType;
37 typedef Traits TraitsType;
39 BasicRange() :
40 mnMinimum(Traits::maxVal()),
41 mnMaximum(Traits::minVal())
45 explicit BasicRange( T nValue ) :
46 mnMinimum(nValue),
47 mnMaximum(nValue)
51 void reset()
53 mnMinimum = Traits::maxVal();
54 mnMaximum = Traits::minVal();
57 bool isEmpty() const
59 return Traits::maxVal() == mnMinimum;
62 T getMinimum() const { return mnMinimum; }
63 T getMaximum() const { return mnMaximum; }
65 double getCenter() const
67 if(isEmpty())
69 return 0.0;
71 else
73 return ((mnMaximum + mnMinimum) / 2.0);
77 bool isInside(T nValue) const
79 if(isEmpty())
81 return false;
83 else
85 return (nValue >= mnMinimum) && (nValue <= mnMaximum);
89 bool isInside(const BasicRange& rRange) const
91 if(isEmpty())
93 return false;
95 else
97 if(rRange.isEmpty())
99 return false;
101 else
103 return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
108 bool overlaps(const BasicRange& rRange) const
110 if(isEmpty())
112 return false;
114 else
116 if(rRange.isEmpty())
118 return false;
120 else
122 return (rRange.mnMaximum >= mnMinimum) && (rRange.mnMinimum <= mnMaximum);
127 bool overlapsMore(const BasicRange& rRange) const
129 if(isEmpty() || rRange.isEmpty())
130 return false;
131 // returns true if the overlap is more than just a touching at the limits
132 return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
135 bool operator==( const BasicRange& rRange ) const
137 return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
140 bool operator!=( const BasicRange& rRange ) const
142 return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
145 bool equal(const BasicRange& rRange) const
147 return (
148 fTools::equal(mnMinimum, rRange.mnMinimum) &&
149 fTools::equal(mnMaximum, rRange.mnMaximum));
152 void expand(T nValue)
154 if(isEmpty())
156 mnMinimum = mnMaximum = nValue;
158 else
160 // Silence over-eager warning emitted at least by GCC 4.9.2 in certain
161 // instantiations:
162 #if defined __GNUC__ && !defined __clang__
163 #pragma GCC diagnostic push
164 #pragma GCC diagnostic ignored "-Wstrict-overflow"
165 #endif
166 if(nValue < mnMinimum)
167 #if defined __GNUC__ && !defined __clang__
168 #pragma GCC diagnostic pop
169 #endif
171 mnMinimum = nValue;
174 if(nValue > mnMaximum)
176 mnMaximum = nValue;
181 void expand(const BasicRange& rRange)
183 if(isEmpty())
185 mnMinimum = rRange.mnMinimum;
186 mnMaximum = rRange.mnMaximum;
188 else
190 if(!rRange.isEmpty())
192 if(rRange.mnMinimum < mnMinimum)
194 mnMinimum = rRange.mnMinimum;
197 if(rRange.mnMaximum > mnMaximum)
199 mnMaximum = rRange.mnMaximum;
205 void intersect(const BasicRange& rRange)
207 // here, overlaps also tests all isEmpty() conditions already.
208 if( !overlaps( rRange ) )
210 reset();
212 else
214 if(rRange.mnMinimum > mnMinimum)
216 mnMinimum = rRange.mnMinimum;
219 if(rRange.mnMaximum < mnMaximum)
221 mnMaximum = rRange.mnMaximum;
226 void grow(T nValue)
228 if(isEmpty())
229 return;
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;
250 T clamp(T nValue) const
252 if(isEmpty())
254 return nValue;
256 else
258 if(nValue < mnMinimum)
260 return mnMinimum;
263 if(nValue > mnMaximum)
265 return mnMaximum;
268 return nValue;
272 #if defined _MSC_VER && defined(_M_ARM64)
273 #pragma warning(push)
274 #pragma warning(disable: 4723) /* ignore: warning for (C4723) potential divide by 0 on windows arm64 build */
275 #endif
276 typename Traits::DifferenceType getRange() const
278 if(isEmpty())
280 return Traits::neutral();
282 else
284 return (mnMaximum - mnMinimum);
287 #if defined _MSC_VER && defined(_M_ARM64)
288 #pragma warning( pop )
289 #endif
292 // some pre-fabricated traits
293 struct DoubleTraits
295 static constexpr double minVal() { return DBL_MIN; };
296 static constexpr double maxVal() { return DBL_MAX; };
297 static constexpr double neutral() { return 0.0; };
299 typedef double DifferenceType;
302 struct Int32Traits
304 static constexpr sal_Int32 minVal() { return SAL_MIN_INT32; };
305 static constexpr sal_Int32 maxVal() { return SAL_MAX_INT32; };
306 static constexpr sal_Int32 neutral() { return 0; };
308 typedef sal_Int64 DifferenceType;
311 } // end of namespace basegfx
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */