summaryrefslogtreecommitdiff
path: root/src/sequence/root.zig
blob: 107233bbe307bcf0d07257a139ff31ec3d9b4167 (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
109
110
111
112
113
const c = @import("c");
const zpy = @import("../root.zig");

pub const Sequence = extern struct {
    const Self = @This();
    pub const Fast = @import("fast.zig").Fast;

    data: c.PyObject,

    pub fn size(self: *Self) usize {
        return @intCast(c.PySequence_Size(@ptrCast(self)));
    }

    pub fn concat(self: *Self, tail: *Self) !*Self {
        return @ptrCast(c.PySequence_Concat(
            @ptrCast(self),
            @ptrCast(tail),
        ) orelse return error.Exception);
    }

    pub fn repeat(self: *Self, n: usize) !*Self {
        return @ptrCast(c.PySequence_Repeat(
            @ptrCast(self),
            @intCast(n),
        ) orelse return error.Exception);
    }

    pub fn inPlaceConcat(self: *Self, tail: *Self) !*Self {
        return @ptrCast(c.PySequence_InPlaceConcat(
            @ptrCast(self),
            @ptrCast(tail),
        ) orelse return error.Exception);
    }

    pub fn inPlaceRepeat(self: *Self, n: usize) !*Self {
        return @ptrCast(c.PySequence_InPlaceRepeat(
            @ptrCast(self),
            @intCast(n),
        ) orelse return error.Exception);
    }

    pub fn get(self: *Self, index: usize) ?*zpy.Object {
        return @ptrCast(c.PySequence_GetItem(@ptrCast(self), @intCast(index)));
    }

    pub fn getSlice(self: *Self, from: usize, to: usize) ?*Self {
        return @ptrCast(c.PySequence_GetSlice(@ptrCast(self), @intCast(from), @intCast(to)));
    }

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

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

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

    pub fn count(self: *const Self, value: *zpy.Object) !usize {
        const result = c.PySequence_Count(@ptrCast(@constCast(self)), @ptrCast(value));

        if (result == -1) {
            return error.Exception;
        }

        return result;
    }

    pub fn contains(self: *const Self, value: *zpy.Object) !bool {
        const result = c.PySequence_Contains(@ptrCast(@constCast(self)), @ptrCast(value));

        if (result == -1) {
            return error.Exception;
        }

        return result != 0;
    }

    pub fn indexOf(self: *const Self, value: *zpy.Object) !usize {
        const index = c.PySequence_Index(@ptrCast(@constCast(self)), @ptrCast(value));

        if (index == -1) {
            return error.Exception;
        }

        return index;
    }

    pub fn toList(self: *Self) !*zpy.List {
        return @ptrCast(c.PySequence_List(@ptrCast(self)) orelse return error.Exception);
    }

    pub fn toTuple(self: *Self) !*zpy.Tuple {
        return @ptrCast(c.PySequence_Tuple(@ptrCast(self)) orelse return error.Exception);
    }

    pub fn asFast(self: *Self) !*Fast {
        return @ptrCast(c.PySequence_Fast(@ptrCast(self), null) orelse return error.Exception);
    }

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