| ... | @@ -20,11 +20,9 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -20,11 +20,9 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 20 | return struct { | 20 | return struct { |
| 21 | const Self = @This(); | 21 | const Self = @This(); |
| 22 | | 22 | |
| 23 | /// Use `span` instead of slicing this directly, because if you don't | 23 | /// Content of the ArrayList |
| 24 | /// specify the end position of the slice, this will potentially give | | |
| 25 | /// you uninitialized memory. | | |
| 26 | items: Slice, | 24 | items: Slice, |
| 27 | len: usize, | 25 | capacity: usize, |
| 28 | allocator: *Allocator, | 26 | allocator: *Allocator, |
| 29 | | 27 | |
| 30 | pub const Slice = if (alignment) |a| ([]align(a) T) else []T; | 28 | pub const Slice = if (alignment) |a| ([]align(a) T) else []T; |
| ... | @@ -34,7 +32,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -34,7 +32,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 34 | pub fn init(allocator: *Allocator) Self { | 32 | pub fn init(allocator: *Allocator) Self { |
| 35 | return Self{ | 33 | return Self{ |
| 36 | .items = &[_]T{}, | 34 | .items = &[_]T{}, |
| 37 | .len = 0, | 35 | .capacity = 0, |
| 38 | .allocator = allocator, | 36 | .allocator = allocator, |
| 39 | }; | 37 | }; |
| 40 | } | 38 | } |
| ... | @@ -49,60 +47,55 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -49,60 +47,55 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 49 | | 47 | |
| 50 | /// Release all allocated memory. | 48 | /// Release all allocated memory. |
| 51 | pub fn deinit(self: Self) void { | 49 | pub fn deinit(self: Self) void { |
| 52 | self.allocator.free(self.items); | 50 | self.allocator.free(self.allocatedSlice()); |
| 53 | } | 51 | } |
| 54 | | 52 | |
| | 53 | /// Deprecated: use `items` field directly. |
| 55 | /// Return contents as a slice. Only valid while the list | 54 | /// Return contents as a slice. Only valid while the list |
| 56 | /// doesn't change size. | 55 | /// doesn't change size. |
| 57 | pub fn span(self: var) @TypeOf(self.items[0..self.len]) { | 56 | pub fn span(self: var) @TypeOf(self.items) { |
| 58 | return self.items[0..self.len]; | 57 | return self.items; |
| 59 | } | 58 | } |
| 60 | | 59 | |
| 61 | /// Deprecated: use `span`. | 60 | /// Deprecated: use `items` field directly. |
| 62 | pub fn toSlice(self: Self) Slice { | 61 | pub fn toSlice(self: Self) Slice { |
| 63 | return self.span(); | 62 | return self.items; |
| 64 | } | 63 | } |
| 65 | | 64 | |
| 66 | /// Deprecated: use `span`. | 65 | /// Deprecated: use `items` field directly. |
| 67 | pub fn toSliceConst(self: Self) SliceConst { | 66 | pub fn toSliceConst(self: Self) SliceConst { |
| 68 | return self.span(); | 67 | return self.items; |
| 69 | } | 68 | } |
| 70 | | 69 | |
| 71 | /// Deprecated: use `span()[i]`. | 70 | /// Deprecated: use `list.items[i]`. |
| 72 | pub fn at(self: Self, i: usize) T { | 71 | pub fn at(self: Self, i: usize) T { |
| 73 | return self.span()[i]; | 72 | return self.items[i]; |
| 74 | } | 73 | } |
| 75 | | 74 | |
| 76 | /// Deprecated: use `&span()[i]`. | 75 | /// Deprecated: use `&list.items[i]`. |
| 77 | pub fn ptrAt(self: Self, i: usize) *T { | 76 | pub fn ptrAt(self: Self, i: usize) *T { |
| 78 | return &self.span()[i]; | 77 | return &self.items[i]; |
| 79 | } | 78 | } |
| 80 | | 79 | |
| 81 | /// Deprecated: use `if (i >= list.len) return error.OutOfBounds else span()[i] = item`. | 80 | /// Deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.items[i] = item`. |
| 82 | pub fn setOrError(self: Self, i: usize, item: T) !void { | 81 | pub fn setOrError(self: Self, i: usize, item: T) !void { |
| 83 | if (i >= self.len) return error.OutOfBounds; | 82 | if (i >= self.items.len) return error.OutOfBounds; |
| 84 | self.items[i] = item; | 83 | self.items[i] = item; |
| 85 | } | 84 | } |
| 86 | | 85 | |
| 87 | /// Deprecated: use `list.span()[i] = item`. | 86 | /// Deprecated: use `list.items[i] = item`. |
| 88 | pub fn set(self: *Self, i: usize, item: T) void { | 87 | pub fn set(self: *Self, i: usize, item: T) void { |
| 89 | assert(i < self.len); | 88 | assert(i < self.items.len); |
| 90 | self.items[i] = item; | 89 | self.items[i] = item; |
| 91 | } | 90 | } |
| 92 | | 91 | |
| 93 | /// Return the maximum number of items the list can hold | | |
| 94 | /// without allocating more memory. | | |
| 95 | pub fn capacity(self: Self) usize { | | |
| 96 | return self.items.len; | | |
| 97 | } | | |
| 98 | | | |
| 99 | /// ArrayList takes ownership of the passed in slice. The slice must have been | 92 | /// ArrayList takes ownership of the passed in slice. The slice must have been |
| 100 | /// allocated with `allocator`. | 93 | /// allocated with `allocator`. |
| 101 | /// Deinitialize with `deinit` or use `toOwnedSlice`. | 94 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 102 | pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self { | 95 | pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self { |
| 103 | return Self{ | 96 | return Self{ |
| 104 | .items = slice, | 97 | .items = slice, |
| 105 | .len = slice.len, | 98 | .capacity = slice.len, |
| 106 | .allocator = allocator, | 99 | .allocator = allocator, |
| 107 | }; | 100 | }; |
| 108 | } | 101 | } |
| ... | @@ -110,7 +103,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -110,7 +103,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 110 | /// The caller owns the returned memory. ArrayList becomes empty. | 103 | /// The caller owns the returned memory. ArrayList becomes empty. |
| 111 | pub fn toOwnedSlice(self: *Self) Slice { | 104 | pub fn toOwnedSlice(self: *Self) Slice { |
| 112 | const allocator = self.allocator; | 105 | const allocator = self.allocator; |
| 113 | const result = allocator.shrink(self.items, self.len); | 106 | const result = allocator.shrink(self.allocatedSlice(), self.items.len); |
| 114 | self.* = init(allocator); | 107 | self.* = init(allocator); |
| 115 | return result; | 108 | return result; |
| 116 | } | 109 | } |
| ... | @@ -118,10 +111,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -118,10 +111,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 118 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` | 111 | /// Insert `item` at index `n`. Moves `list[n .. list.len]` |
| 119 | /// to make room. | 112 | /// to make room. |
| 120 | pub fn insert(self: *Self, n: usize, item: T) !void { | 113 | pub fn insert(self: *Self, n: usize, item: T) !void { |
| 121 | try self.ensureCapacity(self.len + 1); | 114 | try self.ensureCapacity(self.items.len + 1); |
| 122 | self.len += 1; | 115 | self.items.len += 1; |
| 123 | | 116 | |
| 124 | mem.copyBackwards(T, self.items[n + 1 .. self.len], self.items[n .. self.len - 1]); | 117 | mem.copyBackwards(T, self.items[n + 1 .. self.items.len], self.items[n .. self.items.len - 1]); |
| 125 | self.items[n] = item; | 118 | self.items[n] = item; |
| 126 | } | 119 | } |
| 127 | | 120 | |
| ... | @@ -129,10 +122,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -129,10 +122,10 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 129 | /// `list[i .. list.len]` to make room. | 122 | /// `list[i .. list.len]` to make room. |
| 130 | /// This operation is O(N). | 123 | /// This operation is O(N). |
| 131 | pub fn insertSlice(self: *Self, i: usize, items: SliceConst) !void { | 124 | pub fn insertSlice(self: *Self, i: usize, items: SliceConst) !void { |
| 132 | try self.ensureCapacity(self.len + items.len); | 125 | try self.ensureCapacity(self.items.len + items.len); |
| 133 | self.len += items.len; | 126 | self.items.len += items.len; |
| 134 | | 127 | |
| 135 | mem.copyBackwards(T, self.items[i + items.len .. self.len], self.items[i .. self.len - items.len]); | 128 | mem.copyBackwards(T, self.items[i + items.len .. self.items.len], self.items[i .. self.items.len - items.len]); |
| 136 | mem.copy(T, self.items[i .. i + items.len], items); | 129 | mem.copy(T, self.items[i .. i + items.len], items); |
| 137 | } | 130 | } |
| 138 | | 131 | |
| ... | @@ -153,13 +146,13 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -153,13 +146,13 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 153 | /// Asserts the array has at least one item. | 146 | /// Asserts the array has at least one item. |
| 154 | /// This operation is O(N). | 147 | /// This operation is O(N). |
| 155 | pub fn orderedRemove(self: *Self, i: usize) T { | 148 | pub fn orderedRemove(self: *Self, i: usize) T { |
| 156 | const newlen = self.len - 1; | 149 | const newlen = self.items.len - 1; |
| 157 | if (newlen == i) return self.pop(); | 150 | if (newlen == i) return self.pop(); |
| 158 | | 151 | |
| 159 | const old_item = self.at(i); | 152 | const old_item = self.items[i]; |
| 160 | for (self.items[i..newlen]) |*b, j| b.* = self.items[i + 1 + j]; | 153 | for (self.items[i..newlen]) |*b, j| b.* = self.items[i + 1 + j]; |
| 161 | self.items[newlen] = undefined; | 154 | self.items[newlen] = undefined; |
| 162 | self.len = newlen; | 155 | self.items.len = newlen; |
| 163 | return old_item; | 156 | return old_item; |
| 164 | } | 157 | } |
| 165 | | 158 | |
| ... | @@ -167,26 +160,28 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -167,26 +160,28 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 167 | /// The empty slot is filled from the end of the list. | 160 | /// The empty slot is filled from the end of the list. |
| 168 | /// This operation is O(1). | 161 | /// This operation is O(1). |
| 169 | pub fn swapRemove(self: *Self, i: usize) T { | 162 | pub fn swapRemove(self: *Self, i: usize) T { |
| 170 | if (self.len - 1 == i) return self.pop(); | 163 | if (self.items.len - 1 == i) return self.pop(); |
| 171 | | 164 | |
| 172 | const slice = self.span(); | 165 | const old_item = self.items[i]; |
| 173 | const old_item = slice[i]; | 166 | self.items[i] = self.pop(); |
| 174 | slice[i] = self.pop(); | | |
| 175 | return old_item; | 167 | return old_item; |
| 176 | } | 168 | } |
| 177 | | 169 | |
| 178 | /// Deprecated: use `if (i >= list.len) return error.OutOfBounds else list.swapRemove(i)`. | 170 | /// Deprecated: use `if (i >= list.items.len) return error.OutOfBounds else list.swapRemove(i)`. |
| 179 | pub fn swapRemoveOrError(self: *Self, i: usize) !T { | 171 | pub fn swapRemoveOrError(self: *Self, i: usize) !T { |
| 180 | if (i >= self.len) return error.OutOfBounds; | 172 | if (i >= self.items.len) return error.OutOfBounds; |
| 181 | return self.swapRemove(i); | 173 | return self.swapRemove(i); |
| 182 | } | 174 | } |
| 183 | | 175 | |
| 184 | /// Append the slice of items to the list. Allocates more | 176 | /// Append the slice of items to the list. Allocates more |
| 185 | /// memory as necessary. | 177 | /// memory as necessary. |
| 186 | pub fn appendSlice(self: *Self, items: SliceConst) !void { | 178 | pub fn appendSlice(self: *Self, items: SliceConst) !void { |
| 187 | try self.ensureCapacity(self.len + items.len); | 179 | const oldlen = self.items.len; |
| 188 | mem.copy(T, self.items[self.len..], items); | 180 | const newlen = self.items.len + items.len; |
| 189 | self.len += items.len; | 181 | |
| | 182 | try self.ensureCapacity(newlen); |
| | 183 | self.items.len = newlen; |
| | 184 | mem.copy(T, self.items[oldlen..], items); |
| 190 | } | 185 | } |
| 191 | | 186 | |
| 192 | /// Same as `append` except it returns the number of bytes written, which is always the same | 187 | /// Same as `append` except it returns the number of bytes written, which is always the same |
| ... | @@ -206,50 +201,55 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -206,50 +201,55 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 206 | /// Append a value to the list `n` times. | 201 | /// Append a value to the list `n` times. |
| 207 | /// Allocates more memory as necessary. | 202 | /// Allocates more memory as necessary. |
| 208 | pub fn appendNTimes(self: *Self, value: T, n: usize) !void { | 203 | pub fn appendNTimes(self: *Self, value: T, n: usize) !void { |
| 209 | const old_len = self.len; | 204 | const old_len = self.items.len; |
| 210 | try self.resize(self.len + n); | 205 | try self.resize(self.items.len + n); |
| 211 | mem.set(T, self.items[old_len..self.len], value); | 206 | mem.set(T, self.items[old_len..self.items.len], value); |
| 212 | } | 207 | } |
| 213 | | 208 | |
| 214 | /// Adjust the list's length to `new_len`. | 209 | /// Adjust the list's length to `new_len`. |
| 215 | /// Does not initialize added items if any. | 210 | /// Does not initialize added items if any. |
| 216 | pub fn resize(self: *Self, new_len: usize) !void { | 211 | pub fn resize(self: *Self, new_len: usize) !void { |
| 217 | try self.ensureCapacity(new_len); | 212 | try self.ensureCapacity(new_len); |
| 218 | self.len = new_len; | 213 | self.items.len = new_len; |
| 219 | } | 214 | } |
| 220 | | 215 | |
| 221 | /// Reduce allocated capacity to `new_len`. | 216 | /// Reduce allocated capacity to `new_len`. |
| 222 | /// Invalidates element pointers. | 217 | /// Invalidates element pointers. |
| 223 | pub fn shrink(self: *Self, new_len: usize) void { | 218 | pub fn shrink(self: *Self, new_len: usize) void { |
| 224 | assert(new_len <= self.len); | 219 | assert(new_len <= self.items.len); |
| 225 | self.len = new_len; | 220 | |
| 226 | self.items = self.allocator.realloc(self.items, new_len) catch |e| switch (e) { | 221 | self.items = self.allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) { |
| 227 | error.OutOfMemory => return, // no problem, capacity is still correct then. | 222 | error.OutOfMemory => return, // no problem, capacity is still correct then. |
| 228 | }; | 223 | }; |
| | 224 | self.capacity = new_len; |
| 229 | } | 225 | } |
| 230 | | 226 | |
| 231 | pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { | 227 | pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { |
| 232 | var better_capacity = self.capacity(); | 228 | var better_capacity = self.capacity; |
| 233 | if (better_capacity >= new_capacity) return; | 229 | if (better_capacity >= new_capacity) return; |
| | 230 | |
| 234 | while (true) { | 231 | while (true) { |
| 235 | better_capacity += better_capacity / 2 + 8; | 232 | better_capacity += better_capacity / 2 + 8; |
| 236 | if (better_capacity >= new_capacity) break; | 233 | if (better_capacity >= new_capacity) break; |
| 237 | } | 234 | } |
| 238 | self.items = try self.allocator.realloc(self.items, better_capacity); | 235 | |
| | 236 | const new_memory = try self.allocator.realloc(self.allocatedSlice(), better_capacity); |
| | 237 | self.items.ptr = new_memory.ptr; |
| | 238 | self.capacity = new_memory.len; |
| 239 | } | 239 | } |
| 240 | | 240 | |
| 241 | /// Increases the array's length to match the full capacity that is already allocated. | 241 | /// Increases the array's length to match the full capacity that is already allocated. |
| 242 | /// The new elements have `undefined` values. This operation does not invalidate any | 242 | /// The new elements have `undefined` values. This operation does not invalidate any |
| 243 | /// element pointers. | 243 | /// element pointers. |
| 244 | pub fn expandToCapacity(self: *Self) void { | 244 | pub fn expandToCapacity(self: *Self) void { |
| 245 | self.len = self.items.len; | 245 | self.items.len = self.capacity; |
| 246 | } | 246 | } |
| 247 | | 247 | |
| 248 | /// Increase length by 1, returning pointer to the new item. | 248 | /// Increase length by 1, returning pointer to the new item. |
| 249 | /// The returned pointer becomes invalid when the list is resized. | 249 | /// The returned pointer becomes invalid when the list is resized. |
| 250 | pub fn addOne(self: *Self) !*T { | 250 | pub fn addOne(self: *Self) !*T { |
| 251 | const new_length = self.len + 1; | 251 | const newlen = self.items.len + 1; |
| 252 | try self.ensureCapacity(new_length); | 252 | try self.ensureCapacity(newlen); |
| 253 | return self.addOneAssumeCapacity(); | 253 | return self.addOneAssumeCapacity(); |
| 254 | } | 254 | } |
| 255 | | 255 | |
| ... | @@ -257,25 +257,32 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -257,25 +257,32 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 257 | /// Asserts that there is already space for the new item without allocating more. | 257 | /// Asserts that there is already space for the new item without allocating more. |
| 258 | /// The returned pointer becomes invalid when the list is resized. | 258 | /// The returned pointer becomes invalid when the list is resized. |
| 259 | pub fn addOneAssumeCapacity(self: *Self) *T { | 259 | pub fn addOneAssumeCapacity(self: *Self) *T { |
| 260 | assert(self.len < self.capacity()); | 260 | assert(self.items.len < self.capacity); |
| 261 | const result = &self.items[self.len]; | 261 | |
| 262 | self.len += 1; | 262 | self.items.len += 1; |
| 263 | return result; | 263 | return &self.items[self.items.len - 1]; |
| 264 | } | 264 | } |
| 265 | | 265 | |
| 266 | /// Remove and return the last element from the list. | 266 | /// Remove and return the last element from the list. |
| 267 | /// Asserts the list has at least one item. | 267 | /// Asserts the list has at least one item. |
| 268 | pub fn pop(self: *Self) T { | 268 | pub fn pop(self: *Self) T { |
| 269 | self.len -= 1; | 269 | const val = self.items[self.items.len - 1]; |
| 270 | return self.items[self.len]; | 270 | self.items.len -= 1; |
| | 271 | return val; |
| 271 | } | 272 | } |
| 272 | | 273 | |
| 273 | /// Remove and return the last element from the list. | 274 | /// Remove and return the last element from the list. |
| 274 | /// If the list is empty, returns `null`. | 275 | /// If the list is empty, returns `null`. |
| 275 | pub fn popOrNull(self: *Self) ?T { | 276 | pub fn popOrNull(self: *Self) ?T { |
| 276 | if (self.len == 0) return null; | 277 | if (self.items.len == 0) return null; |
| 277 | return self.pop(); | 278 | return self.pop(); |
| 278 | } | 279 | } |
| | 280 | |
| | 281 | // For a nicer API, `items.len` is the length, not the capacity. |
| | 282 | // This requires "unsafe" slicing. |
| | 283 | fn allocatedSlice(self: Self) Slice { |
| | 284 | return self.items.ptr[0..self.capacity]; |
| | 285 | } |
| 279 | }; | 286 | }; |
| 280 | } | 287 | } |
| 281 | | 288 | |
| ... | @@ -283,15 +290,15 @@ test "std.ArrayList.init" { | ... | @@ -283,15 +290,15 @@ test "std.ArrayList.init" { |
| 283 | var list = ArrayList(i32).init(testing.allocator); | 290 | var list = ArrayList(i32).init(testing.allocator); |
| 284 | defer list.deinit(); | 291 | defer list.deinit(); |
| 285 | | 292 | |
| 286 | testing.expect(list.len == 0); | 293 | testing.expect(list.items.len == 0); |
| 287 | testing.expect(list.capacity() == 0); | 294 | testing.expect(list.capacity == 0); |
| 288 | } | 295 | } |
| 289 | | 296 | |
| 290 | test "std.ArrayList.initCapacity" { | 297 | test "std.ArrayList.initCapacity" { |
| 291 | var list = try ArrayList(i8).initCapacity(testing.allocator, 200); | 298 | var list = try ArrayList(i8).initCapacity(testing.allocator, 200); |
| 292 | defer list.deinit(); | 299 | defer list.deinit(); |
| 293 | testing.expect(list.len == 0); | 300 | testing.expect(list.items.len == 0); |
| 294 | testing.expect(list.capacity() >= 200); | 301 | testing.expect(list.capacity >= 200); |
| 295 | } | 302 | } |
| 296 | | 303 | |
| 297 | test "std.ArrayList.basic" { | 304 | test "std.ArrayList.basic" { |
| ... | @@ -315,7 +322,7 @@ test "std.ArrayList.basic" { | ... | @@ -315,7 +322,7 @@ test "std.ArrayList.basic" { |
| 315 | } | 322 | } |
| 316 | } | 323 | } |
| 317 | | 324 | |
| 318 | for (list.span()) |v, i| { | 325 | for (list.items) |v, i| { |
| 319 | testing.expect(v == @intCast(i32, i + 1)); | 326 | testing.expect(v == @intCast(i32, i + 1)); |
| 320 | } | 327 | } |
| 321 | | 328 | |
| ... | @@ -324,19 +331,19 @@ test "std.ArrayList.basic" { | ... | @@ -324,19 +331,19 @@ test "std.ArrayList.basic" { |
| 324 | } | 331 | } |
| 325 | | 332 | |
| 326 | testing.expect(list.pop() == 10); | 333 | testing.expect(list.pop() == 10); |
| 327 | testing.expect(list.len == 9); | 334 | testing.expect(list.items.len == 9); |
| 328 | | 335 | |
| 329 | list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable; | 336 | list.appendSlice(&[_]i32{ 1, 2, 3 }) catch unreachable; |
| 330 | testing.expect(list.len == 12); | 337 | testing.expect(list.items.len == 12); |
| 331 | testing.expect(list.pop() == 3); | 338 | testing.expect(list.pop() == 3); |
| 332 | testing.expect(list.pop() == 2); | 339 | testing.expect(list.pop() == 2); |
| 333 | testing.expect(list.pop() == 1); | 340 | testing.expect(list.pop() == 1); |
| 334 | testing.expect(list.len == 9); | 341 | testing.expect(list.items.len == 9); |
| 335 | | 342 | |
| 336 | list.appendSlice(&[_]i32{}) catch unreachable; | 343 | list.appendSlice(&[_]i32{}) catch unreachable; |
| 337 | testing.expect(list.len == 9); | 344 | testing.expect(list.items.len == 9); |
| 338 | | 345 | |
| 339 | // can only set on indices < self.len | 346 | // can only set on indices < self.items.len |
| 340 | list.set(7, 33); | 347 | list.set(7, 33); |
| 341 | list.set(8, 42); | 348 | list.set(8, 42); |
| 342 | | 349 | |
| ... | @@ -352,8 +359,8 @@ test "std.ArrayList.appendNTimes" { | ... | @@ -352,8 +359,8 @@ test "std.ArrayList.appendNTimes" { |
| 352 | defer list.deinit(); | 359 | defer list.deinit(); |
| 353 | | 360 | |
| 354 | try list.appendNTimes(2, 10); | 361 | try list.appendNTimes(2, 10); |
| 355 | testing.expectEqual(@as(usize, 10), list.len); | 362 | testing.expectEqual(@as(usize, 10), list.items.len); |
| 356 | for (list.span()) |element| { | 363 | for (list.items) |element| { |
| 357 | testing.expectEqual(@as(i32, 2), element); | 364 | testing.expectEqual(@as(i32, 2), element); |
| 358 | } | 365 | } |
| 359 | } | 366 | } |
| ... | @@ -378,17 +385,17 @@ test "std.ArrayList.orderedRemove" { | ... | @@ -378,17 +385,17 @@ test "std.ArrayList.orderedRemove" { |
| 378 | | 385 | |
| 379 | //remove from middle | 386 | //remove from middle |
| 380 | testing.expectEqual(@as(i32, 4), list.orderedRemove(3)); | 387 | testing.expectEqual(@as(i32, 4), list.orderedRemove(3)); |
| 381 | testing.expectEqual(@as(i32, 5), list.at(3)); | 388 | testing.expectEqual(@as(i32, 5), list.items[3]); |
| 382 | testing.expectEqual(@as(usize, 6), list.len); | 389 | testing.expectEqual(@as(usize, 6), list.items.len); |
| 383 | | 390 | |
| 384 | //remove from end | 391 | //remove from end |
| 385 | testing.expectEqual(@as(i32, 7), list.orderedRemove(5)); | 392 | testing.expectEqual(@as(i32, 7), list.orderedRemove(5)); |
| 386 | testing.expectEqual(@as(usize, 5), list.len); | 393 | testing.expectEqual(@as(usize, 5), list.items.len); |
| 387 | | 394 | |
| 388 | //remove from front | 395 | //remove from front |
| 389 | testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); | 396 | testing.expectEqual(@as(i32, 1), list.orderedRemove(0)); |
| 390 | testing.expectEqual(@as(i32, 2), list.at(0)); | 397 | testing.expectEqual(@as(i32, 2), list.items[0]); |
| 391 | testing.expectEqual(@as(usize, 4), list.len); | 398 | testing.expectEqual(@as(usize, 4), list.items.len); |
| 392 | } | 399 | } |
| 393 | | 400 | |
| 394 | test "std.ArrayList.swapRemove" { | 401 | test "std.ArrayList.swapRemove" { |
| ... | @@ -405,17 +412,17 @@ test "std.ArrayList.swapRemove" { | ... | @@ -405,17 +412,17 @@ test "std.ArrayList.swapRemove" { |
| 405 | | 412 | |
| 406 | //remove from middle | 413 | //remove from middle |
| 407 | testing.expect(list.swapRemove(3) == 4); | 414 | testing.expect(list.swapRemove(3) == 4); |
| 408 | testing.expect(list.at(3) == 7); | 415 | testing.expect(list.items[3] == 7); |
| 409 | testing.expect(list.len == 6); | 416 | testing.expect(list.items.len == 6); |
| 410 | | 417 | |
| 411 | //remove from end | 418 | //remove from end |
| 412 | testing.expect(list.swapRemove(5) == 6); | 419 | testing.expect(list.swapRemove(5) == 6); |
| 413 | testing.expect(list.len == 5); | 420 | testing.expect(list.items.len == 5); |
| 414 | | 421 | |
| 415 | //remove from front | 422 | //remove from front |
| 416 | testing.expect(list.swapRemove(0) == 1); | 423 | testing.expect(list.swapRemove(0) == 1); |
| 417 | testing.expect(list.at(0) == 5); | 424 | testing.expect(list.items[0] == 5); |
| 418 | testing.expect(list.len == 4); | 425 | testing.expect(list.items.len == 4); |
| 419 | } | 426 | } |
| 420 | | 427 | |
| 421 | test "std.ArrayList.swapRemoveOrError" { | 428 | test "std.ArrayList.swapRemoveOrError" { |
| ... | @@ -478,7 +485,7 @@ test "std.ArrayList.insertSlice" { | ... | @@ -478,7 +485,7 @@ test "std.ArrayList.insertSlice" { |
| 478 | | 485 | |
| 479 | const items = [_]i32{1}; | 486 | const items = [_]i32{1}; |
| 480 | try list.insertSlice(0, items[0..0]); | 487 | try list.insertSlice(0, items[0..0]); |
| 481 | testing.expect(list.len == 6); | 488 | testing.expect(list.items.len == 6); |
| 482 | testing.expect(list.items[0] == 1); | 489 | testing.expect(list.items[0] == 1); |
| 483 | } | 490 | } |
| 484 | | 491 | |