| ... | @@ -1,6 +1,6 @@ | ... | @@ -1,6 +1,6 @@ |
| 1 | const RangeSet = @This(); | 1 | const RangeSet = @This(); |
| 2 | | 2 | |
| 3 | ranges: std.ArrayList(Range), | 3 | ranges: std.MultiArrayList(Range), |
| 4 | | 4 | |
| 5 | pub const Range = struct { | 5 | pub const Range = struct { |
| 6 | first: Value, | 6 | first: Value, |
| ... | @@ -22,15 +22,19 @@ pub fn ensureUnusedCapacity(self: *RangeSet, allocator: Allocator, additional_co | ... | @@ -22,15 +22,19 @@ pub fn ensureUnusedCapacity(self: *RangeSet, allocator: Allocator, additional_co |
| 22 | pub fn addAssumeCapacity(set: *RangeSet, new: Range, ty: Type, zcu: *Zcu) ?LazySrcLoc { | 22 | pub fn addAssumeCapacity(set: *RangeSet, new: Range, ty: Type, zcu: *Zcu) ?LazySrcLoc { |
| 23 | assert(new.first.typeOf(zcu).eql(ty)); | 23 | assert(new.first.typeOf(zcu).eql(ty)); |
| 24 | assert(new.last.typeOf(zcu).eql(ty)); | 24 | assert(new.last.typeOf(zcu).eql(ty)); |
| | 25 | assert(new.first.compareScalar(.lte, new.last, ty, zcu)); |
| 25 | | 26 | |
| 26 | for (set.ranges.items) |range| { | 27 | const idx = std.sort.lowerBound(Value, set.ranges.items(.last), @as(SearchCtx, .{ |
| 27 | if (new.last.compareScalar(.gte, range.first, ty, zcu) and | 28 | .val = new.first, |
| 28 | new.first.compareScalar(.lte, range.last, ty, zcu)) | 29 | .zcu = zcu, |
| 29 | { | 30 | }), compare); |
| 30 | return range.src; // They overlap. | 31 | |
| 31 | } | 32 | if (idx != set.ranges.len and // `new.first` is *not* greater than all `old.last` |
| | 33 | new.last.compareScalar(.gte, set.ranges.items(.first)[idx], ty, zcu)) |
| | 34 | { |
| | 35 | return set.ranges.items(.src)[idx]; // `new` overlaps with existing range. |
| 32 | } | 36 | } |
| 33 | set.ranges.appendAssumeCapacity(new); | 37 | set.ranges.insertAssumeCapacity(idx, new); |
| 34 | return null; | 38 | return null; |
| 35 | } | 39 | } |
| 36 | | 40 | |
| ... | @@ -39,15 +43,6 @@ pub fn add(set: *RangeSet, allocator: Allocator, new: Range, ty: Type, zcu: *Zcu | ... | @@ -39,15 +43,6 @@ pub fn add(set: *RangeSet, allocator: Allocator, new: Range, ty: Type, zcu: *Zcu |
| 39 | return set.addAssumeCapacity(new, ty, zcu); | 43 | return set.addAssumeCapacity(new, ty, zcu); |
| 40 | } | 44 | } |
| 41 | | 45 | |
| 42 | const SortCtx = struct { | | |
| 43 | ty: Type, | | |
| 44 | zcu: *Zcu, | | |
| 45 | }; | | |
| 46 | /// Assumes a and b do not overlap | | |
| 47 | fn lessThan(ctx: SortCtx, a: Range, b: Range) bool { | | |
| 48 | return a.first.compareScalar(.lt, b.first, ctx.ty, ctx.zcu); | | |
| 49 | } | | |
| 50 | | | |
| 51 | pub fn spans( | 46 | pub fn spans( |
| 52 | set: *RangeSet, | 47 | set: *RangeSet, |
| 53 | allocator: Allocator, | 48 | allocator: Allocator, |
| ... | @@ -58,35 +53,36 @@ pub fn spans( | ... | @@ -58,35 +53,36 @@ pub fn spans( |
| 58 | ) Allocator.Error!bool { | 53 | ) Allocator.Error!bool { |
| 59 | assert(first.typeOf(zcu).eql(ty)); | 54 | assert(first.typeOf(zcu).eql(ty)); |
| 60 | assert(last.typeOf(zcu).eql(ty)); | 55 | assert(last.typeOf(zcu).eql(ty)); |
| 61 | if (set.ranges.items.len == 0) return false; | 56 | if (set.ranges.len == 0) return false; |
| 62 | | 57 | |
| 63 | std.mem.sort(Range, set.ranges.items, SortCtx{ .ty = ty, .zcu = zcu }, lessThan); | 58 | assert(std.sort.isSorted(Value, set.ranges.items(.first), @as(SortCtx, .{ .ty = ty, .zcu = zcu }), lessThan)); |
| | 59 | assert(std.sort.isSorted(Value, set.ranges.items(.last), @as(SortCtx, .{ .ty = ty, .zcu = zcu }), lessThan)); |
| 64 | | 60 | |
| 65 | if (!set.ranges.items[0].first.eql(first, ty, zcu) or | 61 | if (!set.ranges.items(.first)[0].eql(first, ty, zcu) or |
| 66 | !set.ranges.items[set.ranges.items.len - 1].last.eql(last, ty, zcu)) | 62 | !set.ranges.items(.last)[set.ranges.len - 1].eql(last, ty, zcu)) |
| 67 | { | 63 | { |
| 68 | return false; | 64 | return false; |
| 69 | } | 65 | } |
| 70 | | 66 | |
| 71 | const limbs = try allocator.alloc( | 67 | const limbs = try allocator.alloc( |
| 72 | std.math.big.Limb, | 68 | math.big.Limb, |
| 73 | std.math.big.int.calcTwosCompLimbCount(ty.intInfo(zcu).bits), | 69 | math.big.int.calcTwosCompLimbCount(ty.intInfo(zcu).bits), |
| 74 | ); | 70 | ); |
| 75 | defer allocator.free(limbs); | 71 | defer allocator.free(limbs); |
| 76 | var counter: std.math.big.int.Mutable = .init(limbs, 0); | 72 | var counter: math.big.int.Mutable = .init(limbs, 0); |
| 77 | | 73 | |
| 78 | var space: InternPool.Key.Int.Storage.BigIntSpace = undefined; | 74 | var space: InternPool.Key.Int.Storage.BigIntSpace = undefined; |
| 79 | | 75 | |
| 80 | // look for gaps | 76 | // look for gaps |
| 81 | for (set.ranges.items[1..], 0..) |cur, i| { | 77 | for ( |
| 82 | // i starts counting from the second item. | 78 | set.ranges.items(.first)[1..], |
| 83 | const prev = set.ranges.items[i]; | 79 | set.ranges.items(.last)[0 .. set.ranges.len - 1], |
| 84 | | 80 | ) |cur_first, prev_last| { |
| 85 | // prev.last + 1 == cur.first | 81 | // prev_last + 1 == cur_first |
| 86 | counter.copy(prev.last.toBigInt(&space, zcu)); | 82 | counter.copy(prev_last.toBigInt(&space, zcu)); |
| 87 | counter.addScalar(counter.toConst(), 1); | 83 | counter.addScalar(counter.toConst(), 1); |
| 88 | | 84 | |
| 89 | const cur_start_int = cur.first.toBigInt(&space, zcu); | 85 | const cur_start_int = cur_first.toBigInt(&space, zcu); |
| 90 | if (!cur_start_int.eql(counter.toConst())) { | 86 | if (!cur_start_int.eql(counter.toConst())) { |
| 91 | return false; | 87 | return false; |
| 92 | } | 88 | } |
| ... | @@ -95,7 +91,24 @@ pub fn spans( | ... | @@ -95,7 +91,24 @@ pub fn spans( |
| 95 | return true; | 91 | return true; |
| 96 | } | 92 | } |
| 97 | | 93 | |
| | 94 | const SearchCtx = struct { |
| | 95 | val: Value, |
| | 96 | zcu: *const Zcu, |
| | 97 | }; |
| | 98 | fn compare(ctx: SearchCtx, other: Value) math.Order { |
| | 99 | return ctx.val.order(other, ctx.zcu); |
| | 100 | } |
| | 101 | |
| | 102 | const SortCtx = struct { |
| | 103 | ty: Type, |
| | 104 | zcu: *Zcu, |
| | 105 | }; |
| | 106 | fn lessThan(ctx: SortCtx, a: Value, b: Value) bool { |
| | 107 | return a.compareScalar(.lt, b, ctx.ty, ctx.zcu); |
| | 108 | } |
| | 109 | |
| 98 | const std = @import("std"); | 110 | const std = @import("std"); |
| | 111 | const math = std.math; |
| 99 | const assert = std.debug.assert; | 112 | const assert = std.debug.assert; |
| 100 | const Allocator = std.mem.Allocator; | 113 | const Allocator = std.mem.Allocator; |
| 101 | | 114 | |