mdds
Loading...
Searching...
No Matches
sorted_string_map.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3// SPDX-FileCopyrightText: 2022 - 2025 Kohei Yoshida
4//
5// SPDX-License-Identifier: MIT
6
7#pragma once
8
9#include "./cref_wrapper.hpp"
10
11#include <string_view>
12#include <span>
13#include <unordered_map>
14
15namespace mdds {
16
17namespace ssmap {
18namespace detail {
19
20template<typename ValueT>
22{
23 std::string_view key;
24 ValueT value;
25};
26
27} // namespace detail
28
33template<typename ValueT>
34class linear_key_finder
35{
36 using value_type = ValueT;
37 using size_type = typename std::string_view::size_type;
38 using entry_type = detail::map_entry<ValueT>;
39
40 const entry_type* m_entries;
41 const entry_type* m_entries_end;
42
43public:
44 constexpr linear_key_finder(const entry_type* entries, const entry_type* entries_end) noexcept;
45
46 constexpr std::string_view operator()(const value_type& v) const;
47};
48
53template<typename ValueT>
54class hash_key_finder
55{
56 using value_type = ValueT;
57 using size_type = typename std::string_view::size_type;
58 using entry_type = detail::map_entry<ValueT>;
59 using keystore_type = std::unordered_map<
61
62 const entry_type* m_entries;
63 keystore_type m_keys;
64
65public:
66 hash_key_finder(const entry_type* entries, const entry_type* entries_end);
67
68 std::string_view operator()(const value_type& v) const;
69};
70
71} // namespace ssmap
72
89template<typename ValueT, template<typename> class KeyFinderT = ssmap::linear_key_finder>
91{
92 using func_find_key_type = KeyFinderT<ValueT>;
93
94public:
95 using value_type = ValueT;
96 using size_type = typename std::string_view::size_type;
97
103
111 constexpr sorted_string_map(std::span<const entry_type> entries, value_type null_value);
112
121 [[deprecated("use the constructor taking std::span<const entry_type> instead")]] constexpr sorted_string_map(
122 const entry_type* entries, size_type entry_size, value_type null_value);
123
134 [[deprecated("use the overload taking std::string_view instead")]] constexpr const value_type& find(
135 const char* input, size_type len) const;
136
145 constexpr const value_type& find(std::string_view input) const;
146
159 constexpr std::string_view find_key(const value_type& v) const;
160
168 constexpr size_type size() const;
169
170private:
171 const entry_type* m_entries;
172 const value_type m_null_value;
173 const size_type m_entry_size;
174 const entry_type* m_entries_end;
175
176 func_find_key_type m_func_find_key;
177};
178
179} // namespace mdds
180
181#include "sorted_string_map_def.inl"
182
183/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition cref_wrapper.hpp:20
constexpr std::string_view find_key(const value_type &v) const
constexpr const value_type & find(std::string_view input) const
constexpr const value_type & find(const char *input, size_type len) const
ssmap::detail::map_entry< ValueT > entry_type
Definition sorted_string_map.hpp:102
constexpr size_type size() const
constexpr sorted_string_map(const entry_type *entries, size_type entry_size, value_type null_value)
constexpr sorted_string_map(std::span< const entry_type > entries, value_type null_value)
Definition sorted_string_map.hpp:35
Definition cref_wrapper.hpp:43
Definition sorted_string_map.hpp:22