1
0
Fork 0
forked from vv/efemra

adding what is hopefully a bit more safety to table.

This commit is contained in:
Vivianne 2022-07-26 03:16:52 -07:00
parent 73a91195b6
commit b1ac3f4631

View file

@ -110,9 +110,13 @@ pub fn Table(comptime K: type, comptime V: type) type {
};
} else {
self.len += 1;
errdefer self.len -= 1;
try self.keys.append(key);
errdefer self.keys.shrinkRetainingCapacity(self.len - 1);
try self.values.append(val);
errdefer self.values.shrinkRetainingCapacity(self.len - 1);
try self.gens.append(0);
errdefer self.gens.shrinkRetainingCapacity(self.len - 1);
assert(self.len == self.keys.items.len);
assert(self.keys.items.len == self.values.items.len);
@ -134,6 +138,8 @@ pub fn Table(comptime K: type, comptime V: type) type {
pub fn remove(self: *Self, id: Id) !V {
assert(self.len > 0);
try self.free_list.push(id.index);
const uindex = @as(usize, id.index);
const key = self.keys.items[uindex] orelse unreachable;
@ -142,10 +148,9 @@ pub fn Table(comptime K: type, comptime V: type) type {
self.keys.items[uindex] = null;
self.gens.items[uindex] += 1;
self.gens.items[uindex] = self.gens.items[uindex] +% 1;
const val = self.values.items[uindex] orelse unreachable;
self.values.items[uindex] = null;
try self.free_list.push(id.index);
self.len -= 1;
return val;
}