-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.h
71 lines (56 loc) · 1.65 KB
/
tree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include <cassert>
#include <cstdint>
#include <limits>
#include <string>
enum class Color : uint8_t { Red, Black, Unknown };
enum class Direction : uint8_t { Left, Right, Unknown };
namespace {
using NodeIndexType = uint32_t;
}
class NodeIndex {
NodeIndexType Index = std::numeric_limits<NodeIndexType>::max();
public:
constexpr explicit NodeIndex(uint64_t Index) : Index(Index) {}
NodeIndex() = default;
constexpr bool operator==(const NodeIndex &rhs) const {
return Index == rhs.Index;
}
constexpr bool operator!=(const NodeIndex &rhs) const {
return Index != rhs.Index;
}
constexpr bool operator<(const NodeIndex &rhs) const { return Index < rhs.Index; }
constexpr NodeIndexType getValue() const { return Index; }
};
static constexpr NodeIndex Nullptr =
NodeIndex(std::numeric_limits<NodeIndexType>::max());
class alignas(32) Node {
public:
uint64_t key = 0;
uint64_t value = 0;
NodeIndex Up = Nullptr;
NodeIndex left = Nullptr;
NodeIndex right = Nullptr;
Color color = Color::Red;
Direction UpDir = Direction::Unknown;
int8_t Balance = 0;
public:
void setKey(uint64_t k) { key = k; }
void setValue(uint64_t v) { value = v; }
uint64_t getKey() const { return key; }
NodeIndex getLeft() const { return left; }
NodeIndex getRight() const { return right; }
Color getColor() const { return color; }
NodeIndex &getChild(Direction dir) {
if (dir == Direction::Left)
return left;
else if (dir == Direction::Right)
return right;
else {
assert(false);
return left;
}
}
};
extern std::string Color2String(Color);
extern std::string Direction2String(Direction);