aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/kstd/flat_map.hpp113
-rw-r--r--libs/kstd/kstd/flat_map.tests.cpp2
2 files changed, 110 insertions, 5 deletions
diff --git a/libs/kstd/kstd/flat_map.hpp b/libs/kstd/kstd/flat_map.hpp
index c0e302a8..af269c6a 100644
--- a/libs/kstd/kstd/flat_map.hpp
+++ b/libs/kstd/kstd/flat_map.hpp
@@ -65,10 +65,17 @@ namespace kstd
//! Compare two object of type value_type.
struct value_compare
{
+ explicit constexpr value_compare(key_compare comparator)
+ : m_comparator{comparator}
+ {}
+
constexpr auto operator()(const_reference lhs, const_reference rhs) const -> bool
{
- return lhs.first < rhs.first;
+ return m_comparator(lhs.first, rhs.first);
}
+
+ private:
+ key_compare m_comparator;
};
template<bool IsConst>
@@ -607,6 +614,23 @@ namespace kstd
};
}
+ //! Swap the contents of this flat map with the one of a different one.
+ //!
+ //! @param other The flat map instance to swap with.
+ constexpr auto swap(flat_map & other) noexcept -> void
+ {
+ std::ranges::swap(m_comparator, other.m_comparator);
+ std::ranges::swap(m_containers.keys, other.m_containers.keys);
+ std::ranges::swap(m_containers.values, other.m_containers.values);
+ }
+
+ //! Clear this flat map of all its contents.
+ constexpr auto clear() noexcept -> void
+ {
+ m_containers.keys.clear();
+ m_containers.values.clear();
+ }
+
//! Find an element with an equivalent key.
//!
//! @param key The key to look up.
@@ -642,7 +666,7 @@ namespace kstd
//! @param x The key to look up.
//! @return An iterator to the element with the equivalent key, or end() if no such element is found.
template<typename K>
- requires requires(K const & k, key_type const & l) { typename key_compare::is_transparent; }
+ requires requires { typename key_compare::is_transparent; }
[[nodiscard]] auto find(K const & x) noexcept -> iterator
{
auto found = std::ranges::lower_bound(m_containers.keys, x, m_comparator);
@@ -659,7 +683,7 @@ namespace kstd
//! @param x The key to look up.
//! @return An iterator to the element with the equivalent key, or end() if no such element is found.
template<typename K>
- requires requires(K const & k, key_type const & l) { typename key_compare::is_transparent; }
+ requires requires { typename key_compare::is_transparent; }
[[nodiscard]] auto find(K const & x) const noexcept -> const_iterator
{
auto found = std::ranges::lower_bound(m_containers.keys, x, m_comparator);
@@ -671,6 +695,35 @@ namespace kstd
return cend();
}
+ //! Count the number of occurrences of a given key in this map.
+ //!
+ //! @param key The key to look for.
+ //! @return 0 if the key does not exist, 1 otherwise.
+ [[nodiscard]] constexpr auto count(key_type const & key) const noexcept -> std::size_t
+ {
+ if (contains(key))
+ {
+ return 1;
+ }
+ return 0;
+ }
+
+ //! Count the number of occurrences of keys equivalent to a given key in this flat map.
+ //!
+ //! @tparam K The type of the key to find equivalents for.
+ //! @param key The key to look for.
+ //! @return 0 if the key does not exist, 1 otherwise.
+ template<typename K>
+ requires requires { typename key_compare::is_transparent; }
+ [[nodiscard]] constexpr auto count(key_type const & key) const noexcept -> std::size_t
+ {
+ if (contains(key))
+ {
+ return 1;
+ }
+ return 0;
+ }
+
//! Check if the map contains the given key.
//!
//! @param key The key to check.
@@ -680,6 +733,36 @@ namespace kstd
return find(key) != cend();
}
+ //! Check if the map contains a key equivalent to the given one.
+ //!
+ //! @tparam K The type of the key to find equivalents for.
+ //! @param key The key to check.
+ //! @return true iff. the key is found, false otherwise.
+ template<typename K>
+ requires requires { typename key_compare::is_transparent; }
+ [[nodiscard]] constexpr auto contains(K const & key) const noexcept -> bool
+ {
+ return find(key) != cend();
+ }
+
+ //! Get the key comparator of this flat map.
+ //!
+ //! @return The key comparator of this flat map.
+ [[nodiscard]] constexpr auto key_comp() const noexcept(std::is_nothrow_copy_constructible_v<key_compare>)
+ -> key_compare
+ {
+ return m_comparator;
+ }
+
+ //! Get a comparator that compares values by their key.
+ //!
+ //! @return The key comparator of this flat map.
+ [[nodiscard]] constexpr auto value_comp() const noexcept(std::is_nothrow_copy_constructible_v<value_compare>)
+ -> value_compare
+ {
+ return value_compare{m_comparator};
+ }
+
//! Get a reference to the keys container.
//!
//! @return a reference to the keys container.
@@ -696,6 +779,28 @@ namespace kstd
return m_containers.values;
}
+ //! Compare two flat maps and check if their contents is equal.
+ //!
+ //! @return @p true iff. the contents of both objects compares equal, @p false otherwise.
+ constexpr auto friend operator==(flat_map const & lhs, flat_map const & rhs) noexcept -> bool
+ {
+ return lhs.m_containers.keys == rhs.m_containers.keys && lhs.m_containers.values == rhs.m_containers.values;
+ }
+
+ //! Lexicographically compare the contents of two flat maps.
+ //!
+ //! @return whether the contents of @p lhs compares less-than, equivalent, or greater than the one of @p rhs.
+ constexpr auto friend operator<=>(flat_map const & lhs, flat_map const & rhs) noexcept
+ {
+ return std::lexicographical_compare_three_way(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend());
+ }
+
+ //! Swap the contents of the two flat maps.
+ constexpr auto friend swap(flat_map & lhs, flat_map & rhs) noexcept -> void
+ {
+ lhs.swap(rhs);
+ }
+
private:
struct key_equivalent
{
@@ -715,7 +820,7 @@ namespace kstd
constexpr auto do_sort() -> void
{
auto zipped = std::views::zip(m_containers.keys, m_containers.values);
- std::ranges::sort(zipped, value_compare{});
+ std::ranges::sort(zipped, value_compare{m_comparator});
}
constexpr auto do_unique() -> void
diff --git a/libs/kstd/kstd/flat_map.tests.cpp b/libs/kstd/kstd/flat_map.tests.cpp
index 22f4c570..42c60de5 100644
--- a/libs/kstd/kstd/flat_map.tests.cpp
+++ b/libs/kstd/kstd/flat_map.tests.cpp
@@ -48,7 +48,7 @@ SCENARIO("Flat Map initialization and construction", "[flat_map]")
THEN("the map compare equal")
{
- REQUIRE(std::ranges::equal(map, copy));
+ REQUIRE(map == copy);
}
}
}