Coverage Report

Created: 2026-04-09 11:41

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
92.5M
            mnMinimum(Traits::maxVal()),
41
92.5M
            mnMaximum(Traits::minVal())
42
92.5M
        {
43
92.5M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::BasicRange()
Line
Count
Source
40
92.1M
            mnMinimum(Traits::maxVal()),
41
92.1M
            mnMaximum(Traits::minVal())
42
92.1M
        {
43
92.1M
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::BasicRange()
Line
Count
Source
40
315k
            mnMinimum(Traits::maxVal()),
41
315k
            mnMaximum(Traits::minVal())
42
315k
        {
43
315k
        }
44
45
        explicit BasicRange( T nValue ) :
46
3.28G
            mnMinimum(nValue),
47
3.28G
            mnMaximum(nValue)
48
3.28G
        {
49
3.28G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::BasicRange(double)
Line
Count
Source
46
3.27G
            mnMinimum(nValue),
47
3.27G
            mnMaximum(nValue)
48
3.27G
        {
49
3.27G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::BasicRange(int)
Line
Count
Source
46
5.27M
            mnMinimum(nValue),
47
5.27M
            mnMaximum(nValue)
48
5.27M
        {
49
5.27M
        }
50
51
        void reset()
52
37.3M
        {
53
37.3M
            mnMinimum = Traits::maxVal();
54
37.3M
            mnMaximum = Traits::minVal();
55
37.3M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::reset()
Line
Count
Source
52
37.3M
        {
53
37.3M
            mnMinimum = Traits::maxVal();
54
37.3M
            mnMaximum = Traits::minVal();
55
37.3M
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::reset()
56
57
        bool isEmpty() const
58
12.8G
        {
59
12.8G
            return Traits::maxVal() == mnMinimum;
60
12.8G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::isEmpty() const
Line
Count
Source
58
12.8G
        {
59
12.8G
            return Traits::maxVal() == mnMinimum;
60
12.8G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::isEmpty() const
Line
Count
Source
58
5.36M
        {
59
5.36M
            return Traits::maxVal() == mnMinimum;
60
5.36M
        }
61
62
138M
        T getMinimum() const { return mnMinimum; }
basegfx::BasicRange<double, basegfx::DoubleTraits>::getMinimum() const
Line
Count
Source
62
133M
        T getMinimum() const { return mnMinimum; }
basegfx::BasicRange<int, basegfx::Int32Traits>::getMinimum() const
Line
Count
Source
62
5.06M
        T getMinimum() const { return mnMinimum; }
63
131M
        T getMaximum() const { return mnMaximum; }
basegfx::BasicRange<double, basegfx::DoubleTraits>::getMaximum() const
Line
Count
Source
63
126M
        T getMaximum() const { return mnMaximum; }
basegfx::BasicRange<int, basegfx::Int32Traits>::getMaximum() const
Line
Count
Source
63
5.06M
        T getMaximum() const { return mnMaximum; }
64
65
        double getCenter() const
66
12.1k
        {
67
12.1k
            if(isEmpty())
68
0
            {
69
0
                return 0.0;
70
0
            }
71
12.1k
            else
72
12.1k
            {
73
12.1k
                return ((mnMaximum + mnMinimum) / 2.0);
74
12.1k
            }
75
12.1k
        }
76
77
        bool isInside(T nValue) const
78
5.66G
        {
79
5.66G
            if(isEmpty())
80
0
            {
81
0
                return false;
82
0
            }
83
5.66G
            else
84
5.66G
            {
85
5.66G
                return (nValue >= mnMinimum) && (nValue <= mnMaximum);
86
5.66G
            }
87
5.66G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::isInside(double) const
Line
Count
Source
78
5.66G
        {
79
5.66G
            if(isEmpty())
80
0
            {
81
0
                return false;
82
0
            }
83
5.66G
            else
84
5.66G
            {
85
5.66G
                return (nValue >= mnMinimum) && (nValue <= mnMaximum);
86
5.66G
            }
87
5.66G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::isInside(int) const
Line
Count
Source
78
45.4k
        {
79
45.4k
            if(isEmpty())
80
0
            {
81
0
                return false;
82
0
            }
83
45.4k
            else
84
45.4k
            {
85
45.4k
                return (nValue >= mnMinimum) && (nValue <= mnMaximum);
86
45.4k
            }
87
45.4k
        }
88
89
        bool isInside(const BasicRange& rRange) const
90
44.1M
        {
91
44.1M
            if(isEmpty())
92
0
            {
93
0
                return false;
94
0
            }
95
44.1M
            else
96
44.1M
            {
97
44.1M
                if(rRange.isEmpty())
98
0
                {
99
0
                    return false;
100
0
                }
101
44.1M
                else
102
44.1M
                {
103
44.1M
                    return (rRange.mnMinimum >= mnMinimum) && (rRange.mnMaximum <= mnMaximum);
104
44.1M
                }
105
44.1M
            }
106
44.1M
        }
107
108
        bool overlaps(const BasicRange& rRange) const
109
809M
        {
110
809M
            if(isEmpty())
111
4.50M
            {
112
4.50M
                return false;
113
4.50M
            }
114
804M
            else
115
804M
            {
116
804M
                if(rRange.isEmpty())
117
1.64k
                {
118
1.64k
                    return false;
119
1.64k
                }
120
804M
                else
121
804M
                {
122
804M
                    return (rRange.mnMaximum >= mnMinimum) && (rRange.mnMinimum <= mnMaximum);
123
804M
                }
124
804M
            }
125
809M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::overlaps(basegfx::BasicRange<double, basegfx::DoubleTraits> const&) const
Line
Count
Source
109
809M
        {
110
809M
            if(isEmpty())
111
4.50M
            {
112
4.50M
                return false;
113
4.50M
            }
114
804M
            else
115
804M
            {
116
804M
                if(rRange.isEmpty())
117
1.64k
                {
118
1.64k
                    return false;
119
1.64k
                }
120
804M
                else
121
804M
                {
122
804M
                    return (rRange.mnMaximum >= mnMinimum) && (rRange.mnMinimum <= mnMaximum);
123
804M
                }
124
804M
            }
125
809M
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::overlaps(basegfx::BasicRange<int, basegfx::Int32Traits> const&) const
126
127
        bool overlapsMore(const BasicRange& rRange) const
128
793M
        {
129
793M
            if(isEmpty() || rRange.isEmpty())
130
0
                return false;
131
            // returns true if the overlap is more than just a touching at the limits
132
793M
            return ((rRange.mnMaximum > mnMinimum) && (rRange.mnMinimum < mnMaximum));
133
793M
        }
134
135
        bool operator==( const BasicRange& rRange ) const
136
530
        {
137
530
            return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
138
530
        }
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
530
        {
137
530
            return (mnMinimum == rRange.mnMinimum && mnMaximum == rRange.mnMaximum);
138
530
        }
139
140
        bool operator!=( const BasicRange& rRange ) const
141
265k
        {
142
265k
            return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
143
265k
        }
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
265k
        {
142
265k
            return (mnMinimum != rRange.mnMinimum || mnMaximum != rRange.mnMaximum);
143
265k
        }
144
145
        bool equal(const BasicRange& rRange) const
146
31.3k
        {
147
31.3k
            return (
148
31.3k
                fTools::equal(mnMinimum, rRange.mnMinimum) &&
149
26.1k
                fTools::equal(mnMaximum, rRange.mnMaximum));
150
31.3k
        }
151
152
        void expand(T nValue)
153
3.53G
        {
154
3.53G
            if(isEmpty())
155
31.9M
            {
156
31.9M
                mnMinimum = mnMaximum = nValue;
157
31.9M
            }
158
3.49G
            else
159
3.49G
            {
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
3.49G
                if(nValue < mnMinimum)
167
#if defined __GNUC__ && !defined __clang__
168
#pragma GCC diagnostic pop
169
#endif
170
1.27G
                {
171
1.27G
                    mnMinimum = nValue;
172
1.27G
                }
173
174
3.49G
                if(nValue > mnMaximum)
175
1.41G
                {
176
1.41G
                    mnMaximum = nValue;
177
1.41G
                }
178
3.49G
            }
179
3.53G
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::expand(double)
Line
Count
Source
153
3.52G
        {
154
3.52G
            if(isEmpty())
155
31.9M
            {
156
31.9M
                mnMinimum = mnMaximum = nValue;
157
31.9M
            }
158
3.49G
            else
159
3.49G
            {
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
3.49G
                if(nValue < mnMinimum)
167
#if defined __GNUC__ && !defined __clang__
168
#pragma GCC diagnostic pop
169
#endif
170
1.27G
                {
171
1.27G
                    mnMinimum = nValue;
172
1.27G
                }
173
174
3.49G
                if(nValue > mnMaximum)
175
1.41G
                {
176
1.41G
                    mnMaximum = nValue;
177
1.41G
                }
178
3.49G
            }
179
3.52G
        }
basegfx::BasicRange<int, basegfx::Int32Traits>::expand(int)
Line
Count
Source
153
5.27M
        {
154
5.27M
            if(isEmpty())
155
0
            {
156
0
                mnMinimum = mnMaximum = nValue;
157
0
            }
158
5.27M
            else
159
5.27M
            {
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.27M
                if(nValue < mnMinimum)
167
#if defined __GNUC__ && !defined __clang__
168
#pragma GCC diagnostic pop
169
#endif
170
63
                {
171
63
                    mnMinimum = nValue;
172
63
                }
173
174
5.27M
                if(nValue > mnMaximum)
175
5.13M
                {
176
5.13M
                    mnMaximum = nValue;
177
5.13M
                }
178
5.27M
            }
179
5.27M
        }
180
181
        void expand(const BasicRange& rRange)
182
123M
        {
183
123M
            if(isEmpty())
184
40.1M
            {
185
40.1M
                mnMinimum = rRange.mnMinimum;
186
40.1M
                mnMaximum = rRange.mnMaximum;
187
40.1M
            }
188
82.8M
            else
189
82.8M
            {
190
82.8M
                if(!rRange.isEmpty())
191
82.8M
                {
192
82.8M
                    if(rRange.mnMinimum < mnMinimum)
193
1.95M
                    {
194
1.95M
                        mnMinimum = rRange.mnMinimum;
195
1.95M
                    }
196
197
82.8M
                    if(rRange.mnMaximum > mnMaximum)
198
39.3M
                    {
199
39.3M
                        mnMaximum = rRange.mnMaximum;
200
39.3M
                    }
201
82.8M
                }
202
82.8M
            }
203
123M
        }
204
205
        void intersect(const BasicRange& rRange)
206
20.1M
        {
207
            // here, overlaps also tests all isEmpty() conditions already.
208
20.1M
            if( !overlaps( rRange ) )
209
17.7M
            {
210
17.7M
                reset();
211
17.7M
            }
212
2.34M
            else
213
2.34M
            {
214
2.34M
                if(rRange.mnMinimum > mnMinimum)
215
1.74M
                {
216
1.74M
                    mnMinimum = rRange.mnMinimum;
217
1.74M
                }
218
219
2.34M
                if(rRange.mnMaximum < mnMaximum)
220
1.91M
                {
221
1.91M
                    mnMaximum = rRange.mnMaximum;
222
1.91M
                }
223
2.34M
            }
224
20.1M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::intersect(basegfx::BasicRange<double, basegfx::DoubleTraits> const&)
Line
Count
Source
206
20.1M
        {
207
            // here, overlaps also tests all isEmpty() conditions already.
208
20.1M
            if( !overlaps( rRange ) )
209
17.7M
            {
210
17.7M
                reset();
211
17.7M
            }
212
2.34M
            else
213
2.34M
            {
214
2.34M
                if(rRange.mnMinimum > mnMinimum)
215
1.74M
                {
216
1.74M
                    mnMinimum = rRange.mnMinimum;
217
1.74M
                }
218
219
2.34M
                if(rRange.mnMaximum < mnMaximum)
220
1.91M
                {
221
1.91M
                    mnMaximum = rRange.mnMaximum;
222
1.91M
                }
223
2.34M
            }
224
20.1M
        }
Unexecuted instantiation: basegfx::BasicRange<int, basegfx::Int32Traits>::intersect(basegfx::BasicRange<int, basegfx::Int32Traits> const&)
225
226
        void grow(T nValue)
227
1.64k
        {
228
1.64k
            if(isEmpty())
229
0
                return;
230
231
1.64k
            bool bLessThanZero(nValue < 0);
232
233
1.64k
            if(nValue > 0 || bLessThanZero)
234
1.64k
            {
235
1.64k
                mnMinimum -= nValue;
236
1.64k
                mnMaximum += nValue;
237
238
1.64k
                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.64k
            }
248
1.64k
        }
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
21.0M
        {
278
21.0M
            if(isEmpty())
279
44.6k
            {
280
44.6k
                return Traits::neutral();
281
44.6k
            }
282
20.9M
            else
283
20.9M
            {
284
20.9M
                return (mnMaximum - mnMinimum);
285
20.9M
            }
286
21.0M
        }
basegfx::BasicRange<double, basegfx::DoubleTraits>::getRange() const
Line
Count
Source
277
21.0M
        {
278
21.0M
            if(isEmpty())
279
44.6k
            {
280
44.6k
                return Traits::neutral();
281
44.6k
            }
282
20.9M
            else
283
20.9M
            {
284
20.9M
                return (mnMaximum - mnMinimum);
285
20.9M
            }
286
21.0M
        }
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
129M
        static constexpr double minVal() { return DBL_MIN; };
296
12.9G
        static constexpr double maxVal() { return DBL_MAX; };
297
44.6k
        static constexpr double neutral() { return 0.0; };
298
299
        typedef double DifferenceType;
300
    };
301
302
    struct Int32Traits
303
    {
304
315k
        static constexpr sal_Int32 minVal() { return SAL_MIN_INT32; };
305
5.68M
        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: */