17#include <range/v3/algorithm/find_if.hpp>
18#include <range/v3/range/concepts.hpp>
19#include <range/v3/range/conversion.hpp>
20#include <range/v3/view/concat.hpp>
21#include <range/v3/view/partial_sum.hpp>
22#include <range/v3/view/single.hpp>
43 std::vector<T>
const& src_vec,
44 std::vector<std::size_t>
const& exclude_positions)
46 std::vector<T> dest_vec;
47 if (exclude_positions.empty())
53 assert(exclude_positions.back() < src_vec.size());
55 std::copy_n(src_vec.cbegin(), exclude_positions[0],
56 std::back_inserter(dest_vec));
57 for (std::size_t i = 1; i < exclude_positions.size(); ++i)
59 std::copy_n(src_vec.cbegin() + exclude_positions[i - 1] + 1,
60 exclude_positions[i] - (exclude_positions[i - 1] + 1),
61 std::back_inserter(dest_vec));
63 std::copy(src_vec.cbegin() + exclude_positions.back() + 1, src_vec.cend(),
64 std::back_inserter(dest_vec));
71 std::vector<std::size_t>
const& exclude_positions,
72 std::vector<T>& dest_vec)
80template <ranges::input_range Range>
83 std::predicate<ranges::range_reference_t<Range>>
auto&& predicate,
84 std::invocable
auto error_callback)
87 ranges::find_if(range, std::forward<
decltype(predicate)>(predicate));
88 if (it == ranges::end(range))
92 "Element not found in the input range. The user provided error "
93 "callback is meant not to return. That has not happened.");
103template <
typename Map,
typename Key,
typename Value>
105 std::string
const& error_message)
107 auto const inserted = map.emplace(key, std::forward<Value>(value));
108 if (!inserted.second)
110 OGS_FATAL(
"{} Key `{}' already exists.", error_message, key);
117template <
typename Map,
typename Key>
119 Map& map, Key
const& key, std::string
const& error_message)
121 auto it = map.find(key);
124 if constexpr (std::is_convertible<Key, std::string>::value)
126 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
130 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
131 std::to_string(key));
138template <
typename Map,
typename Key>
140 Map
const& map, Key
const& key, std::string
const& error_message)
142 auto it = map.find(key);
145 if constexpr (std::is_convertible<Key, std::string>::value)
147 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
151 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
152 std::to_string(key));
162template <
typename Container,
typename Predicate>
164 Container
const& container,
165 Predicate&& predicate,
166 std::string
const& error_message)
168 auto it = std::find_if(begin(container), end(container), predicate);
169 if (it == end(container))
171 OGS_FATAL(
"Could not find element matching the predicate: {:s}",
182 std::sort(v.begin(), v.end());
183 auto it = std::unique(v.begin(), v.end());
184 v.erase(it, v.end());
189template <
typename T,
class Compare>
192 std::sort(v.begin(), v.end(), comp);
193 auto it = std::unique(v.begin(), v.end());
194 v.erase(it, v.end());
202template <
typename ValueType,
typename IndexType>
204 std::vector<IndexType>
const& order)
206 std::vector<ValueType> temp_v(v.size());
209 for (std::size_t i = 0; i < order.size(); i++)
211 std::swap(v[i], temp_v[order[i]]);
215template <
typename Container>
217 typename Container::value_type
const& element)
219 if (std::find(container.begin(), container.end(), element) ==
222 container.push_back(element);
226template <
typename Container>
228 Container
const& container,
typename Container::value_type
const& element)
231 std::find_if_not(container.begin(), container.end(),
232 [&element](
typename Container::value_type
const& e)
233 { return e == element; });
234 return it == container.end() ? std::nullopt : std::make_optional(*it);
242template <
typename Container>
244 typename Container::value_type
const& element)
246 auto const it = std::find(container.begin(), container.end(), element);
247 if (it == container.end())
249 return std::numeric_limits<std::size_t>::max();
251 return std::distance(container.begin(), it);
258 for (
auto item : items)
272template <
typename T1,
typename... Args>
281template <ranges::range R>
282 requires std::is_integral_v<ranges::range_value_t<R>>
285 return ranges::views::concat(
286 ranges::views::single(ranges::range_value_t<R>{0}),
287 ranges::views::partial_sum(sizes)) |
288 ranges::to<std::vector<ranges::range_value_t<R>>>();
292template <
typename List>
296 for (
auto& value : values)
298 if (
static_cast<bool>(value))
308template <
typename List>
312 for (
auto& value : values)
314 if (!
static_cast<bool>(value))
324template <
typename List>
334template <
class... Ts>
337 using Ts::operator()...;
339#if defined(__clang__)
340#if (__clang_major__ <= 16)
342template <
class... Ts>
Wraps a pair of iterators for use as a range in range-based for-loops.
std::vector< T > excludeObjectCopy(std::vector< T > const &src_vec, std::vector< std::size_t > const &exclude_positions)
std::size_t findIndex(Container const &container, typename Container::value_type const &element)
void insertIfKeyUniqueElseError(Map &map, Key const &key, Value &&value, std::string const &error_message)
ranges::range_reference_t< Range > findElementOrError(Range &range, std::predicate< ranges::range_reference_t< Range > > auto &&predicate, std::invocable auto error_callback)
constexpr bool none_of(List const &values)
Checks if none of the elements in the given list are true.
void cleanupVectorElements(std::vector< T * > &items)
void uniquePushBack(Container &container, typename Container::value_type const &element)
constexpr bool all_of(List const &values)
Checks if all of the elements in the given list are true.
OGS_NO_DANGLING Container::value_type const & getIfOrError(Container const &container, Predicate &&predicate, std::string const &error_message)
std::vector< ranges::range_value_t< R > > sizesToOffsets(R const &sizes)
void reorderVector(std::vector< ValueType > &v, std::vector< IndexType > const &order)
OGS_NO_DANGLING Map::mapped_type & getOrError(Map &map, Key const &key, std::string const &error_message)
void makeVectorUnique(std::vector< T > &v)
std::optional< typename Container::value_type > findFirstNotEqualElement(Container const &container, typename Container::value_type const &element)
constexpr bool any_of(List const &values)
Checks if any of the elements in the given list is true.