|
| 1 | +# Numeric Range |
| 2 | +A header-only C++ library that enables the representation of a range of values in a linear space (via the `NumericRange` class). |
| 3 | + |
| 4 | +The linear space, of `type T`, must have a well-defined `operator<` to enable sorting and comparison of ranges (via the `NumericRangeComparator`). Hence while this library could be used for a variety of types, numeric types are the only ones with correctness guarantees for the classes and methods shipped in this version of the library. |
| 5 | + |
| 6 | +## Structure |
| 7 | + |
| 8 | +A numeric range is represented by a lower bound and upper bound of value `type T`, as well as booleans defining whether these bounds are inclusive or exclusive, respectively. |
| 9 | + |
| 10 | +```c++ |
| 11 | +template<typename T> |
| 12 | +class NumericRange |
| 13 | +{ |
| 14 | +public: |
| 15 | + T lb; |
| 16 | + bool lb_inclusive; |
| 17 | + T ub; |
| 18 | + bool ub_inclusive; |
| 19 | +}; |
| 20 | + |
| 21 | +NumericRange<int> zero_to_one{0, true, 1, false}; |
| 22 | +``` |
| 23 | +
|
| 24 | +### Scalars |
| 25 | +
|
| 26 | +A special exception is made for a "scalar", which represents a single value in the linear space of type `T`. For a scalar, the lower bound equals the upper bound and both bounds are inclusive. |
| 27 | +
|
| 28 | +```c++ |
| 29 | +NumericRange<int> scalar_one{1, true, 1, true}; |
| 30 | +``` |
| 31 | + |
| 32 | +## Ordering |
| 33 | + |
| 34 | +Numeric ranges can also be ordered **as long as they do not overlap**. This is implemented by the comparator class `NumericRangeComparator`. If the ranges do overlap, comparing them throws an `std::runtime_error`. |
| 35 | + |
| 36 | +```c++ |
| 37 | +NumericRangeComparator comp; |
| 38 | +NumericRange<int> zero_to_one{0, true, 1, false}; |
| 39 | +NumericRange<int> one_to_two{1, true, 2, false}; |
| 40 | + |
| 41 | +assert(comp(zero_to_one, one_to_two)); |
| 42 | +``` |
| 43 | +
|
| 44 | +A special exception is made for scalars so that `NumericRangeComparator` can be used to determine whether a _scalar is included in a range_. |
| 45 | +
|
| 46 | +```c++ |
| 47 | +NumericRangeComparator comp; |
| 48 | +NumericRange<int> zero_to_two{0, true, 2, false}; |
| 49 | +NumericRange<int> scalar_one{1, true, 1, true}; |
| 50 | +
|
| 51 | +assert(!comp(scalar_one, zero_to_two)); // comp returns false |
| 52 | +assert(!comp(zero_to_two, scalar_one)); // comp returns false again |
| 53 | +``` |
| 54 | + |
| 55 | +The result of this is that scalar `1` is neither less than nor greater than `[0, 2)`. This can be useful when dealing with a container of ranges that are being indexed with scalars. This is demonstrated in the example program [`range_map.cpp`](https://github.com/amalbansode/numeric-range/blob/master/example/range_map.cpp). |
| 56 | + |
| 57 | +## Limitations |
| 58 | + |
| 59 | +A numeric range or a comparison of ranges must not violate these constraints: |
| 60 | +1. In a range, the lower bound must be `<=` the upper bound. |
| 61 | +2. If a range's lower bound `==` upper bound, both bounds must be inclusive. |
| 62 | +3. Overlapping ranges cannot be compared. |
| 63 | +4. This library has only been tested with the default numeric types in C++. Using other custom types may result in unexpected or undefined behavior. |
| 64 | + |
| 65 | +## License and Contributing |
| 66 | + |
| 67 | +This library is provided under the MIT License. Contributions are welcome, please open a relevant issue or PR. |
0 commit comments