Skip to content

Commit

Permalink
feat(range): Range.subsetOf
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed May 31, 2024
1 parent b2b18a7 commit f31ab72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/builtin/range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,17 @@ pub fn invert(ctx: *obj.NativeCtx) c_int {

return 1;
}

pub fn subsetOf(ctx: *obj.NativeCtx) c_int {
const rangeA = ctx.vm.peek(1).obj().access(obj.ObjRange, .Range, ctx.vm.gc).?;
const rangeB = ctx.vm.peek(0).obj().access(obj.ObjRange, .Range, ctx.vm.gc).?;

ctx.vm.push(
Value.fromBoolean(
@min(rangeA.low, rangeA.high) >= @min(rangeB.low, rangeB.high) and
@max(rangeA.low, rangeA.high) <= @max(rangeB.low, rangeB.high),
),
);

return 1;
}
2 changes: 2 additions & 0 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,7 @@ pub const ObjRange = struct {
.{ "toList", buzz_builtin.range.toList },
.{ "len", buzz_builtin.range.len },
.{ "invert", buzz_builtin.range.invert },
.{ "subsetOf", buzz_builtin.range.subsetOf },
},
);

Expand All @@ -2507,6 +2508,7 @@ pub const ObjRange = struct {
.{ "toList", "extern Function toList() > [int]" },
.{ "len", "extern Function len() > int" },
.{ "invert", "extern Function invert() > rg" },
.{ "subsetOf", "extern Function subsetOf(rg other) > bool" },
},
);

Expand Down

0 comments on commit f31ab72

Please sign in to comment.