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

Iterates over all the terms in a DAWG dictionary. More...

#include <dawg_iterator.h>

Collaboration diagram for liblevenshtein::DawgIterator:

Public Member Functions

 DawgIterator (DawgNode *root)
 Constructs an iterator over the terms in a dictionary.
 
 DawgIterator (std::size_t term_index)
 Constructs an iterator representing the boundary following the final term in a dictionary.
 
 ~DawgIterator ()
 Cleans up all the prefixes allocated by this iterator.
 
auto operator++ () -> DawgIterator &
 Advances the iterator by one term.
 
auto operator* () const -> std::string
 Returns the current dictionary term.
 
auto operator== (const DawgIterator &other) const -> bool
 Compares this iterator with one representing the boundary following the final term in the dictionary.
 

Private Member Functions

void advance ()
 Advances this iterator by one term.
 

Private Attributes

std::vector< Prefix * > _prefixes
 Used to construct dictionary terms by collecting consecutive edge labels from the root node to their respective final nodes.
 
std::queue< Prefix * > _pending
 Intermediate prefixes that have not reached all their final nodes yet.
 
std::string _next_value
 Value of this iterator, i.e.
 
std::size_t _term_index = 0
 Current index of the enumerated terms in the dictinary.
 

Detailed Description

Iterates over all the terms in a DAWG dictionary.

Definition at line 15 of file dawg_iterator.h.

Constructor & Destructor Documentation

◆ DawgIterator() [1/2]

liblevenshtein::DawgIterator::DawgIterator ( DawgNode * root)

Constructs an iterator over the terms in a dictionary.

Terms are yielded as they are reached by traversing the consecutive outgoing edges from the root node.

Parameters
rootRoot node of the dictionary.

Definition at line 6 of file dawg_iterator.cpp.

6 {
7 auto *prefix = new Prefix(root);
8 _prefixes.push_back(prefix);
9 _pending.push(prefix);
10 advance();
11}
std::vector< Prefix * > _prefixes
Used to construct dictionary terms by collecting consecutive edge labels from the root node to their ...
void advance()
Advances this iterator by one term.
std::queue< Prefix * > _pending
Intermediate prefixes that have not reached all their final nodes yet.
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25

References _pending, _prefixes, advance(), and query().

Here is the call graph for this function:

◆ DawgIterator() [2/2]

liblevenshtein::DawgIterator::DawgIterator ( std::size_t term_index)

Constructs an iterator representing the boundary following the final term in a dictionary.

Parameters
term_indexSize of the dictionary. Each term is enumerated from 0, so when the term_index reaches the size of the dictionary, all terms have been traversed.

Definition at line 13 of file dawg_iterator.cpp.

14 : _term_index(term_index)
15{}
std::size_t _term_index
Current index of the enumerated terms in the dictinary.

◆ ~DawgIterator()

liblevenshtein::DawgIterator::~DawgIterator ( )

Cleans up all the prefixes allocated by this iterator.

Definition at line 17 of file dawg_iterator.cpp.

17 {
18 for (Prefix* prefix : _prefixes) {
19 delete prefix;
20 }
21}

References _prefixes, and query().

Here is the call graph for this function:

Member Function Documentation

◆ advance()

void liblevenshtein::DawgIterator::advance ( )
private

Advances this iterator by one term.

Definition at line 37 of file dawg_iterator.cpp.

37 {
38 if (!_pending.empty()) {
39 DawgNode* node;
40 std::string value;
41 Prefix* prefix;
42
43 do {
44 prefix = _pending.front();
45 _pending.pop();
46
47 node = prefix->node();
48 node->for_each_edge([&] (char label, DawgNode* target) {
49 auto *child = new Prefix(target, prefix, label);
50 _prefixes.push_back(child);
51 _pending.push(child);
52 });
53 }
54 while (!node->is_final() && !_pending.empty());
55
56 if (node->is_final()) {
57 _next_value = prefix->str();
58 }
59 }
60}
std::string _next_value
Value of this iterator, i.e.

References _next_value, _pending, _prefixes, liblevenshtein::DawgNode::for_each_edge(), liblevenshtein::DawgNode::is_final(), and query().

Referenced by DawgIterator().

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

◆ operator*()

auto liblevenshtein::DawgIterator::operator* ( ) const -> std::string

Returns the current dictionary term.

Returns
The term at term_index of the dictionary.

Definition at line 29 of file dawg_iterator.cpp.

29 {
30 return _next_value;
31}

References _next_value.

◆ operator++()

auto liblevenshtein::DawgIterator::operator++ ( ) -> DawgIterator &

Advances the iterator by one term.

Returns
An iterator whose value is the next term in the dictionary.

Definition at line 23 of file dawg_iterator.cpp.

23 {
24 advance();
25 _term_index += 1;
26 return *this;
27}

◆ operator==()

auto liblevenshtein::DawgIterator::operator== ( const DawgIterator & other) const -> bool

Compares this iterator with one representing the boundary following the final term in the dictionary.

When this returns false, all terms in the dictionary have been iterated over. By convention, this is only used to compare iterators over the same dictionary.

Parameters
otherAn iterator whose location in the dictionary must be compared with the boundary following its final term.
Returns
Whether the iterator has reached the end of the dictionary.

Definition at line 33 of file dawg_iterator.cpp.

33 {
34 return _term_index == other._term_index;
35}

References query().

Here is the call graph for this function:

Member Data Documentation

◆ _next_value

std::string liblevenshtein::DawgIterator::_next_value
private

Value of this iterator, i.e.

the current dictionary term.

Definition at line 78 of file dawg_iterator.h.

Referenced by advance(), and operator*().

◆ _pending

std::queue<Prefix *> liblevenshtein::DawgIterator::_pending
private

Intermediate prefixes that have not reached all their final nodes yet.

Definition at line 75 of file dawg_iterator.h.

Referenced by advance(), and DawgIterator().

◆ _prefixes

std::vector<Prefix *> liblevenshtein::DawgIterator::_prefixes
private

Used to construct dictionary terms by collecting consecutive edge labels from the root node to their respective final nodes.

Definition at line 72 of file dawg_iterator.h.

Referenced by advance(), DawgIterator(), and ~DawgIterator().

◆ _term_index

std::size_t liblevenshtein::DawgIterator::_term_index = 0
private

Current index of the enumerated terms in the dictinary.

Definition at line 81 of file dawg_iterator.h.


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