26 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT) 27 #error "Only <final/final.h> can be included directly." 33 #include <unordered_map> 36 #include "final/input/fkey_map.h" 49 static constexpr
auto getMaxHashSize() noexcept -> std::size_t
51 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a::value_type)) 52 constexpr std::size_t key_map_size = ARRAY_SIZE(FKeyMap::KeyMapType);
53 constexpr std::size_t keycap_map_size = ARRAY_SIZE(FKeyMap::KeyCapMapType);
55 return std::max(key_map_size, keycap_map_size) * 2;
60 template <
typename BufferT>
68 constexpr
explicit KeySequence (
const BufferT& buf)
72 const char*
string{
nullptr};
73 const uInt8 length{0};
74 const BufferT* buffer{
nullptr};
78 template <
typename IterT>
79 constexpr
auto hash_function (IterT iter,
const IterT end) -> std::size_t
82 constexpr std::size_t FNV_OFFSET_BASIS = \
83 ( std::numeric_limits<std::size_t>::digits == 64 )
84 ? 14695981039346656037ULL
87 constexpr std::size_t FNV_PRIME = \
88 ( std::numeric_limits<unsigned int>::digits == 64 )
92 std::size_t hash = FNV_OFFSET_BASIS;
95 , [&hash] (
auto item) noexcept
97 hash ^=
static_cast<std::size_t
>(item);
101 return hash & (Const::getMaxHashSize() - 1);
105 template <
typename BufferT>
106 constexpr
auto hash_function (
const BufferT& buf) -> std::size_t
108 return hash_function (std::begin(buf), std::end(buf));
112 template <
typename BufferT>
117 if ( key.string && ! key.buffer )
118 return hash_function (key.string, std::next(key.string, key.length));
121 return hash_function (*key.buffer);
123 static constexpr
char unknown_key[] =
"unknown";
124 return hash_function(unknown_key, std::next(unknown_key,
sizeof(unknown_key) - 1));
129 template <
typename BufferT>
135 if ( lhs.string && ! lhs.buffer && rhs.string && ! rhs.buffer )
137 return lhs.length == rhs.length
138 && std::memcmp(lhs.string, rhs.string, rhs.length) == 0;
141 if ( ! lhs.string && lhs.buffer && rhs.string && ! rhs.buffer )
143 return lhs.buffer->getSize() == rhs.length
144 && lhs.buffer->strncmp_front(rhs.string, rhs.length);
147 if ( lhs.string && ! lhs.buffer && ! rhs.string && rhs.buffer )
149 return lhs.length == rhs.buffer->getSize()
150 && rhs.buffer->strncmp_front(lhs.string, lhs.length);
159 template <
typename BufferT>
160 using HashMap = std::unordered_map<KeySequence<BufferT>
166 template <
typename BufferT>
167 auto createKeyCapMap() -> HashMap<BufferT>
169 const auto& fkey_cap_table = FKeyMap::getKeyCapMap();
170 HashMap<BufferT> fkey_cap_map;
173 fkey_cap_map.reserve((fkey_cap_table.size() * 5) / 4);
175 for (
const auto& item : fkey_cap_table)
176 if ( item.string && item.length != 0 )
177 fkey_cap_map[{item.string, item.length}] = item.num;
183 template <
typename BufferT,
typename IterT>
184 auto createKeyCapMap (IterT begin, IterT end) -> HashMap<BufferT>
186 HashMap<BufferT> fkey_cap_map;
187 fkey_cap_map.reserve(std::size_t(std::distance(begin, end)));
189 std::for_each ( begin
191 , [&fkey_cap_map] (
const auto& item)
193 if ( item.string && item.length != 0 )
194 fkey_cap_map[{item.string, item.length}] = item.num;
201 template <
typename BufferT>
202 auto createKeyMap() -> HashMap<BufferT>
204 auto& fkey_table = FKeyMap::getKeyMap();
205 HashMap<BufferT> fkey_map;
208 fkey_map.reserve((fkey_table.size() * 5) / 4);
210 for (
auto& item : fkey_table)
211 if ( item.length != 0 )
212 fkey_map[{item.string.data(), item.length}] = item.num;
220 template <
typename BufferT>
221 auto getKeyCapMap() -> internal::HashMap<BufferT>&
223 using HashMapType = internal::HashMap<BufferT>;
224 static const auto& fkey_cap_map = std::make_unique<HashMapType>(internal::createKeyCapMap<BufferT>());
225 return *fkey_cap_map;
229 template <
typename BufferT,
typename IterT>
230 void setKeyCapMap (IterT begin, IterT end)
232 getKeyCapMap<BufferT>() = internal::createKeyCapMap<BufferT>(begin, end);
236 template <
typename BufferT>
237 auto getKeyMap() -> internal::HashMap<BufferT>&
239 using HashMapType = internal::HashMap<BufferT>;
240 static const auto& fkey_map = std::make_unique<HashMapType>(internal::createKeyMap<BufferT>());
245 template <
typename BufferT>
246 auto getTermcapKey (
const BufferT& char_rbuf) -> FKey
248 auto& hashmap = getKeyCapMap<BufferT>();
251 if ( iter != hashmap.end() )
258 template <
typename BufferT>
259 auto getKnownKey (
const BufferT& char_rbuf) -> FKey
261 auto& hashmap = getKeyMap<BufferT>();
264 if ( iter != hashmap.end() )
274 #endif // FKEYHASHMAP_H Definition: fkey_hashmap.h:61
Definition: fkey_hashmap.h:130
Definition: class_template.cpp:25
Definition: fkey_hashmap.h:113
Definition: fkey_hashmap.h:47