mdds
sorted_string_map.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * Copyright (c) 2022 Kohei Yoshida
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  ************************************************************************/
28 
29 #pragma once
30 
31 #include "./cref_wrapper.hpp"
32 
33 #include <string_view>
34 #include <unordered_map>
35 
36 namespace mdds {
37 
38 namespace ssmap {
39 namespace detail {
40 
41 template<typename ValueT>
42 struct map_entry
43 {
44  std::string_view key;
45  ValueT value;
46 };
47 
48 } // namespace detail
49 
54 template<typename ValueT>
56 {
57  using value_type = ValueT;
58  using size_type = typename std::string_view::size_type;
60 
61  const entry_type* m_entries;
62  const entry_type* m_entries_end;
63 
64 public:
65  linear_key_finder(const entry_type* entries, const entry_type* entries_end);
66 
67  std::string_view operator()(const value_type& v) const;
68 };
69 
74 template<typename ValueT>
76 {
77  using value_type = ValueT;
78  using size_type = typename std::string_view::size_type;
80  using keystore_type = std::unordered_map<
82 
83  const entry_type* m_entries;
84  keystore_type m_keys;
85 
86 public:
87  hash_key_finder(const entry_type* entries, const entry_type* entries_end);
88 
89  std::string_view operator()(const value_type& v) const;
90 };
91 
92 } // namespace ssmap
93 
110 template<typename ValueT, template<typename> class KeyFinderT = ssmap::linear_key_finder>
112 {
113  using func_find_key_type = KeyFinderT<ValueT>;
114 
115 public:
116  using value_type = ValueT;
117  using size_type = typename std::string_view::size_type;
118 
124 
133  sorted_string_map(const entry_type* entries, size_type entry_size, value_type null_value);
134 
145  const value_type& find(const char* input, size_type len) const;
146 
155  const value_type& find(std::string_view input) const;
156 
169  std::string_view find_key(const value_type& v) const;
170 
178  size_type size() const;
179 
180 private:
181  const entry_type* m_entries;
182  const value_type m_null_value;
183  const size_type m_entry_size;
184  const entry_type* m_entries_end;
185 
186  func_find_key_type m_func_find_key;
187 };
188 
189 } // namespace mdds
190 
191 #include "sorted_string_map_def.inl"
192 
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const value_type & find(const char *input, size_type len) const
Definition: cref_wrapper.hpp:41
size_type size() const
Definition: sorted_string_map.hpp:55
Definition: sorted_string_map.hpp:111
Definition: sorted_string_map.hpp:75
sorted_string_map(const entry_type *entries, size_type entry_size, value_type null_value)
Definition: cref_wrapper.hpp:64
std::string_view find_key(const value_type &v) const
Definition: cref_wrapper.hpp:34
Definition: sorted_string_map.hpp:42