21 template <
typename T,
typename U>
24 const auto it = std::ranges::find(vector, value);
25 if (it == vector.end())
27 vector.push_back(std::forward<U>(value));
28 return std::prev(vector.end());
34 template <
typename T,
typename U>
35 constexpr std::optional<T>
erase_single(std::vector<T>& vector, U&& value)
37 const auto it = std::ranges::find(vector, value);
38 if (it == std::ranges::end(vector))
40 auto&& result = std::move(*it);
46 template <
typename K,
typename V,
typename COMP,
typename U>
47 constexpr std::optional<V>
erase_single(std::map<K, V, COMP>& map, U&& key)
49 const auto it = map.find(key);
52 auto&& result = std::move(it->second);
58 template <
typename T,
typename PRED>
61 const auto it = std::ranges::find_if(vector,
pred);
62 if (it == std::ranges::end(vector))
64 auto&& result = std::move(*it);
70 template <
typename T,
typename U>
73 const auto it = std::ranges::find(vector, value);
74 if (it == std::ranges::end(vector))
77 auto&& result = std::exchange(*it, std::move(vector.back()));
84 template <
typename T,
typename PRED>
87 std::vector<T> result;
89 while (i < vector.size())
93 result.push_back(std::exchange(vector[i], std::move(vector.back())));
103 template <
typename T,
typename PRED>
106 const auto it = std::ranges::find_if(vector,
pred);
107 if (it == std::ranges::end(vector))
110 auto result = std::exchange(*it, std::move(vector.back()));
116 template <
typename T>
117 constexpr std::optional<T>
erase_at_swap(std::vector<T>& vector,
size_t index)
122 auto&& result = std::exchange(vector[index], std::move(vector.back()));
128 template <
typename KEY,
typename MAP>
131 auto it = map.find(std::forward<KEY>(key));
132 return (it != map.end()) ? &it->second :
nullptr;
136 template <
typename DEF,
typename KEY,
typename MAP>
139 auto it = map.find(std::forward<KEY>(key));
141 return ghassanpl::forward_like<MAP>(it->second);
142 return decltype(it->second){ std::forward<DEF>(def) };
146 template <
typename KEY,
typename MAP>
149 auto it = map.find(std::forward<KEY>(key));
151 return ghassanpl::forward_like<MAP>(it->second);
152 return decltype(it->second){};
156 template <
typename KEY,
typename MAP>
157 [[nodiscard]]
decltype(
auto)
map_at(MAP&& map, KEY&& key)
159 auto it = map.find(std::forward<KEY>(key));
161 return ghassanpl::forward_like<MAP>(it->second);
162 throw std::out_of_range(
"invalid map key");
167 template <
typename MAP>
168 inline auto map_key_type(MAP
const& val)
170 auto& [k, v] = *std::begin(val);
171 return decltype(k){};
176 template <
typename MAP,
typename VAL>
179 for (
auto& [k, v] : map)
184 using map_key_type =
decltype(detail::map_key_type(map));
185 return (map_key_type
const*)
nullptr;
190 template <
typename K,
typename V,
typename C,
typename VAL>
191 [[nodiscard]]
auto at_ptr(std::map<K, V, C>
const& map, VAL&& value) {
return map_find(map, std::forward<VAL>(value)); }
195 template <
typename K,
typename V,
typename C,
typename VAL>
196 [[nodiscard]]
auto at_ptr(std::map<K, V, C>& map, VAL&& value) {
return map_find(map, std::forward<VAL>(value)); }
auto map_at_or_default(MAP &&map, KEY &&key, DEF &&def)
Finds the value associated with key in the map and retuns it, or def if none found.
auto at_ptr(std::map< K, V, C > const &map, VAL &&value)
Same as map_find().
constexpr std::optional< T > erase_single_if(std::vector< T > &vector, PRED &&pred)
Finds a value in the vector by predicate, and erases it.
constexpr std::vector< T > erase_swap_if(std::vector< T > &vector, PRED &&pred)
Finds and erases a value in vector, not preserving item order (swapping last item to erased).
constexpr std::optional< T > erase_single_swap_if(std::vector< T > &vector, PRED &&pred)
Finds and erases a value in vector by predicate, not preserving item order (swapping last item to era...
auto map_find_value(MAP &map, VAL const *value)
Finds the first value of a map element, and returns a pointer to its key, or nullptr if none found.
constexpr std::optional< T > erase_single(std::vector< T > &vector, U &&value)
Finds a value in the vector, and erases it, but returns the value.
decltype(auto) map_at(MAP &&map, KEY &&key)
Basically map.at() but works with heterogenous key types.
auto map_find(MAP &map, KEY &&key)
Finds the value associated with key in the map and retuns a pointer to it, or nullptr if none found.
constexpr auto push_back_unique(std::vector< T > &vector, U &&value)
Pushes a value to a vector if it doesn't already exists.
constexpr std::optional< T > erase_single_swap(std::vector< T > &vector, U &&value)
Finds and erases a value in vector, not preserving item order (swapping last item to erased).
constexpr std::optional< T > erase_at_swap(std::vector< T > &vector, size_t index)
Erases the element at index in vector, not preserving item order (swapping last item to erased).
constexpr bool valid_index(random_access_range auto &range, std::integral auto index)
Returns whether or not a given integer is a valid index to a random access range.
The below code is based on Sun's libm library code, which is licensed under the following license:
Contains functions that return predicates; appropriate for functions like std::all_of.
Primary namespace for everything in this library.