OGLplus  (0.59.0) a C++ wrapper for rendering APIs

edit_distance.hpp
Go to the documentation of this file.
1 #ifndef EAGINE_EDIT_DISTANCE_HPP
9 #define EAGINE_EDIT_DISTANCE_HPP
10 
11 #include "flat_map.hpp"
12 #include "math/tvec.hpp"
13 #include "string_span.hpp"
14 
15 namespace eagine {
16 
17 class default_keyboard_layout {
18 public:
19  using coord_type = math::tvec<float, 3, true>;
20 
21  default_keyboard_layout(const float shift = 1.F);
22 
23  auto get_coord(char c) noexcept -> coord_type {
24  const auto pos = _key_coord.find(c);
25  if(pos != _key_coord.end()) {
26  return pos->second;
27  }
28  return {6.F, 3.F, 0.5F};
29  }
30 
31  template <typename Function>
32  auto for_each_char_coord(Function function) const noexcept {
33  for(const auto& [kchr, coord] : _key_coord) {
34  function(kchr, coord);
35  }
36  }
37 
38 protected:
39  void add_key_coord(char key, math::tvec<float, 3, true> coord) {
40  _key_coord[key] = coord;
41  }
42 
43 private:
44  flat_map<char, math::tvec<float, 3, true>> _key_coord;
45 };
46 //------------------------------------------------------------------------------
47 class keyboard_distance {
48 public:
49  template <typename Layout>
50  keyboard_distance(const Layout& layout, float multiplier = 1.F) {
51  layout.for_each_char_coord([&](char lc, auto lp) {
52  layout.for_each_char_coord([&](char rc, auto rp) {
53  if(lc != rc) {
54  _key_dist[{lc, rc}] = multiplier * math::distance(lp, rp);
55  }
56  });
57  });
58  }
59 
60  auto operator()(string_view ls, string_view rs) const -> float;
61 
62 private:
63  flat_map<std::pair<char, char>, float> _key_dist;
64 };
65 
66 } // namespace eagine
67 
68 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
69 #include <eagine/edit_distance.inl>
70 #endif
71 
72 #endif // EAGINE_EDIT_DISTANCE_HPP
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto distance(const vector< T, N, V > &a, const vector< T, N, V > &b) noexcept
Returns the distance between two vectors.
Definition: vector.hpp:388

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).