17#include <range/v3/algorithm/find_if.hpp>
18#include <range/v3/range/concepts.hpp>
38 std::vector<T>
const& src_vec,
39 std::vector<std::size_t>
const& exclude_positions)
41 std::vector<T> dest_vec;
42 if (exclude_positions.empty())
48 assert(exclude_positions.back() < src_vec.size());
50 std::copy_n(src_vec.cbegin(), exclude_positions[0],
51 std::back_inserter(dest_vec));
52 for (std::size_t i = 1; i < exclude_positions.size(); ++i)
54 std::copy_n(src_vec.cbegin() + exclude_positions[i - 1] + 1,
55 exclude_positions[i] - (exclude_positions[i - 1] + 1),
56 std::back_inserter(dest_vec));
58 std::copy(src_vec.cbegin() + exclude_positions.back() + 1, src_vec.cend(),
59 std::back_inserter(dest_vec));
66 std::vector<std::size_t>
const& exclude_positions,
67 std::vector<T>& dest_vec)
75template <ranges::input_range Range>
78 std::predicate<ranges::range_reference_t<Range>>
auto&& predicate,
79 std::invocable
auto error_callback)
82 ranges::find_if(range, std::forward<
decltype(predicate)>(predicate));
83 if (it == ranges::end(range))
87 "Element not found in the input range. The user provided error "
88 "callback is meant not to return. That has not happened.");
98template <
typename Map,
typename Key,
typename Value>
100 std::string
const& error_message)
102 auto const inserted = map.emplace(key, std::forward<Value>(value));
103 if (!inserted.second)
105 OGS_FATAL(
"{} Key `{}' already exists.", error_message, key);
112template <
typename Map,
typename Key>
113typename Map::mapped_type&
getOrError(Map& map, Key
const& key,
114 std::string
const& error_message)
116 auto it = map.find(key);
119 if constexpr (std::is_convertible<Key, std::string>::value)
121 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
125 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
126 std::to_string(key));
133template <
typename Map,
typename Key>
134typename Map::mapped_type
const&
getOrError(Map
const& map, Key
const& key,
135 std::string
const& error_message)
137 auto it = map.find(key);
140 if constexpr (std::is_convertible<Key, std::string>::value)
142 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message, key);
146 OGS_FATAL(
"{:s} Key `{:s}' does not exist.", error_message,
147 std::to_string(key));
157template <
typename Container,
typename Predicate>
159 Container
const& container,
160 Predicate&& predicate,
161 std::string
const& error_message)
163 auto it = std::find_if(begin(container), end(container), predicate);
164 if (it == end(container))
166 OGS_FATAL(
"Could not find element matching the predicate: {:s}",
177 std::sort(v.begin(), v.end());
178 auto it = std::unique(v.begin(), v.end());
179 v.erase(it, v.end());
184template <
typename T,
class Compare>
187 std::sort(v.begin(), v.end(), comp);
188 auto it = std::unique(v.begin(), v.end());
189 v.erase(it, v.end());
197template <
typename ValueType,
typename IndexType>
199 std::vector<IndexType>
const& order)
201 std::vector<ValueType> temp_v(v.size());
204 for (std::size_t i = 0; i < order.size(); i++)
206 std::swap(v[i], temp_v[order[i]]);
210template <
typename Container>
212 typename Container::value_type
const& element)
214 if (std::find(container.begin(), container.end(), element) ==
217 container.push_back(element);
221template <
typename Container>
223 Container
const& container,
typename Container::value_type
const& element)
226 std::find_if_not(container.begin(), container.end(),
227 [&element](
typename Container::value_type
const& e)
228 { return e == element; });
229 return it == container.end() ? std::nullopt : std::make_optional(*it);
237template <
typename Container>
239 typename Container::value_type
const& element)
241 auto const it = std::find(container.begin(), container.end(), element);
242 if (it == container.end())
244 return std::numeric_limits<std::size_t>::max();
246 return std::distance(container.begin(), it);
253 for (
auto item : items)
267template <
typename T1,
typename... Args>
275template <
typename List>
279 for (
auto& value : values)
281 if (
static_cast<bool>(value))
291template <
typename List>
295 for (
auto& value : values)
297 if (!
static_cast<bool>(value))
307template <
typename List>
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)
Map::mapped_type & getOrError(Map &map, Key const &key, std::string const &error_message)
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.
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)
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.