Skip to content

Commit

Permalink
Add wildcard tree doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo committed Sep 29, 2024
1 parent de1a843 commit 9bf2a71
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 35 deletions.
62 changes: 31 additions & 31 deletions src/wildcardtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,6 @@
#include <stack>
#include <tuple>

WildcardTreeNode* WildcardTreeNode::find_child(char c)
{
auto it = children.find(c);
if (it == children.end()) {
return nullptr;
}
return &it->second;
}

const WildcardTreeNode* WildcardTreeNode::find_child(char c) const
{
auto it = children.find(c);
if (it == children.end()) {
return nullptr;
}
return &it->second;
}

WildcardTreeNode* WildcardTreeNode::add_child(char c, bool breakpoint)
{
if (auto child = find_child(c)) {
if (breakpoint && !child->breakpoint) {
child->breakpoint = true;
}
return child;
}

auto pair = children.emplace(std::piecewise_construct, std::forward_as_tuple(c), std::forward_as_tuple(breakpoint));
return &pair.first->second;
}

void WildcardTreeNode::add(const std::string& s)
{
auto node = this;
Expand Down Expand Up @@ -111,3 +80,34 @@ std::pair<WildcardTreeNode::SearchResult, std::string> WildcardTreeNode::search(
node = &it->second;
} while (true);
}

WildcardTreeNode* WildcardTreeNode::find_child(char c)
{
auto it = children.find(c);
if (it == children.end()) {
return nullptr;
}
return &it->second;
}

const WildcardTreeNode* WildcardTreeNode::find_child(char c) const
{
auto it = children.find(c);
if (it == children.end()) {
return nullptr;
}
return &it->second;
}

WildcardTreeNode* WildcardTreeNode::add_child(char c, bool breakpoint)
{
if (auto child = find_child(c)) {
if (breakpoint && !child->breakpoint) {
child->breakpoint = true;
}
return child;
}

auto pair = children.emplace(std::piecewise_construct, std::forward_as_tuple(c), std::forward_as_tuple(breakpoint));
return &pair.first->second;
}
94 changes: 90 additions & 4 deletions src/wildcardtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,120 @@

#include "enums.h"

/**
* @class WildcardTreeNode
* @brief Represents a node in a wildcard tree. Each node can store children nodes, with
* an optional "breakpoint" to mark the end of a valid string.
*/
class WildcardTreeNode
{
public:
/**
* @brief Constructs a WildcardTreeNode with an optional breakpoint.
* @param {breakpoint} A boolean
* value indicating if the node represents a breakpoint (i.e., the end of a string).
*/
explicit WildcardTreeNode(bool breakpoint) : breakpoint(breakpoint) {}

/**
* @brief Move constructor for WildcardTreeNode.
* @param other The node to move from.
*/
WildcardTreeNode(WildcardTreeNode&& other) = default;

/**
* @enum SearchResult
* @brief Represents the result of a search operation in the wildcard tree.
*
* - Found: The exact string or path was found.
* - NotFound: The string or path does not exist in the tree.
* - Ambiguous: The search result is ambiguous (e.g., multiple valid continuations exist).
*/
enum SearchResult
{
Found,
NotFound,
Ambiguous,
};

explicit WildcardTreeNode(bool breakpoint) : breakpoint(breakpoint) {}
WildcardTreeNode(WildcardTreeNode&& other) = default;

// non-copyable
/// Deleted copy constructor to ensure WildcardTreeNode is non-copyable.
WildcardTreeNode(const WildcardTreeNode&) = delete;
/// Deleted assignment operator to ensure WildcardTreeNode is non-copyable.
WildcardTreeNode& operator=(const WildcardTreeNode&) = delete;

/**
* @brief Adds a string to the wildcard tree.
*
* This method breaks the string into characters
* and adds them as children nodes.
* If the string already exists, it updates the final node to represent a
* valid breakpoint.
*
* @param {s} The string to be added.
*/
void add(const std::string& s);

/**
* @brief Removes a string from the wildcard tree.
*
* This method traverses the tree based on
* the input string and marks the final node as non-breakpoint.
* If the node becomes orphaned (no children and
* not a breakpoint), it is removed.
*
* @param {s} The string to be removed.
*/
void remove(const std::string& s);

/**
* @brief Searches for a string in the wildcard tree.
*
* This method traverses the tree based on
* the input query string and returns the search result and matched string.
*
* @param {query} The string
* to search for.
*
* @return A pair containing the search result and the matched string.
*/
std::pair<WildcardTreeNode::SearchResult, std::string> search(const std::string& query) const;

private:
/**
* @brief Finds a child node corresponding to a character.
* @param {c} The character to search for
* among the children.
* @return A pointer to the child node if found, otherwise nullptr.
*/
WildcardTreeNode* find_child(char c);

/**
* @brief Finds a child node corresponding to a character (const version).
* @param {c} The character
* to search for among the children.
* @return A const pointer to the child node if found, otherwise nullptr.
*/
const WildcardTreeNode* find_child(char c) const;

/**
* @brief Adds a child node for a specific character.
*
* If the child node for the character
* already exists, it will be updated.
* If it does not exist, a new node will be created.
*
*
* @param {c} The character to add as a child node.
* @param {breakpoint} A boolean value indicating if this
* node represents a breakpoint.
* @return A pointer to the added or existing child node.
*/
WildcardTreeNode* add_child(char c, bool breakpoint);

/// A map of child nodes indexed by characters.
std::map<char, WildcardTreeNode> children;

/// Indicates whether this node represents a breakpoint (end of a valid string).
bool breakpoint;
};

Expand Down

0 comments on commit 9bf2a71

Please sign in to comment.