tree.h
Go to the documentation of this file.
1 //
2 // Created by Eduard Valeyev on 12/29/17.
3 //
4 
5 #ifndef TTG_TREE_H
6 #define TTG_TREE_H
7 
8 #include <cassert>
9 #include <utility>
10 
11 namespace ttg {
12 
18  public:
19  BinarySpanningTree(int size, int root) : size_(size), root_(root) {
20  assert(root >= 0 && root < size);
21  assert(size >= 0);
22  }
23  ~BinarySpanningTree() = default;
24 
26  const auto size() const { return size_; }
28  const auto root() const { return root_; }
29 
32  int parent_key(const int child_key) const {
33  const auto child_rank = (child_key + size_ - root_) % size_; // cyclically shifted key such that root's key is 0
34  const auto parent_key =
35  (child_rank == 0 ? -1 : (((child_rank - 1) >> 1) + root_) % size_); // Parent's key in binary tree
36  return parent_key;
37  }
40  std::pair<int, int> child_keys(const int parent_key) const {
41  const auto parent_rank =
42  (parent_key + size_ - root_) % size_; // cyclically shifted key such that root's key is 0
43  int child0 = (parent_rank << 1) + 1 + root_; // Left child
44  int child1 = child0 + 1; // Right child
45  const int size_plus_root = size_ + root_;
46  if (child0 < size_plus_root)
47  child0 %= size_;
48  else
49  child0 = -1;
50  if (child1 < size_plus_root)
51  child1 %= size_;
52  else
53  child1 = -1;
54  return std::make_pair(child0, child1);
55  }
56 
57  private:
58  int size_;
59  int root_;
60  };
61 
62 } // namespace ttg
63 
64 #endif // TTG_TREE_H
a binary spanning tree of integers in the [0,size) interval
Definition: tree.h:17
int parent_key(const int child_key) const
Definition: tree.h:32
const auto size() const
Definition: tree.h:26
const auto root() const
Definition: tree.h:28
BinarySpanningTree(int size, int root)
Definition: tree.h:19
std::pair< int, int > child_keys(const int parent_key) const
Definition: tree.h:40
top-level TTG namespace contains runtime-neutral functionality
Definition: keymap.h:8