Skip to content

Commit ddccf82

Browse files
committed
Refactor
1 parent 5195781 commit ddccf82

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

std/array_list.zig

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -229,39 +229,18 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
229229
it.removed_index = null;
230230
}
231231

232-
//
233-
// NOTE: inlining these removal routines is important.
234-
// It gains ~5% in debug mode, and ~8% in release-safe; within 1% of the performance of ArrayList.swapRemove/orderedRemove.
235-
//
236-
// Also note that you'd think that just checking for the index==0 in the removal routines would be
237-
// good for making it illegal if next has not been called yet.
238-
// But no, it is slower to do this for release-safe, and debug modes.
239-
//
240-
241-
pub inline fn swapRemove(it: *Iterator) T {
242-
const cursor = it.cursor orelse @panic("must call next at least once");
243-
if (it.removed_index == null or
244-
it.removed_index.? != cursor
245-
) {
246-
it.removed_index = cursor;
247-
return it.list.swapRemove(cursor);
248-
} else switch (builtin.mode) {
249-
.ReleaseFast => unreachable,
250-
else => @panic("removed element more than once"),
251-
}
232+
pub fn swapRemove(it: *Iterator) T {
233+
var cursor = it.cursor.?; // must call .next() at least once
234+
if (it.removed_index) |ri| assert(ri != cursor); // removed the same element more than once
235+
it.removed_index = cursor;
236+
return it.list.swapRemove(cursor);
252237
}
253238

254-
pub inline fn orderedRemove(it: *Iterator) T {
255-
const cursor = it.cursor orelse @panic("must call next at least once");
256-
if (it.removed_index == null or
257-
it.removed_index.? != cursor
258-
) {
259-
it.removed_index = cursor;
260-
return it.list.orderedRemove(cursor);
261-
} else switch (builtin.mode) {
262-
.ReleaseFast => unreachable,
263-
else => @panic("removed element more than once"),
264-
}
239+
pub fn orderedRemove(it: *Iterator) T {
240+
var cursor = it.cursor.?; // must call .next() at least once
241+
if (it.removed_index) |ri| assert(ri != cursor); // removed the same element more than once
242+
it.removed_index = cursor;
243+
return it.list.orderedRemove(cursor);
265244
}
266245
};
267246

0 commit comments

Comments
 (0)