20 template <
typename T,
typename U>
23 const auto it = std::ranges::find(
vector, value);
26 vector.push_back(std::forward<U>(value));
27 return std::prev(
vector.end());
33 template <
typename T,
typename U>
36 const auto it = std::ranges::find(
vector, value);
39 auto&& result = std::move(*
it);
45 template <
typename K,
typename V,
typename COMP,
typename U>
48 const auto it = map.find(
key);
51 auto&& result = std::move(
it->second);
57 template <
typename T,
typename PRED>
63 auto&& result = std::move(*
it);
69 template <
typename T,
typename U>
72 const auto it = std::ranges::find(
vector, value);
76 auto&& result = std::exchange(*
it,
vector.back());
82 template <
typename T,
typename PRED>
89 auto&& result = std::exchange(*
it,
vector.back());
101 auto&& result = std::exchange(
vector[index],
vector.back());
107 template <
typename KEY,
typename MAP>
110 auto it = map.find(std::forward<KEY>(
key));
111 return (
it != map.end()) ? &
it->second :
nullptr;
115 template <
typename DEF,
typename KEY,
typename MAP>
118 auto it = map.find(std::forward<KEY>(
key));
121 return decltype(
it->second){ std::forward<DEF>(
def) };
125 template <
typename KEY,
typename MAP>
128 auto it = map.find(std::forward<KEY>(
key));
131 throw std::out_of_range(
"invalid map key");
136 template <
typename MAP>
137 inline auto map_key_type(
MAP const&
val)
139 auto& [
k, v] = *std::begin(
val);
140 return decltype(
k){};
145 template <
typename MAP,
typename VAL>
148 for (
auto& [
k, v] : map)
153 using map_key_type =
decltype(detail::map_key_type(map));
154 return (map_key_type
const*)
nullptr;
159 template <
typename K,
typename V,
typename C,
typename VAL>
160 auto at_ptr(std::map<K, V, C>
const& map,
VAL&& value) {
return map_find(map, std::forward<VAL>(value)); }
164 template <
typename K,
typename V,
typename C,
typename VAL>
165 auto at_ptr(std::map<K, V, C>& map,
VAL&& value) {
return map_find(map, std::forward<VAL>(value)); }
constexpr auto bit_count
Equal to the number of bits in the type.
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::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)
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:
Primary namespace for everything in this library.