authorgravatar for pyrogx1133@gmail.comPyrolistical <pyrogx1133@gmail.com> 2022-12-05 14:35:54-08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-06 21:09:33+02:00
logbf316e550671cc71eb498b3cf799493627bb0fdc
treef974d30b6142871a223f0ee33d36faff45227c90
parent36da3000c0076c5a2bd0b2258718510cf11dd3e2

std: added eql to DynamicBitSet and DynamicBitSetUnmanaged


1 files changed, 39 insertions(+), 20 deletions(-)

lib/std/bit_set.zig+39-20
...@@ -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 both556 /// 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 }
947944
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 in961 /// The default options (.{}) will iterate indices of set bits in
950 /// ascending order. Modifications to the underlying bit set may962 /// 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 }
11151127
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 in1135 /// The default options (.{}) will iterate indices of set bits in
1118 /// ascending order. Modifications to the underlying bit set may1136 /// 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 {
12541272
1255const testing = std.testing;1273const testing = std.testing;
12561274
1275fn 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
1257fn testBitSet(a: anytype, b: anytype, len: usize) !void {1290fn 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 };
14761509
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());
15691585
1586 try testEql(a, b, Set.bit_length);
1570 try testBitSet(&a, &b, Set.bit_length);1587 try testBitSet(&a, &b, Set.bit_length);
15711588
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());
16311648
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());
16711689
1690 try testEql(tmp, b, size);
1672 try testBitSet(&a, &b, size);1691 try testBitSet(&a, &b, size);
1673 }1692 }
1674}1693}