All Projects → tcbrindle → Nanorange

tcbrindle / Nanorange

Licence: bsl-1.0
Range-based goodness for C++17

Programming Languages

cplusplus
227 projects

Labels

Projects that are alternatives of or similar to Nanorange

BottomSheetTimeRangePicker
A tiny Android library for displaying a TimePicker with From and To ranges as a BottomSheetDialogFragment.
Stars: ✭ 60 (-79.8%)
Mutual labels:  range
react-simple-range
🔉 React slider component for inputting a numeric value within a range.
Stars: ✭ 20 (-93.27%)
Mutual labels:  range
homebridge-ranger
A HomeKit range extender for Bluetooth Low Energy (BLE) accessories.
Stars: ✭ 65 (-78.11%)
Mutual labels:  range
httputils
Http工具包:OkHttp轻量封装 、功能全面、设计力求优雅与纯粹,Java领域前后端处Http问题的新选择。
Stars: ✭ 21 (-92.93%)
Mutual labels:  range
AORangeSlider
AORangeSlider is a custom UISlider with two handlers to pick a minimum and a maximum range.
Stars: ✭ 82 (-72.39%)
Mutual labels:  range
web-marker
a web page highlighter - Please star if you like this project
Stars: ✭ 51 (-82.83%)
Mutual labels:  range
jquery-datepicker
A full-featured datepicker jquery plugin
Stars: ✭ 35 (-88.22%)
Mutual labels:  range
Range V3
Range library for C++14/17/20, basis for C++20's std::ranges
Stars: ✭ 3,231 (+987.88%)
Mutual labels:  range
vl53l0x-linux
Library for interfacing with VL53L0X time-of-flight distance sensor under Linux
Stars: ✭ 19 (-93.6%)
Mutual labels:  range
HCSR04
Arduino library for HC-SR04, HC-SRF05, DYP-ME007, BLJ-ME007Y, JSN-SR04T ultrasonic ranging sensor
Stars: ✭ 27 (-90.91%)
Mutual labels:  range
text-editor
A text selection range API written in pure JavaScript, for modern browsers.
Stars: ✭ 24 (-91.92%)
Mutual labels:  range
range-slider
Customizable slider (range) component for JavaScript with no dependencies
Stars: ✭ 26 (-91.25%)
Mutual labels:  range
ip scan
Scan a list of IPs quickly using multithreading
Stars: ✭ 13 (-95.62%)
Mutual labels:  range
staticstep
Provides truly zero-cost alternatives to Iterator::step_by for both incrementing and decrementing any type that satisfies RangeBounds<T: Copy + Default + Step>.
Stars: ✭ 13 (-95.62%)
Mutual labels:  range
jquery-rsSliderLens
UI slider control that magnifies the current value
Stars: ✭ 20 (-93.27%)
Mutual labels:  range
LinqBenchmarks
Benchmarking LINQ and alternative implementations
Stars: ✭ 138 (-53.54%)
Mutual labels:  range
react-native-daterange-picker
A React Native component for picking date ranges or single dates.
Stars: ✭ 86 (-71.04%)
Mutual labels:  range
Deskapp
DeskApp Admin is a free to use Bootstrap 4 admin template.
Stars: ✭ 296 (-0.34%)
Mutual labels:  range
Input Range Scss
Styling Cross-Browser Compatible Range Inputs with Sass
Stars: ✭ 272 (-8.42%)
Mutual labels:  range
texthighlighter
a no dependency typescript npm package for highlighting user selected text
Stars: ✭ 17 (-94.28%)
Mutual labels:  range

Standard License Build Status Build status download Try it on godbolt online

NanoRange

NanoRange is a C++17 implementation of the C++20 Ranges proposals (formerly the Ranges TS). It provides SFINAE-based implementations of all the proposed Concepts, and constrained and range-based versions the algorithms in the <algorithm> standard library header.

It is intended for users who want range-based goodness in their C++, but don't want to (or can't) use the full-blown Range-V3. It also aims to provide an easy upgrade path to standard ranges when they arrive.

NanoRange is compatible with all three major C++ compilers, including the latest version of Microsoft Visual C++.

Usage

The easiest way to use NanoRange is to simply download the latest, automatically-generated single-header version and include it in your own sources like any other header. This is currently the recommended way to use the library.

Alternatively, you can clone this repository and use the individual headers in the include/nanorange directory. This may give a slight boost to compile times, although there doesn't seem to be too much difference at present. (In any case, the single-header version is similar to what you'll get when you #include <algorithm> in C++20).

If you use Conan you can find the latest testing package at bintray or just use the nanorange/20191001 package in conan-center.

Finally, if you use vcpkg, you can install the latest version of NanoRange from master using

vcpkg install nanorange --head

Compatibility

NanoRange requires a conforming C++17 compiler, and is tested with GCC 7 and Clang 4.0 and newer. Older versions may work in some cases, but this is not guaranteed.

In addition, NanoRange works with MSVC 2017 version 15.9. Note that the /permissive- switch is required for correct two-phase lookup.

Documentation

There is currently no reference documentation for NanoRange itself, but the Ranges TS on which it is based is partially documented on cppreference.

What it provides

Concepts

NanoRange provides all of the concepts from the ranges papers in the form of constexpr bool variable templates. You can use these to constrain your own templates via std::enable_if, or in if constexpr statements in C++17. For example

template <typename Rng>
void foo(Rng&& rng) {
    if constexpr (nano::RandomAccessRange<Rng>) {
         // do something
    } else if constexpr (nano::BidirectionalRange<Rng>>) {
        // do something else
    } else if constexpr (nano::ForwardRange<Rng>) {
        // do a third thing
    }
}

Iterator adaptors

The One Ranges proposal P0896 adds two new iterator adaptors to the standard library, namely common_iterator and counted_iterator, which are both implemented in NanoRange.

Additionally, P0896 modifies the existing iterator adaptors for compatibility with the new concepts. NanoRange therefore provides drop-in replacements for these, specifically:

  • reverse_iterator
  • front_insert_iterator
  • back_insert_iterator
  • insert_iterator
  • istream_iterator
  • ostream_iterator
  • istreambuf_iterator
  • ostreambuf_iterator

Lastly, NanoRange provides an implementation of subrange from P0896. This can be used to turn an iterator/sentinel pair into in range, or as a span-like view of a subset of another range.

Algorithms

Range-based overloads

NanoRange provides range-based overloads of all the algorithms in <algorithm>. This means that you can finally say

std::vector<int> vec{5, 4, 3, 2, 1};
nano::sort(vec);

and it will Just Work.

Constraint checking

In the existing STL, the algorithm calls are unconstrained. This means that if you pass an argument which doesn't meet the requirements of the function, the compiler will go ahead and try to instantiate the template anyway, usually resulting in pages of error messages with template backtraces for which C++ is infamous.

For example, the following program has an error: a std::list iterator is not random-access, and so doesn't meet the requirements of std::sort():

int main()
{
    std::list<int> list{5, 4, 3, 2, 1};
    std::sort(list.begin(), list.end());
}

Compiling this two-line example with Clang 6.0 results in over 350 lines of error messages!

Calling nano::sort() instead of std::sort() on the other hand means that constraints are checked before the template is instantated. This time, the entire error output is:

example.cpp:9:5: error: no matching function for call to object of type 'const nano::ranges::detail::sort_fn'
    nano::sort(list.begin(), list.end());
    ^~~~~~~~~~
include/nanorange/algorithm/stl/sort.hpp:26:5: note: candidate template ignored: requirement 'RandomAccessIterator<std::__1::__list_iterator<int, void *> >' was not satisfied [with I = std::__1::__list_iterator<int, void *>, Comp = nano::ranges::less<void>]
    operator()(I first, I last, Comp comp = Comp{}) const
    ^
include/nanorange/algorithm/stl/sort.hpp:37:5: note: candidate template ignored: substitution failure [with Rng = std::__1::__list_iterator<int, void *>, Comp = std::__1::__list_iterator<int, void *>]: no matching function for call to object of type 'const nano::ranges::detail::begin_::fn'
    operator()(Rng&& rng, Comp comp = Comp{}) const
    ^
1 error generated.

which makes it clear that the first overload was rejected because list::iterator doesn't meet the requirements of RandomAccessIterator, and the second overload was rejected because list::iterator is not a range (you can't call nano::begin() on it). Much better.

Function objects

The algorithms in NanoRange are implemented as non-template function objects with templated function call operators. This means that you can pass them around without needing to specify template arguments or wrap them in lambdas. For example:

std::vector<std::vector<int>> vec_of_vecs = ...

nano::for_each(vec_of_vecs, nano::sort); // sorts each inner vector

Projections

The fully reimplemented algorithms (see below) offer support for projections. A projection is a unary callable which modifies ("projects") the view of the data that the algorithm sees. Because projections are routed through (an implementation of) std::invoke(), it's possible to use pointers to member functions and pointers to member data as projections. For example:

struct S {
    int i;
    float f;
};

std::vector<S> vec = ...

auto iter = nano::find(vec, 10, &S::i);
// iter points to the first member of vec for which i == 10

constexpr support

In P0896, almost all the algorithms are marked as constexpr (the exceptions being those which can potentially allocate temporary storage). NanoRange fully supports this, meaning the vast majority of algorithms can be called at compile-time. For example

auto sort_copy = [](auto rng) {
    nano::sort(rng);
    return rng;
};

int main()
{
    constexpr std::array a{5, 4, 3, 2, 1};
    constexpr auto b = sort_copy(a);

    static_assert(nano::is_sorted(b));
}

Ranges papers

The Ranges proposals have been consolidated into two main papers:

NanoRange fully implements the first, and implements most of the second (except for Views).

Stability

NanoRange aims to track the various C++20 ranges proposals, and will be updated as new papers are published. As such, there are no API stability guarantees at this time.

Licence

NanoRange is provided under the Boost licence. See LICENSE_1_0.txt for details.

Acknowledgements

Many thanks to the following:

  • Eric Niebler and Casey Carter for the Ranges TS, Range-V3 and CMCSTL2. You guys are awesome.

  • Orson Peters for pdqsort

  • Phil Nash for the fantastic Catch testing framework

  • The editors of cppreference.com for painstakingly detailing the existing requirements of standard library algorithms, and more generally for maintaining the C++ programmer's bible.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].