Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/basegfx/range/basicrange.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#pragma once
21
22
#include <sal/types.h>
23
#include <float.h>
24
#include <basegfx/numeric/ftools.hxx>
25
26
27
namespace basegfx
28
{
29
    template< typename T, typename Traits > class BasicRange
30
    {
31
    protected:
32
        T mnMinimum;
33
        T mnMaximum;
34
35
    public:
36
        typedef T       ValueType;
37
        typedef Traits  TraitsType;
38
39
        BasicRange() :
40
110M
            mnMinimum(Traits::maxVal()),
41
110M
            mnMaximum(Traits::minVal())
42
110M
        {
43
110M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::BasicRange()
Line
Count
Source
40
109M
            mnMinimum(Traits::maxVal()),
41
109M
            mnMaximum(Traits::minVal())
42
109M
        {
43
109M
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::BasicRange()
Line
Count
Source
40
326k
            mnMinimum(Traits::maxVal()),
41
326k
            mnMaximum(Traits::minVal())
42
326k
        {
43
326k
        }
44
45
        explicit BasicRange( T nValue ) :
46
7.69G
            mnMinimum(nValue),
47
7.69G
            mnMaximum(nValue)
48
7.69G
        {
49
7.69G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::BasicRange(double)
Line
Count
Source
46
7.69G
            mnMinimum(nValue),
47
7.69G
            mnMaximum(nValue)
48
7.69G
        {
49
7.69G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::BasicRange(int)
Line
Count
Source
46
5.66M
            mnMinimum(nValue),
47
5.66M
            mnMaximum(nValue)
48
5.66M
        {
49
5.66M
        }
50
51
        void reset()
52
40.7M
        {
53
40.7M
            mnMinimum = Traits::maxVal();
54
40.7M
            mnMaximum = Traits::minVal();
55
40.7M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::reset()
Line
Count
Source
52
40.7M
        {
53
40.7M
            mnMinimum = Traits::maxVal();
54
40.7M
            mnMaximum = Traits::minVal();
55
40.7M
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::reset()
56
57
        bool isEmpty() const
58
28.1G
        {
59
28.1G
            return Traits::maxVal() == mnMinimum;
60
28.1G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::isEmpty() const
Line
Count
Source
58
28.1G
        {
59
28.1G
            return Traits::maxVal() == mnMinimum;
60
28.1G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::isEmpty() const
Line
Count
Source
58
5.74M
        {
59
5.74M
            return Traits::maxVal() == mnMinimum;
60
5.74M
        }
61
62
163M
        T getMinimum() const { return mnMinimum; }
basegfx::BasicRange<double, basegfx::DoubleTraits>::getMinimum() const
Line
Count
Source
62
157M
        T getMinimum() const { return mnMinimum; }
basegfx::BasicRange<int, basegfx::Int32Traits>::getMinimum() const
Line
Count
Source
62
5.44M
        T getMinimum() const { return mnMinimum; }
63
157M
        T getMaximum() const { return mnMaximum; }
basegfx::BasicRange<double, basegfx::DoubleTraits>::getMaximum() const
Line
Count
Source
63
152M
        T getMaximum() const { return mnMaximum; }
basegfx::BasicRange<int, basegfx::Int32Traits>::getMaximum() const
Line
Count
Source
63
5.44M
        T getMaximum() const { return mnMaximum; }
64
65
        double getCenter() const
66
11.1k
        {
67
11.1k
            if(isEmpty())
68
0
            {
69
0
                return 0.0;
70
0
            }
71
11.1k
            else
72
11.1k
            {
73
11.1k
                return ((mnMaximum + mnMinimum) / 2.0);
74
11.1k
            }
75
11.1k
        }
76
77
        bool isInside(T nValue) const
78
11.2G
        {
79
11.2G
            if(isEmpty())
80
0
            {
81
0
                return false;
82
0
            }
83
11.2G
            else
84
11.2G
            {
85
11.2G
                return (nValue >= mnMinimum) && (nValue <= mnMaximum);
86
11.2G
            }
87
11.2G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::isInside(double) const
Line
Count
Source
78
11.2G
        {
79
11.2G
            if(isEmpty())
80
0
            {
81
0
                return false;
82
0
            }
83
11.2G
            else
84
11.2G
            {
85
11.2G
                return (nValue >= mnMinimum) && (nValue <= mnMaximum);
86
11.2G
            }
87
11.2G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::isInside(int) const
Line
Count
Source
78
41.4k
        {
79
41.4k
            if(isEmpty())
80
0
            {
81
0
                return false;
82
0
            }
83
41.4k
            else
84
41.4k
            {
85
41.4k
                return (nValue >= mnMinimum) && (nValue <= mnMaximum);
86
41.4k
            }
87
41.4k
        }
88
89
        bool isInside(const BasicRange& rRange) const
90
49.9M
        {
91
49.9M
            if(isEmpty())
92
0
            {
93
0
                return false;
94
0
            }
95
49.9M
            else
96
49.9M
            {
97
49.9M
                if(rRange.isEmpty())
98
0
                {
99
0
                    return false;
100
0
                }
101
49.9M
                else
102
49.9M
                {
103
49.9M
                    return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
104
49.9M
                }
105
49.9M
            }
106
49.9M
        }
107
108
        bool overlaps(const BasicRange& rRange) const
109
2.79G
        {
110
2.79G
            if(isEmpty())
111
4.50M
            {
112
4.50M
                return false;
113
4.50M
            }
114
2.79G
            else
115
2.79G
            {
116
2.79G
                if(rRange.isEmpty())
117
3.64k
                {
118
3.64k
                    return false;
119
3.64k
                }
120
2.79G
                else
121
2.79G
                {
122
2.79G
                    return (rRange.mnMaximum >= mnMinimum) && (rRange.mnMinimum <= mnMaximum);
123
2.79G
                }
124
2.79G
            }
125
2.79G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::overlaps(basegfx::BasicRange<double, basegfx::DoubleTraits> const&) const
Line
Count
Source
109
2.79G
        {
110
2.79G
            if(isEmpty())
111
4.50M
            {
112
4.50M
                return false;
113
4.50M
            }
114
2.79G
            else
115
2.79G
            {
116
2.79G
                if(rRange.isEmpty())
117
3.64k
                {
118
3.64k
                    return false;
119
3.64k
                }
120
2.79G
                else
121
2.79G
                {
122
2.79G
                    return (rRange.mnMaximum >= mnMinimum) && (rRange.mnMinimum <= mnMaximum);
123
2.79G
                }
124
2.79G
            }
125
2.79G
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::overlaps(basegfx::BasicRange<int, basegfx::Int32Traits> const&) const
126
127
        bool overlapsMore(const BasicRange& rRange) const
128
1.15G
        {
129
1.15G
            if(isEmpty() || rRange.isEmpty())
130
0
                return false;
131
            // returns true if the overlap is more than just a touching at the limits
132
1.15G
            return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
133
1.15G
        }
134
135
        bool operator==( const BasicRange& rRange ) const
136
768
        {
137
768
            return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
138
768
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::operator==(basegfx::BasicRange<int, basegfx::Int32Traits> const&) const
basegfx::BasicRange<double, basegfx::DoubleTraits>::operator==(basegfx::BasicRange<double, basegfx::DoubleTraits> const&) const
Line
Count
Source
136
768
        {
137
768
            return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
138
768
        }
139
140
        bool operator!=( const BasicRange& rRange ) const
141
274k
        {
142
274k
            return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
143
274k
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::operator!=(basegfx::BasicRange<int, basegfx::Int32Traits> const&) const
basegfx::BasicRange<double, basegfx::DoubleTraits>::operator!=(basegfx::BasicRange<double, basegfx::DoubleTraits> const&) const
Line
Count
Source
141
274k
        {
142
274k
            return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
143
274k
        }
144
145
        bool equal(const BasicRange& rRange) const
146
30.3k
        {
147
30.3k
            return (
148
30.3k
                fTools::equal(mnMinimum, rRange.mnMinimum) &&
149
25.2k
                fTools::equal(mnMaximum, rRange.mnMaximum));
150
30.3k
        }
151
152
        void expand(T nValue)
153
8.41G
        {
154
8.41G
            if(isEmpty())
155
36.8M
            {
156
36.8M
                mnMinimum = mnMaximum = nValue;
157
36.8M
            }
158
8.37G
            else
159
8.37G
            {
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
8.37G
                if(nValue < mnMinimum)
167
#if defined __GNUC__ && !defined __clang__
168
#pragma GCC diagnostic pop
169
#endif
170
2.97G
                {
171
2.97G
                    mnMinimum = nValue;
172
2.97G
                }
173
174
8.37G
                if(nValue > mnMaximum)
175
3.19G
                {
176
3.19G
                    mnMaximum = nValue;
177
3.19G
                }
178
8.37G
            }
179
8.41G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::expand(double)
Line
Count
Source
153
8.40G
        {
154
8.40G
            if(isEmpty())
155
36.8M
            {
156
36.8M
                mnMinimum = mnMaximum = nValue;
157
36.8M
            }
158
8.37G
            else
159
8.37G
            {
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
8.37G
                if(nValue < mnMinimum)
167
#if defined __GNUC__ && !defined __clang__
168
#pragma GCC diagnostic pop
169
#endif
170
2.97G
                {
171
2.97G
                    mnMinimum = nValue;
172
2.97G
                }
173
174
8.37G
                if(nValue > mnMaximum)
175
3.18G
                {
176
3.18G
                    mnMaximum = nValue;
177
3.18G
                }
178
8.37G
            }
179
8.40G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::expand(int)
Line
Count
Source
153
5.66M
        {
154
5.66M
            if(isEmpty())
155
0
            {
156
0
                mnMinimum = mnMaximum = nValue;
157
0
            }
158
5.66M
            else
159
5.66M
            {
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
5.66M
                if(nValue < mnMinimum)
167
#if defined __GNUC__ && !defined __clang__
168
#pragma GCC diagnostic pop
169
#endif
170
141
                {
171
141
                    mnMinimum = nValue;
172
141
                }
173
174
5.66M
                if(nValue > mnMaximum)
175
5.51M
                {
176
5.51M
                    mnMaximum = nValue;
177
5.51M
                }
178
5.66M
            }
179
5.66M
        }
180
181
        void expand(const BasicRange& rRange)
182
157M
        {
183
157M
            if(isEmpty())
184
51.0M
            {
185
51.0M
                mnMinimum = rRange.mnMinimum;
186
51.0M
                mnMaximum = rRange.mnMaximum;
187
51.0M
            }
188
106M
            else
189
106M
            {
190
106M
                if(!rRange.isEmpty())
191
106M
                {
192
106M
                    if(rRange.mnMinimum < mnMinimum)
193
3.96M
                    {
194
3.96M
                        mnMinimum = rRange.mnMinimum;
195
3.96M
                    }
196
197
106M
                    if(rRange.mnMaximum > mnMaximum)
198
49.9M
                    {
199
49.9M
                        mnMaximum = rRange.mnMaximum;
200
49.9M
                    }
201
106M
                }
202
106M
            }
203
157M
        }
204
205
        void intersect(const BasicRange& rRange)
206
21.2M
        {
207
            // here, overlaps also tests all isEmpty() conditions already.
208
21.2M
            if( !overlaps( rRange ) )
209
18.3M
            {
210
18.3M
                reset();
211
18.3M
            }
212
2.91M
            else
213
2.91M
            {
214
2.91M
                if(rRange.mnMinimum > mnMinimum)
215
2.34M
                {
216
2.34M
                    mnMinimum = rRange.mnMinimum;
217
2.34M
                }
218
219
2.91M
                if(rRange.mnMaximum < mnMaximum)
220
1.85M
                {
221
1.85M
                    mnMaximum = rRange.mnMaximum;
222
1.85M
                }
223
2.91M
            }
224
21.2M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::intersect(basegfx::BasicRange<double, basegfx::DoubleTraits> const&)
Line
Count
Source
206
21.2M
        {
207
            // here, overlaps also tests all isEmpty() conditions already.
208
21.2M
            if( !overlaps( rRange ) )
209
18.3M
            {
210
18.3M
                reset();
211
18.3M
            }
212
2.91M
            else
213
2.91M
            {
214
2.91M
                if(rRange.mnMinimum > mnMinimum)
215
2.34M
                {
216
2.34M
                    mnMinimum = rRange.mnMinimum;
217
2.34M
                }
218
219
2.91M
                if(rRange.mnMaximum < mnMaximum)
220
1.85M
                {
221
1.85M
                    mnMaximum = rRange.mnMaximum;
222
1.85M
                }
223
2.91M
            }
224
21.2M
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::intersect(basegfx::BasicRange<int, basegfx::Int32Traits> const&)
225
226
        void grow(T nValue)
227
1.58k
        {
228
1.58k
            if(isEmpty())
229
0
                return;
230
231
1.58k
            bool bLessThanZero(nValue < 0);
232
233
1.58k
            if(nValue > 0 || bLessThanZero)
234
1.58k
            {
235
1.58k
                mnMinimum -= nValue;
236
1.58k
                mnMaximum += nValue;
237
238
1.58k
                if(bLessThanZero)
239
0
                {
240
                    // test if range did collapse
241
0
                    if(mnMinimum > mnMaximum)
242
0
                    {
243
                        // if yes, collapse to center
244
0
                        mnMinimum = mnMaximum = (mnMinimum + mnMaximum) / 2;
245
0
                    }
246
0
                }
247
1.58k
            }
248
1.58k
        }
249
250
        T clamp(T nValue) const
251
0
        {
252
0
            if(isEmpty())
253
0
            {
254
0
                return nValue;
255
0
            }
256
0
            else
257
0
            {
258
0
                if(nValue < mnMinimum)
259
0
                {
260
0
                    return mnMinimum;
261
0
                }
262
0
263
0
                if(nValue > mnMaximum)
264
0
                {
265
0
                    return mnMaximum;
266
0
                }
267
0
268
0
                return nValue;
269
0
            }
270
0
        }
271
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
277
24.8M
        {
278
24.8M
            if(isEmpty())
279
40.9k
            {
280
40.9k
                return Traits::neutral();
281
40.9k
            }
282
24.8M
            else
283
24.8M
            {
284
24.8M
                return (mnMaximum - mnMinimum);
285
24.8M
            }
286
24.8M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::getRange() const
Line
Count
Source
277
24.8M
        {
278
24.8M
            if(isEmpty())
279
40.9k
            {
280
40.9k
                return Traits::neutral();
281
40.9k
            }
282
24.8M
            else
283
24.8M
            {
284
24.8M
                return (mnMaximum - mnMinimum);
285
24.8M
            }
286
24.8M
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::getRange() const
287
#if defined _MSC_VER && defined(_M_ARM64)
288
#pragma warning( pop )
289
#endif
290
    };
291
292
    // some pre-fabricated traits
293
    struct DoubleTraits
294
    {
295
150M
        static constexpr double minVal() { return DBL_MIN; };
296
28.2G
        static constexpr double maxVal() { return DBL_MAX; };
297
40.9k
        static constexpr double neutral() { return 0.0; };
298
299
        typedef double DifferenceType;
300
    };
301
302
    struct Int32Traits
303
    {
304
326k
        static constexpr sal_Int32 minVal() { return SAL_MIN_INT32; };
305
6.07M
        static constexpr sal_Int32 maxVal() { return SAL_MAX_INT32; };
306
0
        static constexpr sal_Int32 neutral() { return 0; };
307
308
        typedef sal_Int64 DifferenceType;
309
    };
310
311
} // end of namespace basegfx
312
313
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */