Skip to content

Commit

Permalink
Documentation and initialisation conventions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Nov 5, 2022
1 parent 1288538 commit 0a20572
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 145 deletions.
17 changes: 6 additions & 11 deletions src/node.tcc
Expand Up @@ -8,28 +8,23 @@
using std::array;


/*!
* Leaf.
*/
/*! Leaf. */
struct Leaf {
size_t count = 0;
size_t count {0};
};

/*!
* Node.
*/
/*! Node. */
template <uint8_t alphabetSize, class T>
class Node {
public:
bool isEmpty() const;

array<Node*, alphabetSize> child = {};
T* leaf = nullptr;
array<Node*, alphabetSize> child {};
T* leaf {nullptr};
};


/*!
* Check whether a node neither has any children, nor a leaf.
/*! Check whether a node neither has any children, nor a leaf.
*
* \return True is the node is empty, false otherwise.
*/
Expand Down
127 changes: 61 additions & 66 deletions src/trie.tcc
Expand Up @@ -4,147 +4,142 @@

#include "trieFunctions.tcc"

/*!
* Trie.
*/
/*! Trie. */
template <uint8_t alphabetSize, class T>
class Trie {
public:
/*! Constructor. */
Trie();

/*! Destructor. */
~Trie();


/*! Add a word.
*
* \param word Word.
*
* \return Leaf.
*/
T* add(vector<uint8_t> const&) const;

/*! Remove a word.
*
* \param word Word.
*/
void remove(vector<uint8_t> const&) const;

/*! Find a word.
*
* \param word Word.
*
* \return `node` if found, `nullptr` otherwise.
*/
Node<alphabetSize, T>* find(vector<uint8_t> const&) const;

/*! Traverse.
*
* \return Traversal results.
*/
generator<Result<T>> walk() const;

/*! Hamming.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
generator<Result<T>> hamming(vector<uint8_t> const&, int const) const;

/*! Asymmetric Hamming.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
generator<Result<T>> asymmetricHamming(
vector<uint8_t> const&, int const) const;

/*! Levenshtein.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
generator<Result<T>> levenshtein(vector<uint8_t> const&, int const) const;

/*! Asymmetric Levenshtein.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
generator<Result<T>> asymmetricLevenshtein(
vector<uint8_t> const&, int const) const;

private:
Node<alphabetSize, T>* root_ = nullptr;
Node<alphabetSize, T>* root_ {nullptr};
};


/*!
* Constructor.
*/
template <uint8_t alphabetSize, class T>
Trie<alphabetSize, T>::Trie() {
root_ = new Node<alphabetSize, T>;
}

/*!
* Destructor.
*/
template <uint8_t alphabetSize, class T>
Trie<alphabetSize, T>::~Trie() {
delete_(root_);
}


/*!
* Add a word.
*
* \param word Word.
*
* \return Leaf.
*/
template <uint8_t alphabetSize, class T>
T* Trie<alphabetSize, T>::add(vector<uint8_t> const& word) const {
return add_(root_, word);
}

/*!
* Remove a word.
*
* \param word Word.
*/
template <uint8_t alphabetSize, class T>
void Trie<alphabetSize, T>::remove(vector<uint8_t> const& word) const {
remove_(root_, word, 0);
}

/*!
* Find a word.
*
* \param word Word.
*
* \return `node` if found, `nullptr` otherwise.
*/
template <uint8_t alphabetSize, class T>
Node<alphabetSize, T>* Trie<alphabetSize, T>::find(
vector<uint8_t> const& word) const {
return find_(root_, word);
}

/*!
* Traverse.
*
* \return Traversal results.
*/
template <uint8_t alphabetSize, class T>
generator<Result<T>> Trie<alphabetSize, T>::walk() const {
vector<uint8_t> path;
co_yield walk_(root_, path);
}

/*!
* Hamming.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
template <uint8_t alphabetSize, class T>
generator<Result<T>> Trie<alphabetSize, T>::hamming(
vector<uint8_t> const& word, int const distance) const {
vector<uint8_t> path;
co_yield hamming_<alphabetSize, T, true>(root_, word, 0, distance, path);
}

/*!
* Asymmetric Hamming.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
template <uint8_t alphabetSize, class T>
generator<Result<T>> Trie<alphabetSize, T>::asymmetricHamming(
vector<uint8_t> const& word, int const distance) const {
vector<uint8_t> path;
co_yield hamming_<alphabetSize, T, false>(root_, word, 0, distance, path);
}

/*!
* Levenshtein.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
template <uint8_t alphabetSize, class T>
generator<Result<T>> Trie<alphabetSize, T>::levenshtein(
vector<uint8_t> const& word, int const distance) const {
vector<uint8_t> path;
co_yield levenshtein_<alphabetSize, T, true>(root_, word, 0, distance, path);
}

/*!
* Asymmetric Levenshtein.
*
* \param word Word.
* \param distance Maximum distance.
*
* \return Traversal results.
*/
template <uint8_t alphabetSize, class T>
generator<Result<T>> Trie<alphabetSize, T>::asymmetricLevenshtein(
vector<uint8_t> const& word, int const distance) const {
Expand Down
43 changes: 17 additions & 26 deletions src/trieFunctions.tcc
Expand Up @@ -6,18 +6,15 @@

using std::vector;

/*!
* Result.
*/
/*! Result. */
template <class T>
struct Result {
vector<uint8_t> path;
T* leaf;
};


/*
* Delete a (sub)trie.
/* Delete a (sub)trie.
*
* \param node Root.
*/
Expand All @@ -34,8 +31,7 @@ void delete_(Node<alphabetSize, T>* const node) {
delete node;
}

/*
* Add a word to a (sub)trie.
/* Add a word to a (sub)trie.
*
* \param node Root.
* \param word Word.
Expand All @@ -58,8 +54,7 @@ T* add_(Node<alphabetSize, T>* node, vector<uint8_t> const& word) {
return node->leaf;
}

/*
* Remove a word from a (sub)trie.
/* Remove a word from a (sub)trie.
*
* \param node Root.
* \param word Word.
Expand Down Expand Up @@ -87,7 +82,7 @@ bool remove_(
return false;
}

bool result = remove_(node->child[word[position]], word, position + 1);
bool result {remove_(node->child[word[position]], word, position + 1)};
if (result) {
if (node->child[word[position]]->isEmpty()) {
delete node->child[word[position]];
Expand All @@ -98,8 +93,7 @@ bool remove_(
return result;
}

/*
* Find a word in a (sub)trie.
/* Find a word in a (sub)trie.
*
* \param node Root.
* \param word Word.
Expand All @@ -118,8 +112,7 @@ Node<alphabetSize, T>* find_(
return node;
}

/*
* Traverse a (sub)trie.
/* Traverse a (sub)trie.
*
* \param node Root.
* \param path Path.
Expand All @@ -130,10 +123,10 @@ template <uint8_t alphabetSize, class T>
generator<Result<T>> walk_(
Node<alphabetSize, T> const* const node, vector<uint8_t>& path) {
if (node->leaf) {
Result<T> result = {path, node->leaf};
Result<T> result {path, node->leaf};
co_yield result;
}
for (size_t i = 0; i < alphabetSize; i++) {
for (size_t i {0}; i < alphabetSize; i++) {
if (node->child[i]) {
path.push_back(i);
co_yield walk_(node->child[i], path);
Expand All @@ -142,8 +135,7 @@ generator<Result<T>> walk_(
}
}

/*
* Find all words within Hamming distance `distance` of `word`.
/* Find all words within Hamming distance `distance` of `word`.
*
* \param node Root.
* \param word Word.
Expand All @@ -159,11 +151,11 @@ generator<Result<T>> hamming_(
size_t const position, int const distance, vector<uint8_t>& path) {
if (distance >= 0) {
if (position < word.size()) {
uint8_t start = 0;
uint8_t start {0};
if (!full) {
start = word[position];
}
for (uint8_t i = start; i < alphabetSize; i++) {
for (uint8_t i {start}; i < alphabetSize; i++) {
if (node->child[i]) {
path.push_back(i);
co_yield hamming_<alphabetSize, T, full>(
Expand All @@ -174,14 +166,13 @@ generator<Result<T>> hamming_(
}
}
else {
Result<T> result = {path, node->leaf};
Result<T> result {path, node->leaf};
co_yield result;
}
}
}

/*
* Find all words within Levenshtein distance `distance` of `word`.
/* Find all words within Levenshtein distance `distance` of `word`.
*
* \tparam full
*
Expand All @@ -202,11 +193,11 @@ generator<Result<T>> levenshtein_(
co_yield levenshtein_<alphabetSize, T, full>(
node, word, position + 1, distance - 1, path);

uint8_t start = 0;
uint8_t start {0};
if (!full && position < word.size()) {
start = word[position];
}
for (uint8_t i = start; i < alphabetSize; i++) {
for (uint8_t i {start}; i < alphabetSize; i++) {
if (node->child[i]) {
path.push_back(i);

Expand All @@ -226,7 +217,7 @@ generator<Result<T>> levenshtein_(
}

if (position >= word.size() && node->leaf) {
Result<T> result = {path, node->leaf};
Result<T> result {path, node->leaf};
co_yield result;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functions.cc
Expand Up @@ -9,7 +9,7 @@ using std::vector;
TEST_CASE("Internal remove function", "[remove]") {
Trie<4, Leaf> trie;

vector<uint8_t> word = {0x00, 0x01, 0x02};
vector<uint8_t> word {0x00, 0x01, 0x02};
trie.add(word);
trie.add(word);

Expand Down

0 comments on commit 0a20572

Please sign in to comment.