17#include <range/v3/algorithm/find_if.hpp>
18#include <range/v3/range/concepts.hpp>
39 std::vector<T>
const& src_vec,
40 std::vector<std::size_t>
const& exclude_positions)
42 std::vector<T> dest_vec;
43 if (exclude_positions.empty())
49 assert(exclude_positions.back() < src_vec.size());
51 std::copy_n(src_vec.cbegin(), exclude_positions[0],
52 std::back_inserter(dest_vec));
53 for (std::size_t i = 1; i < exclude_positions.size(); ++i)
55 std::copy_n(src_vec.cbegin() + exclude_positions[i - 1] + 1,
56 exclude_positions[i] - (exclude_positions[i - 1] + 1),
57 std::back_inserter(dest_vec));
59 std::copy(src_vec.cbegin() + exclude_positions.back() + 1, src_vec.cend(),
60 std::back_inserter(dest_vec));
67 std::vector<std::size_t>
const& exclude_positions,
68 std::vector<T>& dest_vec)
76template <ranges::input_range Range>
79 std::predicate<ranges::range_reference_t<Range>>
auto&& predicate,
80 std::invocable
auto error_callback)
83 ranges::find_if(range, std::forward<
decltype(predicate)>(predicate));
84 if (it == ranges::end(range))
88 "Element not found in the input range. The user provided error "
89 "callback is meant not to return. That has not happened.");
99template <
typename Map,
typename Key,
typename Value>
101 std::string
const& error_message)
103 auto const inserted = map.emplace(key, std::forward<Value>(value));
104 if (!inserted.second)
106 OGS_FATAL(
"{} Key `{}' already exists.", error_message, key);
113template <
typename Map,
typename Key>
115 Map& map, Key
const& key, std::string
const& error_message)
117 auto it = map.find(key);
120 if constexpr (std::is_convertible<Key, std::string>::value)
122 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
126 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
127 std::to_string(key));
134template <
typename Map,
typename Key>
136 Map
const& map, Key
const& key, std::string
const& error_message)
138 auto it = map.find(key);
141 if constexpr (std::is_convertible<Key, std::string>::value)
143 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
147 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
148 std::to_string(key));
158template <
typename Container,
typename Predicate>
160 Container
const& container,
161 Predicate&& predicate,
162 std::string
const& error_message)
164 auto it = std::find_if(begin(container), end(container), predicate);
165 if (it == end(container))
167 OGS_FATAL(
"Could not find element matching the predicate: {:s}",
178 std::sort(v.begin(), v.end());
179 auto it = std::unique(v.begin(), v.end());
180 v.erase(it, v.end());
185template <
typename T,
class Compare>
188 std::sort(v.begin(), v.end(), comp);
189 auto it = std::unique(v.begin(), v.end());
190 v.erase(it, v.end());
198template <
typename ValueType,
typename IndexType>
200 std::vector<IndexType>
const& order)
202 std::vector<ValueType> temp_v(v.size());
205 for (std::size_t i = 0; i < order.size(); i++)
207 std::swap(v[i], temp_v[order[i]]);
211template <
typename Container>
213 typename Container::value_type
const& element)
215 if (std::find(container.begin(), container.end(), element) ==
218 container.push_back(element);
222template <
typename Container>
224 Container
const& container,
typename Container::value_type
const& element)
227 std::find_if_not(container.begin(), container.end(),
228 [&element](
typename Container::value_type
const& e)
229 { return e == element; });
230 return it == container.end() ? std::nullopt : std::make_optional(*it);
238template <
typename Container>
240 typename Container::value_type
const& element)
242 auto const it = std::find(container.begin(), container.end(), element);
243 if (it == container.end())
245 return std::numeric_limits<std::size_t>::max();
247 return std::distance(container.begin(), it);
254 for (
auto item : items)
268template <
typename T1,
typename... Args>
276template <
typename List>
280 for (
auto& value : values)
282 if (
static_cast<bool>(value))
292template <
typename List>
296 for (
auto& value : values)
298 if (!
static_cast<bool>(value))
308template <
typename List>
318template <
class... Ts>
321 using Ts::operator()...;
323#if defined(__clang__)
324#if (__clang_major__ <= 16)
326template <
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)
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.