aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/alltest/fsa_create_test.cpp
blob: 0586f89cdb060eaa884e013cc3668bcd65acab03 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <iostream>

#include <vespa/fsa/fsa.h>
#include <vespa/fsa/automaton.h>
#include <vespa/fsa/timestamp.h>

using namespace fsa;

int main(int, char**)
{

  Automaton *aut = new Automaton;

  Blob fruit("Fruit"), veggie("Vegetable"), city("City");

  TimeStamp t;

  aut->init();

  aut->insertSortedString("Cupertino",city);
  aut->insertSortedString("Foster City",city);
  aut->insertSortedString("Los Altos",city);
  aut->insertSortedString("Menlo Park",city);
  aut->insertSortedString("Mountain View",city);
  aut->insertSortedString("Palo Alto",city);
  aut->insertSortedString("San Francisco",city);
  aut->insertSortedString("San Jose",city);
  aut->insertSortedString("Santa Clara",city);
  aut->insertSortedString("Saratoga",city);
  aut->insertSortedString("Sunnyvale",city);
  aut->insertSortedString("apple",fruit);
  aut->insertSortedString("apricot",fruit);
  aut->insertSortedString("artichoke",veggie);
  aut->insertSortedString("banana",fruit);
  aut->insertSortedString("cabbage",veggie);
  aut->insertSortedString("carrot",veggie);
  aut->insertSortedString("cherry",fruit);
  aut->insertSortedString("chili",veggie);
  aut->insertSortedString("cucumber",veggie);
  aut->insertSortedString("eggplant",veggie);
  aut->insertSortedString("grapes",fruit);
  aut->insertSortedString("lettuce",veggie);
  aut->insertSortedString("onion",veggie);
  aut->insertSortedString("paprika",veggie);
  aut->insertSortedString("passion fruit",fruit);
  aut->insertSortedString("pea",veggie);
  aut->insertSortedString("peach",fruit);
  aut->insertSortedString("pear",fruit);
  aut->insertSortedString("pineapple",fruit);
  aut->insertSortedString("plum",fruit);
  aut->insertSortedString("potato",veggie);
  aut->insertSortedString("pumpkin",veggie);
  aut->insertSortedString("sour cherry",fruit);
  aut->insertSortedString("squash",veggie);
  aut->insertSortedString("tomato",veggie);

  aut->finalize();

  double d1 = t.elapsed();

  aut->addPerfectHash();

  double d2 = t.elapsed();

  aut->write("__testfsa__.__fsa__");

  double d3 = t.elapsed();

  FSA *fsa = aut->getFSA();

  double d4 = t.elapsed();

  std::cout << "Automoaton build finished (" << 1000*d1 << "ms," << 1000*(d2-d1) << "ms)"
            << ", fsa retrieval (" << 1000*(d4-d3) << "ms) " << ((fsa==NULL)?"failed":"succeded") << ".\n";

  if(fsa!=NULL){
    FSA::State fs(*fsa);
    const unsigned char *pb = fs.lookup("cucumber");
    std::cout << "Lookup(\"cucumber\") -> ";
    if(pb!=NULL){
      std::cout << "\"" << pb << "\"";
    }
    else{
      std::cout << "not found.";
    }
    std::cout << "\n";
  }

  delete aut;
  delete fsa;

  return 0;
}