mdds
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 <unordered_map>
13 
14 namespace mdds {
15 
16 namespace ssmap {
17 namespace detail {
18 
19 template<typename ValueT>
20 struct map_entry
21 {
22  std::string_view key;
23  ValueT value;
24 };
25 
26 } // namespace detail
27 
32 template<typename ValueT>
34 {
35  using value_type = ValueT;
36  using size_type = typename std::string_view::size_type;
38 
39  const entry_type* m_entries;
40  const entry_type* m_entries_end;
41 
42 public:
43  linear_key_finder(const entry_type* entries, const entry_type* entries_end) noexcept;
44 
45  std::string_view operator()(const value_type& v) const;
46 };
47 
52 template<typename ValueT>
54 {
55  using value_type = ValueT;
56  using size_type = typename std::string_view::size_type;
58  using keystore_type = std::unordered_map<
60 
61  const entry_type* m_entries;
62  keystore_type m_keys;
63 
64 public:
65  hash_key_finder(const entry_type* entries, const entry_type* entries_end);
66 
67  std::string_view operator()(const value_type& v) const;
68 };
69 
70 } // namespace ssmap
71 
88 template<typename ValueT, template<typename> class KeyFinderT = ssmap::linear_key_finder>
90 {
91  using func_find_key_type = KeyFinderT<ValueT>;
92 
93 public:
94  using value_type = ValueT;
95  using size_type = typename std::string_view::size_type;
96 
102 
111  sorted_string_map(const entry_type* entries, size_type entry_size, value_type null_value);
112 
123  const value_type& find(const char* input, size_type len) const;
124 
133  const value_type& find(std::string_view input) const;
134 
147  std::string_view find_key(const value_type& v) const;
148 
156  size_type size() const;
157 
158 private:
159  const entry_type* m_entries;
160  const value_type m_null_value;
161  const size_type m_entry_size;
162  const entry_type* m_entries_end;
163 
164  func_find_key_type m_func_find_key;
165 };
166 
167 } // namespace mdds
168 
169 #include "sorted_string_map_def.inl"
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const value_type & find(const char *input, size_type len) const
Definition: cref_wrapper.hpp:19
size_type size() const
Definition: sorted_string_map.hpp:33
Definition: sorted_string_map.hpp:89
Definition: sorted_string_map.hpp:53
sorted_string_map(const entry_type *entries, size_type entry_size, value_type null_value)
Definition: cref_wrapper.hpp:42
std::string_view find_key(const value_type &v) const
Definition: cref_wrapper.hpp:12
Definition: sorted_string_map.hpp:20