liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
liblevenshtein::State Class Reference

Consists of a closure of Position nodes within the Levenshtein automaton. More...

#include <state.h>

Collaboration diagram for liblevenshtein::State:

Public Member Functions

 State ()=default
 Constructs an empty state, which is most commonly used to represent the intersection of the root nodes of the Levenshtein and dictionary automata.
 
 State (std::initializer_list< Position * > positions)
 Constructs a new State with an initial list of Positions.
 
 State (std::vector< Position * > &positions)
 Constructs a new State with an initial list of Positions.
 
 ~State ()
 Frees any owned allocations.
 
void head (Position *head)
 Sets the first element in the linked-list of Position nodes.
 
auto head () const -> Position *
 Returns the first Position in the linked-list of Position nodes.
 
void add (Position *next)
 Adds a new Position to the tail of the linked-list.
 
void insert_after (Position *curr, Position *next)
 Inserts a new Position into the linked-list following an existing member.
 
void remove (Position *prev, Position *curr)
 Removes a Position from the linked-list.
 
void sort (const Comparator &compare)
 Merge-sorts the linked-list of Positions.
 
auto begin () -> StateIterator
 Returns an iterator over the Positions in this State, beginning at the head and traversing to the tail.
 
auto end () -> StateIterator
 Returns an iterator representing the end of the linked-list of Positions.
 

Private Member Functions

auto merge_sort (const Comparator &compare, Position *lower) -> Position *
 Sorts the linked-list of Positions within the Levenshtein automaton.
 

Static Private Member Functions

static auto merge (const Comparator &compare, Position *lower, Position *upper) -> Position *
 Merge-sorts the nodes in the range between lower and upper.
 
static auto find_middle (Position *lower) -> Position *
 Finds the middle node between lower and the tail.
 

Private Attributes

Position_head = nullptr
 First Position in the linked-list.
 

Detailed Description

Consists of a closure of Position nodes within the Levenshtein automaton.

Each State consists of all possible locations that are reachable within the intersection of the Levenshtein and dictionary automata, guided by the query term, at the current index within the dictionary.

Definition at line 23 of file state.h.

Constructor & Destructor Documentation

◆ State() [1/3]

liblevenshtein::State::State ( )
default

Constructs an empty state, which is most commonly used to represent the intersection of the root nodes of the Levenshtein and dictionary automata.

◆ State() [2/3]

liblevenshtein::State::State ( std::initializer_list< Position * > positions)

Constructs a new State with an initial list of Positions.

Parameters
positionsInitial list of Positions at the current index within the dictionary automaton.

Definition at line 6 of file state.cpp.

6 {
7 Position *curr = nullptr;
8 for (Position *next : positions) {
9 insert_after(curr, next);
10 curr = next;
11 }
12}
void insert_after(Position *curr, Position *next)
Inserts a new Position into the linked-list following an existing member.
Definition state.cpp:48
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25

References insert_after(), and query().

Here is the call graph for this function:

◆ State() [3/3]

liblevenshtein::State::State ( std::vector< Position * > & positions)

Constructs a new State with an initial list of Positions.

Parameters
positionsInitial list of Positions at the current index within the dictionary automaton.

Definition at line 14 of file state.cpp.

14 {
15 Position *curr = nullptr;
16 for (Position *next : positions) {
17 insert_after(curr, next);
18 curr = next;
19 }
20}

References insert_after(), and query().

Here is the call graph for this function:

◆ ~State()

liblevenshtein::State::~State ( )

Frees any owned allocations.

Definition at line 22 of file state.cpp.

22 {
23 delete _head;
24}
Position * _head
First Position in the linked-list.
Definition state.h:121

References _head.

Member Function Documentation

◆ add()

void liblevenshtein::State::add ( Position * next)

Adds a new Position to the tail of the linked-list.

Parameters
nextThe Position to insert at the tail of the linked-list.

Definition at line 35 of file state.cpp.

35 {
36 if (_head == nullptr) {
37 _head = next;
38 }
39 else {
40 Position* curr = _head;
41 while (curr->next() != nullptr) {
42 curr = curr->next();
43 }
44 curr->next(next);
45 }
46}
void next(Position *next)
Assigns the subsequent Position along the current path.
Definition position.cpp:15

References _head, liblevenshtein::Position::next(), and query().

Referenced by insert_after().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ begin()

auto liblevenshtein::State::begin ( ) -> StateIterator

Returns an iterator over the Positions in this State, beginning at the head and traversing to the tail.

Returns
An iterator over Positions in this State.

Definition at line 134 of file state.cpp.

134 {
135 return {this, _head};
136}

Referenced by liblevenshtein::merge< Algorithm::MERGE_AND_SPLIT >(), liblevenshtein::merge< Algorithm::STANDARD >(), liblevenshtein::merge< Algorithm::TRANSPOSITION >(), and liblevenshtein::UnsubsumeFn::operator()().

Here is the caller graph for this function:

◆ end()

auto liblevenshtein::State::end ( ) -> StateIterator

Returns an iterator representing the end of the linked-list of Positions.

Returns
An iterator pointing to the end of the linked-list.

Definition at line 138 of file state.cpp.

138 {
139 return {this, nullptr};
140}

Referenced by liblevenshtein::merge< Algorithm::MERGE_AND_SPLIT >(), liblevenshtein::merge< Algorithm::STANDARD >(), liblevenshtein::merge< Algorithm::TRANSPOSITION >(), and liblevenshtein::UnsubsumeFn::operator()().

Here is the caller graph for this function:

◆ find_middle()

auto liblevenshtein::State::find_middle ( Position * lower) -> Position *
staticprivate

Finds the middle node between lower and the tail.

Parameters
lowerLower bound for the search for the mid-point.
Returns
A pointer to the mid-point between lower and the tail.

Definition at line 118 of file state.cpp.

118 {
119 Position* slow = lower;
120 Position* fast = lower;
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}

References liblevenshtein::Position::next(), and query().

Here is the call graph for this function:

◆ head() [1/2]

auto liblevenshtein::State::head ( ) const -> Position *

Returns the first Position in the linked-list of Position nodes.

Returns
The first Position in the linked-list of Position nodes.

Definition at line 31 of file state.cpp.

31 {
32 return _head;
33}

References _head.

Referenced by head().

Here is the caller graph for this function:

◆ head() [2/2]

void liblevenshtein::State::head ( Position * head)

Sets the first element in the linked-list of Position nodes.

The linked-list is used with a modified version of merge-sort to merge new Positions that will not be redundant.

Parameters
headFirst Position in the linked-list of Positions.

Definition at line 26 of file state.cpp.

26 {
27 head->next(_head);
28 _head = head;
29}
auto head() const -> Position *
Returns the first Position in the linked-list of Position nodes.
Definition state.cpp:31

References _head, head(), and liblevenshtein::Position::next().

Referenced by liblevenshtein::LazyQuery< Result >::advance(), liblevenshtein::StateIterator::insert(), liblevenshtein::insert_after(), and liblevenshtein::StateTransition::operator()().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insert_after()

void liblevenshtein::State::insert_after ( Position * curr,
Position * next )

Inserts a new Position into the linked-list following an existing member.

Parameters
currExisting member of the linked-list whose follow node should be set to next.
nextNew Position to insert into the linked-list following curr.

Definition at line 48 of file state.cpp.

48 {
49 if (curr != nullptr) {
50 next->next(curr->next());
51 curr->next(next);
52 }
53 else {
54 add(next);
55 }
56}
void add(Position *next)
Adds a new Position to the tail of the linked-list.
Definition state.cpp:35

References add(), liblevenshtein::Position::next(), and query().

Referenced by liblevenshtein::StateIterator::insert(), liblevenshtein::insert_after(), State(), and State().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ merge()

auto liblevenshtein::State::merge ( const Comparator & compare,
Position * lower,
Position * upper ) -> Position *
staticprivate

Merge-sorts the nodes in the range between lower and upper.

Parameters
compareComparator used to lexicographically sort the Position nodes.
lowerLower bound of sortation.
upperUpper bound of sortation.
Returns
A pointer to the head of the sorted sub-list.

Definition at line 88 of file state.cpp.

89 {
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}
auto compare(Position *lhs, Position *rhs) -> int
Compares two Positions within the Levenshtein transducer.

References liblevenshtein::compare(), liblevenshtein::Position::next(), and query().

Here is the call graph for this function:

◆ merge_sort()

auto liblevenshtein::State::merge_sort ( const Comparator & compare,
Position * lower ) -> Position *
private

Sorts the linked-list of Positions within the Levenshtein automaton.

Parameters
compareAlgorithm-specific comparator for sorting the linked-list of Position nodes.
lowerLower bound of linked-list nodes for the current branch of sortation.
Returns
A pointer to the head of the sorted sub-list.

Definition at line 73 of file state.cpp.

74 {
75 if (lower == nullptr || lower->next() == nullptr) {
76 return lower;
77 }
78
79 Position* middle = find_middle(lower);
80 Position* upper = middle->next();
81 middle->next(nullptr);
82
83 return merge(compare,
86}
static auto find_middle(Position *lower) -> Position *
Finds the middle node between lower and the tail.
Definition state.cpp:118
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
auto merge_sort(const Comparator &compare, Position *lower) -> Position *
Sorts the linked-list of Positions within the Levenshtein automaton.
Definition state.cpp:73

References liblevenshtein::compare(), liblevenshtein::merge(), liblevenshtein::Position::next(), and query().

Referenced by sort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove()

void liblevenshtein::State::remove ( Position * prev,
Position * curr )

Removes a Position from the linked-list.

By convention, prev must be the immediately preceding Position in the linked-list to curr.

Parameters
prevPosition in the linked-list preceding the one to remove.
currPosition in the linked-lsit to remove.

Definition at line 58 of file state.cpp.

58 {
59 // ASSUMPTION: prev->next() == curr
60 Position* temp;
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}

References _head, liblevenshtein::Position::next(), and query().

Referenced by liblevenshtein::StateIterator::remove().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sort()

void liblevenshtein::State::sort ( const Comparator & compare)

Merge-sorts the linked-list of Positions.

Parameters
compareComparator to use during sortation.

Definition at line 130 of file state.cpp.

130 {
132}

References _head, liblevenshtein::compare(), and merge_sort().

Here is the call graph for this function:

Member Data Documentation

◆ _head

Position* liblevenshtein::State::_head = nullptr
private

First Position in the linked-list.

This will be the lexicographically least Position in the linked-list. Each successive Position is lexicographically greater than the previous ones.

Definition at line 121 of file state.h.

Referenced by add(), head(), head(), remove(), sort(), and ~State().


The documentation for this class was generated from the following files: