cgv
Loading...
Searching...
No Matches
algorithm.h
1#pragma once
2
3#include <algorithm>
4#include <string>
5#include <vector>
6
7#include <utility>
8
9//#include "lib_begin.h"
10
11namespace cgv {
12namespace utils {
13
15template <class T1 = void, class T2 = void>
16struct get_first {
17 constexpr T1 operator()(const std::pair<T1, T2>& pair) const {
18 return pair.first;
19 }
20};
21
23template <>
24struct get_first<void, void> {
25 template <class T1, class T2>
26 constexpr auto operator()(const std::pair<T1, T2>& pair) const
27 -> decltype(static_cast<const T1&>(pair.first)) {
28 return static_cast<const T1&>(pair.first);
29 }
30};
31
33template <class T1 = void, class T2 = void>
34struct get_second {
35 constexpr T2 operator()(const std::pair<T1, T2>& pair) const {
36 return pair.second;
37 }
38};
39
41template <>
42struct get_second<void, void> {
43 template <class T1, class T2>
44 constexpr auto operator()(const std::pair<T1, T2>& pair) const
45 -> decltype(static_cast<const T2&>(pair.second)) {
46 return static_cast<const T2&>(pair.second);
47 }
48};
49
58template<typename ForwardIt, typename T>
59T min_value(ForwardIt first, ForwardIt last, T fallback) {
60 auto it = std::min_element(first, last);
61 return it != last ? static_cast<T>(*it) : fallback;
62}
63
74template<typename ForwardIt, typename UnaryOp, typename T>
75T min_value(ForwardIt first, ForwardIt last, UnaryOp operation, T fallback) {
76 auto it = std::min_element(first, last, [&operation](const auto& left, const auto& right) {
77 return operation(left) < operation(right);
78 });
79 return it != last ? static_cast<T>(operation(*it)) : fallback;
80}
81
90template<typename ForwardIt, typename T>
91T max_value(ForwardIt first, ForwardIt last, T fallback) {
92 auto it = std::max_element(first, last);
93 return it != last ? static_cast<T>(*it) : fallback;
94}
95
106template<typename ForwardIt, typename UnaryOp, typename T>
107T max_value(ForwardIt first, ForwardIt last, UnaryOp operation, T fallback) {
108 auto it = std::max_element(first, last, [&operation](const auto& left, const auto& right) {
109 return operation(left) < operation(right);
110 });
111 return it != last ? static_cast<T>(operation(*it)) : fallback;
112}
113
124template <class InputIt, class UnaryOp>
125std::string transform_join(const InputIt first, const InputIt last, UnaryOp operation, const std::string& separator, bool trailing_separator = false) {
126 std::string res = "";
127 if(last > first) {
128 for(auto curr = first; curr != last; ++curr) {
129 res += operation(*curr);
130 if(last - curr > 1 || trailing_separator)
131 res += separator;
132 }
133 }
134 return res;
135}
136
147template <class InputIt>
148std::string join(const InputIt first, const InputIt last, const std::string& separator, bool trailing_separator = false) {
149 return transform_join(first, last, [](const auto& val) { return to_string(val); }, separator, trailing_separator);
150}
151
162template<class InputIt, class OutputIt>
163OutputIt pair_adjacent(const InputIt first, const InputIt last, OutputIt d_first) {
164 if(std::distance(first, last) > 1) {
165 return std::transform(first, std::prev(last), std::next(first), d_first, [](const auto& a, const auto& b) {
166 return std::make_pair(a, b);
167 });
168 }
169 return d_first;
170}
171
180template<class InputIt>
181std::vector<std::pair<typename InputIt::value_type, typename InputIt::value_type>> pair_adjacent(const InputIt first, const InputIt last) {
182 std::vector<std::pair<typename InputIt::value_type, typename InputIt::value_type>> res;
183 pair_adjacent(first, last, std::back_inserter(res));
184 return res;
185}
186
187} // namespace utils
188} // namespace cgv
189
190//#include <cgv/config/lib_end.h>
std::string join(const InputIt first, const InputIt last, const std::string &separator, bool trailing_separator=false)
Concatenate elements in the range [first, last) to a std::string.
Definition algorithm.h:148
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
std::string transform_join(const InputIt first, const InputIt last, UnaryOp operation, const std::string &separator, bool trailing_separator=false)
Transform elements in the range [first, last) and concatenate them to a std::string.
Definition algorithm.h:125
T min_value(ForwardIt first, ForwardIt last, T fallback)
Find the minimum value in the range [first, last) or return fallback if the range is empty.
Definition algorithm.h:59
OutputIt pair_adjacent(const InputIt first, const InputIt last, OutputIt d_first)
Transform the elements in the range [first, last) to adjacent pairs and store the results in an outpu...
Definition algorithm.h:163
T max_value(ForwardIt first, ForwardIt last, T fallback)
Find the maximum value in the range [first, last) or return fallback if the range is empty.
Definition algorithm.h:91
the cgv namespace
Definition print.h:11
Return std::pair::first.
Definition algorithm.h:16
Return std::pair::second.
Definition algorithm.h:34