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>
37 std::vector<T>
const& src_vec,
38 std::vector<std::size_t>
const& exclude_positions)
40 std::vector<T> dest_vec;
41 if (exclude_positions.empty())
47 assert(exclude_positions.back() < src_vec.size());
49 std::copy_n(src_vec.cbegin(), exclude_positions[0],
50 std::back_inserter(dest_vec));
51 for (std::size_t i = 1; i < exclude_positions.size(); ++i)
53 std::copy_n(src_vec.cbegin() + exclude_positions[i - 1] + 1,
54 exclude_positions[i] - (exclude_positions[i - 1] + 1),
55 std::back_inserter(dest_vec));
57 std::copy(src_vec.cbegin() + exclude_positions.back() + 1, src_vec.cend(),
58 std::back_inserter(dest_vec));
65 std::vector<std::size_t>
const& exclude_positions,
66 std::vector<T>& dest_vec)
74template <ranges::input_range Range>
77 std::predicate<ranges::range_reference_t<Range>>
auto&& predicate,
78 std::invocable
auto error_callback)
81 ranges::find_if(range, std::forward<
decltype(predicate)>(predicate));
82 if (it == ranges::end(range))
86 "Element not found in the input range. The user provided error "
87 "callback is meant not to return. That has not happened.");
97template <
typename Map,
typename Key,
typename Value>
99 std::string
const& error_message)
101 auto const inserted = map.emplace(key, std::forward<Value>(value));
102 if (!inserted.second)
104 OGS_FATAL(
"{} Key `{}' already exists.", error_message, key);
111template <
typename Map,
typename Key>
113 Map& map, Key
const& key, std::string
const& error_message)
115 auto it = map.find(key);
118 if constexpr (std::is_convertible<Key, std::string>::value)
120 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
124 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
125 std::to_string(key));
132template <
typename Map,
typename Key>
134 Map
const& map, Key
const& key, std::string
const& error_message)
136 auto it = map.find(key);
139 if constexpr (std::is_convertible<Key, std::string>::value)
141 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
145 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
146 std::to_string(key));
156template <
typename Container,
typename Predicate>
158 Container
const& container,
159 Predicate&& predicate,
160 std::string
const& error_message)
162 auto it = std::find_if(begin(container), end(container), predicate);
163 if (it == end(container))
165 OGS_FATAL(
"Could not find element matching the predicate: {:s}",
175template <ranges::input_range Range>
176 requires std::totally_ordered<ranges::range_value_t<Range>> &&
177 std::copyable<ranges::range_value_t<Range>>
180 using Element = ranges::range_value_t<Range>;
182 std::set<Element> seen;
183 std::set<Element> duplicates;
184 for (
auto const& element : range)
186 if (!seen.insert(element).second)
188 duplicates.insert(element);
192 return {duplicates.begin(), duplicates.end()};
200 std::sort(v.begin(), v.end());
201 auto it = std::unique(v.begin(), v.end());
202 v.erase(it, v.end());
207template <
typename T,
class Compare>
210 std::sort(v.begin(), v.end(), comp);
211 auto it = std::unique(v.begin(), v.end());
212 v.erase(it, v.end());
220template <
typename ValueType,
typename IndexType>
222 std::vector<IndexType>
const& order)
224 std::vector<ValueType> temp_v(v.size());
227 for (std::size_t i = 0; i < order.size(); i++)
229 std::swap(v[i], temp_v[order[i]]);
233template <
typename Container>
235 typename Container::value_type
const& element)
237 if (std::find(container.begin(), container.end(), element) ==
240 container.push_back(element);
244template <
typename Container>
246 Container
const& container,
typename Container::value_type
const& element)
249 std::find_if_not(container.begin(), container.end(),
250 [&element](
typename Container::value_type
const& e)
251 { return e == element; });
252 return it == container.end() ? std::nullopt : std::make_optional(*it);
260template <
typename Container>
262 typename Container::value_type
const& element)
264 auto const it = std::find(container.begin(), container.end(), element);
265 if (it == container.end())
267 return std::numeric_limits<std::size_t>::max();
269 return std::distance(container.begin(), it);
276 for (
auto item : items)
290template <
typename T1,
typename... Args>
299template <ranges::range R>
300 requires std::is_integral_v<ranges::range_value_t<R>>
303 return ranges::views::concat(
304 ranges::views::single(ranges::range_value_t<R>{0}),
305 ranges::views::partial_sum(sizes)) |
306 ranges::to<std::vector<ranges::range_value_t<R>>>();
310template <
typename List>
314 for (
auto& value : values)
316 if (
static_cast<bool>(value))
326template <
typename List>
330 for (
auto& value : values)
332 if (!
static_cast<bool>(value))
342template <
typename List>
352template <
class... Ts>
355 using Ts::operator()...;
357#if defined(__clang__)
358#if (__clang_major__ <= 16)
360template <
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::vector< ranges::range_value_t< Range > > getDuplicates(Range &&range)
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.