1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/types.h>
24 #include <basegfx/numeric/ftools.hxx>
29 template< typename T
, typename Traits
> class BasicRange
37 typedef Traits TraitsType
;
40 mnMinimum(Traits::maxVal()),
41 mnMaximum(Traits::minVal())
45 explicit BasicRange( T nValue
) :
53 mnMinimum
= Traits::maxVal();
54 mnMaximum
= Traits::minVal();
59 return Traits::maxVal() == mnMinimum
;
62 T
getMinimum() const { return mnMinimum
; }
63 T
getMaximum() const { return mnMaximum
; }
65 double getCenter() const
73 return ((mnMaximum
+ mnMinimum
) / 2.0);
77 bool isInside(T nValue
) const
85 return (nValue
>= mnMinimum
) && (nValue
<= mnMaximum
);
89 bool isInside(const BasicRange
& rRange
) const
103 return (rRange
.mnMinimum
>= mnMinimum
) && (rRange
.mnMaximum
<= mnMaximum
);
108 bool overlaps(const BasicRange
& rRange
) const
122 return (rRange
.mnMaximum
>= mnMinimum
) && (rRange
.mnMinimum
<= mnMaximum
);
127 bool overlapsMore(const BasicRange
& rRange
) const
129 if(isEmpty() || rRange
.isEmpty())
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
148 fTools::equal(mnMinimum
, rRange
.mnMinimum
) &&
149 fTools::equal(mnMaximum
, rRange
.mnMaximum
));
152 void expand(T nValue
)
156 mnMinimum
= mnMaximum
= nValue
;
160 // Silence over-eager warning emitted at least by GCC 4.9.2 in certain
162 #if defined __GNUC__ && !defined __clang__
163 #pragma GCC diagnostic push
164 #pragma GCC diagnostic ignored "-Wstrict-overflow"
166 if(nValue
< mnMinimum
)
167 #if defined __GNUC__ && !defined __clang__
168 #pragma GCC diagnostic pop
174 if(nValue
> mnMaximum
)
181 void expand(const BasicRange
& rRange
)
185 mnMinimum
= rRange
.mnMinimum
;
186 mnMaximum
= rRange
.mnMaximum
;
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
) )
214 if(rRange
.mnMinimum
> mnMinimum
)
216 mnMinimum
= rRange
.mnMinimum
;
219 if(rRange
.mnMaximum
< mnMaximum
)
221 mnMaximum
= rRange
.mnMaximum
;
231 bool bLessThanZero(nValue
< 0);
233 if(nValue
> 0 || 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
258 if(nValue
< mnMinimum
)
263 if(nValue
> mnMaximum
)
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 */
276 typename
Traits::DifferenceType
getRange() const
280 return Traits::neutral();
284 return (mnMaximum
- mnMinimum
);
287 #if defined _MSC_VER && defined(_M_ARM64)
288 #pragma warning( pop )
292 // some pre-fabricated traits
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
;
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: */