Rely on bitSet for child existence in Node48

This commit is contained in:
2024-02-09 18:14:07 -08:00
parent f961947e33
commit ddca6e8511

View File

@@ -152,10 +152,7 @@ struct Node48 : Node {
Node *children[48] = {};
int8_t nextFree = 0;
int8_t index[256];
Node48() {
this->type = Type::Node48;
memset(index, -1, 256);
}
Node48() { this->type = Type::Node48; }
};
struct Node256 : Node {
@@ -241,10 +238,8 @@ Node *&getChildExists(Node *self, uint8_t index) {
return self16->children[getNodeIndex(self16, index)];
} else if (self->type == Type::Node48) {
auto *self48 = static_cast<Node48 *>(self);
int secondIndex = self48->index[index];
if (secondIndex >= 0) {
return self48->children[secondIndex];
}
assert(self48->bitSet.test(index));
return self48->children[self48->index[index]];
} else {
auto *self256 = static_cast<Node256 *>(self);
return self256->children[index];
@@ -416,15 +411,14 @@ Node *&getOrCreateChild(Node *&self, uint8_t index) {
} else if (self->type == Type::Node48) {
insert48:
auto *self48 = static_cast<Node48 *>(self);
int secondIndex = self48->index[index];
if (secondIndex >= 0) {
return self48->children[secondIndex];
if (self48->bitSet.test(index)) {
return self48->children[self48->index[index]];
}
if (self->numChildren == 48) {
auto *newSelf = new (safe_malloc(sizeof(Node256))) Node256;
memcpy((void *)newSelf, self, offsetof(Node, type));
for (int i = 0; i < 256; ++i) {
if (self48->index[i] >= 0) {
if (self48->bitSet.test(i)) {
newSelf->bitSet.set(i);
newSelf->children[i] = self48->children[self48->index[i]];
}
@@ -475,7 +469,7 @@ void eraseChild(Node *self, uint8_t index) {
} else if (self->type == Type::Node48) {
auto *self48 = static_cast<Node48 *>(self);
self48->bitSet.reset(index);
int8_t toRemoveChildrenIndex = std::exchange(self48->index[index], -1);
int8_t toRemoveChildrenIndex = self48->index[index];
int8_t lastChildrenIndex = --self48->nextFree;
assert(toRemoveChildrenIndex >= 0);
assert(lastChildrenIndex >= 0);