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

Memoizes the distance between pairs of terms. More...

#include <memoized_distance.h>

Inheritance diagram for liblevenshtein::distance::MemoizedDistance:
Collaboration diagram for liblevenshtein::distance::MemoizedDistance:

Public Member Functions

virtual auto between (std::string v, std::string w) -> std::size_t=0
 Measures the edit distance between two terms.
 
auto operator() (const std::string &v, const std::string &w) -> std::size_t
 Measures the edit distance between two terms.
 

Protected Member Functions

auto get (const SymmetricPair &key, std::size_t &distance) -> bool
 Collects the memoized distance between the pair of terms represented by the SymmetricPair if the distance has been previously determined.
 
auto set (const SymmetricPair &key, const std::size_t &distance) -> std::size_t
 Memoizes the distance between the SymmetricPair of terms for future reference.
 

Static Protected Member Functions

static auto f (const std::string &u, std::size_t t) -> std::string
 Returns the suffix of \(u\) from position \(t\).
 

Private Attributes

std::unordered_map< SymmetricPair, std::size_t > memo
 Memoized distances between pairs of terms.
 
std::shared_mutex mutex
 Coordinates memoization among threads.
 

Detailed Description

Memoizes the distance between pairs of terms.

Definition at line 16 of file memoized_distance.h.

Member Function Documentation

◆ between()

virtual auto liblevenshtein::distance::Distance::between ( std::string v,
std::string w ) -> std::size_t
pure virtualinherited

Measures the edit distance between two terms.

Parameters
vFirst term to compare.
wSecond term to compare.
Returns
Edit distance between v and w.

Implemented in liblevenshtein::distance::MergeAndSplitDistance, liblevenshtein::distance::StandardDistance, and liblevenshtein::distance::TranspositionDistance.

◆ f()

auto liblevenshtein::distance::MemoizedDistance::f ( const std::string & u,
std::size_t t ) -> std::string
staticprotected

Returns the suffix of \(u\) from position \(t\).

If \(t \ge \left|u\right|\), then the empty string is returned.

Parameters
uTerm whose suffix is to be returned.
tBeginning position in u for the substring.
Returns
The substring of u beginning at position t if \(t < \left|u\right|\), or the empty string otherwise.

Definition at line 26 of file memoized_distance.cpp.

26 {
27 if (t < u.length()) {
28 return u.substr(1 + t);
29 }
30 return "";
31}
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25

References query().

Here is the call graph for this function:

◆ get()

auto liblevenshtein::distance::MemoizedDistance::get ( const SymmetricPair & key,
std::size_t & distance ) -> bool
protected

Collects the memoized distance between the pair of terms represented by the SymmetricPair if the distance has been previously determined.

Otherwise, the distance is not collected and false is returned.

Parameters
keySymmetricPair of terms whose distance may have been memoized.
distanceOutgoing reference containing the memoized distance between the pair of terms, if the distance has been previously determined.
Returns
Whether the distance between the pair of terms has been previously determined.

Definition at line 9 of file memoized_distance.cpp.

9 {
10 std::shared_lock reader(mutex);
11 auto iter = memo.find(key);
12 if (iter != memo.end()) {
13 distance = iter->second;
14 return true;
15 }
16 return false;
17}
std::unordered_map< SymmetricPair, std::size_t > memo
Memoized distances between pairs of terms.
std::shared_mutex mutex
Coordinates memoization among threads.
auto distance(State *state, std::size_t query_length) -> std::size_t
Infers the Levenshtein distance from the given State and query length.

References liblevenshtein::distance(), and query().

Here is the call graph for this function:

◆ operator()()

auto liblevenshtein::distance::Distance::operator() ( const std::string & v,
const std::string & w ) -> std::size_t
inherited

Measures the edit distance between two terms.

This is equivalent to calling between(v, w).

Parameters
vFirst term to compare.
wSecond term to compare.
Returns
Edit distance between v and w.

Definition at line 5 of file distance.cpp.

6 {
7 return between(v, w);
8}
virtual auto between(std::string v, std::string w) -> std::size_t=0
Measures the edit distance between two terms.

References query().

Here is the call graph for this function:

◆ set()

auto liblevenshtein::distance::MemoizedDistance::set ( const SymmetricPair & key,
const std::size_t & distance ) -> std::size_t
protected

Memoizes the distance between the SymmetricPair of terms for future reference.

Parameters
keySymmetricPair of terms whose distance is to be memoized.
distanceDistance between the SymmetricPair of terms to memoize.
Returns
The distance once it has been memoized.

Definition at line 19 of file memoized_distance.cpp.

20 {
21 std::unique_lock writer(mutex);
22 memo[key] = distance;
23 return distance;
24}

References liblevenshtein::distance(), and query().

Here is the call graph for this function:

Member Data Documentation

◆ memo

std::unordered_map<SymmetricPair, std::size_t> liblevenshtein::distance::MemoizedDistance::memo
private

Memoized distances between pairs of terms.

Definition at line 57 of file memoized_distance.h.

◆ mutex

std::shared_mutex liblevenshtein::distance::MemoizedDistance::mutex
mutableprivate

Coordinates memoization among threads.

Definition at line 60 of file memoized_distance.h.


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