ttg
1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
ttg
ttg
util
tree.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: BSD-3-Clause
2
//
3
// Created by Eduard Valeyev on 12/29/17.
4
//
5
6
#ifndef TTG_TREE_H
7
#define TTG_TREE_H
8
9
#include <cassert>
10
#include <utility>
11
12
namespace
ttg
{
13
18
class
BinarySpanningTree
{
19
public
:
20
BinarySpanningTree
(
int
size
,
int
root
) : size_(
size
), root_(
root
) {
21
assert(
root
>= 0 &&
root
<
size
);
22
assert(
size
>= 0);
23
}
24
~BinarySpanningTree
() =
default
;
25
27
const
auto
size
()
const
{
return
size_; }
29
const
auto
root
()
const
{
return
root_; }
30
33
int
parent_key
(
const
int
child_key)
const
{
34
const
auto
child_rank = (child_key + size_ - root_) % size_;
// cyclically shifted key such that root's key is 0
35
const
auto
parent_key
=
36
(child_rank == 0 ? -1 : (((child_rank - 1) >> 1) + root_) % size_);
// Parent's key in binary tree
37
return
parent_key
;
38
}
41
std::pair<int, int>
child_keys
(
const
int
parent_key
)
const
{
42
const
auto
parent_rank =
43
(
parent_key
+ size_ - root_) % size_;
// cyclically shifted key such that root's key is 0
44
int
child0 = (parent_rank << 1) + 1 + root_;
// Left child
45
int
child1 = child0 + 1;
// Right child
46
const
int
size_plus_root = size_ + root_;
47
if
(child0 < size_plus_root)
48
child0 %= size_;
49
else
50
child0 = -1;
51
if
(child1 < size_plus_root)
52
child1 %= size_;
53
else
54
child1 = -1;
55
return
std::make_pair(child0, child1);
56
}
57
58
private
:
59
int
size_;
60
int
root_;
61
};
62
63
}
// namespace ttg
64
65
#endif
// TTG_TREE_H
ttg::BinarySpanningTree
a binary spanning tree of integers in the [0,size) interval
Definition
tree.h:18
ttg::BinarySpanningTree::parent_key
int parent_key(const int child_key) const
Definition
tree.h:33
ttg::BinarySpanningTree::size
const auto size() const
Definition
tree.h:27
ttg::BinarySpanningTree::root
const auto root() const
Definition
tree.h:29
ttg::BinarySpanningTree::child_keys
std::pair< int, int > child_keys(const int parent_key) const
Definition
tree.h:41
ttg::BinarySpanningTree::~BinarySpanningTree
~BinarySpanningTree()=default
ttg::BinarySpanningTree::BinarySpanningTree
BinarySpanningTree(int size, int root)
Definition
tree.h:20
ttg
top-level TTG namespace contains runtime-neutral functionality
Definition
keymap.h:9
Generated at Tue Nov 25 2025 21:38:21 for
ttg
1.0.0 by
1.9.8