Add way to remove elements from dense slot maps

This commit is contained in:
kayomn 2023-05-07 14:39:15 +00:00
parent 1ab9bba9da
commit 946c67f154
1 changed files with 27 additions and 2 deletions

View File

@ -97,8 +97,33 @@ pub fn Dense(comptime key: Key, comptime Element: type) type {
}; };
} }
pub fn remove(_: *Self, _: u128) bool { pub fn remove(self: *Self, slot: KeySlot) bool {
// TODO: Implement. const redirect = &self.slots[slot.index];
if (slot.salt != redirect.salt) {
return false;
}
const free_index = redirect.index;
self.values = self.values[0 .. (self.values.len - 1)];
if (self.values.len > 0) {
const free_data = &self.data[free_index];
const free_erase = &self.erase[free_index];
const last_data = &self.data[self.values.len];
const last_erase = &self.erase[self.values.len];
free_data.* = last_data.*;
free_erase.* = last_erase.*;
self.slots[free_erase.*].index = free_index;
}
redirect.salt = math.max(redirect.salt +% 1, 1);
redirect.index = self.next_free;
self.next_free = slot.index;
return true;
} }
}; };
} }