-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include <iostream> | ||
#include <memory> | ||
#include <list> | ||
#include <algorithm> | ||
|
||
class TimedEntry { | ||
public: | ||
size_t count() const { return m_count; } | ||
double time() const { return m_time; } | ||
|
||
TimedEntry& add_time(double time) { | ||
++m_count; | ||
m_time += time; | ||
return *this; | ||
} | ||
|
||
private: | ||
size_t m_count = 0; | ||
double m_time = 0.0; | ||
}; | ||
|
||
class RadixTreeNode { | ||
private: | ||
using ChildList = std::list<std::unique_ptr<RadixTreeNode>>; | ||
|
||
public: | ||
RadixTreeNode(std::string name) : key(std::move(name)), prev(nullptr) {} | ||
|
||
const std::string& getKey() const { return key; } | ||
TimedEntry& getData() { return data; } | ||
const TimedEntry& getData() const { return data; } | ||
const ChildList& getChildren() const { return children; } | ||
|
||
// Add a child node | ||
RadixTreeNode* addChild(std::string childKey) { | ||
auto newChild = std::make_unique<RadixTreeNode>(std::move(childKey)); | ||
newChild->prev = this; | ||
children.push_back(std::move(newChild)); | ||
return children.back().get(); | ||
} | ||
|
||
// Get previous node (parent) in the tree | ||
RadixTreeNode* getPrev() const { | ||
return prev; | ||
} | ||
|
||
private: | ||
std::string key; | ||
TimedEntry data; | ||
ChildList children; | ||
RadixTreeNode* prev; | ||
}; | ||
|
||
class RadixTree { | ||
public: | ||
RadixTree() : m_root(std::make_unique<RadixTreeNode>("root")), m_cur(m_root.get()) {} | ||
|
||
TimedEntry& entry(const std::string& name) { | ||
auto it = std::find_if(m_cur->getChildren().begin(), m_cur->getChildren().end(), | ||
[&](const auto& child) { return child->getKey() == name; }); | ||
|
||
if (it == m_cur->getChildren().end()) { | ||
m_cur = m_cur->addChild(name); | ||
} else { | ||
m_cur = it->get(); | ||
} | ||
|
||
return m_cur->getData(); | ||
} | ||
|
||
void moveCurToParent() { | ||
m_cur = m_cur->getPrev(); | ||
} | ||
|
||
const RadixTreeNode* getRoot() const { return m_root.get(); } | ||
const RadixTreeNode* getCurrent() const { return m_cur; } | ||
|
||
void printTree() const { | ||
printRecursive(m_root.get(), 0); | ||
} | ||
|
||
private: | ||
void printRecursive(const RadixTreeNode* node, int depth) const { | ||
for (int i = 0; i < depth; ++i) { | ||
std::cout << " "; | ||
} | ||
|
||
std::cout << node->getKey() << " - Count: " << node->getData().count() << " - Time: " << node->getData().time() << std::endl; | ||
|
||
for (const auto& child : node->getChildren()) { | ||
printRecursive(child.get(), depth + 1); | ||
} | ||
} | ||
|
||
std::unique_ptr<RadixTreeNode> m_root; | ||
RadixTreeNode* m_cur; | ||
}; | ||
|
||
int main() { | ||
RadixTree radixTree; | ||
|
||
// a -> b | ||
// a -> c | ||
// c -> d | ||
// d -> e | ||
// a -> f | ||
auto& entry1 = radixTree.entry("a"); | ||
entry1.add_time(5.0); | ||
|
||
auto& entry2 = radixTree.entry("b"); | ||
entry2.add_time(3.0); | ||
radixTree.moveCurToParent(); | ||
|
||
auto& entry3 = radixTree.entry("c"); | ||
entry3.add_time(7.0); | ||
|
||
auto& entry4 = radixTree.entry("d"); | ||
entry4.add_time(9.0); | ||
|
||
auto& entry5 = radixTree.entry("e"); | ||
entry5.add_time(1.0); | ||
radixTree.moveCurToParent(); | ||
radixTree.moveCurToParent(); | ||
radixTree.moveCurToParent(); | ||
|
||
auto& entry6 = radixTree.entry("f"); | ||
entry6.add_time(2.41); | ||
|
||
radixTree.printTree(); | ||
|
||
return 0; | ||
} |