diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-06-03 05:51:34 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-06-03 05:51:34 +0200 |
| commit | 0a772bdb97ea7eccc7bdf079e7bc09e5f27c1718 (patch) | |
| tree | 807d165276a941f1255a3cdc006493b066fd7e2e | |
| parent | 3274bb4377b9f04b7a70139a86283e0fae44b228 (diff) | |
| download | kernel-0a772bdb97ea7eccc7bdf079e7bc09e5f27c1718.tar.xz kernel-0a772bdb97ea7eccc7bdf079e7bc09e5f27c1718.zip | |
kstd: implement flat_map copy assignmentHEADdevelop-BA-FS26
| -rw-r--r-- | libs/kstd/kstd/flat_map | 25 | ||||
| -rw-r--r-- | libs/kstd/kstd/flat_map.test.cpp | 27 |
2 files changed, 52 insertions, 0 deletions
diff --git a/libs/kstd/kstd/flat_map b/libs/kstd/kstd/flat_map index 943e9fc..e51eb91 100644 --- a/libs/kstd/kstd/flat_map +++ b/libs/kstd/kstd/flat_map @@ -19,12 +19,24 @@ namespace kstd typename KeyContainerType = kstd::vector<KeyType>, typename MappedContainerType = kstd::vector<MappedType>> struct flat_map { + //! The type of container used to store the map keys. using key_container_type = KeyContainerType; + + //! The type of container used to store the map values. using mapped_container_type = MappedContainerType; + + //! The type of the map keys. using key_type = KeyType; + + //! The type of the mappe values. using mapped_type = MappedType; + + //! The type of a single key-value value in the map. using value_type = std::pair<key_type, mapped_type>; + + //! The comparator used to sort the keys. using key_compare = KeyCompare; + using reference = std::pair<key_type const &, mapped_type &>; using const_reference = std::pair<key_type const &, mapped_type const &>; using size_type = std::size_t; @@ -64,6 +76,19 @@ namespace kstd , m_comparator{comparator} {} + //! Replace the contents of this flat map with the one of a different one. + //! + //! @param other the flat map to copy from. + //! @return A reference to this flat map. + auto operator=(flat_map const & other) -> flat_map & + { + if (this != &other) + { + std::tie(m_containers, m_comparator) = std::tie(other.m_containers, other.m_comparator); + } + return *this; + } + //! Get a reference to the mapped value associated with the given key. //! //! @warning This function will panic if the key is not found. diff --git a/libs/kstd/kstd/flat_map.test.cpp b/libs/kstd/kstd/flat_map.test.cpp index 6c57173..9df03bb 100644 --- a/libs/kstd/kstd/flat_map.test.cpp +++ b/libs/kstd/kstd/flat_map.test.cpp @@ -62,6 +62,33 @@ SCENARIO("Flat Map modifiers", "[flat_map]") REQUIRE(map.contains(1)); } } + + AND_GIVEN("a populated Flat Map") + { + auto other = kstd::flat_map<int, int>{}; + other.emplace(1, 10); + other.emplace(2, 20); + other.emplace(3, 30); + + WHEN("assigning the populated Flat Map to the empty one") + { + map = other; + + THEN("the elements are copied") + { + REQUIRE(map.at(1) == 10); + REQUIRE(map.at(2) == 20); + REQUIRE(map.at(3) == 30); + } + + THEN("the elements are still in the populated Flat Map") + { + REQUIRE(other.at(1) == 10); + REQUIRE(other.at(2) == 20); + REQUIRE(other.at(3) == 30); + } + } + } } } |
