25#if __cplusplus >= 202302L
32#include <initializer_list>
37#if __cplusplus >= 202302L
38 template<
class T,
class ...Args>
39 using FlatMap = std::flat_map<T,
Args...>;
47 class Equals = std::equal_to<Key>,
66 using reference = std::pair<const key_type&, mapped_type&>;
88 template<
class InputIter>
93 const auto n =
static_cast<size_type>(std::distance(first,
last));
95 cont.values.reserve(n);
96 for (; first !=
last; ++first)
98 cont.keys.push_back(first->first);
99 cont.values.push_back(first->second);
130 template<
class L,
class R>
131 using EqResult = std::invoke_result_t<const Equals&, const L&, const R&>;
133 template<
class L,
class R>
134 static constexpr bool EqComparable =
135 requires (
const Equals &
e,
const L &
l,
const R &
r)
137 { std::invoke(
e,
l,
r) } -> std::convertible_to<bool>;
145 auto it_key = std::ranges::find_if(cont.keys, [&](
const Key&
k) { return eq(k, key); });
146 if (
it_key == cont.keys.end())
153 auto it_key = std::ranges::find_if(cont.keys, [&](
const Key&
k) { return eq(k, key); });
154 if (
it_key == cont.keys.end())
163 auto it_key = std::ranges::find_if(cont.keys, [&](
const Key&
k)
165 if constexpr (EqComparable<Key, K2>)
167 return eq(k, key_like);
171 return eq(key_like, k);
174 if (
it_key == cont.keys.end())
182 auto it_key = std::ranges::find_if(cont.keys, [&](
const Key&
k)
184 if constexpr (EqComparable<Key, K2>)
186 return eq(k, key_like);
190 return eq(key_like, k);
193 if (
it_key == cont.keys.end())
195 return begin() + (
it_key - cont.keys.begin());
203 throw std::out_of_range(
"FlatMap::at: key not found");
211 throw std::out_of_range(
"FlatMap::at: key not found");
219 if (
auto it = find(key); it != end())
221 cont.keys.push_back(key);
222 cont.values.emplace_back();
223 return cont.values.back();
228 if (
auto it = find(key); it != end())
230 cont.keys.push_back(std::move(key));
231 cont.values.emplace_back();
232 return cont.values.back();
237 template<
class... Args>
238 JPL_INLINE std::pair<iterator, bool>
emplace(Args&&... args)
240 std::pair<Key, T> t(std::forward<Args>(args)...);
242 if (
auto it = find(t.first); it != end())
243 return { it,
false };
246 cont.keys.emplace_back(std::move(t.first));
247 cont.values.emplace_back(std::move(t.second));
249 return { end() - 1,
true };
257 const auto idx =
static_cast<size_type>(iter - cbegin());
258 cont.keys.erase(cont.keys.begin() +
static_cast<difference_type>(idx));
259 cont.values.erase(cont.values.begin() +
static_cast<difference_type>(idx));
267 const auto idx =
static_cast<size_type>(it - begin());
268 cont.keys.erase(cont.keys.begin() +
static_cast<difference_type>(idx));
269 cont.values.erase(cont.values.begin() +
static_cast<difference_type>(idx));
274 JPL_INLINE
size_t erase(
const Key& key)
276 if (
auto it = find(key); it != end())
282 template<
class K2>
requires (EqComparable<Key, K2> || EqComparable<K2, Key>)
283 JPL_INLINE
size_t erase(
const K2& key_like)
285 if (
auto it = find(key_like); it != end())
291 template<
class Predicate>
300 if (!std::invoke(pred, ref))
305 cont.keys[w] = std::move(cont.keys[i]);
306 cont.values[w] = std::move(cont.values[i]);
311 const size_t removed = n - w;
313 cont.values.resize(w);
317 [[nodiscard]] JPL_INLINE
bool contains(
const Key& key)
const
319 return find(key) != end();
322 template<
class K2>
requires (EqComparable<Key, K2> || EqComparable<K2, Key>)
323 [[nodiscard]] JPL_INLINE
bool contains(
const K2& key_like)
const
325 return find(key_like) != end();
329 noexcept(std::swap(a.cont.keys, b.cont.keys)) &&
330 noexcept(std::swap(a.cont.values, b.cont.values)) &&
331 noexcept(std::swap(a.eq, b.eq))
335 swap(a.cont.keys, b.cont.keys);
336 swap(a.cont.values, b.cont.values);
343 key_container_type keys;
344 mapped_container_type values;
346 containers() =
default;
347 containers(
const allocator_type& allocator)
354 [[no_unique_address]] Equals eq;
359 template<
class KeyType,
class T,
template<
class>
class AllocatorType>
364 std::equal_to<KeyType>,
365 std::vector<KeyType, AllocatorType<KeyType>>,
366 std::vector<T, AllocatorType<T>>
KeyContainer key_container_type
Definition FlatMap.h:60
JPL_INLINE const_iterator end() const noexcept
Definition FlatMap.h:121
JPL_INLINE std::pair< iterator, bool > emplace(Args &&... args)
Definition FlatMap.h:238
FlatMap(std::initializer_list< value_type > init, const allocator_type &allocator={})
Definition FlatMap.h:103
JPL_INLINE const_iterator begin() const noexcept
Definition FlatMap.h:120
JPL_INLINE const_iterator cbegin() const noexcept
Definition FlatMap.h:122
KeyContainer::allocator_type allocator_type
Definition FlatMap.h:53
JPL_INLINE size_t erase(const_iterator iter)
Definition FlatMap.h:253
JPL_INLINE mapped_type & operator[](const Key &key)
Definition FlatMap.h:217
JPL_INLINE size_t erase(const Key &key)
Definition FlatMap.h:274
JPL_INLINE const_iterator find(const K2 &key_like) const
Definition FlatMap.h:180
JPL_INLINE iterator begin() noexcept
Definition FlatMap.h:118
std::pair< const key_type &, mapped_type & > reference
Definition FlatMap.h:66
FlatMap(InputIter first, InputIter last, Equals eq={}, const allocator_type &allocator={})
Definition FlatMap.h:89
JPL_INLINE const_iterator find(const Key &key) const
Definition FlatMap.h:151
JPL_INLINE bool empty() const
Definition FlatMap.h:115
T mapped_type
Definition FlatMap.h:56
JPL_INLINE const key_container_type & keys() const noexcept
Definition FlatMap.h:109
Key key_type
Definition FlatMap.h:55
JPL_INLINE size_t erase_if(Predicate &&pred)
Definition FlatMap.h:292
JPL_INLINE bool contains(const Key &key) const
Definition FlatMap.h:317
std::size_t size_type
Definition FlatMap.h:63
JPL_INLINE iterator find(const K2 &key_like)
Definition FlatMap.h:161
JPL_INLINE const mapped_type & at(const Key &key) const
Definition FlatMap.h:207
friend void swap(FlatMap &a, FlatMap &b) noexcept(noexcept(std::swap(a.cont.keys, b.cont.keys)) &&noexcept(std::swap(a.cont.values, b.cont.values)) &&noexcept(std::swap(a.eq, b.eq)))
Definition FlatMap.h:328
std::pair< key_type, mapped_type > value_type
Definition FlatMap.h:57
FlatMap(const FlatMap &)=default
JPL_INLINE iterator find(const Key &key)
Definition FlatMap.h:143
FlatMap(FlatMap &&) noexcept=default
JPL_INLINE size_t erase(const K2 &key_like)
Definition FlatMap.h:283
JPL_INLINE void reserve(std::size_t n)
Definition FlatMap.h:113
MappedContainer mapped_container_type
Definition FlatMap.h:61
JPL_INLINE const_iterator cend() const noexcept
Definition FlatMap.h:123
JPL_INLINE iterator end() noexcept
Definition FlatMap.h:119
std::ptrdiff_t difference_type
Definition FlatMap.h:64
JPL_INLINE void push_back(const Key &k, const T &v)
Definition FlatMap.h:126
JPL_INLINE size_t erase(iterator it)
Definition FlatMap.h:263
JPL_INLINE bool contains(const K2 &key_like) const
Definition FlatMap.h:323
JPL_INLINE const mapped_container_type & values() const noexcept
Definition FlatMap.h:110
JPL_INLINE mapped_type & at(const Key &key)
Definition FlatMap.h:199
Equals key_compare
Definition FlatMap.h:58
FlatMap(Equals eq, const allocator_type &allocator={})
Definition FlatMap.h:86
JPL_INLINE size_type size() const
Definition FlatMap.h:114
JPL_INLINE mapped_type & operator[](Key &&key)
Definition FlatMap.h:226
std::pair< const key_type &, const mapped_type & > const_reference
Definition FlatMap.h:67
Definition ZipIterator.h:51
Definition AcousticMaterial.h:36
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Definition ChannelMap.h:268
Zip iterator for the FlatMap.
Definition ZipIterator.h:33