-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrev_multi_array_list.zig
48 lines (41 loc) · 1.32 KB
/
rev_multi_array_list.zig
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
const zignite = @import("../zignite.zig");
const std = @import("std");
const MultiArrayList = std.MultiArrayList;
const expect = std.testing.expect;
const ReverseIndex = @import("reverse_index.zig").ReverseIndex;
test "revMultiArrayList" {
const allocator = std.testing.allocator;
const Foo = struct {
a: u32,
b: u8,
};
{
var list = MultiArrayList(Foo){};
defer list.deinit(allocator);
try list.append(allocator, .{ .a = 1, .b = 2 });
try list.append(allocator, .{ .a = 3, .b = 4 });
const array = try zignite.revMultiArrayList(Foo, &list).toBoundedArray(10);
try expect(array.get(0).a == 3);
try expect(array.get(0).b == 4);
try expect(array.get(1).a == 1);
try expect(array.get(1).b == 2);
try expect(array.len == 2);
}
{
var list = MultiArrayList(Foo){};
defer list.deinit(allocator);
try expect(zignite.revMultiArrayList(Foo, &list).isEmpty());
}
}
pub fn RevMultiArrayList(comptime T: type) type {
return struct {
const List = *const MultiArrayList(T);
const I = ReverseIndex(List, T, len, get);
fn len(list: List) usize {
return list.len;
}
fn get(list: List, index: usize) T {
return list.get(index);
}
}.I;
}