about summary refs log tree commit diff stats
path: root/prefix_search.cpp
blob: 2603061ff1cfdad605504e03edbec7de40213b4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "prefix_search.h"

void prefix_search::add(std::string prefix)
{
  node* cur = ⊤
  for (int c : prefix)
  {
    cur = &cur->children[c];
  }
  
  cur->match = true;
}

int prefix_search::match(std::string in) const
{
  int ret = 0;
  const node* cur = ⊤
  for (int c : in)
  {
    if (cur->children.count(c) == 0)
    {
      return 0;
    }
    
    cur = &cur->children.at(c);
    ret++;
    
    if (cur->match)
    {
      return ret;
    }
  }
  
  return 0;
}