authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-07 12:36:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-07 16:50:33-04:00
logdc23350847f6c6f11dbdbb85c312aad2d4c89ec2
tree17ae1f29a3200bfcd433c60846d319b52b088dd2
parent3b7aa808920dc96c9219a57a9d0f3ac6f85a18de

add std.SegmentedList.Iterator.prev


1 files changed, 27 insertions(+), 2 deletions(-)

std/segmented_list.zig+27-2
......@@ -268,6 +268,25 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
268268 }
269269 return ptr;
270270 }
271
272 pub fn prev(it: &Iterator) ?&T {
273 if (it.index == 0)
274 return null;
275
276 it.index -= 1;
277 if (it.index < prealloc_item_count)
278 return &it.list.prealloc_segment[it.index];
279
280 if (it.box_index == 0) {
281 it.shelf_index -= 1;
282 it.shelf_size /= 2;
283 it.box_index = it.shelf_size - 1;
284 } else {
285 it.box_index -= 1;
286 }
287
288 return &it.list.dynamic_segments[it.shelf_index][it.box_index];
289 }
271290 };
272291
273292 pub fn iterator(self: &Self, start_index: usize) Iterator {
......@@ -316,10 +335,16 @@ fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void {
316335
317336 {
318337 var it = list.iterator(0);
319 var x: i32 = 1;
320 while (it.next()) |item| : (x += 1) {
338 var x: i32 = 0;
339 while (it.next()) |item| {
340 x += 1;
341 assert(*item == x);
342 }
343 assert(x == 100);
344 while (it.prev()) |item| : (x -= 1) {
321345 assert(*item == x);
322346 }
347 assert(x == 0);
323348 }
324349
325350 assert(??list.pop() == 100);