WPILibC++
2025.2.1
Loading...
Searching...
No Matches
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
12
namespace
wpi
{
13
14
// Binary insertion into vector; std::log(n) efficiency.
15
template
<
typename
T>
16
typename
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
*/
28
template
<
typename
F,
typename
... Ts>
29
constexpr
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
wpi
Foonathan namespace.
Definition
ntcore_cpp.h:26
wpi::for_each
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
wpi::insert_sorted
std::vector< T >::iterator insert_sorted(std::vector< T > &vec, T const &item)
Definition
Algorithm.h:16
wpi
Algorithm.h
Generated on Fri Jan 10 2025 07:37:20 for WPILibC++ by
1.12.0