Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What about a constructor from an existing std::array of key value pairs? #14

Open
gotnone opened this issue Oct 18, 2022 · 1 comment
Open

Comments

@gotnone
Copy link

gotnone commented Oct 18, 2022

What do you think about adding an additional constructor for the case where one already has the data array created?

diff --git a/include/constexpr_hash_map/constexpr_hash_map.hpp b/include/constexpr_hash_map/constexpr_hash_map.hpp
index c1b9d50..68d740b 100644
--- a/include/constexpr_hash_map/constexpr_hash_map.hpp
+++ b/include/constexpr_hash_map/constexpr_hash_map.hpp
@@ -42,6 +42,15 @@ public:
         static_assert(N == sizeof...(elements), "Elements size doesn't match expected size of a hash-map");
     }
 
+    /// @brief The only other construction that might be used, array must be provided in the constructor.
+    /// @param std::array<std::pair<K,V>, N>, cannot be empty
+    explicit constexpr hash_map(data_type arr) noexcept
+    : data{std::move(arr)}
+    {
+        static_assert(N > 0, "N should be positive");
+        static_assert(N == data.size(), "Array size doesn't match expected size of a hash-map");
+    }
+
     /// @brief Searches map for a given key and returns iterator.
     /// @param key key to be searched for
     /// @return constant iterator to an element (cend, if not found)

Someone with more experience template programming may find a way to deduce the N, K and V parameters from the std::array itself.

@gotnone
Copy link
Author

gotnone commented Oct 18, 2022

This version fixes an issue in the static assert and provides a deduction guide.

diff --git a/include/constexpr_hash_map/constexpr_hash_map.hpp b/include/constexpr_hash_map/constexpr_hash_map.hpp
index c1b9d50..2a01401 100644
--- a/include/constexpr_hash_map/constexpr_hash_map.hpp
+++ b/include/constexpr_hash_map/constexpr_hash_map.hpp
@@ -42,6 +42,14 @@ public:
         static_assert(N == sizeof...(elements), "Elements size doesn't match expected size of a hash-map");
     }
 
+    /// @brief The only other construction that might be used, array must be provided in the constructor.
+    /// @param std::array<std::pair<K,V>, N>, cannot be empty
+    explicit constexpr hash_map(data_type arr) noexcept
+    : data{std::move(arr)}
+    {
+        static_assert(N > 0, "N should be positive");
+    }
+
     /// @brief Searches map for a given key and returns iterator.
     /// @param key key to be searched for
     /// @return constant iterator to an element (cend, if not found)
@@ -170,6 +178,9 @@ protected:
 private:
     data_type data;
 };
+
+template <std::size_t N, typename K, typename V>
+hash_map(std::array<std::pair<K, V>, N>) -> hash_map<N, K, V>;
 }  // namespace burda::ct
 
 #endif // CONSTEXPR_HASH_MAP_CONSTEXPR_HASH_MAP_HPP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant