-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraylist.zig
More file actions
120 lines (95 loc) · 3.44 KB
/
arraylist.zig
File metadata and controls
120 lines (95 loc) · 3.44 KB
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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const std = @import("std");
pub fn ArrayList(comptime T: type) type {
return struct {
items: []T = &.{},
len: usize = 0,
capacity: usize = 0,
allocator: std.mem.Allocator,
pub fn init(allocator: std.mem.Allocator) @This() {
return .{
.allocator = allocator,
};
}
pub fn deinit(self: *@This()) void {
if (self.items.len > 0) {
self.allocator.free(self.items);
}
self.* = undefined;
}
pub fn append(self: *@This(), item: T) !void {
if (self.len >= self.capacity) {
const new_capacity = if (self.capacity == 0) 8 else self.capacity * 2;
const new_items = try self.allocator.alloc(T, new_capacity);
@memcpy(new_items[0..self.len], self.items);
if (self.items.len > 0) {
self.allocator.free(self.items);
}
self.items = new_items;
self.capacity = new_capacity;
}
self.items[self.len] = item;
self.len += 1;
}
pub fn pop(self: *@This()) ?T {
if (self.len == 0) return null;
self.len -= 1;
return self.items[self.len];
}
pub fn get(self: *const @This(), index: usize) ?*const T {
if (index >= self.len) return null;
return &self.items[index];
}
pub fn clear(self: *@This()) void {
self.len = 0;
}
};
}
test "ArrayList basic" {
var list = ArrayList(i32).init(std.testing.allocator);
defer list.deinit();
try list.append(1);
try list.append(2);
try list.append(3);
try std.testing.expectEqual(list.len, 3);
try std.testing.expectEqual(list.pop(), 3);
try std.testing.expectEqual(list.len, 2);
}
test "ArrayList const get returns const pointer" {
var list = ArrayList(i32).init(std.testing.allocator);
defer list.deinit();
try list.append(123);
const const_list = list;
try std.testing.expectEqual(*const i32, @TypeOf(const_list.get(0).?));
}
test "ArrayList empty pop returns null" {
var list = ArrayList(i32).init(std.testing.allocator);
defer list.deinit();
try std.testing.expectEqual(@as(?i32, null), list.pop());
}
test "ArrayList pop multiple times on empty returns null" {
var list = ArrayList(i32).init(std.testing.allocator);
defer list.deinit();
try std.testing.expectEqual(@as(?i32, null), list.pop());
try std.testing.expectEqual(@as(?i32, null), list.pop());
try std.testing.expectEqual(list.len, 0);
}
test "ArrayList pop until empty" {
var list = ArrayList(i32).init(std.testing.allocator);
defer list.deinit();
try list.append(10);
try list.append(20);
try std.testing.expectEqual(list.pop(), 20);
try std.testing.expectEqual(list.len, 1);
try std.testing.expectEqual(list.pop(), 10);
try std.testing.expectEqual(list.len, 0);
try std.testing.expectEqual(@as(?i32, null), list.pop());
try std.testing.expectEqual(list.len, 0);
}
test "ArrayList get out of bounds" {
var list = ArrayList(i32).init(std.testing.allocator);
defer list.deinit();
try std.testing.expectEqual(@as(?*const i32, null), list.get(0));
try list.append(1);
try std.testing.expectEqual(@as(?*const i32, null), list.get(1));
try std.testing.expectEqual(@as(?*const i32, null), list.get(100));
}