summaryrefslogtreecommitdiff
path: root/src/list.zig
blob: 2d729900041e626e6009b32de0b986df567c755a (plain)
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
const std = @import("std");
const c = @import("c");

const zpy = @import("root.zig");

pub const List = extern struct {
    const Self = @This();

    data: c.PyObject,

    pub fn new(items: []const *zpy.Object) !*Self {
        var list: *Self = @ptrCast(c.PyList_New(@intCast(items.len)) orelse return error.Exception);
        errdefer list.asObject().decref();

        for (items, 0..) |item, index| {
            list.setUnchecked(index, item);
        }

        return list;
    }

    pub fn size(self: *const Self) usize {
        return @as(usize, c.PyList_Size(@ptrCast(@constCast(self))));
    }

    pub fn get(self: *const Self, index: usize) !*zpy.Object {
        return @ptrCast(c.PyList_GetItem(@ptrCast(self), @intCast(index)) orelse return error.Exception);
    }

    pub fn getUnchecked(self: *const Self, index: usize) !*zpy.Object {
        return @ptrCast(c.PyList_GET_ITEM(@ptrCast(self), @intCast(index)) orelse return error.Exception);
    }

    pub fn set(self: *Self, index: usize, value: *zpy.Object) !void {
        if (c.PyList_SetItem(@ptrCast(self), @intCast(index), @ptrCast(value)) == -1) {
            return error.Exception;
        }
    }

    /// Sets item without any error checking or releasing the resource when replacing.
    pub fn setUnchecked(self: *Self, index: usize, value: *zpy.Object) void {
        c.PyList_SET_ITEM(@ptrCast(self), @intCast(index), @ptrCast(value));
    }

    pub fn insert(self: *Self, index: usize, value: *zpy.Object) !void {
        if (c.PyList_Insert(@ptrCast(self), @intCast(index), @ptrCast(value)) == -1) {
            return error.Exception;
        }
    }

    pub fn append(self: *Self, value: *zpy.Object) !void {
        if (c.PyList_Append(@ptrCast(self), @ptrCast(value)) == -1) {
            return error.Exception;
        }
    }

    pub fn getSlice(self: *const Self, from: usize, to: usize) !*Self {
        return @ptrCast(c.PyList_GetSlice(
            @ptrCast(@constCast(self)),
            @intCast(from),
            @intCast(to),
        ) orelse return error.Exception);
    }

    pub fn setSlice(self: *Self, from: usize, to: usize, itemlist: *zpy.Object) !void {
        if (c.PyList_SetSlice(
            @ptrCast(self),
            @intCast(from),
            @intCast(to),
            @ptrCast(itemlist),
        ) == -1) {
            return error.Exception;
        }
    }

    pub fn extend(self: *Self, iterable: *zpy.Object) !void {
        if (c.PyList_Extend(
            @ptrCast(self),
            @ptrCast(iterable),
        ) == -1) {
            return error.Exception;
        }
    }

    pub fn clear(self: *Self) void {
        c.PyList_Clear(@ptrCast(self));
    }

    pub fn sort(self: *Self) !void {
        if (c.PyList_Sort(@ptrCast(self)) == -1) {
            return error.Exception;
        }
    }

    pub fn reverse(self: *Self) !void {
        if (c.PyList_Sort(@ptrCast(self)) == -1) {
            return error.Exception;
        }
    }

    pub fn asTuple(self: *Self) *zpy.Tuple {
        return @ptrCast(c.PyList_AsTuple(@ptrCast(self)));
    }

    pub fn asObject(self: *Self) *zpy.Object {
        return @ptrCast(self);
    }
};