liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
state.cpp
Go to the documentation of this file.
3
4namespace liblevenshtein {
5
6State::State(std::initializer_list<Position *> positions) {
7 Position *curr = nullptr;
8 for (Position *next : positions) {
9 insert_after(curr, next);
10 curr = next;
11 }
12}
13
14State::State(std::vector<Position *> &positions) {
15 Position *curr = nullptr;
16 for (Position *next : positions) {
17 insert_after(curr, next);
18 curr = next;
19 }
20}
21
23 delete _head;
24}
25
26void State::head(Position* head) {
27 head->next(_head);
28 _head = head;
29}
30
32 return _head;
33}
34
35void State::add(Position* next) {
36 if (_head == nullptr) {
37 _head = next;
38 }
39 else {
41 while (curr->next() != nullptr) {
42 curr = curr->next();
43 }
44 curr->next(next);
45 }
46}
47
49 if (curr != nullptr) {
50 next->next(curr->next());
51 curr->next(next);
52 }
53 else {
54 add(next);
55 }
56}
57
59 // ASSUMPTION: prev->next() == curr
61 if (prev != nullptr) {
62 temp = prev->next();
63 prev->next(curr->next());
64 }
65 else {
66 temp = _head;
67 _head = _head->next();
68 }
69 temp->next(nullptr);
70 delete temp;
71}
72
74 -> Position * {
75 if (lower == nullptr || lower->next() == nullptr) {
76 return lower;
77 }
78
79 Position* middle = find_middle(lower);
81 middle->next(nullptr);
82
83 return merge(compare,
84 merge_sort(compare, lower),
85 merge_sort(compare, upper));
86}
87
89 -> Position * {
90 Position temp(-1, -1);
91 Position* next = &temp;
92 Position* curr = next;
93
94 while (lower != nullptr && upper != nullptr) {
95 if (compare(lower, upper) < 1) {
96 curr->next(lower);
97 lower = lower->next();
98 }
99 else {
100 curr->next(upper);
101 upper = upper->next();
102 }
103 curr = curr->next();
104 }
105
106 if (upper != nullptr) {
107 curr->next(upper);
108 }
109 else if (lower != nullptr) {
110 curr->next(lower);
111 }
112
113 curr = next->next();
114 temp.next(nullptr);
115 return curr;
116}
117
121
122 while (fast->next() != nullptr && fast->next()->next() != nullptr) {
123 slow = slow->next();
124 fast = fast->next()->next();
125 }
126
127 return slow;
128}
129
133
135 return {this, _head};
136}
137
139 return {this, nullptr};
140}
141
142} // namespace liblevenshtein
Represents a location within the Levenshtein automaton.
Definition position.h:11
void next(Position *next)
Assigns the subsequent Position along the current path.
Definition position.cpp:15
Iterates over the Position nodes in the linked-list of a Levenshtein State.
static auto find_middle(Position *lower) -> Position *
Finds the middle node between lower and the tail.
Definition state.cpp:118
Position * _head
First Position in the linked-list.
Definition state.h:121
void sort(const Comparator &compare)
Merge-sorts the linked-list of Positions.
Definition state.cpp:130
void insert_after(Position *curr, Position *next)
Inserts a new Position into the linked-list following an existing member.
Definition state.cpp:48
void add(Position *next)
Adds a new Position to the tail of the linked-list.
Definition state.cpp:35
auto begin() -> StateIterator
Returns an iterator over the Positions in this State, beginning at the head and traversing to the tai...
Definition state.cpp:134
auto end() -> StateIterator
Returns an iterator representing the end of the linked-list of Positions.
Definition state.cpp:138
auto head() const -> Position *
Returns the first Position in the linked-list of Position nodes.
Definition state.cpp:31
static auto merge(const Comparator &compare, Position *lower, Position *upper) -> Position *
Merge-sorts the nodes in the range between lower and upper.
Definition state.cpp:88
void remove(Position *prev, Position *curr)
Removes a Position from the linked-list.
Definition state.cpp:58
auto merge_sort(const Comparator &compare, Position *lower) -> Position *
Sorts the linked-list of Positions within the Levenshtein automaton.
Definition state.cpp:73
~State()
Frees any owned allocations.
Definition state.cpp:22
State()=default
Constructs an empty state, which is most commonly used to represent the intersection of the root node...
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25
Various utilities regarding Levenshtein transducers.
Definition namespaces.dox:9
std::function< int(Position *, Position *)> Comparator
Definition state.h:12
auto compare(Position *lhs, Position *rhs) -> int
Compares two Positions within the Levenshtein transducer.
void merge(State *state, const std::vector< Position * > &positions)
Merges a list of Positions into the Levenshtein state.