add overload that allows optional remove as well
This commit is contained in:
parent
8644899714
commit
6f4f72cf27
1 changed files with 18 additions and 1 deletions
|
@ -137,9 +137,10 @@ pub fn Table(comptime K: type, comptime V: type) type {
|
|||
}
|
||||
}
|
||||
|
||||
/// Remove the item from the table. This will panic if the item does not exist in the table.
|
||||
/// to check first, use tryRemove instead.
|
||||
pub fn remove(self: *Self, id: Id) !V {
|
||||
assert(self.len > 0);
|
||||
|
||||
try self.free_list.push(id.index);
|
||||
const index = id.index;
|
||||
|
||||
|
@ -156,6 +157,11 @@ pub fn Table(comptime K: type, comptime V: type) type {
|
|||
return val;
|
||||
}
|
||||
|
||||
/// Attempt to remove an item from the table, returning null if it does not exist.
|
||||
pub fn tryRemove(self: *Self, id: Id) !?V {
|
||||
return if (self.exists(id)) self.remove(id) else null;
|
||||
}
|
||||
|
||||
pub fn getPtr(self: *Self, id: Id) ?*V {
|
||||
return if (self.exists(id)) &(self.values.items[id.index] orelse unreachable) else null;
|
||||
}
|
||||
|
@ -338,6 +344,17 @@ pub fn RefTable(comptime K: type, comptime V: type) type {
|
|||
return self.table.size();
|
||||
}
|
||||
|
||||
pub fn tryRemove(self: *Self, id: Id) !?V {
|
||||
if (self.table.tryRemove(id)) |v| {
|
||||
self.ref_counts.items[id.index] = 0;
|
||||
return v;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Remove an item from the table. This panics if the item is not already present.
|
||||
/// use tryRemove to verify the item is present.
|
||||
pub fn remove(self: *Self, id: Id) !V {
|
||||
self.ref_counts.items[id.index] = 0;
|
||||
return self.table.remove(id);
|
||||
|
|
Loading…
Reference in a new issue