65template <std::
size_t N,
typename K,
typename V>
class hash_map
83 template <
typename... E>
84 explicit constexpr hash_map(E &&...elements) noexcept
85 : data{std::forward<E>(elements)...}
87 static_assert(N > 0,
"N should be positive");
88 static_assert(N ==
sizeof...(elements),
89 "Elements size doesn't match expected size of a hash-map");
96 find(
const K &key)
const noexcept
98 return search<0, N>(key);
108 [[nodiscard]]
constexpr std::pair<bool, const V &>
109 at(
const K &key)
const noexcept
111 const auto it =
find(key);
114 return {
true, it->second};
126 [[nodiscard]]
constexpr const V &
129 return find(key)->second;
135 [[nodiscard]]
constexpr bool
138 return search<0, N>(key) !=
cend();
167 return std::cbegin(data);
187 return std::cend(data);
194 [[nodiscard]]
constexpr bool
202 [[nodiscard]]
constexpr std::array<K, N>
205 return keys_impl(std::make_index_sequence<N>{});
214 template <index_type L, index_type R>
216 search(
const K &key)
const noexcept
218 if constexpr (L < R) {
219 if (equal(data[L].first, key)) {
220 return std::next(
cbegin(), L);
223 return search<L + 1, R>(key);
230 template <
typename T = K>
231 [[nodiscard]]
constexpr bool
232 equal(
const T &lhs,
const T &rhs)
const noexcept
239 [[nodiscard]]
constexpr bool
240 equal(
const char *lhs,
const char *rhs)
const noexcept
243 return *lhs == *rhs && (*lhs ==
'\0' || equal(lhs + 1, rhs + 1));
246 template <
const auto... KeyIdx>
247 [[nodiscard]]
constexpr std::array<K, N>
250 return {data[KeyIdx].first...};
constexpr hash_map(E &&...elements) noexcept
The only construction that might be used, all keys and values must be provided in the constructor.
constexpr const_iterator end() const noexcept
Gives constant iterator to an end, needed for the C++11 for-each cycle or the std::for_each.
constexpr std::array< K, N > keys_impl(std::index_sequence< KeyIdx... >) const
constexpr size_type size() const noexcept
Retrieves size of a hash-map, might also be called indirectly using the std::size(....
constexpr const_iterator cend() const noexcept
Gives constant iterator to a beginning, might be also called using std::cend(...).
constexpr std::pair< bool, const V & > at(const K &key) const noexcept
Searches for a given key, aimed to return associated value with it.
std::array< std::pair< K, V >, N > data_type
underlying structure used for the actual implementation
constexpr const_iterator begin() const noexcept
Gives constant iterator to a beginning, needed for the C++11 for-each cycle or the std::for_each.
constexpr const_iterator cbegin() const noexcept
Gives constant iterator to a beginning, might be also called using std::cbegin(......
constexpr bool empty() const noexcept
Hash-map cannot be empty; this might also be called using std::empty(...).
constexpr const_iterator find(const K &key) const noexcept
Searches map for a given key and returns iterator.
constexpr std::array< K, N > keys() const noexcept
Returns all keys in the hash-map.
typename data_type::const_iterator const_iterator
constexpr const V & operator[](const K &key) const noexcept
Retrieves reference to constant to a value. Doesn't perform any bounds checking, behaviour is undefin...
constexpr bool contains(const K &key) const noexcept
Checks if element with given key exists.