diff options
| -rw-r--r-- | libs/kstd/kstd/flat_map.hpp | 81 |
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. |
