2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("std.zig");
|
2018-05-07 06:04:43 +01:00
|
|
|
const assert = std.debug.assert;
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2018-05-07 06:04:43 +01:00
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
|
2018-08-03 22:59:11 +01:00
|
|
|
// Imagine that `fn at(self: *Self, index: usize) &T` is a customer asking for a box
|
2018-05-07 06:04:43 +01:00
|
|
|
// from a warehouse, based on a flat array, boxes ordered from 0 to N - 1.
|
|
|
|
// But the warehouse actually stores boxes in shelves of increasing powers of 2 sizes.
|
2018-05-29 01:23:55 +01:00
|
|
|
// So when the customer requests a box index, we have to translate it to shelf index
|
2018-05-07 06:04:43 +01:00
|
|
|
// and box index within that shelf. Illustration:
|
|
|
|
//
|
|
|
|
// customer indexes:
|
|
|
|
// shelf 0: 0
|
|
|
|
// shelf 1: 1 2
|
|
|
|
// shelf 2: 3 4 5 6
|
|
|
|
// shelf 3: 7 8 9 10 11 12 13 14
|
|
|
|
// shelf 4: 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
|
|
|
// shelf 5: 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// warehouse indexes:
|
2018-05-07 14:53:52 +01:00
|
|
|
// shelf 0: 0
|
|
|
|
// shelf 1: 0 1
|
|
|
|
// shelf 2: 0 1 2 3
|
|
|
|
// shelf 3: 0 1 2 3 4 5 6 7
|
|
|
|
// shelf 4: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
|
|
|
// shelf 5: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
2018-05-07 06:04:43 +01:00
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// With this arrangement, here are the equations to get the shelf index and
|
|
|
|
// box index based on customer box index:
|
|
|
|
//
|
|
|
|
// shelf_index = floor(log2(customer_index + 1))
|
|
|
|
// shelf_count = ceil(log2(box_count + 1))
|
|
|
|
// box_index = customer_index + 1 - 2 ** shelf
|
|
|
|
// shelf_size = 2 ** shelf_index
|
|
|
|
//
|
|
|
|
// Now we complicate it a little bit further by adding a preallocated shelf, which must be
|
|
|
|
// a power of 2:
|
|
|
|
// prealloc=4
|
2018-05-29 01:23:55 +01:00
|
|
|
//
|
2018-05-07 06:04:43 +01:00
|
|
|
// customer indexes:
|
|
|
|
// prealloc: 0 1 2 3
|
|
|
|
// shelf 0: 4 5 6 7 8 9 10 11
|
|
|
|
// shelf 1: 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
|
|
|
// shelf 2: 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
|
|
|
// ...
|
2018-05-29 01:23:55 +01:00
|
|
|
//
|
2018-05-07 06:04:43 +01:00
|
|
|
// warehouse indexes:
|
|
|
|
// prealloc: 0 1 2 3
|
|
|
|
// shelf 0: 0 1 2 3 4 5 6 7
|
|
|
|
// shelf 1: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
|
|
|
// shelf 2: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
|
|
|
// ...
|
|
|
|
//
|
|
|
|
// Now the equations are:
|
|
|
|
//
|
|
|
|
// shelf_index = floor(log2(customer_index + prealloc)) - log2(prealloc) - 1
|
|
|
|
// shelf_count = ceil(log2(box_count + prealloc)) - log2(prealloc) - 1
|
|
|
|
// box_index = customer_index + prealloc - 2 ** (log2(prealloc) + 1 + shelf)
|
|
|
|
// shelf_size = prealloc * 2 ** (shelf_index + 1)
|
|
|
|
|
|
|
|
/// This is a stack data structure where pointers to indexes have the same lifetime as the data structure
|
|
|
|
/// itself, unlike ArrayList where push() invalidates all existing element pointers.
|
|
|
|
/// The tradeoff is that elements are not guaranteed to be contiguous. For that, use ArrayList.
|
|
|
|
/// Note however that most elements are contiguous, making this data structure cache-friendly.
|
|
|
|
///
|
|
|
|
/// Because it never has to copy elements from an old location to a new location, it does not require
|
|
|
|
/// its elements to be copyable, and it avoids wasting memory when backed by an ArenaAllocator.
|
2018-05-07 14:53:52 +01:00
|
|
|
/// Note that the push() and pop() convenience methods perform a copy, but you can instead use
|
|
|
|
/// addOne(), at(), setCapacity(), and shrinkCapacity() to avoid copying items.
|
2018-05-07 06:04:43 +01:00
|
|
|
///
|
|
|
|
/// This data structure has O(1) push and O(1) pop.
|
|
|
|
///
|
|
|
|
/// It supports preallocated elements, making it especially well suited when the expected maximum
|
|
|
|
/// size is small. `prealloc_item_count` must be 0, or a power of 2.
|
|
|
|
pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type {
|
2018-11-13 13:08:37 +00:00
|
|
|
return struct {
|
2018-09-13 21:34:33 +01:00
|
|
|
const Self = @This();
|
2019-07-26 00:59:34 +01:00
|
|
|
const ShelfIndex = std.math.Log2Int(usize);
|
2018-05-07 14:53:52 +01:00
|
|
|
|
2019-07-26 00:59:34 +01:00
|
|
|
const prealloc_exp: ShelfIndex = blk: {
|
|
|
|
// we don't use the prealloc_exp constant when prealloc_item_count is 0
|
|
|
|
// but lazy-init may still be triggered by other code so supply a value
|
|
|
|
if (prealloc_item_count == 0) {
|
|
|
|
break :blk 0;
|
|
|
|
} else {
|
|
|
|
assert(std.math.isPowerOfTwo(prealloc_item_count));
|
|
|
|
const value = std.math.log2_int(usize, prealloc_item_count);
|
|
|
|
break :blk value;
|
|
|
|
}
|
2018-05-07 06:04:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
prealloc_segment: [prealloc_item_count]T,
|
2018-06-04 06:09:15 +01:00
|
|
|
dynamic_segments: [][*]T,
|
2018-05-31 15:56:59 +01:00
|
|
|
allocator: *Allocator,
|
2018-05-07 17:01:20 +01:00
|
|
|
len: usize,
|
2018-05-07 06:04:43 +01:00
|
|
|
|
2018-05-08 02:57:44 +01:00
|
|
|
pub const prealloc_count = prealloc_item_count;
|
|
|
|
|
2018-08-03 22:59:11 +01:00
|
|
|
fn AtType(comptime SelfType: type) type {
|
|
|
|
if (@typeInfo(SelfType).Pointer.is_const) {
|
|
|
|
return *const T;
|
|
|
|
} else {
|
|
|
|
return *T;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:04:43 +01:00
|
|
|
/// Deinitialize with `deinit`
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn init(allocator: *Allocator) Self {
|
2018-11-13 13:08:37 +00:00
|
|
|
return Self{
|
2018-05-07 06:04:43 +01:00
|
|
|
.allocator = allocator,
|
|
|
|
.len = 0,
|
|
|
|
.prealloc_segment = undefined,
|
2019-11-27 08:30:39 +00:00
|
|
|
.dynamic_segments = &[_][*]T{},
|
2018-05-07 06:04:43 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn deinit(self: *Self) void {
|
2018-06-17 07:57:07 +01:00
|
|
|
self.freeShelves(@intCast(ShelfIndex, self.dynamic_segments.len), 0);
|
2018-05-07 06:04:43 +01:00
|
|
|
self.allocator.free(self.dynamic_segments);
|
2018-05-10 05:29:49 +01:00
|
|
|
self.* = undefined;
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
|
2018-08-03 22:59:11 +01:00
|
|
|
pub fn at(self: var, i: usize) AtType(@typeOf(self)) {
|
2018-05-07 06:04:43 +01:00
|
|
|
assert(i < self.len);
|
|
|
|
return self.uncheckedAt(i);
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:23:47 +01:00
|
|
|
pub fn count(self: Self) usize {
|
2018-05-07 06:04:43 +01:00
|
|
|
return self.len;
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:23:47 +01:00
|
|
|
pub fn push(self: *Self, item: T) !void {
|
2018-05-07 06:04:43 +01:00
|
|
|
const new_item_ptr = try self.addOne();
|
2018-10-15 23:23:47 +01:00
|
|
|
new_item_ptr.* = item;
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn pushMany(self: *Self, items: []const T) !void {
|
2018-05-07 06:04:43 +01:00
|
|
|
for (items) |item| {
|
|
|
|
try self.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn pop(self: *Self) ?T {
|
2018-05-10 05:29:49 +01:00
|
|
|
if (self.len == 0) return null;
|
2018-05-07 06:04:43 +01:00
|
|
|
|
|
|
|
const index = self.len - 1;
|
2018-08-03 22:59:11 +01:00
|
|
|
const result = uncheckedAt(self, index).*;
|
2018-05-07 06:04:43 +01:00
|
|
|
self.len = index;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn addOne(self: *Self) !*T {
|
2018-05-07 06:04:43 +01:00
|
|
|
const new_length = self.len + 1;
|
2018-05-07 15:34:38 +01:00
|
|
|
try self.growCapacity(new_length);
|
2018-08-03 22:59:11 +01:00
|
|
|
const result = uncheckedAt(self, self.len);
|
2018-05-07 06:04:43 +01:00
|
|
|
self.len = new_length;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-07 15:34:38 +01:00
|
|
|
/// Grows or shrinks capacity to match usage.
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn setCapacity(self: *Self, new_capacity: usize) !void {
|
2019-07-26 00:59:34 +01:00
|
|
|
if (prealloc_item_count != 0) {
|
2019-11-07 04:25:57 +00:00
|
|
|
if (new_capacity <= @as(usize, 1) << (prealloc_exp + @intCast(ShelfIndex, self.dynamic_segments.len))) {
|
2019-07-26 00:59:34 +01:00
|
|
|
return self.shrinkCapacity(new_capacity);
|
|
|
|
}
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
2019-07-26 00:59:34 +01:00
|
|
|
return self.growCapacity(new_capacity);
|
2018-05-07 15:34:38 +01:00
|
|
|
}
|
2018-05-07 06:04:43 +01:00
|
|
|
|
2018-05-07 15:34:38 +01:00
|
|
|
/// Only grows capacity, or retains current capacity
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn growCapacity(self: *Self, new_capacity: usize) !void {
|
2018-05-07 06:04:43 +01:00
|
|
|
const new_cap_shelf_count = shelfCount(new_capacity);
|
2018-06-17 07:57:07 +01:00
|
|
|
const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
|
2018-05-07 06:04:43 +01:00
|
|
|
if (new_cap_shelf_count > old_shelf_count) {
|
2019-03-15 21:47:47 +00:00
|
|
|
self.dynamic_segments = try self.allocator.realloc(self.dynamic_segments, new_cap_shelf_count);
|
2018-05-07 06:04:43 +01:00
|
|
|
var i = old_shelf_count;
|
|
|
|
errdefer {
|
|
|
|
self.freeShelves(i, old_shelf_count);
|
2019-03-15 21:47:47 +00:00
|
|
|
self.dynamic_segments = self.allocator.shrink(self.dynamic_segments, old_shelf_count);
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
while (i < new_cap_shelf_count) : (i += 1) {
|
|
|
|
self.dynamic_segments[i] = (try self.allocator.alloc(T, shelfSize(i))).ptr;
|
|
|
|
}
|
2018-05-07 15:34:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Only shrinks capacity or retains current capacity
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn shrinkCapacity(self: *Self, new_capacity: usize) void {
|
2018-05-07 15:34:38 +01:00
|
|
|
if (new_capacity <= prealloc_item_count) {
|
2018-06-17 07:57:07 +01:00
|
|
|
const len = @intCast(ShelfIndex, self.dynamic_segments.len);
|
2018-05-07 15:34:38 +01:00
|
|
|
self.freeShelves(len, 0);
|
|
|
|
self.allocator.free(self.dynamic_segments);
|
2019-11-27 08:30:39 +00:00
|
|
|
self.dynamic_segments = &[_][*]T{};
|
2018-05-07 06:04:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-05-07 15:34:38 +01:00
|
|
|
|
|
|
|
const new_cap_shelf_count = shelfCount(new_capacity);
|
2018-06-17 07:57:07 +01:00
|
|
|
const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
|
2018-05-07 15:34:38 +01:00
|
|
|
assert(new_cap_shelf_count <= old_shelf_count);
|
2018-05-07 06:04:43 +01:00
|
|
|
if (new_cap_shelf_count == old_shelf_count) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-07 15:34:38 +01:00
|
|
|
|
2018-05-07 06:04:43 +01:00
|
|
|
self.freeShelves(old_shelf_count, new_cap_shelf_count);
|
2019-03-15 21:47:47 +00:00
|
|
|
self.dynamic_segments = self.allocator.shrink(self.dynamic_segments, new_cap_shelf_count);
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
|
2018-09-30 08:02:01 +01:00
|
|
|
pub fn shrink(self: *Self, new_len: usize) void {
|
|
|
|
assert(new_len <= self.len);
|
2019-03-15 21:47:47 +00:00
|
|
|
// TODO take advantage of the new realloc semantics
|
2018-09-30 08:02:01 +01:00
|
|
|
self.len = new_len;
|
|
|
|
}
|
|
|
|
|
2018-08-03 22:59:11 +01:00
|
|
|
pub fn uncheckedAt(self: var, index: usize) AtType(@typeOf(self)) {
|
2018-05-07 06:04:43 +01:00
|
|
|
if (index < prealloc_item_count) {
|
|
|
|
return &self.prealloc_segment[index];
|
|
|
|
}
|
|
|
|
const shelf_index = shelfIndex(index);
|
|
|
|
const box_index = boxIndex(index, shelf_index);
|
|
|
|
return &self.dynamic_segments[shelf_index][box_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shelfCount(box_count: usize) ShelfIndex {
|
|
|
|
if (prealloc_item_count == 0) {
|
|
|
|
return std.math.log2_int_ceil(usize, box_count + 1);
|
|
|
|
}
|
2018-05-07 14:53:52 +01:00
|
|
|
return std.math.log2_int_ceil(usize, box_count + prealloc_item_count) - prealloc_exp - 1;
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shelfSize(shelf_index: ShelfIndex) usize {
|
|
|
|
if (prealloc_item_count == 0) {
|
2019-11-07 04:25:57 +00:00
|
|
|
return @as(usize, 1) << shelf_index;
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
2019-11-07 04:25:57 +00:00
|
|
|
return @as(usize, 1) << (shelf_index + (prealloc_exp + 1));
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shelfIndex(list_index: usize) ShelfIndex {
|
|
|
|
if (prealloc_item_count == 0) {
|
|
|
|
return std.math.log2_int(usize, list_index + 1);
|
|
|
|
}
|
2018-05-07 14:53:52 +01:00
|
|
|
return std.math.log2_int(usize, list_index + prealloc_item_count) - prealloc_exp - 1;
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn boxIndex(list_index: usize, shelf_index: ShelfIndex) usize {
|
|
|
|
if (prealloc_item_count == 0) {
|
2019-11-07 04:25:57 +00:00
|
|
|
return (list_index + 1) - (@as(usize, 1) << shelf_index);
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
2019-11-07 04:25:57 +00:00
|
|
|
return list_index + prealloc_item_count - (@as(usize, 1) << ((prealloc_exp + 1) + shelf_index));
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
fn freeShelves(self: *Self, from_count: ShelfIndex, to_count: ShelfIndex) void {
|
2018-05-07 06:04:43 +01:00
|
|
|
var i = from_count;
|
|
|
|
while (i != to_count) {
|
|
|
|
i -= 1;
|
|
|
|
self.allocator.free(self.dynamic_segments[i][0..shelfSize(i)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const Iterator = struct {
|
2018-05-31 15:56:59 +01:00
|
|
|
list: *Self,
|
2018-05-07 17:01:20 +01:00
|
|
|
index: usize,
|
|
|
|
box_index: usize,
|
|
|
|
shelf_index: ShelfIndex,
|
|
|
|
shelf_size: usize,
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn next(it: *Iterator) ?*T {
|
2018-05-10 05:29:49 +01:00
|
|
|
if (it.index >= it.list.len) return null;
|
2018-05-07 17:01:20 +01:00
|
|
|
if (it.index < prealloc_item_count) {
|
|
|
|
const ptr = &it.list.prealloc_segment[it.index];
|
|
|
|
it.index += 1;
|
|
|
|
if (it.index == prealloc_item_count) {
|
|
|
|
it.box_index = 0;
|
|
|
|
it.shelf_index = 0;
|
|
|
|
it.shelf_size = prealloc_item_count * 2;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ptr = &it.list.dynamic_segments[it.shelf_index][it.box_index];
|
|
|
|
it.index += 1;
|
|
|
|
it.box_index += 1;
|
|
|
|
if (it.box_index == it.shelf_size) {
|
|
|
|
it.shelf_index += 1;
|
|
|
|
it.box_index = 0;
|
|
|
|
it.shelf_size *= 2;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
2018-05-07 17:36:54 +01:00
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn prev(it: *Iterator) ?*T {
|
2018-05-10 05:29:49 +01:00
|
|
|
if (it.index == 0) return null;
|
2018-05-07 17:36:54 +01:00
|
|
|
|
|
|
|
it.index -= 1;
|
2018-05-10 05:29:49 +01:00
|
|
|
if (it.index < prealloc_item_count) return &it.list.prealloc_segment[it.index];
|
2018-05-07 17:36:54 +01:00
|
|
|
|
|
|
|
if (it.box_index == 0) {
|
|
|
|
it.shelf_index -= 1;
|
|
|
|
it.shelf_size /= 2;
|
|
|
|
it.box_index = it.shelf_size - 1;
|
|
|
|
} else {
|
|
|
|
it.box_index -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &it.list.dynamic_segments[it.shelf_index][it.box_index];
|
|
|
|
}
|
2018-05-08 02:57:44 +01:00
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn peek(it: *Iterator) ?*T {
|
2018-05-08 02:57:44 +01:00
|
|
|
if (it.index >= it.list.len)
|
|
|
|
return null;
|
|
|
|
if (it.index < prealloc_item_count)
|
|
|
|
return &it.list.prealloc_segment[it.index];
|
|
|
|
|
|
|
|
return &it.list.dynamic_segments[it.shelf_index][it.box_index];
|
|
|
|
}
|
2018-05-17 05:44:55 +01:00
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn set(it: *Iterator, index: usize) void {
|
2018-05-17 05:52:36 +01:00
|
|
|
it.index = index;
|
|
|
|
if (index < prealloc_item_count) return;
|
2018-05-17 05:44:55 +01:00
|
|
|
it.shelf_index = shelfIndex(index);
|
|
|
|
it.box_index = boxIndex(index, it.shelf_index);
|
|
|
|
it.shelf_size = shelfSize(it.shelf_index);
|
|
|
|
}
|
2018-05-07 17:01:20 +01:00
|
|
|
};
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn iterator(self: *Self, start_index: usize) Iterator {
|
2018-11-13 13:08:37 +00:00
|
|
|
var it = Iterator{
|
2018-05-07 17:01:20 +01:00
|
|
|
.list = self,
|
2018-05-17 05:44:55 +01:00
|
|
|
.index = undefined,
|
2018-05-07 17:01:20 +01:00
|
|
|
.shelf_index = undefined,
|
|
|
|
.box_index = undefined,
|
|
|
|
.shelf_size = undefined,
|
|
|
|
};
|
2018-05-17 05:44:55 +01:00
|
|
|
it.set(start_index);
|
2018-05-07 17:01:20 +01:00
|
|
|
return it;
|
|
|
|
}
|
2018-05-07 06:04:43 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "std.SegmentedList" {
|
2019-11-25 22:25:06 +00:00
|
|
|
var a = std.heap.page_allocator;
|
2018-05-07 06:04:43 +01:00
|
|
|
|
|
|
|
try testSegmentedList(0, a);
|
|
|
|
try testSegmentedList(1, a);
|
|
|
|
try testSegmentedList(2, a);
|
|
|
|
try testSegmentedList(4, a);
|
|
|
|
try testSegmentedList(8, a);
|
|
|
|
try testSegmentedList(16, a);
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:56:59 +01:00
|
|
|
fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
|
2018-05-07 06:04:43 +01:00
|
|
|
var list = SegmentedList(i32, prealloc).init(allocator);
|
|
|
|
defer list.deinit();
|
|
|
|
|
2018-05-10 05:29:49 +01:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < 100) : (i += 1) {
|
2018-06-17 07:57:07 +01:00
|
|
|
try list.push(@intCast(i32, i + 1));
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(list.len == i + 1);
|
2018-05-10 05:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 06:04:43 +01:00
|
|
|
|
2018-05-10 05:29:49 +01:00
|
|
|
{
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < 100) : (i += 1) {
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(list.at(i).* == @intCast(i32, i + 1));
|
2018-05-10 05:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-07 06:04:43 +01:00
|
|
|
|
2018-05-07 17:01:20 +01:00
|
|
|
{
|
|
|
|
var it = list.iterator(0);
|
2018-05-07 17:36:54 +01:00
|
|
|
var x: i32 = 0;
|
|
|
|
while (it.next()) |item| {
|
|
|
|
x += 1;
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(item.* == x);
|
2018-05-07 17:36:54 +01:00
|
|
|
}
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(x == 100);
|
2018-05-07 17:36:54 +01:00
|
|
|
while (it.prev()) |item| : (x -= 1) {
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(item.* == x);
|
2018-05-07 17:01:20 +01:00
|
|
|
}
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(x == 0);
|
2018-05-07 17:01:20 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(list.pop().? == 100);
|
|
|
|
testing.expect(list.len == 99);
|
2018-05-07 06:04:43 +01:00
|
|
|
|
2019-11-27 08:30:39 +00:00
|
|
|
try list.pushMany(&[_]i32{ 1, 2, 3 });
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(list.len == 102);
|
|
|
|
testing.expect(list.pop().? == 3);
|
|
|
|
testing.expect(list.pop().? == 2);
|
|
|
|
testing.expect(list.pop().? == 1);
|
|
|
|
testing.expect(list.len == 99);
|
2018-05-07 06:04:43 +01:00
|
|
|
|
2019-11-27 08:30:39 +00:00
|
|
|
try list.pushMany(&[_]i32{});
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(list.len == 99);
|
2018-05-07 06:04:43 +01:00
|
|
|
|
|
|
|
var i: i32 = 99;
|
|
|
|
while (list.pop()) |item| : (i -= 1) {
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(item == i);
|
2018-05-07 06:04:43 +01:00
|
|
|
list.shrinkCapacity(list.len);
|
|
|
|
}
|
2019-07-26 00:59:34 +01:00
|
|
|
|
|
|
|
try list.setCapacity(0);
|
2018-05-07 06:04:43 +01:00
|
|
|
}
|