@@ -399,14 +399,14 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
399
399
400
400
/// Returns true if the bit at the specified index
401
401
/// is present in the set, false otherwise.
402
- pub fn isSet (self : Self , index : usize ) bool {
402
+ pub fn isSet (self : * const Self , index : usize ) bool {
403
403
assert (index < bit_length );
404
404
if (num_masks == 0 ) return false ; // doesn't compile in this case
405
405
return (self .masks [maskIndex (index )] & maskBit (index )) != 0 ;
406
406
}
407
407
408
408
/// Returns the total number of set bits in this bit set.
409
- pub fn count (self : Self ) usize {
409
+ pub fn count (self : * const Self ) usize {
410
410
var total : usize = 0 ;
411
411
for (self .masks ) | mask | {
412
412
total += @popCount (mask );
@@ -493,9 +493,10 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
493
493
494
494
/// Flips all bits in this bit set which are present
495
495
/// in the toggles bit set.
496
- pub fn toggleSet (self : * Self , toggles : Self ) void {
496
+ pub fn toggleSet (self : * Self , toggles : * const Self ) void {
497
+ const other = & toggles .masks ;
497
498
for (self .masks ) | * mask , i | {
498
- mask .* ^= toggles . masks [i ];
499
+ mask .* ^= other [i ];
499
500
}
500
501
}
501
502
@@ -514,7 +515,7 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
514
515
/// Performs a union of two bit sets, and stores the
515
516
/// result in the first one. Bits in the result are
516
517
/// set if the corresponding bits were set in either input.
517
- pub fn setUnion (self : * Self , other : Self ) void {
518
+ pub fn setUnion (self : * Self , other : * const Self ) void {
518
519
for (self .masks ) | * mask , i | {
519
520
mask .* |= other .masks [i ];
520
521
}
@@ -523,15 +524,15 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
523
524
/// Performs an intersection of two bit sets, and stores
524
525
/// the result in the first one. Bits in the result are
525
526
/// set if the corresponding bits were set in both inputs.
526
- pub fn setIntersection (self : * Self , other : Self ) void {
527
+ pub fn setIntersection (self : * Self , other : * const Self ) void {
527
528
for (self .masks ) | * mask , i | {
528
529
mask .* &= other .masks [i ];
529
530
}
530
531
}
531
532
532
533
/// Finds the index of the first set bit.
533
534
/// If no bits are set, returns null.
534
- pub fn findFirstSet (self : Self ) ? usize {
535
+ pub fn findFirstSet (self : * const Self ) ? usize {
535
536
var offset : usize = 0 ;
536
537
const mask = for (self .masks ) | mask | {
537
538
if (mask != 0 ) break mask ;
@@ -555,7 +556,7 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
555
556
556
557
/// Returns true iff every corresponding bit in both
557
558
/// bit sets are the same.
558
- pub fn eql (self : Self , other : Self ) bool {
559
+ pub fn eql (self : * const Self , other : * const Self ) bool {
559
560
var i : usize = 0 ;
560
561
return while (i < num_masks ) : (i += 1 ) {
561
562
if (self .masks [i ] != other .masks [i ]) {
@@ -566,57 +567,57 @@ pub fn ArrayBitSet(comptime MaskIntType: type, comptime size: usize) type {
566
567
567
568
/// Returns true iff the first bit set is the subset
568
569
/// of the second one.
569
- pub fn subsetOf (self : Self , other : Self ) bool {
570
+ pub fn subsetOf (self : * const Self , other : * const Self ) bool {
570
571
return self .intersectWith (other ).eql (self );
571
572
}
572
573
573
574
/// Returns true iff the first bit set is the superset
574
575
/// of the second one.
575
- pub fn supersetOf (self : Self , other : Self ) bool {
576
+ pub fn supersetOf (self : * const Self , other : * const Self ) bool {
576
577
return other .subsetOf (self );
577
578
}
578
579
579
580
/// Returns the complement bit sets. Bits in the result
580
581
/// are set if the corresponding bits were not set.
581
- pub fn complement (self : Self ) Self {
582
- var result = self ;
582
+ pub fn complement (self : * const Self ) Self {
583
+ var result = self .* ;
583
584
result .toggleAll ();
584
585
return result ;
585
586
}
586
587
587
588
/// Returns the union of two bit sets. Bits in the
588
589
/// result are set if the corresponding bits were set
589
590
/// in either input.
590
- pub fn unionWith (self : Self , other : Self ) Self {
591
- var result = self ;
591
+ pub fn unionWith (self : * const Self , other : * const Self ) Self {
592
+ var result = self .* ;
592
593
result .setUnion (other );
593
594
return result ;
594
595
}
595
596
596
597
/// Returns the intersection of two bit sets. Bits in
597
598
/// the result are set if the corresponding bits were
598
599
/// set in both inputs.
599
- pub fn intersectWith (self : Self , other : Self ) Self {
600
- var result = self ;
600
+ pub fn intersectWith (self : * const Self , other : * const Self ) Self {
601
+ var result = self .* ;
601
602
result .setIntersection (other );
602
603
return result ;
603
604
}
604
605
605
606
/// Returns the xor of two bit sets. Bits in the
606
607
/// result are set if the corresponding bits were
607
608
/// not the same in both inputs.
608
- pub fn xorWith (self : Self , other : Self ) Self {
609
- var result = self ;
609
+ pub fn xorWith (self : * const Self , other : * const Self ) Self {
610
+ var result = self .* ;
610
611
result .toggleSet (other );
611
612
return result ;
612
613
}
613
614
614
615
/// Returns the difference of two bit sets. Bits in
615
616
/// the result are set if set in the first but not
616
617
/// set in the second set.
617
- pub fn differenceWith (self : Self , other : Self ) Self {
618
- var result = self ;
619
- result .setIntersection (other .complement ());
618
+ pub fn differenceWith (self : * const Self , other : * const Self ) Self {
619
+ var result = self .* ;
620
+ result .setIntersection (& other .complement ());
620
621
return result ;
621
622
}
622
623
@@ -1360,6 +1361,7 @@ fn testSupersetOf(empty: anytype, full: anytype, even: anytype, odd: anytype, le
1360
1361
fn testBitSet (a : anytype , b : anytype , len : usize ) ! void {
1361
1362
try testing .expectEqual (len , a .capacity ());
1362
1363
try testing .expectEqual (len , b .capacity ());
1364
+ const needs_ptr = @hasField (std .meta .Child (@TypeOf (a )), "masks" ) and @typeInfo (@TypeOf (@field (a , "masks" ))) != .Pointer ;
1363
1365
1364
1366
{
1365
1367
var i : usize = 0 ;
@@ -1416,7 +1418,12 @@ fn testBitSet(a: anytype, b: anytype, len: usize) !void {
1416
1418
}
1417
1419
}
1418
1420
1419
- a .setUnion (b .* );
1421
+ if (comptime needs_ptr ) {
1422
+ a .setUnion (b );
1423
+ } else {
1424
+ a .setUnion (b .* );
1425
+ }
1426
+
1420
1427
{
1421
1428
var i : usize = 0 ;
1422
1429
while (i < len ) : (i += 1 ) {
@@ -1443,7 +1450,11 @@ fn testBitSet(a: anytype, b: anytype, len: usize) !void {
1443
1450
try testing .expectEqual (@as (? usize , null ), unset .next ());
1444
1451
}
1445
1452
1446
- a .toggleSet (b .* );
1453
+ if (comptime needs_ptr ) {
1454
+ a .toggleSet (b );
1455
+ } else {
1456
+ a .toggleSet (b .* );
1457
+ }
1447
1458
{
1448
1459
try testing .expectEqual (len / 4 , a .count ());
1449
1460
@@ -1459,7 +1470,11 @@ fn testBitSet(a: anytype, b: anytype, len: usize) !void {
1459
1470
}
1460
1471
}
1461
1472
1462
- a .setIntersection (b .* );
1473
+ if (comptime needs_ptr ) {
1474
+ a .setIntersection (b );
1475
+ } else {
1476
+ a .setIntersection (b .* );
1477
+ }
1463
1478
{
1464
1479
try testing .expectEqual ((len + 3 ) / 4 , a .count ());
1465
1480
@@ -1470,7 +1485,11 @@ fn testBitSet(a: anytype, b: anytype, len: usize) !void {
1470
1485
}
1471
1486
}
1472
1487
1473
- a .toggleSet (a .* );
1488
+ if (comptime needs_ptr ) {
1489
+ a .toggleSet (a );
1490
+ } else {
1491
+ a .toggleSet (a .* );
1492
+ }
1474
1493
{
1475
1494
var iter = a .iterator (.{});
1476
1495
try testing .expectEqual (@as (? usize , null ), iter .next ());
@@ -1570,21 +1589,27 @@ fn fillOdd(set: anytype, len: usize) void {
1570
1589
}
1571
1590
1572
1591
fn testPureBitSet (comptime Set : type ) ! void {
1573
- const empty = Set .initEmpty ();
1574
- const full = Set .initFull ();
1592
+ var empty_ = Set .initEmpty ();
1593
+ var full_ = Set .initFull ();
1594
+ const needs_ptr = @hasField (Set , "masks" ) and @typeInfo (@TypeOf (empty_ .masks )) != .Pointer ;
1575
1595
1576
- const even = even : {
1596
+ var even_ = even : {
1577
1597
var bit_set = Set .initEmpty ();
1578
1598
fillEven (& bit_set , Set .bit_length );
1579
1599
break :even bit_set ;
1580
1600
};
1581
1601
1582
- const odd = odd : {
1602
+ var odd_ = odd : {
1583
1603
var bit_set = Set .initEmpty ();
1584
1604
fillOdd (& bit_set , Set .bit_length );
1585
1605
break :odd bit_set ;
1586
1606
};
1587
1607
1608
+ var empty = if (needs_ptr ) & empty_ else empty_ ;
1609
+ var full = if (needs_ptr ) & full_ else full_ ;
1610
+ var even = if (needs_ptr ) & even_ else even_ ;
1611
+ var odd = if (needs_ptr ) & odd_ else odd_ ;
1612
+
1588
1613
try testSubsetOf (empty , full , even , odd , Set .bit_length );
1589
1614
try testSupersetOf (empty , full , even , odd , Set .bit_length );
1590
1615
@@ -1622,36 +1647,41 @@ fn testPureBitSet(comptime Set: type) !void {
1622
1647
try testing .expect (full .differenceWith (even ).eql (odd ));
1623
1648
}
1624
1649
1625
- fn testStaticBitSet (comptime Set : type ) ! void {
1650
+ fn testStaticBitSet (comptime Set : type , comptime Container : @Type ( .EnumLiteral ) ) ! void {
1626
1651
var a = Set .initEmpty ();
1627
1652
var b = Set .initFull ();
1628
1653
try testing .expectEqual (@as (usize , 0 ), a .count ());
1629
1654
try testing .expectEqual (@as (usize , Set .bit_length ), b .count ());
1630
1655
1631
- try testEql (a , b , Set .bit_length );
1656
+ if (comptime Container == .ArrayBitSet ) {
1657
+ try testEql (& a , & b , Set .bit_length );
1658
+ } else {
1659
+ try testEql (a , b , Set .bit_length );
1660
+ }
1661
+
1632
1662
try testBitSet (& a , & b , Set .bit_length );
1633
1663
1634
1664
try testPureBitSet (Set );
1635
1665
}
1636
1666
1637
1667
test "IntegerBitSet" {
1638
- try testStaticBitSet (IntegerBitSet (0 ));
1639
- try testStaticBitSet (IntegerBitSet (1 ));
1640
- try testStaticBitSet (IntegerBitSet (2 ));
1641
- try testStaticBitSet (IntegerBitSet (5 ));
1642
- try testStaticBitSet (IntegerBitSet (8 ));
1643
- try testStaticBitSet (IntegerBitSet (32 ));
1644
- try testStaticBitSet (IntegerBitSet (64 ));
1645
- try testStaticBitSet (IntegerBitSet (127 ));
1668
+ try testStaticBitSet (IntegerBitSet (0 ), .IntegerBitSet );
1669
+ try testStaticBitSet (IntegerBitSet (1 ), .IntegerBitSet );
1670
+ try testStaticBitSet (IntegerBitSet (2 ), .IntegerBitSet );
1671
+ try testStaticBitSet (IntegerBitSet (5 ), .IntegerBitSet );
1672
+ try testStaticBitSet (IntegerBitSet (8 ), .IntegerBitSet );
1673
+ try testStaticBitSet (IntegerBitSet (32 ), .IntegerBitSet );
1674
+ try testStaticBitSet (IntegerBitSet (64 ), .IntegerBitSet );
1675
+ try testStaticBitSet (IntegerBitSet (127 ), .IntegerBitSet );
1646
1676
}
1647
1677
1648
1678
test "ArrayBitSet" {
1649
1679
inline for (.{ 0 , 1 , 2 , 31 , 32 , 33 , 63 , 64 , 65 , 254 , 500 , 3000 }) | size | {
1650
- try testStaticBitSet (ArrayBitSet (u8 , size ));
1651
- try testStaticBitSet (ArrayBitSet (u16 , size ));
1652
- try testStaticBitSet (ArrayBitSet (u32 , size ));
1653
- try testStaticBitSet (ArrayBitSet (u64 , size ));
1654
- try testStaticBitSet (ArrayBitSet (u128 , size ));
1680
+ try testStaticBitSet (ArrayBitSet (u8 , size ), .ArrayBitSet );
1681
+ try testStaticBitSet (ArrayBitSet (u16 , size ), .ArrayBitSet );
1682
+ try testStaticBitSet (ArrayBitSet (u32 , size ), .ArrayBitSet );
1683
+ try testStaticBitSet (ArrayBitSet (u64 , size ), .ArrayBitSet );
1684
+ try testStaticBitSet (ArrayBitSet (u128 , size ), .ArrayBitSet );
1655
1685
}
1656
1686
}
1657
1687
0 commit comments