Numeric algorithms for C++20 Ranges

<numeric> algorithms for C++20 Ranges

Standard License Try it on godbolt online

C++20 includes updated versions of the many algorithms in the <algorithm> header. This header supplements these with updated versions of other algorithms from the <numeric> header.

If you're using C++20 ranges (or NanoRange -- see below) then you can drop the numeric_ranges.hpp header into your project and use it as a modern replacement for <numeric> until such time as C++23 comes along.

Examples

constexpr std::array arr{1, 2, 3, 4};
std::vector<int> out;

tcb::partial_sum(arr, std::back_inserter(out));
// out contains [1, 3, 6, 10]

const int prod = tcb::inner_product(arr, out, 0);
// prod = (1 * 1) + (2 * 3) + (3 * 6) + (4 * 10) = 65
assert(prod == 65);

constexpr auto sq = [](int i) { return i * i; };
constexpr int sum = tcb::accumulate(arr, 0, {}, sq);
// sum = 1 + 4 + 9 + 16 = 30
static_assert(sum == 30);

Try it on godbolt online

Usage

If your standard library provides an implementation of C++20 ranges, you can just copy numeric_ranges.hpp into your project and use it as an alternative to the <numeric> header.

The rest of this respository contains testing machinery and is not required for use.

numeric_ranges.hpp is also compatible with NanoRange. To use it, define the symbol TCB_NUMERIC_RANGES_USE_NANORANGE before #include-ing this header, for example

#define TCB_NUMERIC_RANGES_USE_NANORANGE
#include <numeric_ranges.hpp>

(or by using an equivalent compiler define).

Algorithms

The following algorithms are provided in this header:

Note that in this implementation, reduce and transform_reduce always perform their operations in order and so are equivalent to accumulate and inner_product respectively.

Caveats

Unlike the other algorithms in std::ranges (and the eventual C++23 version of <numeric>) the implementations in this library are unconstrained.

This means that it's possible to get horrible, C++98-style template error messages when things go wrong. It's also possible that in rare cases, code you write today with this library may fail constraint checks when they are added in future.

Licence

This library is provided under the terms of the Boost licence.

Acknowledgements

Thanks to Eric Niebler for writing the Range-V3 tests for all these algorithms, which I have lovingly "borrowed".

Thanks to Phil Nash et al for the fantastic Catch2 testing framework.

Similar Resources

C++17 (-O2) template for competitive programming algorithms, which contains numerous math algorithms.

cpplibForCP C++17 (-O2) template for competitive programming algorithms, which contains numerous math algorithms. Aims: build a stable, fast, easy-to-

Nov 25, 2022

Try to implement some algorithms in the book Introduction to Algorithms in C (and maybe C++).

CAlgoIntro Try to implement some algorithms in the book Introduction to Algorithms in C (and maybe C++). Note that not every algorithm & problem in th

Sep 28, 2021

Bolt is a C++ template library optimized for GPUs. Bolt provides high-performance library implementations for common algorithms such as scan, reduce, transform, and sort.

Bolt is a C++ template library optimized for heterogeneous computing. Bolt is designed to provide high-performance library implementations for common

Dec 27, 2022

A c++ toolbox of locality-sensitive hashing (LSH), provides several popular LSH algorithms, also support python and matlab.

LSHBOX-0.9 A C++ Toolbox of Locality-Sensitive Hashing for Large Scale Image Retrieval, Also Support Python and MATLAB. Change Log 2015.07.04 A new LS

Nov 13, 2022

An open library of computer vision algorithms

VLFeat -- Vision Lab Features Library Version 0.9.21 The VLFeat open source library implements popular computer vision algorithms specialising in imag

Dec 29, 2022

🔗 Common Data Structures and Algorithms

🔗 Data Structures and Algorithms This library provides common data structures. It will also provide some data structures which needed in render or ga

Dec 10, 2022

Sqrt OS is a simulation of an OS scheduler and memory manager using different scheduling algorithms including Highest Priority First (non-preemptive), Shortest Remaining Time Next, and Round Robin

Sqrt OS is a simulation of an OS scheduler and memory manager using different scheduling algorithms including Highest Priority First (non-preemptive), Shortest Remaining Time Next, and Round Robin

A CPU scheduler determines an order for the execution of its scheduled processes; it decides which process will run according to a certain data structure that keeps track of the processes in the system and their status.

Sep 7, 2022

A collection of various algorithms to produce length-limited prefix codes

Introduction This is a collection of various algorithms to produce length-limited prefix codes. My library is written in plain C with tons of comments

Nov 7, 2022

Sqrt OS is a simulation of an OS scheduler and memory manager using different scheduling algorithms including Highest Priority First (non-preemptive), Shortest Remaining Time Next, and Round Robin.

Sqrt OS is a simulation of an OS scheduler and memory manager using different scheduling algorithms including Highest Priority First (non-preemptive), Shortest Remaining Time Next, and Round Robin.

A CPU scheduler determines an order for the execution of its scheduled processes; it decides which process will run according to a certain data structure that keeps track of the processes in the system and their status. A process, upon creation, has one of the three states: Running, Ready, Blocked (doing I/O, using other resources than CPU or waiting on unavailable resource).

Apr 15, 2022

Operating system project - implementing scheduling algorithms and some system calls for XV6 OS

About XV6 xv6 is a modern reimplementation of Sixth Edition Unix in ANSI C for multiprocessor x86 and RISC-V systems.

Dec 22, 2022

Data Structure and Algorithms problems across various platforms.

Data Structure and Algorithms problems across various platforms.

Covering various practice problems of Arrays, Stacks, queue, tree, graph and different Algorithms. A collection of solutions for Data Structure and Algorithms problems across various platforms in different languages.

Oct 29, 2022

Repository of problems and solutions of labsheets used for Data Structures and Algorithms (CS F211) in Semester 2, 2020-21 at BITS Pilani - Hyderabad Campus.

CS F211 Data Structures and Algorithms (BITS Pilani - Hyderabad Campus) This repository contains the problems, solution approaches & explanations and

Oct 31, 2022

Solutions for problems given in ETH course Algorithms Lab in Fall 2020

Algolab2020 Solutions for problems given in ETH course Algorithms Lab in Fall 2020. The code for these problems is written with the following in mind:

Jan 3, 2023

An assortment of commonly used algorithms and data structures implemented with C++.

Algorithms-And-Data-Structures This repo contains C++ implementations of common algorithms and data structures, grouped by topics. The list will be sp

Nov 9, 2021

A library of common data structures and algorithms written in C.

C Algorithms The C programming language includes a very limited standard library in comparison to other modern programming languages. This is a coll

Jan 9, 2023

Collection of various algorithms in mathematics, machine learning, computer science, physics, etc implemented in C for educational purposes.

The Algorithms - C # {#mainpage} Overview The repository is a collection of open-source implementation of a variety of algorithms implemented in C and

Jan 5, 2023

Algorithms & Data structures in C++.

Algorithms & Data structures in C++.

Algorithms & Data Structures in C++ 目标 ( goal ) : 经典的算法实现 (classical algorithms implementations) 服务器端 (based on linux/gcc) 正确,易于使用和改造, 一个头文件一个算法,并附带一个

Dec 30, 2022

Several algorithms and data structures implemented in C++ by me (credited to others where necessary).

Algorithms This repository contains my implementations of several algorithms and data structures in C++ (credited to others where necessary). It has i

Dec 19, 2022
ranges features for c+23 ported to C++20

Ranges For C++23 This repository implements a set of views intended to be proposed to a future C++ standard. The library can only be used with a confo

Dec 24, 2022
A collection of commonly used code for Algorithms Context written in C++

Algo Template CPP A collection of commonly used code for Algorithms Contest written in C++. Number int from_bin(string bin) string to_bin(int num) int

Feb 7, 2022
Replacements to standard numeric types which throw exceptions on errors

safe_numerics Arithmetic operations in C++ are NOT guaranteed to yield a correct mathematical result. This feature is inherited from the early days of

Jan 4, 2023
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

Jan 4, 2023
Single-header, ranges-compatible generator type built on C++20 coroutines

generator Single-header, ranges-compatible generator type built with C++20 coroutines. A generator allows implementing sequence producers which are te

Dec 20, 2022
Ranges that didn't make C++20

ranges Implementations of ranges that didn't make C++20. Coded live on Twitch. Types tl::enumerate_view/tl::views::enumerate A view which lets you ite

Dec 20, 2022
Companion source code for "Programming with C++20 - Concepts, Coroutines, Ranges, and more"
Companion source code for

Companion Source Code for "Programming with C++20 - Concepts, Coroutines, Ranges, and more" 1. Edition Code examples This repository contains runnable

Dec 31, 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

Dec 29, 2022
ranges features for c+23 ported to C++20

Ranges For C++23 This repository implements a set of views intended to be proposed to a future C++ standard. The library can only be used with a confo

Dec 24, 2022
Cross-platform STL-styled and STL-compatible library with implementing containers, ranges, iterators, type traits and other tools; actors system; type-safe config interface.

Yato A small repository where I'm gatherting useful snippets and abstractions for C++ development. Yato includes 3 main modules: multidimensional cont

Dec 18, 2022