-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraylist.zig
More file actions
148 lines (114 loc) · 4.15 KB
/
arraylist.zig
File metadata and controls
148 lines (114 loc) · 4.15 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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 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));
}
test "ArrayList pop complex types" {
var list = ArrayList([]const u8).init(std.testing.allocator);
defer list.deinit();
try list.append("hello");
try list.append("world");
try std.testing.expectEqual(list.len, 2);
const pop1 = list.pop();
try std.testing.expectEqualStrings("world", pop1.?);
try std.testing.expectEqual(list.len, 1);
const pop2 = list.pop();
try std.testing.expectEqualStrings("hello", pop2.?);
try std.testing.expectEqual(list.len, 0);
try std.testing.expectEqual(@as(?[]const u8, null), list.pop());
}
test "ArrayList get after pop returns null" {
var list = ArrayList(i32).init(std.testing.allocator);
defer list.deinit();
try list.append(42);
try std.testing.expectEqual(list.len, 1);
// Element is accessible before pop
const val_ptr = list.get(0);
try std.testing.expectEqual(@as(i32, 42), val_ptr.?.*);
_ = list.pop();
// Element is out of bounds after pop
try std.testing.expectEqual(@as(?*const i32, null), list.get(0));
}