panwave
Tree.h
1 //-------------------------------------------------------------------------------------------------------
2 // Copyright (C) Taylor Woll and panwave contributors. All rights reserved.
3 // Licensed under the MIT license. See LICENSE.txt file in the project root for
4 // full license information.
5 //-------------------------------------------------------------------------------------------------------
6 
7 #ifndef TREE_H
8 #define TREE_H
9 
10 #include <cassert>
11 #include <cmath>
12 #include <vector>
13 
14 namespace panwave {
15 
31 template <class Element, size_t k>
32 class Tree {
33  public:
39  explicit Tree(size_t height) : height_(height) {
40  assert(height != 0);
41 
42  this->leaf_count_ = static_cast<size_t>(std::pow(k, height_ - 1));
43  size_t const total_nodes = (k * this->leaf_count_ - 1) / (k - 1);
44 
45  this->nodes_.resize(total_nodes);
46  this->mark_.resize(total_nodes);
47  }
48 
49  protected:
55  size_t GetLeafCount() const { return this->leaf_count_; }
56 
60  size_t GetFirstLeaf() const {
61  return this->nodes_.size() - this->leaf_count_;
62  }
63 
67  size_t GetLastLeaf() const { return this->nodes_.size() - 1; }
68 
73  bool IsLeaf(size_t node) const {
74  return node >= this->GetFirstLeaf() && node <= this->GetLastLeaf();
75  }
76 
85  size_t GetChild(size_t parent, size_t child_index) const {
86  assert(child_index < k);
87  assert(parent < this->nodes_.size());
88 
89  return k * parent + 1 + child_index;
90  }
91 
96  size_t GetParent(size_t child) const {
97  assert(child < this->nodes_.size());
98  assert(child != 0);
99 
100  return (child - 1) / k;
101  }
102 
106  Element& GetNodeData(size_t index) {
107  assert(index < this->nodes_.size());
108 
109  return this->nodes_.at(index);
110  }
111 
116  bool IsMarked(size_t node) const {
117  assert(node < this->mark_.size());
118 
119  return this->mark_[node];
120  }
121 
126  void SetMark(size_t node) {
127  assert(node < this->mark_.size());
128 
129  this->mark_[node] = true;
130  }
131 
135  void Unmark() { this->mark_.assign(this->mark_.size(), false); }
136 
143  size_t GetHeight() const { return this->height_; }
144 
145  private:
146  size_t height_;
147  size_t leaf_count_ = 0;
148  std::vector<Element> nodes_;
149  std::vector<bool> mark_;
150 };
151 
152 } // namespace panwave
153 
154 #endif // TREE_H
panwave::Tree::GetFirstLeaf
size_t GetFirstLeaf() const
Definition: Tree.h:60
panwave::Tree::SetMark
void SetMark(size_t node)
Definition: Tree.h:126
panwave::Tree::GetHeight
size_t GetHeight() const
Definition: Tree.h:143
panwave::Tree::GetChild
size_t GetChild(size_t parent, size_t child_index) const
Definition: Tree.h:85
panwave::Tree::IsMarked
bool IsMarked(size_t node) const
Definition: Tree.h:116
panwave::Tree::Tree
Tree(size_t height)
Definition: Tree.h:39
panwave::Tree
Definition: Tree.h:32
panwave::Tree::Unmark
void Unmark()
Definition: Tree.h:135
panwave::Tree::GetNodeData
Element & GetNodeData(size_t index)
Definition: Tree.h:106
panwave::Tree::GetLastLeaf
size_t GetLastLeaf() const
Definition: Tree.h:67
panwave::Tree::GetLeafCount
size_t GetLeafCount() const
Definition: Tree.h:55
panwave::Tree::IsLeaf
bool IsLeaf(size_t node) const
Definition: Tree.h:73
panwave::Tree::GetParent
size_t GetParent(size_t child) const
Definition: Tree.h:96