teditor  1.8.0@@fee5e94
Terminal based editor written in C++
trie.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <unordered_map>
4 #include <string>
5 #include "keys.h"
6 #include "utils.h"
7 
8 namespace teditor {
9 
10 class Node {
11 public:
12  Node(): nodes(), isLeaf(false), leafData() {}
13  ~Node() { clear(); }
14  Node* addNode(const std::string& key);
15  void delNode(const Strings& keys, size_t pos);
16  Node* getNode(const std::string& key);
17  void makeLeaf(const std::string& d);
18  bool leaf() const { return isLeaf; }
19  const std::string& data() { return leafData; }
20  void clear();
21 
22 private:
23  std::unordered_map<std::string, Node*> nodes;
24  bool isLeaf;
25  std::string leafData;
26 };
27 
28 
29 class Trie {
30 public:
31  Trie(): root(new Node) {}
32  ~Trie();
33  void add(const std::string& keys, const std::string& str);
34  void del(const std::string& keys);
35  Node* getRoot() { return root; }
36  void clear() { root->clear(); }
37 
38 private:
39  Node* root;
40 };
41 
42 }; // end namespace teditor
teditor::split
Strings split(const std::string &str, char delim)
Definition: utils.cpp:48
teditor::Trie::getRoot
Node * getRoot()
Definition: trie.h:35
teditor::Node::data
const std::string & data()
Definition: trie.h:19
keys.h
teditor::Node::makeLeaf
void makeLeaf(const std::string &d)
Definition: trie.cpp:47
teditor::Node::addNode
Node * addNode(const std::string &key)
Definition: trie.cpp:8
teditor::Node::Node
Node()
Definition: trie.h:12
teditor::Trie::~Trie
~Trie()
Definition: trie.cpp:19
teditor::Strings
std::vector< std::string > Strings
Definition: utils.h:42
utils.h
teditor::Trie
Definition: trie.h:29
teditor::Trie::add
void add(const std::string &keys, const std::string &str)
Definition: trie.cpp:59
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::Node::delNode
void delNode(const Strings &keys, size_t pos)
Definition: trie.cpp:29
teditor::Node::clear
void clear()
Definition: trie.cpp:53
teditor::Node::getNode
Node * getNode(const std::string &key)
Definition: trie.cpp:24
teditor::Node::~Node
~Node()
Definition: trie.h:13
teditor::Trie::Trie
Trie()
Definition: trie.h:31
teditor::Node
Definition: trie.h:10
teditor::Trie::clear
void clear()
Definition: trie.h:36
teditor
Definition: any.hpp:10
trie.h
teditor::Trie::del
void del(const std::string &keys)
Definition: trie.cpp:73
teditor::Node::leaf
bool leaf() const
Definition: trie.h:18