| ... | @@ -556,9 +556,6 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type { | ... | @@ -556,9 +556,6 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type { |
| 556 | /// Returns true iff every corresponding bit in both | 556 | /// Returns true iff every corresponding bit in both |
| 557 | /// bit sets are the same. | 557 | /// bit sets are the same. |
| 558 | pub fn eql(self: Self, other: Self) bool { | 558 | pub fn eql(self: Self, other: Self) bool { |
| 559 | if (bit_length == 0) { | | |
| 560 | return true; | | |
| 561 | } | | |
| 562 | var i: usize = 0; | 559 | var i: usize = 0; |
| 563 | return while (i < num_masks) : (i += 1) { | 560 | return while (i < num_masks) : (i += 1) { |
| 564 | if (self.masks[i] != other.masks[i]) { | 561 | if (self.masks[i] != other.masks[i]) { |
| ... | @@ -945,6 +942,21 @@ pub const DynamicBitSetUnmanaged = struct { | ... | @@ -945,6 +942,21 @@ pub const DynamicBitSetUnmanaged = struct { |
| 945 | return offset + index; | 942 | return offset + index; |
| 946 | } | 943 | } |
| 947 | | 944 | |
| | 945 | /// Returns true iff every corresponding bit in both |
| | 946 | /// bit sets are the same. |
| | 947 | pub fn eql(self: Self, other: Self) bool { |
| | 948 | if (self.bit_length != other.bit_length) { |
| | 949 | return false; |
| | 950 | } |
| | 951 | const num_masks = numMasks(self.bit_length); |
| | 952 | var i: usize = 0; |
| | 953 | return while (i < num_masks) : (i += 1) { |
| | 954 | if (self.masks[i] != other.masks[i]) { |
| | 955 | break false; |
| | 956 | } |
| | 957 | } else true; |
| | 958 | } |
| | 959 | |
| 948 | /// Iterates through the items in the set, according to the options. | 960 | /// Iterates through the items in the set, according to the options. |
| 949 | /// The default options (.{}) will iterate indices of set bits in | 961 | /// The default options (.{}) will iterate indices of set bits in |
| 950 | /// ascending order. Modifications to the underlying bit set may | 962 | /// ascending order. Modifications to the underlying bit set may |
| ... | @@ -1113,6 +1125,12 @@ pub const DynamicBitSet = struct { | ... | @@ -1113,6 +1125,12 @@ pub const DynamicBitSet = struct { |
| 1113 | return self.unmanaged.toggleFirstSet(); | 1125 | return self.unmanaged.toggleFirstSet(); |
| 1114 | } | 1126 | } |
| 1115 | | 1127 | |
| | 1128 | /// Returns true iff every corresponding bit in both |
| | 1129 | /// bit sets are the same. |
| | 1130 | pub fn eql(self: Self, other: Self) bool { |
| | 1131 | return self.unmanaged.eql(other.unmanaged); |
| | 1132 | } |
| | 1133 | |
| 1116 | /// Iterates through the items in the set, according to the options. | 1134 | /// Iterates through the items in the set, according to the options. |
| 1117 | /// The default options (.{}) will iterate indices of set bits in | 1135 | /// The default options (.{}) will iterate indices of set bits in |
| 1118 | /// ascending order. Modifications to the underlying bit set may | 1136 | /// ascending order. Modifications to the underlying bit set may |
| ... | @@ -1254,6 +1272,21 @@ pub const Range = struct { | ... | @@ -1254,6 +1272,21 @@ pub const Range = struct { |
| 1254 | | 1272 | |
| 1255 | const testing = std.testing; | 1273 | const testing = std.testing; |
| 1256 | | 1274 | |
| | 1275 | fn testEql(empty: anytype, full: anytype, len: usize) !void { |
| | 1276 | try testing.expect(empty.eql(empty)); |
| | 1277 | try testing.expect(full.eql(full)); |
| | 1278 | switch (len) { |
| | 1279 | 0 => { |
| | 1280 | try testing.expect(empty.eql(full)); |
| | 1281 | try testing.expect(full.eql(empty)); |
| | 1282 | }, |
| | 1283 | else => { |
| | 1284 | try testing.expect(!empty.eql(full)); |
| | 1285 | try testing.expect(!full.eql(empty)); |
| | 1286 | }, |
| | 1287 | } |
| | 1288 | } |
| | 1289 | |
| 1257 | fn testBitSet(a: anytype, b: anytype, len: usize) !void { | 1290 | fn testBitSet(a: anytype, b: anytype, len: usize) !void { |
| 1258 | try testing.expectEqual(len, a.capacity()); | 1291 | try testing.expectEqual(len, a.capacity()); |
| 1259 | try testing.expectEqual(len, b.capacity()); | 1292 | try testing.expectEqual(len, b.capacity()); |
| ... | @@ -1474,23 +1507,6 @@ fn testPureBitSet(comptime Set: type) !void { | ... | @@ -1474,23 +1507,6 @@ fn testPureBitSet(comptime Set: type) !void { |
| 1474 | break :odd bit_set; | 1507 | break :odd bit_set; |
| 1475 | }; | 1508 | }; |
| 1476 | | 1509 | |
| 1477 | try testing.expect(empty.eql(empty)); | | |
| 1478 | try testing.expect(full.eql(full)); | | |
| 1479 | switch (Set.bit_length) { | | |
| 1480 | 0 => { | | |
| 1481 | try testing.expect(empty.eql(full)); | | |
| 1482 | try testing.expect(full.eql(empty)); | | |
| 1483 | try testing.expect(even.eql(odd)); | | |
| 1484 | try testing.expect(odd.eql(even)); | | |
| 1485 | }, | | |
| 1486 | else => { | | |
| 1487 | try testing.expect(!empty.eql(full)); | | |
| 1488 | try testing.expect(!full.eql(empty)); | | |
| 1489 | try testing.expect(!even.eql(odd)); | | |
| 1490 | try testing.expect(!odd.eql(even)); | | |
| 1491 | }, | | |
| 1492 | } | | |
| 1493 | | | |
| 1494 | try testing.expect(empty.subsetOf(empty)); | 1510 | try testing.expect(empty.subsetOf(empty)); |
| 1495 | try testing.expect(empty.subsetOf(full)); | 1511 | try testing.expect(empty.subsetOf(full)); |
| 1496 | try testing.expect(full.subsetOf(full)); | 1512 | try testing.expect(full.subsetOf(full)); |
| ... | @@ -1567,6 +1583,7 @@ fn testStaticBitSet(comptime Set: type) !void { | ... | @@ -1567,6 +1583,7 @@ fn testStaticBitSet(comptime Set: type) !void { |
| 1567 | try testing.expectEqual(@as(usize, 0), a.count()); | 1583 | try testing.expectEqual(@as(usize, 0), a.count()); |
| 1568 | try testing.expectEqual(@as(usize, Set.bit_length), b.count()); | 1584 | try testing.expectEqual(@as(usize, Set.bit_length), b.count()); |
| 1569 | | 1585 | |
| | 1586 | try testEql(a, b, Set.bit_length); |
| 1570 | try testBitSet(&a, &b, Set.bit_length); | 1587 | try testBitSet(&a, &b, Set.bit_length); |
| 1571 | | 1588 | |
| 1572 | try testPureBitSet(Set); | 1589 | try testPureBitSet(Set); |
| ... | @@ -1629,6 +1646,7 @@ test "DynamicBitSetUnmanaged" { | ... | @@ -1629,6 +1646,7 @@ test "DynamicBitSetUnmanaged" { |
| 1629 | defer b.deinit(allocator); | 1646 | defer b.deinit(allocator); |
| 1630 | try testing.expectEqual(@as(usize, size), b.count()); | 1647 | try testing.expectEqual(@as(usize, size), b.count()); |
| 1631 | | 1648 | |
| | 1649 | try testEql(tmp, b, size); |
| 1632 | try testBitSet(&a, &b, size); | 1650 | try testBitSet(&a, &b, size); |
| 1633 | } | 1651 | } |
| 1634 | } | 1652 | } |
| ... | @@ -1669,6 +1687,7 @@ test "DynamicBitSet" { | ... | @@ -1669,6 +1687,7 @@ test "DynamicBitSet" { |
| 1669 | defer b.deinit(); | 1687 | defer b.deinit(); |
| 1670 | try testing.expectEqual(@as(usize, size), b.count()); | 1688 | try testing.expectEqual(@as(usize, size), b.count()); |
| 1671 | | 1689 | |
| | 1690 | try testEql(tmp, b, size); |
| 1672 | try testBitSet(&a, &b, size); | 1691 | try testBitSet(&a, &b, size); |
| 1673 | } | 1692 | } |
| 1674 | } | 1693 | } |