Skip to content

Commit

Permalink
feat(wasm-api): add ManagedIndex.initCapacity()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 23, 2023
1 parent babe681 commit 788dfa2
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/wasm-api/zig/managed-index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ pub fn ManagedIndex(comptime T: type, comptime I: type) type {
};
}

/// Initializes the underlying array list with given initial capacity
pub fn initCapacity(allocator: Allocator, num: usize) !Self {
return .{
.list = try std.ArrayList(Item).initCapacity(allocator, num),
};
}

/// De-initializes the underlying array list
pub fn deinit(self: *Self) void {
self.list.deinit();
Expand Down Expand Up @@ -89,7 +96,7 @@ pub fn ManagedIndex(comptime T: type, comptime I: type) type {
}

/// Returns true if given `id` is valid, i.e. is referring to a
/// currently used item (and isn't part of the list of free slots).
/// currently used item and isn't part of the list of free slots.
pub fn has(self: *const Self, id: I) bool {
if (id >= self.list.items.len) return false;
var freeID = self.freeID;
Expand All @@ -112,7 +119,7 @@ pub fn ManagedIndex(comptime T: type, comptime I: type) type {
return .{ .parent = parent };
}

pub fn next(self: *@This()) ?T {
pub inline fn next(self: *@This()) ?T {
const items = self.parent.list.items;
if (self.i >= items.len) return null;
while (self.i < items.len) {
Expand Down Expand Up @@ -155,8 +162,8 @@ pub fn ManagedIndex(comptime T: type, comptime I: type) type {
while (i < slice.len) {
if (freeID) |fid| {
slice[i] = fid;
i += 1;
freeID = self.list.items[fid].id;
i += 1;
} else {
break;
}
Expand Down

0 comments on commit 788dfa2

Please sign in to comment.