A header-only C++ library that enables the representation of a range of values in a linear space

Numeric Range

A header-only C++ library that enables the representation of a range of values in a linear space (via the NumericRange class).

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.

Structure

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.

template<typename T>
class NumericRange
{
public:
    T lb;
    bool lb_inclusive;
    T ub;
    bool ub_inclusive;
};

NumericRange<int> zero_to_one{0, true, 1, false};

Scalars

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.

NumericRange<int> scalar_one{1, true, 1, true};

Ordering

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.

NumericRangeComparator comp;
NumericRange<int> zero_to_one{0, true, 1, false};
NumericRange<int> one_to_two{1, true, 2, false};

assert(comp(zero_to_one, one_to_two));

A special exception is made for scalars so that NumericRangeComparator can be used to determine whether a scalar is included in a range.

NumericRangeComparator comp;
NumericRange<int> zero_to_two{0, true, 2, false};
NumericRange<int> scalar_one{1, true, 1, true};

assert(!comp(scalar_one, zero_to_two)); // comp returns false
assert(!comp(zero_to_two, scalar_one)); // comp returns false again

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.

Limitations

A numeric range or a comparison of ranges must not violate these constraints:

  1. In a range, the lower bound must be <= the upper bound.
  2. If a range's lower bound == upper bound, both bounds must be inclusive.
  3. Overlapping ranges cannot be compared.
  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.

License and Contributing

This library is provided under the MIT License. Contributions are welcome, please open a relevant issue or PR.

Owner
Amal Bansode
Strategy/Software Dev at the University of Michigan Solar Car Team
Amal Bansode
Similar Resources

MAXREFDES1277 is a reference design that enables the evaluation of MAX17853/52 for battery management in a 48V system.

MAXREFDES1277 is a reference design that enables the evaluation of MAX17853/52 for battery management in a 48V system.

MAXREFDES1277 The MAXREFDES1277 reference design enables quick evaluation of MAX17853/52 for battery management in a 48V system. It can be used to tes

Dec 2, 2021

Avian is an experimental digital currency that enables instant payments to anyone, anywhere in the world.

Avian is an experimental digital currency that enables instant payments to anyone, anywhere in the world.

Avian Network [AVN] What is Avian? Avian is an experimental digital currency that enables instant payments to anyone, anywhere in the world. Avian use

Sep 12, 2022

This plugin enables the use of Live++ by Molecular Matter inside of Cryengine.

Live++ // CRYENGINE This plugin enables the use of Live++ by Molecular Matter inside of Cryengine. Live++ allows for easy hot-reloading and on the fly

Dec 4, 2019

OpenScan is an open-source document scanner app that enables users to scan hard copies of documents or notes and convert it into a PDF file. No ads. No data collection. We respect your privacy.

OpenScan is an open-source document scanner app that enables users to scan hard copies of documents or notes and convert it into a PDF file. No ads. No data collection. We respect your privacy.

OpenScan An open source app that enables users to scan hardcopies of documents or notes and convert it to a PDF file. No ads. No data collection. We r

Sep 13, 2022

This Program Enables And Disables Hyper-V Hypervisor So You Can Use Other Virtualisation Tools Such As (VMware, VirtualBox) Simultaneously.

Hyper-V-Switch This Program Enables And Disables Hyper-V Hypervisor So You Can Use Other Virtualisation Tools Such As (VMware, VirtualBox) Simultaneou

May 27, 2022

Project #2: FP10 Representation (Computer Architecture, Fall 2021)

4190.308 Computer Architecture (Fall 2021) Project #2: FP10 (10-bit Floating Point) Representation Due: 11:59PM, October 17 (Sunday) Introduction The

Oct 14, 2021

In this project ı'am trying to implement dijkstra algorithm with adjacency list representation.

AirportCheapestPath In this project, I have tried to make a flight advisor program to the 3rd party users. To do that, This program gets the data of a

Sep 1, 2022

Edit a PF-DTA content in hex on a side-by-side display of EBCDIC character representation.

Edit a PF-DTA content in hex on a side-by-side display of EBCDIC character representation.

AS400 Hex Editor Edit a PF-DTA content in hex on a side-by-side display of EBCDIC character representation. Introduction This tool was written to edit

May 3, 2022

RemixDB: A read- and write-optimized concurrent KV store. Fast point and range queries. Extremely low write-amplification.

REMIX and RemixDB The REMIX data structure was introduced in paper "REMIX: Efficient Range Query for LSM-trees", FAST'21. This repository maintains a

Sep 10, 2022
Comments
  • Comparator (`NumericRangeComparator<T>::operator()`) should never fail

    Comparator (`NumericRangeComparator::operator()`) should never fail

    This is a feature request.

    I recognize that NumericRangeComparator<T>::operator() throws a runtime error because it only produces valid results for disjoint ranges. However, the result of this behavior is that this comparator can't produce a total ordering of an arbitrary set of NumericRange objects. This comparator doesn't live up to the guarantees that most other comparators provide, and this could upset users who wanted to sort (or store in a std::map) ranges that happen to overlap. I would argue that this comparator is not as useful as some possible alternatives, and I think you should implement all three of these:

    1. NumericRangeStartComparator: Compare two ranges by their minima, or starting values. Ending values serve as a tiebreaker.
    2. NumericRangeEndComparator: Compare two ranges by their maxima, or ending values. Starting values serve as a tiebreaker.
    3. NumericRangeCenterComparator: Compare two ranges by their center values. The length of the range (end minus start) serves as a tiebreaker.

    I suspect that each of these will be a bit cleaner to implement than the original NumericRangeComparator.

    All of these give total orderings of arbitrary sets of NumericRange objects, and produce deterministic results. (Don't forget the tiebreakers, because they provide this determinism).

Flat map - Header only associative linear container.

flat_map flat_map is a header only associative container library that constructed on linear container. It compliants C++17/20 standard associative con

Aug 26, 2022
Enables Permanent UART output across boots. Supports PS4 version 6.72 only for now

Enables Permanent UART for PS4. Requires a 5.05/6.72/7.02 Jailbroken PS4. To build, you require ps4-sdk from Scene-Collective/ps4-payload-sdk In the t

May 12, 2022
A refactored Proof-of-concept originally developed in 2017 to print all function calls with their arguments data types and values using Ptrace during program execution.

print-function-args-debugger A refactored Proof-of-concept originally developed in 2017 to print all function calls with their arguments data types an

Jun 17, 2022
WTD is a python tool for replacing values with the C preprocessor macro which defined them.

Where's That Define WTD is a python tool for replacing values with the C preprocessor macro which defined them. An example of this is when trying to u

Mar 28, 2022
A simple Oscilloscope application that reads the values in the serial generated by an Arduino board
A simple Oscilloscope application that reads the values in the serial generated by an Arduino board

Oscilloscope Serial A simple oscilloscope application that reads the values at serial port generated by an Arduino board and based on the value of the

Jun 14, 2022
An Arduino library which allows you to communicate seamlessly with the full range of u-blox GNSS modules
An Arduino library which allows you to communicate seamlessly with the full range of u-blox GNSS modules

u-blox makes some incredible GNSS receivers covering everything from low-cost, highly configurable modules such as the SAM-M8Q all the way up to the surveyor grade ZED-F9P with precision of the diameter of a dime.

Sep 13, 2022
Range library for C++14/17/20, basis for C++20's std::ranges

range-v3 Range library for C++14/17/20. This code was the basis of a formal proposal to add range support to the C++ standard library. That proposal e

Sep 21, 2022
A C++ concepts and range based character encoding and code point enumeration library

Travis CI (Linux:gcc) Text_view A C++ Concepts based character encoding and code point enumeration library. This project is the reference implementati

Sep 9, 2022
C++ template library for linear algebra

SNUV_TOMO: 2D CT using SNUV with IRCD Project tree and description File/folder Description Eigen C++ template library for linear algebra include Conta

Jan 23, 2022
Custom linux kernel module that re-enables fn-keys on the Gigabyte Aero 15 SB

aerofnkeys Custom HID Quirks Driver that fixes function keys not working in the Gigabyte Aero 15 SB on Linux. Works by intercepting non-HID compliant

Aug 30, 2022