Controlpp
Loading...
Searching...
No Matches
algorithm.hpp
Go to the documentation of this file.
1#pragma once
2
3// std
4#include <utility>
5#include <optional>
6
7// Eigen
8#include <Eigen/Dense>
9
16namespace controlpp{
17
28 template<class Itr, class T>
29 std::optional<std::pair<Itr, Itr>> find_enclosing(Itr first, Itr last, const T& v){
30 Itr itr_low = first;
31 Itr itr_high = first;
32 ++itr_high;
33
34 for(; itr_high != last; (void)++itr_low, (void)++itr_high){
35 if(*itr_low <= v && v <= *itr_high){
36 std::pair<Itr, Itr> result(itr_low, itr_high);
37 return result;
38 }
39 }
40
41 return std::nullopt;
42 }
43
51 template<class T, int Size = Eigen::Dynamic>
52 std::optional<std::pair<const T*, const T*>> find_enclosing(const Eigen::Vector<T, Size>& range, const T& v){
53 const T* first = range.data();
54 const T* last = range.data() + range.size();
55 return find_enclosing(first, last, v);
56 }
57
58
67 template<class Iterator, class T>
68 void shift_up(Iterator first, Iterator last, const T& v0 = T(0)){
69 // the iterator that will be assigned to
70 Iterator itr_to = last;
71 --itr_to;
72
73 // the iterator that will be read from
74 Iterator itr_from = last;
75 --itr_from;
76 --itr_from;
77 while(itr_to != first){
78 *itr_to = std::move(*itr_from);
79 --itr_to;
80 --itr_from;
81 }
82
83 // assign the first value
84 *first = v0;
85 }
86
95 template<class Iterator, class T>
96 void shift_up(Iterator first, Iterator last, T&& v0 = T(0)){
97 // the iterator that will be assigned to
98 Iterator itr_to = last;
99 --itr_to;
100
101 // the iterator that will be read from
102 Iterator itr_from = last;
103 --itr_from;
104 --itr_from;
105
106 while(itr_to != first){
107 *itr_to = std::move(*itr_from);
108 --itr_to;
109 --itr_from;
110 }
111
112 // assign the first value
113 *first = std::move(v0);
114 }
115
116}
The main namespace for the Control++ library.
Definition Bode.cpp:3
void shift_up(Iterator first, Iterator last, const T &v0=T(0))
Shifts the values in a range up by one position and inserts a new value (copy operation) at the begin...
Definition algorithm.hpp:68
std::optional< std::pair< Itr, Itr > > find_enclosing(Itr first, Itr last, const T &v)
Finds elements in a range that enclose v.
Definition algorithm.hpp:29