10#include <range/v3/algorithm/find_if.hpp>
11#include <range/v3/range/concepts.hpp>
12#include <range/v3/range/conversion.hpp>
13#include <range/v3/view/concat.hpp>
14#include <range/v3/view/partial_sum.hpp>
15#include <range/v3/view/single.hpp>
36 std::vector<T>
const& src_vec,
37 std::vector<std::size_t>
const& exclude_positions)
39 std::vector<T> dest_vec;
40 if (exclude_positions.empty())
46 assert(exclude_positions.back() < src_vec.size());
48 std::copy_n(src_vec.cbegin(), exclude_positions[0],
49 std::back_inserter(dest_vec));
50 for (std::size_t i = 1; i < exclude_positions.size(); ++i)
52 std::copy_n(src_vec.cbegin() + exclude_positions[i - 1] + 1,
53 exclude_positions[i] - (exclude_positions[i - 1] + 1),
54 std::back_inserter(dest_vec));
56 std::copy(src_vec.cbegin() + exclude_positions.back() + 1, src_vec.cend(),
57 std::back_inserter(dest_vec));
64 std::vector<std::size_t>
const& exclude_positions,
65 std::vector<T>& dest_vec)
73template <ranges::input_range Range>
76 std::predicate<ranges::range_reference_t<Range>>
auto&& predicate,
77 std::invocable
auto error_callback)
80 ranges::find_if(range, std::forward<
decltype(predicate)>(predicate));
81 if (it == ranges::end(range))
85 "Element not found in the input range. The user provided error "
86 "callback is meant not to return. That has not happened.");
96template <
typename Map,
typename Key,
typename Value>
98 std::string
const& error_message)
100 auto const inserted = map.emplace(key, std::forward<Value>(value));
101 if (!inserted.second)
103 OGS_FATAL(
"{} Key `{}' already exists.", error_message, key);
110template <
typename Map,
typename Key>
112 Map& map, Key
const& key, std::string
const& error_message)
114 auto it = map.find(key);
117 if constexpr (std::is_convertible<Key, std::string>::value)
119 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
123 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
124 std::to_string(key));
131template <
typename Map,
typename Key>
133 Map
const& map, Key
const& key, std::string
const& error_message)
135 auto it = map.find(key);
138 if constexpr (std::is_convertible<Key, std::string>::value)
140 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
144 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
145 std::to_string(key));
155template <
typename Container,
typename Predicate>
157 Container
const& container,
158 Predicate&& predicate,
159 std::string
const& error_message)
161 auto it = std::find_if(begin(container), end(container), predicate);
162 if (it == end(container))
164 OGS_FATAL(
"Could not find element matching the predicate: {:s}",
175 std::sort(v.begin(), v.end());
176 auto it = std::unique(v.begin(), v.end());
177 v.erase(it, v.end());
182template <
typename T,
class Compare>
185 std::sort(v.begin(), v.end(), comp);
186 auto it = std::unique(v.begin(), v.end());
187 v.erase(it, v.end());
195template <
typename ValueType,
typename IndexType>
197 std::vector<IndexType>
const& order)
199 std::vector<ValueType> temp_v(v.size());
202 for (std::size_t i = 0; i < order.size(); i++)
204 std::swap(v[i], temp_v[order[i]]);
208template <
typename Container>
210 typename Container::value_type
const& element)
212 if (std::find(container.begin(), container.end(), element) ==
215 container.push_back(element);
219template <
typename Container>
221 Container
const& container,
typename Container::value_type
const& element)
224 std::find_if_not(container.begin(), container.end(),
225 [&element](
typename Container::value_type
const& e)
226 { return e == element; });
227 return it == container.end() ? std::nullopt : std::make_optional(*it);
235template <
typename Container>
237 typename Container::value_type
const& element)
239 auto const it = std::find(container.begin(), container.end(), element);
240 if (it == container.end())
242 return std::numeric_limits<std::size_t>::max();
244 return std::distance(container.begin(), it);
251 for (
auto item : items)
265template <
typename T1,
typename... Args>
274template <ranges::range R>
275 requires std::is_integral_v<ranges::range_value_t<R>>
278 return ranges::views::concat(
279 ranges::views::single(ranges::range_value_t<R>{0}),
280 ranges::views::partial_sum(sizes)) |
281 ranges::to<std::vector<ranges::range_value_t<R>>>();
285template <
typename List>
289 for (
auto& value : values)
291 if (
static_cast<bool>(value))
301template <
typename List>
305 for (
auto& value : values)
307 if (!
static_cast<bool>(value))
317template <
typename List>
327template <
class... Ts>
330 using Ts::operator()...;
332#if defined(__clang__)
333#if (__clang_major__ <= 16)
335template <
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.