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 .
20 #ifndef INCLUDED_BASEGFX_RANGE_BASICRANGE_HXX
21 #define INCLUDED_BASEGFX_RANGE_BASICRANGE_HXX
23 #include <sal/types.h>
25 #include <basegfx/numeric/ftools.hxx>
30 template< typename T
, typename Traits
> class BasicRange
38 typedef Traits TraitsType
;
41 mnMinimum(Traits::maxVal()),
42 mnMaximum(Traits::minVal())
46 explicit BasicRange( T nValue
) :
54 mnMinimum
= Traits::maxVal();
55 mnMaximum
= Traits::minVal();
60 return Traits::maxVal() == mnMinimum
;
63 T
getMinimum() const { return mnMinimum
; }
64 T
getMaximum() const { return mnMaximum
; }
66 double getCenter() const
74 return ((mnMaximum
+ mnMinimum
) / 2.0);
78 bool isInside(T nValue
) const
86 return (nValue
>= mnMinimum
) && (nValue
<= mnMaximum
);
90 bool isInside(const BasicRange
& rRange
) const
104 return (rRange
.mnMinimum
>= mnMinimum
) && (rRange
.mnMaximum
<= mnMaximum
);
109 bool overlaps(const BasicRange
& rRange
) const
123 return !((rRange
.mnMaximum
< mnMinimum
) || (rRange
.mnMinimum
> mnMaximum
));
128 bool overlapsMore(const BasicRange
& rRange
) const
130 if(isEmpty() || rRange
.isEmpty())
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
149 fTools::equal(mnMinimum
, rRange
.mnMinimum
) &&
150 fTools::equal(mnMaximum
, rRange
.mnMaximum
));
153 void expand(T nValue
)
157 mnMinimum
= mnMaximum
= nValue
;
161 // Silence over-eager warning emitted at least by GCC 4.9.2 in certain
163 #if defined __GNUC__ && !defined __clang__
164 #pragma GCC diagnostic push
165 #pragma GCC diagnostic ignored "-Wstrict-overflow"
167 if(nValue
< mnMinimum
)
168 #if defined __GNUC__ && !defined __clang__
169 #pragma GCC diagnostic pop
175 if(nValue
> mnMaximum
)
182 void expand(const BasicRange
& rRange
)
186 mnMinimum
= rRange
.mnMinimum
;
187 mnMaximum
= rRange
.mnMaximum
;
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
) )
215 if(rRange
.mnMinimum
> mnMinimum
)
217 mnMinimum
= rRange
.mnMinimum
;
220 if(rRange
.mnMaximum
< mnMaximum
)
222 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;
251 typename
Traits::DifferenceType
getRange() const
255 return Traits::neutral();
259 return (mnMaximum
- mnMinimum
);
264 // some pre-fabricated traits
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
;
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: */