Add way to remove elements from dense slot maps
This commit is contained in:
parent
1ab9bba9da
commit
946c67f154
|
@ -97,8 +97,33 @@ pub fn Dense(comptime key: Key, comptime Element: type) type {
|
|||
};
|
||||
}
|
||||
|
||||
pub fn remove(_: *Self, _: u128) bool {
|
||||
// TODO: Implement.
|
||||
pub fn remove(self: *Self, slot: KeySlot) bool {
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue