diff options
Diffstat (limited to 'src/sequence')
| -rw-r--r-- | src/sequence/fast.zig | 20 | ||||
| -rw-r--r-- | src/sequence/root.zig | 113 |
2 files changed, 133 insertions, 0 deletions
diff --git a/src/sequence/fast.zig b/src/sequence/fast.zig new file mode 100644 index 0000000..07f5de6 --- /dev/null +++ b/src/sequence/fast.zig @@ -0,0 +1,20 @@ +const c = @import("c"); +const zpy = @import("../root.zig"); + +pub const Fast = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn size(self: *Self) !usize { + return @intCast(c.PySequence_Fast_GET_SIZE(@ptrCast(self))); + } + + pub fn get(self: *Self, index: usize) *zpy.Object { + return @ptrCast(c.PySequence_Fast_GET_ITEM(@ptrCast(self), @intCast(index)) orelse return error.Exception); + } + + pub fn items(self: *Self) [*:null][*c]zpy.Object { + return @ptrCast(c.PySequence_Fast_ITEMS(@ptrCast(self))); + } +}; diff --git a/src/sequence/root.zig b/src/sequence/root.zig new file mode 100644 index 0000000..107233b --- /dev/null +++ b/src/sequence/root.zig @@ -0,0 +1,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); + } +}; |