Created
January 17, 2019 18:02
-
-
Save rwy7/15b4389afcf0a5e98f5e1abd39e525d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Branch factor, power of two. | |
static constexpr SizeP2 RADIX = SIZEP2_32; | |
enum class NodeKind { | |
LEAF = 1, BRANCH | |
}; | |
class Node : public RefCounted { | |
public: | |
constexpr Node(NodeKind kind) noexcept : kind_(kind) {} | |
private: | |
NodeKind kind_; | |
}; | |
class Leaf final : public Node { | |
public: | |
Leaf() : Node(NodeKind::LEAF) {} | |
~Leaf() = default; | |
T values[std::size_t(RADIX)]; | |
}; | |
class Branch final : public Node { | |
public: | |
Branch() : Node(NodeKind::BRANCH) {} | |
~Branch() = default; | |
Rc<const Node> children[std::size_t(RADIX)] = {0}; | |
}; | |
class Either final : public Node { | |
~Either() noexcept { | |
switch(kind_) { | |
case NodeKind::LEAF: | |
static_cast<Leaf*>(this)->~Leaf(); | |
break; | |
case NodeKind::BRANCH: | |
static_cast<Branch*>(this)->~Branch(); | |
break; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment