Skip to content

Commit

Permalink
Use std::shared_ptr instead raw pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
ramon-bernardo committed Oct 3, 2024
1 parent 4d9d335 commit dff97d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
24 changes: 12 additions & 12 deletions src/wildcardtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

void WildcardTreeNode::add(const std::string& s)
{
auto node = this;
auto node = shared_from_this();

auto length = s.length() - 1;
for (size_t pos = 0; pos < length; ++pos) {
Expand All @@ -21,9 +21,9 @@ void WildcardTreeNode::add(const std::string& s)

void WildcardTreeNode::remove(const std::string& s)
{
auto node = this;
auto node = shared_from_this();

std::stack<WildcardTreeNode*> path;
std::stack<std::shared_ptr<WildcardTreeNode>> path;
path.push(node);

auto len = s.length();
Expand Down Expand Up @@ -56,7 +56,7 @@ void WildcardTreeNode::remove(const std::string& s)

std::pair<WildcardTreeNode::SearchResult, std::string> WildcardTreeNode::search(const std::string& query) const
{
auto node = this;
auto node = shared_from_this();

for (auto c : query) {
node = node->find_child(c);
Expand All @@ -77,29 +77,29 @@ std::pair<WildcardTreeNode::SearchResult, std::string> WildcardTreeNode::search(

auto it = node->children.begin();
result += it->first;
node = &it->second;
node = it->second;
} while (true);
}

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

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

WildcardTreeNode* WildcardTreeNode::add_child(char c, bool breakpoint)
std::shared_ptr<WildcardTreeNode> WildcardTreeNode::add_child(char c, bool breakpoint)
{
if (auto child = find_child(c)) {
if (breakpoint && !child->breakpoint) {
Expand All @@ -108,6 +108,6 @@ WildcardTreeNode* WildcardTreeNode::add_child(char c, bool breakpoint)
return child;
}

auto pair = children.emplace(std::piecewise_construct, std::forward_as_tuple(c), std::forward_as_tuple(breakpoint));
return &pair.first->second;
auto pair = children.emplace(c, std::make_shared<WildcardTreeNode>(breakpoint));
return pair.first->second;
}
12 changes: 7 additions & 5 deletions src/wildcardtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

#include "enums.h"

#include <memory>

/**
* @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 string.
*/
class WildcardTreeNode
class WildcardTreeNode final : public std::enable_shared_from_this<WildcardTreeNode>
{
public:
/**
Expand Down Expand Up @@ -86,15 +88,15 @@ class WildcardTreeNode
* @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);
std::shared_ptr<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;
std::shared_ptr<const WildcardTreeNode> find_child(char c) const;

/**
* @brief Adds a child node for a specific character.
Expand All @@ -106,10 +108,10 @@ class WildcardTreeNode
* @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);
std::shared_ptr<WildcardTreeNode> add_child(char c, bool breakpoint);

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

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

0 comments on commit dff97d6

Please sign in to comment.