aboutsummaryrefslogtreecommitdiff
path: root/src/RangeSet.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/RangeSet.zig')
-rw-r--r--src/RangeSet.zig32
1 files changed, 16 insertions, 16 deletions
diff --git a/src/RangeSet.zig b/src/RangeSet.zig
index 79bd22fd7f..5b4c654529 100644
--- a/src/RangeSet.zig
+++ b/src/RangeSet.zig
@@ -1,12 +1,14 @@
const std = @import("std");
const Order = std.math.Order;
-const Type = @import("type.zig").Type;
-const Value = @import("value.zig").Value;
+
const RangeSet = @This();
+const Module = @import("Module.zig");
const SwitchProngSrc = @import("Module.zig").SwitchProngSrc;
+const Type = @import("type.zig").Type;
+const Value = @import("value.zig").Value;
ranges: std.ArrayList(Range),
-target: std.Target,
+module: *Module,
pub const Range = struct {
first: Value,
@@ -14,10 +16,10 @@ pub const Range = struct {
src: SwitchProngSrc,
};
-pub fn init(allocator: std.mem.Allocator, target: std.Target) RangeSet {
+pub fn init(allocator: std.mem.Allocator, module: *Module) RangeSet {
return .{
.ranges = std.ArrayList(Range).init(allocator),
- .target = target,
+ .module = module,
};
}
@@ -32,11 +34,9 @@ pub fn add(
ty: Type,
src: SwitchProngSrc,
) !?SwitchProngSrc {
- const target = self.target;
-
for (self.ranges.items) |range| {
- if (last.compare(.gte, range.first, ty, target) and
- first.compare(.lte, range.last, ty, target))
+ if (last.compare(.gte, range.first, ty, self.module) and
+ first.compare(.lte, range.last, ty, self.module))
{
return range.src; // They overlap.
}
@@ -49,26 +49,24 @@ pub fn add(
return null;
}
-const LessThanContext = struct { ty: Type, target: std.Target };
+const LessThanContext = struct { ty: Type, module: *Module };
/// Assumes a and b do not overlap
fn lessThan(ctx: LessThanContext, a: Range, b: Range) bool {
- return a.first.compare(.lt, b.first, ctx.ty, ctx.target);
+ return a.first.compare(.lt, b.first, ctx.ty, ctx.module);
}
pub fn spans(self: *RangeSet, first: Value, last: Value, ty: Type) !bool {
if (self.ranges.items.len == 0)
return false;
- const target = self.target;
-
std.sort.sort(Range, self.ranges.items, LessThanContext{
.ty = ty,
- .target = target,
+ .module = self.module,
}, lessThan);
- if (!self.ranges.items[0].first.eql(first, ty, target) or
- !self.ranges.items[self.ranges.items.len - 1].last.eql(last, ty, target))
+ if (!self.ranges.items[0].first.eql(first, ty, self.module) or
+ !self.ranges.items[self.ranges.items.len - 1].last.eql(last, ty, self.module))
{
return false;
}
@@ -78,6 +76,8 @@ pub fn spans(self: *RangeSet, first: Value, last: Value, ty: Type) !bool {
var counter = try std.math.big.int.Managed.init(self.ranges.allocator);
defer counter.deinit();
+ const target = self.module.getTarget();
+
// look for gaps
for (self.ranges.items[1..]) |cur, i| {
// i starts counting from the second item.