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 {
556556 /// Returns true iff every corresponding bit in both
557557 /// bit sets are the same.
558558 pub fn eql(self: Self, other: Self) bool {
559 if (bit_length == 0) {
560 return true;
561 }
562559 var i: usize = 0;
563560 return while (i < num_masks) : (i += 1) {
564561 if (self.masks[i] != other.masks[i]) {
......@@ -945,6 +942,21 @@ pub const DynamicBitSetUnmanaged = struct {
945942 return offset + index;
946943 }
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
948960 /// Iterates through the items in the set, according to the options.
949961 /// The default options (.{}) will iterate indices of set bits in
950962 /// ascending order. Modifications to the underlying bit set may
......@@ -1113,6 +1125,12 @@ pub const DynamicBitSet = struct {
11131125 return self.unmanaged.toggleFirstSet();
11141126 }
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
11161134 /// Iterates through the items in the set, according to the options.
11171135 /// The default options (.{}) will iterate indices of set bits in
11181136 /// ascending order. Modifications to the underlying bit set may
......@@ -1254,6 +1272,21 @@ pub const Range = struct {
12541272
12551273const 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
12571290fn testBitSet(a: anytype, b: anytype, len: usize) !void {
12581291 try testing.expectEqual(len, a.capacity());
12591292 try testing.expectEqual(len, b.capacity());
......@@ -1474,23 +1507,6 @@ fn testPureBitSet(comptime Set: type) !void {
14741507 break :odd bit_set;
14751508 };
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
14941510 try testing.expect(empty.subsetOf(empty));
14951511 try testing.expect(empty.subsetOf(full));
14961512 try testing.expect(full.subsetOf(full));
......@@ -1567,6 +1583,7 @@ fn testStaticBitSet(comptime Set: type) !void {
15671583 try testing.expectEqual(@as(usize, 0), a.count());
15681584 try testing.expectEqual(@as(usize, Set.bit_length), b.count());
15691585
1586 try testEql(a, b, Set.bit_length);
15701587 try testBitSet(&a, &b, Set.bit_length);
15711588
15721589 try testPureBitSet(Set);
......@@ -1629,6 +1646,7 @@ test "DynamicBitSetUnmanaged" {
16291646 defer b.deinit(allocator);
16301647 try testing.expectEqual(@as(usize, size), b.count());
16311648
1649 try testEql(tmp, b, size);
16321650 try testBitSet(&a, &b, size);
16331651 }
16341652}
......@@ -1669,6 +1687,7 @@ test "DynamicBitSet" {
16691687 defer b.deinit();
16701688 try testing.expectEqual(@as(usize, size), b.count());
16711689
1690 try testEql(tmp, b, size);
16721691 try testBitSet(&a, &b, size);
16731692 }
16741693}