Euphoria
editdistance.search.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <queue>
5 
6 #include <algorithm>
7 
8 #include "base/editdistance.h"
9 
10 
11 namespace eu::search
12 {
13  struct Match
14  {
15  std::string name;
16  unsigned long changes;
17 
18  Match(const std::string& str, const std::string& input)
19  : name(str)
20  , changes(calc_edit_distance(str, input))
21  {
22  }
23 
24  bool
25  operator<(const Match& rhs) const
26  {
27  return changes < rhs.changes;
28  }
29  };
30 
31  template<typename T>
32  struct Searcher
33  {
34  std::priority_queue<T> matches;
35 
36  void add(const T& t, std::size_t max_size)
37  {
38  matches.push(t);
39  if(matches.size() > max_size)
40  {
41  matches.pop();
42  }
43  }
44  };
45 
46  template<typename T, typename F, typename L>
47  std::vector<T>
48  find_closest(std::size_t max_size, const L& list, F&& f)
49  {
50  search::Searcher<T> searcher;
51  for(auto entry: list)
52  {
53  searcher.add(f(entry), max_size);
54  }
55  std::vector<T> ret;
56 
57  while(!searcher.matches.empty())
58  {
59  ret.emplace_back(std::move(searcher.matches.top()));
60  searcher.matches.pop();
61  }
62 
63  std::reverse(ret.begin(), ret.end());
64 
65  return ret;
66  }
67 }
std::vector< T > find_closest(std::size_t max_size, const L &list, F &&f)
int calc_edit_distance(const std::string &source, const std::string &target)
Definition: editdistance.cc:14
Match(const std::string &str, const std::string &input)
bool operator<(const Match &rhs) const
std::priority_queue< T > matches
void add(const T &t, std::size_t max_size)