-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.zig
57 lines (47 loc) · 1.51 KB
/
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
49
50
51
52
53
54
55
56
57
const std = @import("std");
const app = @import("app.zig");
const ItemType = ?*anyopaque;
const fn_delete = *const fn (ItemType) callconv(.C) void;
pub const ContainerType = std.ArrayList(ItemType);
pub fn create(capacity: usize) callconv(.C) *ContainerType {
const b = app.allocator.create(ContainerType) catch {
unreachable;
};
b.* = ContainerType.initCapacity(app.allocator, capacity) catch {
unreachable;
};
return b;
}
pub fn delete(b: *ContainerType, deleter: ?fn_delete) callconv(.C) void {
if (deleter) |del| {
for (b.items) |i| {
if (i != null) {
del(i);
}
}
}
b.deinit();
app.allocator.destroy(b);
}
pub fn append(b: *ContainerType, data: ItemType) callconv(.C) void {
b.append(data) catch {
unreachable;
};
}
pub fn length(b: *const ContainerType) callconv(.C) usize {
return b.items.len;
}
pub fn get(b: *const ContainerType, i: usize) callconv(.C) ItemType {
return b.items[i];
}
pub fn set(b: *ContainerType, i: usize, data: ItemType) callconv(.C) void {
b.items[i] = data;
}
comptime {
@export(create, .{ .name = "List_create", .linkage = .strong });
@export(delete, .{ .name = "List_delete", .linkage = .strong });
@export(append, .{ .name = "List_append", .linkage = .strong });
@export(length, .{ .name = "List_length", .linkage = .strong });
@export(get, .{ .name = "List_get", .linkage = .strong });
@export(set, .{ .name = "List_set", .linkage = .strong });
}