WPILibC++ 2024.3.2
Algorithm.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <algorithm>
8#include <cstddef>
9#include <utility>
10#include <vector>
11
12namespace wpi {
13
14// Binary insertion into vector; std::log(n) efficiency.
15template <typename T>
16typename std::vector<T>::iterator insert_sorted(std::vector<T>& vec,
17 T const& item) {
18 return vec.insert(std::upper_bound(vec.begin(), vec.end(), item), item);
19}
20
21/**
22 * Calls f(i, elem) for each element of elems where i is the index of the
23 * element in elems and elem is the element.
24 *
25 * @param f The callback.
26 * @param elems The elements.
27 */
28template <typename F, typename... Ts>
29constexpr void for_each(F&& f, Ts&&... elems) {
30 [&]<size_t... Is>(std::index_sequence<Is...>) {
31 (f(Is, elems), ...);
32 }(std::index_sequence_for<Ts...>{});
33}
34
35} // namespace wpi
integer_sequence< size_t, N... > index_sequence
Definition: ranges.h:193
make_index_sequence< sizeof...(Ts)> index_sequence_for
Definition: cpp_future.h:142
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.
Definition: ntcore_cpp.h:26
constexpr void for_each(F &&f, Ts &&... elems)
Calls f(i, elem) for each element of elems where i is the index of the element in elems and elem is t...
Definition: Algorithm.h:29
std::vector< T >::iterator insert_sorted(std::vector< T > &vec, T const &item)
Definition: Algorithm.h:16