Skip to content

Instantly share code, notes, and snippets.

@rwy7
Created January 17, 2019 18:02
Show Gist options
  • Save rwy7/15b4389afcf0a5e98f5e1abd39e525d4 to your computer and use it in GitHub Desktop.
Save rwy7/15b4389afcf0a5e98f5e1abd39e525d4 to your computer and use it in GitHub Desktop.
// 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