diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-09-02 08:17:42 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-09-02 08:17:42 +0200 |
| commit | 6c2e06ed20cdea1a67629554d8aa1d5fcda030f9 (patch) | |
| tree | e5fa1f199a35e455917f661a62b38505afa703d7 /src/sequence/root.zig | |
| parent | 804204db7cf1e8a28defd359d17fa72949e13eff (diff) | |
Implement sequences and tuples
Diffstat (limited to 'src/sequence/root.zig')
| -rw-r--r-- | src/sequence/root.zig | 113 |
1 files changed, 113 insertions, 0 deletions
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); + } +}; |