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 <unordered_map>
13
14namespace mdds {
15
16namespace ssmap {
17namespace detail {
18
19template<typename ValueT>
21{
22 std::string_view key;
23 ValueT value;
24};
25
26} // namespace detail
27
32template<typename ValueT>
33class linear_key_finder
34{
35 using value_type = ValueT;
36 using size_type = typename std::string_view::size_type;
37 using entry_type = detail::map_entry<ValueT>;
38
39 const entry_type* m_entries;
40 const entry_type* m_entries_end;
41
42public:
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
52template<typename ValueT>
53class hash_key_finder
54{
55 using value_type = ValueT;
56 using size_type = typename std::string_view::size_type;
57 using entry_type = detail::map_entry<ValueT>;
58 using keystore_type = std::unordered_map<
60
61 const entry_type* m_entries;
62 keystore_type m_keys;
63
64public:
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
88template<typename ValueT, template<typename> class KeyFinderT = ssmap::linear_key_finder>
90{
91 using func_find_key_type = KeyFinderT<ValueT>;
92
93public:
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
158private:
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: */
Definition cref_wrapper.hpp:20
const value_type & find(std::string_view input) const
std::string_view find_key(const value_type &v) const
sorted_string_map(const entry_type *entries, size_type entry_size, value_type null_value)
size_type size() const
ssmap::detail::map_entry< ValueT > entry_type
Definition sorted_string_map.hpp:101
const value_type & find(const char *input, size_type len) const
Definition sorted_string_map.hpp:34
Definition cref_wrapper.hpp:43
Definition sorted_string_map.hpp:21