| ... | ... | @@ -268,6 +268,25 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type |
| 268 | 268 | } |
| 269 | 269 | return ptr; |
| 270 | 270 | } |
| 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 | } |
| 271 | 290 | }; |
| 272 | 291 | |
| 273 | 292 | pub fn iterator(self: &Self, start_index: usize) Iterator { |
| ... | ... | @@ -316,10 +335,16 @@ fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void { |
| 316 | 335 | |
| 317 | 336 | { |
| 318 | 337 | 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) { |
| 321 | 345 | assert(*item == x); |
| 322 | 346 | } |
| 347 | assert(x == 0); |
| 323 | 348 | } |
| 324 | 349 | |
| 325 | 350 | assert(??list.pop() == 100); |