From 99182a1e40d86fefd6b8866060dff42594d59f4f Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 11 Aug 2026 14:33:04 -0400 Subject: kstd: implement more flat_map ctors --- libs/kstd/kstd/flat_map.hpp | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'libs') 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 KeyContainerType = kstd::vector, typename MappedContainerType = kstd::vector> 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 + requires(std::uses_allocator_v && + std::uses_allocator_v) + 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(allocator, keys), + std::make_obj_using_allocator(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 + requires(std::uses_allocator_v && + std::uses_allocator_v) + 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(allocator, keys), + std::make_obj_using_allocator(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 + requires(std::uses_allocator_v && + std::uses_allocator_v) + 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(allocator, keys), + std::make_obj_using_allocator(allocator, mapped)} + , m_comparator{comparator} + {} + //! Construct an empty flat map using the given custom comparator. //! //! @param comparator The comparator to use for comparing keys. -- cgit v1.2.3