aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-08-11 14:33:04 -0400
committerFelix Morgner <felix.morgner@ost.ch>2026-08-11 14:33:04 -0400
commit99182a1e40d86fefd6b8866060dff42594d59f4f (patch)
tree7e213b1b3ee3e5851480667008f27f15262a014d /libs
parent26223cf25fa38804eca224df2f0cdaea28f6e1a1 (diff)
downloadkernel-99182a1e40d86fefd6b8866060dff42594d59f4f.tar.xz
kernel-99182a1e40d86fefd6b8866060dff42594d59f4f.zip
kstd: implement more flat_map ctors
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/kstd/flat_map.hpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/libs/kstd/kstd/flat_map.hpp b/libs/kstd/kstd/flat_map.hpp
index 2502b7e7..c0e302a8 100644
--- a/libs/kstd/kstd/flat_map.hpp
+++ b/libs/kstd/kstd/flat_map.hpp
@@ -18,6 +18,10 @@
namespace kstd
{
+ struct sorted_unique_t
+ {
+ } constexpr sorted_unique;
+
template<typename KeyType, typename MappedType, typename KeyCompare = std::less<KeyType>,
typename KeyContainerType = kstd::vector<KeyType>, typename MappedContainerType = kstd::vector<MappedType>>
struct flat_map
@@ -286,6 +290,83 @@ namespace kstd
do_unique();
}
+ //! Construct a new flat map using the supplied keys and values, using the given comparator and allocator.
+ //!
+ //! After copying the and mapped values, this constructor sorts the value pairs using the the comparator of this
+ //! flat map and then erases the duplicate keys.
+ //!
+ //! @note This constructor only participates in overload resolution if both the key and the mapped container use
+ //! allocator construction using the given allocator.
+ //!
+ //! @tparam Allocator The type of the allocator to use for the underlying containers.
+ //! @param keys The container containing the keys of the new flat map.
+ //! @param mapped The container containing the mapped values of the new flat map.
+ //! @param comparator The comparator to use for comparing keys.
+ //! @param allocator The allocator to use for the underlying containers.
+ template<typename Allocator>
+ requires(std::uses_allocator_v<key_container_type, Allocator> &&
+ std::uses_allocator_v<mapped_container_type, Allocator>)
+ constexpr flat_map(key_container_type const & keys, mapped_container_type const & mapped,
+ key_compare const & comparator, Allocator const & allocator)
+ : m_containers{std::make_obj_using_allocator<key_container_type>(allocator, keys),
+ std::make_obj_using_allocator<mapped_container_type>(allocator, mapped)}
+ , m_comparator{comparator}
+ {
+ do_sort();
+ do_unique();
+ }
+
+ //! Construct a new flat map using the supplied keys and values.
+ //!
+ //! This constructor assumes that the key-value pairs are sorted with respect to the comparator and all keys are
+ //! unique.
+ //!
+ //! @param keys The container containing the keys of the new flat map.
+ //! @param mapped The container containing the mapped values of the new flat map.
+ //! @param comparator The comparator to use for sorting the keys.
+ constexpr flat_map(sorted_unique_t, key_container_type keys, mapped_container_type mapped,
+ key_compare const & comparator = key_compare{})
+ : m_containers{std::move(keys), std::move(mapped)}
+ , m_comparator{comparator}
+ {}
+
+ //! Construct a new flat map using the supplied keys and values, using the given allocator.
+ //!
+ //! This constructor assumes that the key-value pairs are sorted with respect to the comparator and all keys are
+ //! unique.
+ //!
+ //! @param keys The container containing the keys of the new flat map.
+ //! @param mapped The container containing the mapped values of the new flat map.
+ //! @param allocator The allocator to use for the underlying containers.
+ template<typename Allocator>
+ requires(std::uses_allocator_v<key_container_type, Allocator> &&
+ std::uses_allocator_v<mapped_container_type, Allocator>)
+ constexpr flat_map(sorted_unique_t, key_container_type const & keys, mapped_container_type const & mapped,
+ Allocator const & allocator)
+ : m_containers{std::make_obj_using_allocator<key_container_type>(allocator, keys),
+ std::make_obj_using_allocator<mapped_container_type>(allocator, mapped)}
+ , m_comparator{}
+ {}
+
+ //! Construct a new flat map using the supplied keys and values, using the given comparator and allocator.
+ //!
+ //! This constructor assumes that the key-value pairs are sorted with respect to the comparator and all keys are
+ //! unique.
+ //!
+ //! @param keys The container containing the keys of the new flat map.
+ //! @param mapped The container containing the mapped values of the new flat map.
+ //! @param comparator The comparator to use for comparing keys.
+ //! @param allocator The allocator to use for the underlying containers.
+ template<typename Allocator>
+ requires(std::uses_allocator_v<key_container_type, Allocator> &&
+ std::uses_allocator_v<mapped_container_type, Allocator>)
+ constexpr flat_map(sorted_unique_t, key_container_type const & keys, mapped_container_type const & mapped,
+ key_compare const & comparator, Allocator const & allocator)
+ : m_containers{std::make_obj_using_allocator<key_container_type>(allocator, keys),
+ std::make_obj_using_allocator<mapped_container_type>(allocator, mapped)}
+ , m_comparator{comparator}
+ {}
+
//! Construct an empty flat map using the given custom comparator.
//!
//! @param comparator The comparator to use for comparing keys.