Line data Source code
1 : /* File: u8_interval.inl; Copyright and License: see below */
2 :
3 : #include <math.h>
4 :
5 : static inline u8_interval_t u8_interval_new_void()
6 : {
7 : return (u8_interval_t){ .low = NAN, .high = NAN };
8 : }
9 :
10 120 : static inline u8_interval_t u8_interval_new_unordered ( double a, double b )
11 : {
12 120 : return ( a < b ) ? (u8_interval_t){ .low = a, .high = b } : (u8_interval_t){ .low = b, .high = a };
13 : }
14 :
15 14 : static inline u8_interval_t u8_interval_new ( double low, double high )
16 : {
17 14 : assert( low < high );
18 14 : return (u8_interval_t){ .low = low, .high = high };
19 : }
20 :
21 60 : static inline u8_interval_t u8_interval_new_intersect ( const u8_interval_t *a, const u8_interval_t *b )
22 : {
23 : u8_interval_t result;
24 :
25 60 : if (( (*a).high < (*b).low )||( (*a).low > (*b).high ))
26 4 : {
27 : /* there is no overlap */
28 4 : result = (u8_interval_t){ .low = NAN, .high = NAN };
29 : }
30 : else
31 : {
32 : /* there is an overlap, lower border is the max of the low values, upper border is the min of the high values: */
33 56 : result = (u8_interval_t){ .low = fmax( (*a).low, (*b).low ), .high = fmin( (*a).high, (*b).high ) };
34 : }
35 :
36 60 : return result;
37 : }
38 :
39 60 : static inline bool u8_interval_is_valid ( const u8_interval_t *this_ )
40 : {
41 60 : return (( ! isnan( (*this_).low ) )&&( ! isnan( (*this_).high ) ));
42 : }
43 :
44 : static inline double u8_interval_get_low ( const u8_interval_t *this_ )
45 : {
46 : return (*this_).low;
47 : }
48 :
49 : static inline double u8_interval_get_high ( const u8_interval_t *this_ )
50 : {
51 : return (*this_).high;
52 : }
53 :
54 60 : static inline double u8_interval_get_size ( const u8_interval_t *this_ )
55 : {
56 60 : return u8_interval_is_valid( this_ ) ? ( (*this_).high - (*this_).low ) : 0.0;
57 : }
58 :
59 :
60 : /*
61 : Copyright 2025-2025 Andreas Warnke
62 :
63 : Licensed under the Apache License, Version 2.0 (the "License");
64 : you may not use this file except in compliance with the License.
65 : You may obtain a copy of the License at
66 :
67 : http://www.apache.org/licenses/LICENSE-2.0
68 :
69 : Unless required by applicable law or agreed to in writing, software
70 : distributed under the License is distributed on an "AS IS" BASIS,
71 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72 : See the License for the specific language governing permissions and
73 : limitations under the License.
74 : */
|