1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
extension BitSet {
public struct Counted {
@usableFromInline
internal var _bits: BitSet
@usableFromInline
internal var _count: Int
internal init(_bits: BitSet, count: Int) {
self._bits = _bits
self._count = count
_checkInvariants()
}
}
}
extension BitSet.Counted: Sendable {}
extension BitSet.Counted {
#if COLLECTIONS_INTERNAL_CHECKS
@inline(never)
@_effects(releasenone)
public func _checkInvariants() {
_bits._checkInvariants()
precondition(_count == _bits.count)
}
#else
@inline(__always) @inlinable
public func _checkInvariants() {}
#endif // COLLECTIONS_INTERNAL_CHECKS
}
extension BitSet.Counted: _UniqueCollection {}
extension BitSet.Counted {
public init() {
self.init(BitSet())
}
@inlinable
public init(words: some Sequence<UInt>) {
self.init(BitSet(words: words))
}
@inlinable
public init(bitPattern x: some BinaryInteger) {
self.init(words: x.words)
}
public init(_ array: BitArray) {
self.init(BitSet(array))
}
@inlinable
public init(_ elements: __owned some Sequence<Int>) {
self.init(BitSet(elements))
}
public init(_ range: Range<Int>) {
self.init(BitSet(range))
}
}
extension BitSet.Counted {
public init(_ bits: BitSet) {
self.init(_bits: bits, count: bits.count)
}
public var uncounted: BitSet {
get { _bits }
@inline(__always) // https://github.com/apple/swift-collections/issues/164
_modify {
defer {
_count = _bits.count
}
yield &_bits
}
}
}
extension BitSet {
public init(_ bits: BitSet.Counted) {
self = bits._bits
}
public var counted: BitSet.Counted {
get {
BitSet.Counted(_bits: self, count: self.count)
}
@inline(__always) // https://github.com/apple/swift-collections/issues/164
_modify {
var value = BitSet.Counted(self)
self = []
defer { self = value._bits }
yield &value
}
}
}
extension BitSet.Counted: Sequence {
public typealias Iterator = BitSet.Iterator
public var underestimatedCount: Int {
_count
}
public func makeIterator() -> BitSet.Iterator {
_bits.makeIterator()
}
public func _customContainsEquatableElement(
_ element: Int
) -> Bool? {
_bits._customContainsEquatableElement(element)
}
}
extension BitSet.Counted: BidirectionalCollection {
public typealias Index = BitSet.Index
public var isEmpty: Bool { _count == 0 }
public var count: Int { _count }
public var startIndex: Index { _bits.startIndex }
public var endIndex: Index { _bits.endIndex }
public subscript(position: Index) -> Int {
_bits[position]
}
public func index(after index: Index) -> Index {
_bits.index(after: index)
}
public func index(before index: Index) -> Index {
_bits.index(before: index)
}
public func distance(from start: Index, to end: Index) -> Int {
_bits.distance(from: start, to: end)
}
public func index(_ index: Index, offsetBy distance: Int) -> Index {
_bits.index(index, offsetBy: distance)
}
public func index(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
) -> Index? {
_bits.index(i, offsetBy: distance, limitedBy: limit)
}
public func _customIndexOfEquatableElement(_ element: Int) -> Index?? {
_bits._customIndexOfEquatableElement(element)
}
public func _customLastIndexOfEquatableElement(_ element: Int) -> Index?? {
_bits._customLastIndexOfEquatableElement(element)
}
}
extension BitSet.Counted: Codable {
public func encode(to encoder: Encoder) throws {
try _bits.encode(to: encoder)
}
public init(from decoder: Decoder) throws {
self.init(try BitSet(from: decoder))
}
}
extension BitSet.Counted: CustomStringConvertible {
// A textual representation of this instance.
public var description: String {
_bits.description
}
}
extension BitSet.Counted: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
description
}
}
extension BitSet.Counted: CustomReflectable {
public var customMirror: Mirror {
_bits.customMirror
}
}
extension BitSet.Counted: Equatable {
public static func ==(left: Self, right: Self) -> Bool {
guard left._count == right._count else { return false }
return left._bits == right._bits
}
}
extension BitSet.Counted: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(_bits)
}
}
extension BitSet.Counted: ExpressibleByArrayLiteral {
@inlinable
public init(arrayLiteral elements: Int...) {
let bits = BitSet(elements)
self.init(bits)
}
}
extension BitSet.Counted { // Extras
public init(reservingCapacity maximumValue: Int) {
self.init(_bits: BitSet(reservingCapacity: maximumValue), count: 0)
}
public mutating func reserveCapacity(_ maximumValue: Int) {
_bits.reserveCapacity(maximumValue)
}
public subscript(member member: Int) -> Bool {
get { contains(member) }
set {
if newValue {
insert(member)
} else {
remove(member)
}
}
}
public subscript(members bounds: Range<Int>) -> Slice<BitSet> {
_bits[members: bounds]
}
public subscript(members bounds: some RangeExpression<Int>) -> Slice<BitSet> {
_bits[members: bounds]
}
public mutating func remove(at index: Index) -> Int {
defer { self._count &-= 1 }
return _bits.remove(at: index)
}
public func filter(
_ isIncluded: (Element) throws -> Bool
) rethrows -> Self {
BitSet.Counted(try _bits.filter(isIncluded))
}
}
extension BitSet.Counted: _SortedCollection {
/// Returns the current set (already sorted).
///
/// - Complexity: O(1)
public func sorted() -> BitSet.Counted { self }
/// Returns the minimum element in this set.
///
/// Bit sets are sorted, so the minimum element is always at the first
/// position in the set.
///
/// - Returns: The bit set's minimum element. If the sequence has no
/// elements, returns `nil`.
///
/// - Complexity: O(1)
@warn_unqualified_access
public func min() -> Element? {
first
}
/// Returns the maximum element in this set.
///
/// Bit sets are sorted, so the maximum element is always at the last
/// position in the set.
///
/// - Returns: The bit set's maximum element. If the sequence has no
/// elements, returns `nil`.
///
/// - Complexity: O(1)
@warn_unqualified_access
public func max() -> Element? {
last
}
}
extension BitSet.Counted {
public static func random(upTo limit: Int) -> BitSet.Counted {
BitSet.Counted(BitSet.random(upTo: limit))
}
public static func random(
upTo limit: Int,
using rng: inout some RandomNumberGenerator
) -> BitSet.Counted {
BitSet.Counted(BitSet.random(upTo: limit, using: &rng))
}
}
extension BitSet.Counted: SetAlgebra {
public func contains(_ member: Int) -> Bool {
_bits.contains(member)
}
@discardableResult
public mutating func insert(
_ newMember: Int
) -> (inserted: Bool, memberAfterInsert: Int) {
let r = _bits.insert(newMember)
if r.inserted { _count += 1 }
return r
}
@discardableResult
public mutating func update(with newMember: Int) -> Int? {
_bits.update(with: newMember)
}
@discardableResult
public mutating func remove(_ member: Int) -> Int? {
let old = _bits.remove(member)
if old != nil { _count -= 1 }
return old
}
}
extension BitSet.Counted {
/// Returns a new set with the elements of both this and the given set.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet.Counted = [0, 2, 4, 6]
/// set.union(other) // [0, 1, 2, 3, 4, 6]
///
/// - Parameter other: The set of elements to insert.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func union(_ other: BitSet.Counted) -> BitSet.Counted {
_bits.union(other).counted
}
/// Returns a new set with the elements of both this and the given set.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet = [0, 2, 4, 6]
/// set.union(other) // [0, 1, 2, 3, 4, 6]
///
/// - Parameter other: The set of elements to insert.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func union(_ other: BitSet) -> BitSet.Counted {
_bits.union(other).counted
}
/// Returns a new set with the elements of both this set and the given
/// range of integers.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// set.union(3 ..< 7) // [1, 2, 3, 4, 5, 6]
///
/// - Parameter other: A range of nonnegative integers.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func union(_ other: Range<Int>) -> BitSet.Counted {
_bits.union(other).counted
}
/// Returns a new set with the elements of both this set and the given
/// sequence of integers.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other = [6, 4, 2, 0, 2, 0]
/// set.union(other) // [0, 1, 2, 3, 4, 6]
///
/// - Parameter other: A sequence of nonnegative integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in either
/// input, and *k* is the complexity of iterating over all elements in
/// `other`.
@inlinable
public func union(
_ other: __owned some Sequence<Int>
) -> Self {
_bits.union(other).counted
}
}
extension BitSet.Counted {
/// Returns a new bit set with the elements that are common to both this set
/// and the given set.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet.Counted = [6, 4, 2, 0]
/// let c = a.intersection(b)
/// // c is now [2, 4]
///
/// - Parameter other: A bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public func intersection(_ other: BitSet.Counted) -> BitSet.Counted {
_bits.intersection(other).counted
}
/// Returns a new bit set with the elements that are common to both this set
/// and the given set.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet = [6, 4, 2, 0]
/// let c = a.intersection(b)
/// // c is now [2, 4]
///
/// - Parameter other: A bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public func intersection(_ other: BitSet) -> BitSet.Counted {
_bits.intersection(other).counted
}
/// Returns a new bit set with the elements that are common to both this set
/// and the given range of integers.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let c = a.intersection(-10 ..< 3)
/// // c is now [3, 4]
///
/// - Parameter other: A range of integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
public func intersection(_ other: Range<Int>) -> BitSet.Counted {
_bits.intersection(other).counted
}
/// Returns a new bit set with the elements that are common to both this set
/// and the given sequence.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b = [6, 4, 2, 0]
/// let c = a.intersection(b)
/// // c is now [2, 4]
///
/// - Parameter other: A sequence of integer values.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public func intersection(
_ other: __owned some Sequence<Int>
) -> Self {
_bits.intersection(other).counted
}
}
extension BitSet.Counted {
/// Returns a new bit set with the elements that are either in this set or in
/// `other`, but not in both.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet.Counted = [6, 4, 2, 0]
/// set.symmetricDifference(other) // [0, 1, 3, 6]
///
/// - Parameter other: Another set.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public func symmetricDifference(_ other: BitSet.Counted) -> BitSet.Counted {
_bits.symmetricDifference(other).counted
}
/// Returns a new bit set with the elements that are either in this set or in
/// `other`, but not in both.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet = [6, 4, 2, 0]
/// set.symmetricDifference(other) // [0, 1, 3, 6]
///
/// - Parameter other: Another set.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public func symmetricDifference(_ other: BitSet) -> BitSet.Counted {
_bits.symmetricDifference(other).counted
}
/// Returns a new bit set with the elements that are either in this set or in
/// `other`, but not in both.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// set.formSymmetricDifference(3 ..< 7) // [1, 2, 5, 6]
///
/// - Parameter other: A range of nonnegative integers.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func symmetricDifference(_ other: Range<Int>) -> BitSet.Counted {
_bits.symmetricDifference(other).counted
}
/// Returns a new bit set with the elements that are either in this set or in
/// `other`, but not in both.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other = [6, 4, 2, 0, 2, 0]
/// set.formSymmetricDifference(other) // [0, 1, 3, 6]
///
/// - Parameter other: A sequence of nonnegative integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in either
/// input, and *k* is the complexity of iterating over all elements in
/// `other`.
@inlinable
public func symmetricDifference(
_ other: __owned some Sequence<Int>
) -> Self {
_bits.symmetricDifference(other).counted
}
}
extension BitSet.Counted {
/// Returns a new set containing the elements of this set that do not occur
/// in the given other set.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet.Counted = [0, 2, 4, 6]
/// set.subtracting(other) // [1, 3]
///
/// - Parameter other: Another bit set.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func subtracting(_ other: BitSet.Counted) -> BitSet.Counted {
_bits.subtracting(other).counted
}
/// Returns a new set containing the elements of this set that do not occur
/// in the given other set.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet = [0, 2, 4, 6]
/// set.subtracting(other) // [1, 3]
///
/// - Parameter other: Another bit set.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func subtracting(_ other: BitSet) -> BitSet.Counted {
_bits.subtracting(other).counted
}
/// Returns a new set containing the elements of this set that do not occur
/// in the given range of integers.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// set.subtracting(-10 ..< 3) // [3, 4]
///
/// - Parameter other: A range of arbitrary integers.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*), where *max* is the largest item in self.
public func subtracting(_ other: Range<Int>) -> BitSet.Counted {
_bits.subtracting(other).counted
}
/// Returns a new set containing the elements of this set that do not occur
/// in the given sequence of integers.
///
/// let set: BitSet.Counted = [1, 2, 3, 4]
/// let other = [6, 4, 2, 0, -2, -4]
/// set.subtracting(other) // [1, 3]
///
/// - Parameter other: A sequence of arbitrary integers.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public func subtracting(
_ other: __owned some Sequence<Int>
) -> Self {
_bits.subtracting(other).counted
}
}
extension BitSet.Counted {
/// Adds the elements of the given set to this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet.Counted = [0, 2, 4, 6]
/// set.formUnion(other)
/// // `set` is now `[0, 1, 2, 3, 4, 6]`
///
/// - Parameter other: The set of elements to insert.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public mutating func formUnion(_ other: BitSet.Counted) {
_bits.formUnion(other._bits)
_count = _bits.count
_checkInvariants()
}
/// Adds the elements of the given set to this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet = [0, 2, 4, 6]
/// set.formUnion(other)
/// // `set` is now `[0, 1, 2, 3, 4, 6]`
///
/// - Parameter other: The set of elements to insert.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public mutating func formUnion(_ other: BitSet) {
_bits.formUnion(other)
_count = _bits.count
_checkInvariants()
}
/// Adds the elements of the given range of integers to this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// set.formUnion(3 ..< 7)
/// // `set` is now `[1, 2, 3, 4, 5, 6]`
///
/// - Parameter other: A range of nonnegative integers.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public mutating func formUnion(_ other: Range<Int>) {
_bits.formUnion(other)
_count = _bits.count
_checkInvariants()
}
/// Adds the elements of the given sequence to this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other = [6, 4, 2, 0, 2, 0]
/// set.formUnion(other)
/// // `set` is now `[0, 1, 2, 3, 4, 6]`
///
/// - Parameter other: A sequence of nonnegative integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in either
/// input, and *k* is the complexity of iterating over all elements in
/// `other`.
@inlinable
public mutating func formUnion(
_ other: __owned some Sequence<Int>
) {
_bits.formUnion(other)
_count = _bits.count
_checkInvariants()
}
}
extension BitSet.Counted {
/// Removes the elements of this set that aren't also in the given one.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet.Counted = [0, 2, 4, 6]
/// set.formIntersection(other)
/// // set is now [2, 4]
///
/// - Parameter other: A bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public mutating func formIntersection(_ other: BitSet.Counted) {
_bits.formIntersection(other._bits)
_count = _bits.count
_checkInvariants()
}
/// Removes the elements of this set that aren't also in the given one.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet = [0, 2, 4, 6]
/// set.formIntersection(other)
/// // set is now [2, 4]
///
/// - Parameter other: A bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public mutating func formIntersection(_ other: BitSet) {
_bits.formIntersection(other)
_count = _bits.count
_checkInvariants()
}
/// Removes the elements of this set that aren't also in the given range.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// set.formIntersection(-10 ..< 3)
/// // set is now [3, 4]
///
/// - Parameter other: A range of integers.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public mutating func formIntersection(_ other: Range<Int>) {
_bits.formIntersection(other)
_count = _bits.count
_checkInvariants()
}
/// Removes the elements of this set that aren't also in the given sequence.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: Set<Int> = [6, 4, 2, 0]
/// set.formIntersection(other)
/// // set is now [2, 4]
///
/// - Parameter other: A sequence of integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public mutating func formIntersection(
_ other: __owned some Sequence<Int>
) {
_bits.formIntersection(other)
_count = _bits.count
_checkInvariants()
}
}
extension BitSet.Counted {
/// Replace this set with the elements contained in this set or the given
/// set, but not both.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet.Counted = [0, 2, 4, 6]
/// set.formSymmetricDifference(other)
/// // set is now [0, 1, 3, 6]
///
/// - Parameter other: Another set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public mutating func formSymmetricDifference(_ other: BitSet.Counted) {
_bits.formSymmetricDifference(other._bits)
_count = _bits.count
_checkInvariants()
}
/// Replace this set with the elements contained in this set or the given
/// set, but not both.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet = [0, 2, 4, 6]
/// set.formSymmetricDifference(other)
/// // set is now [0, 1, 3, 6]
///
/// - Parameter other: Another set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either set.
public mutating func formSymmetricDifference(_ other: BitSet) {
_bits.formSymmetricDifference(other)
_count = _bits.count
_checkInvariants()
}
/// Replace this set with the elements contained in this set or the given
/// range of integers, but not both.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// set.formSymmetricDifference(3 ..< 7)
/// // set is now [1, 2, 5, 6]
///
/// - Parameter other: A range of nonnegative integers.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public mutating func formSymmetricDifference(_ other: Range<Int>) {
_bits.formSymmetricDifference(other)
_count = _bits.count
_checkInvariants()
}
/// Replace this set with the elements contained in this set or the given
/// sequence, but not both.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other = [6, 4, 2, 0, 2, 0]
/// set.formSymmetricDifference(other)
/// // set is now [0, 1, 3, 6]
///
/// - Parameter other: A sequence of nonnegative integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in either
/// input, and *k* is the complexity of iterating over all elements in
/// `other`.
@inlinable
public mutating func formSymmetricDifference(
_ other: __owned some Sequence<Int>
) {
_bits.formSymmetricDifference(other)
_count = _bits.count
_checkInvariants()
}
}
extension BitSet.Counted {
/// Removes the elements of the given bit set from this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet.Counted = [0, 2, 4, 6]
/// set.subtract(other)
/// // set is now [1, 3]
///
/// - Parameter other: Another bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public mutating func subtract(_ other: BitSet.Counted) {
_bits.subtract(other._bits)
_count = _bits.count
_checkInvariants()
}
/// Removes the elements of the given bit set from this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other: BitSet = [0, 2, 4, 6]
/// set.subtract(other)
/// // set is now [1, 3]
///
/// - Parameter other: Another bit set.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public mutating func subtract(_ other: BitSet) {
_bits.subtract(other)
_count = _bits.count
_checkInvariants()
}
/// Removes the elements of the given range of integers from this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// set.subtract(-10 ..< 3)
/// // set is now [3, 4]
///
/// - Parameter other: A range of arbitrary integers.
///
/// - Returns: A new set.
///
/// - Complexity: O(*max*), where *max* is the largest item in self.
public mutating func subtract(_ other: Range<Int>) {
_bits.subtract(other)
_count = _bits.count
_checkInvariants()
}
/// Removes the elements of the given sequence of integers from this set.
///
/// var set: BitSet.Counted = [1, 2, 3, 4]
/// let other = [6, 4, 2, 0, -2, -4]
/// set.subtract(other)
/// // set is now [1, 3]
///
/// - Parameter other: A sequence of arbitrary integers.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public mutating func subtract(
_ other: __owned some Sequence<Int>
) {
_bits.subtract(other)
_count = _bits.count
_checkInvariants()
}
}
extension BitSet.Counted {
/// Returns a Boolean value indicating whether two bit sets are equal. Two
/// bit sets are considered equal if they contain the same elements.
///
/// - Complexity: O(*max*), where *max* is value of the largest member of
/// either set.
public func isEqualSet(to other: Self) -> Bool {
guard self.count == other.count else { return false }
return self._bits.isEqualSet(to: other._bits)
}
/// Returns a Boolean value indicating whether a bit set is equal to a counted
/// bit set, i.e., whether they contain the same values.
///
/// - Complexity: O(*max*), where *max* is value of the largest member of
/// either set.
public func isEqualSet(to other: BitSet) -> Bool {
self._bits.isEqualSet(to: other)
}
/// Returns a Boolean value indicating whether a bit set is equal to a range
/// of integers, i.e., whether they contain the same values.
///
/// - Complexity: O(min(*max*, `other.upperBound`), where *max* is the largest
/// member of `self`.
public func isEqualSet(to other: Range<Int>) -> Bool {
guard self.count == other.count else { return false }
return _bits.isEqualSet(to: other)
}
/// Returns a Boolean value indicating whether this bit set contains the same
/// elements as the given `other` sequence.
///
/// Duplicate items in `other` do not prevent it from comparing equal to
/// `self`.
///
/// let bits: BitSet = [0, 1, 5, 6]
/// let other = [5, 5, 0, 1, 1, 6, 5, 0, 1, 6, 6, 5]
///
/// bits.isEqualSet(to: other) // true
///
/// - Complexity: O(*n*), where *n* is the number of items in `other`.
@inlinable
public func isEqualSet(to other: some Sequence<Int>) -> Bool {
guard self.count >= other.underestimatedCount else { return false }
return _bits.isEqualSet(to: other)
}
}
extension BitSet.Counted {
/// Returns a Boolean value that indicates whether this set is a subset of
/// the given set.
///
/// Set *A* is a subset of another set *B* if every member of *A* is also a
/// member of *B*.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet.Counted = [1, 2, 4]
/// let c: BitSet.Counted = [0, 1]
/// a.isSubset(of: a) // true
/// b.isSubset(of: a) // true
/// c.isSubset(of: a) // false
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public func isSubset(of other: Self) -> Bool {
if self.count > other.count { return false }
return self._bits.isSubset(of: other._bits)
}
/// Returns a Boolean value that indicates whether this set is a subset of
/// the given set.
///
/// Set *A* is a subset of another set *B* if every member of *A* is also a
/// member of *B*.
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public func isSubset(of other: BitSet) -> Bool {
self._bits.isSubset(of: other)
}
/// Returns a Boolean value that indicates whether this set is a subset of
/// the given range of integers.
///
/// Set *A* is a subset of another set *B* if every member of *A* is also a
/// member of *B*.
///
/// let b: BitSet.Counted = [0, 1, 2]
/// let c: BitSet.Counted = [2, 3, 4]
/// b.isSubset(of: -10 ..< 4) // true
/// c.isSubset(of: -10 ..< 4) // false
///
/// - Parameter other: An arbitrary range of integers.
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public func isSubset(of other: Range<Int>) -> Bool {
_bits.isSubset(of: other)
}
/// Returns a Boolean value that indicates whether this set is a subset of
/// the values in a given sequence of integers.
///
/// Set *A* is a subset of another set *B* if every member of *A* is also a
/// member of *B*.
///
/// let a = [1, 2, 3, 4, -10]
/// let b: BitSet.Counted = [1, 2, 4]
/// let c: BitSet.Counted = [0, 1]
/// b.isSubset(of: a) // true
/// c.isSubset(of: a) // false
///
/// - Parameter other: A sequence of arbitrary integers.
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public func isSubset(of other: some Sequence<Int>) -> Bool {
_bits.isSubset(of: other)
}
}
extension BitSet.Counted {
/// Returns a Boolean value that indicates whether this set is a superset of
/// the given set.
///
/// Set *A* is a superset of another set *B* if every member of *B* is also a
/// member of *A*.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet.Counted = [1, 2, 4]
/// let c: BitSet.Counted = [0, 1]
/// a.isSuperset(of: a) // true
/// a.isSuperset(of: b) // true
/// a.isSuperset(of: c) // false
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a superset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `other`.
public func isSuperset(of other: Self) -> Bool {
other.isSubset(of: self)
}
/// Returns a Boolean value that indicates whether this set is a superset of
/// the given set.
///
/// Set *A* is a superset of another set *B* if every member of *B* is also a
/// member of *A*.
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a superset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `other`.
public func isSuperset(of other: BitSet) -> Bool {
other.isSubset(of: self)
}
/// Returns a Boolean value that indicates whether this set is a superset of
/// a given range of integers.
///
/// Set *A* is a superset of another set *B* if every member of *B* is also a
/// member of *A*.
///
/// let a: BitSet = [0, 1, 2, 3, 4, 10]
/// a.isSuperset(of: 0 ..< 4) // true
/// a.isSuperset(of: -10 ..< 4) // false
///
/// - Parameter other: An arbitrary range of integers.
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: O(`range.count`)
public func isSuperset(of other: Range<Int>) -> Bool {
_bits.isSuperset(of: other)
}
/// Returns a Boolean value that indicates whether this set is a superset of
/// the values in a given sequence of integers.
///
/// Set *A* is a superset of another set *B* if every member of *B* is also a
/// member of *A*.
///
/// let a = [1, 2, 3]
/// let b: BitSet = [0, 1, 2, 3, 4]
/// let c: BitSet = [0, 1, 2]
/// b.isSuperset(of: a) // true
/// c.isSuperset(of: a) // false
///
/// - Parameter other: A sequence of arbitrary integers, some of whose members
/// may appear more than once. (Duplicate items are ignored.)
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: The same as the complexity of iterating over all elements
/// in `other`.
@inlinable
public func isSuperset(of other: some Sequence<Int>) -> Bool {
_bits.isSuperset(of: other)
}
}
extension BitSet.Counted {
/// Returns a Boolean value that indicates whether this bit set is a strict
/// subset of the given set.
///
/// Set *A* is a strict subset of another set *B* if every member of *A* is
/// also a member of *B* and *B* contains at least one element that is not a
/// member of *A*.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet.Counted = [1, 2, 4]
/// let c: BitSet.Counted = [0, 1]
/// a.isStrictSubset(of: a) // false
/// b.isStrictSubset(of: a) // true
/// c.isStrictSubset(of: a) // false
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a strict subset of `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public func isStrictSubset(of other: Self) -> Bool {
guard self.count < other.count else { return false }
return _bits.isStrictSubset(of: other._bits)
}
/// Returns a Boolean value that indicates whether this bit set is a strict
/// subset of the given set.
///
/// Set *A* is a strict subset of another set *B* if every member of *A* is
/// also a member of *B* and *B* contains at least one element that is not a
/// member of *A*.
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a strict subset of `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public func isStrictSubset(of other: BitSet) -> Bool {
_bits.isStrictSubset(of: other)
}
/// Returns a Boolean value that indicates whether this set is a strict
/// subset of the given set.
///
/// Set *A* is a strict subset of another set *B* if every member of *A* is
/// also a member of *B* and *B* contains at least one element that is not a
/// member of *A*.
///
/// let b: BitSet.Counted = [0, 1, 2]
/// let c: BitSet.Counted = [2, 3, 4]
/// b.isStrictSubset(of: -10 ..< 4) // true
/// c.isStrictSubset(of: -10 ..< 4) // false
///
/// - Parameter other: An arbitrary range of integers.
///
/// - Returns: `true` if the set is a strict subset of `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public func isStrictSubset(of other: Range<Int>) -> Bool {
guard self.count < other.count else { return false }
return _bits.isStrictSubset(of: other)
}
/// Returns a Boolean value that indicates whether this bit set is a strict
/// subset of the values in a given sequence of integers.
///
/// Set *A* is a strict subset of another set *B* if every member of *A* is
/// also a member of *B* and *B* contains at least one element that is not a
/// member of *A*.
///
/// let a = [1, 2, 3, 4, -10]
/// let b: BitSet.Counted = [1, 2, 4]
/// let c: BitSet.Counted = [0, 1]
/// b.isStrictSubset(of: a) // true
/// c.isStrictSubset(of: a) // false
///
/// - Parameter other: A sequence of arbitrary integers.
///
/// - Returns: `true` if the set is a strict subset of `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public func isStrictSubset(of other: some Sequence<Int>) -> Bool {
_bits.isStrictSubset(of: other)
}
}
extension BitSet.Counted {
/// Returns a Boolean value that indicates whether this set is a strict
/// superset of another set.
///
/// Set *A* is a strict superset of another set *B* if every member of *B* is
/// also a member of *A* and *A* contains at least one element that is *not*
/// a member of *B*.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet.Counted = [1, 2, 4]
/// let c: BitSet.Counted = [0, 1]
/// a.isStrictSuperset(of: a) // false
/// a.isStrictSuperset(of: b) // true
/// a.isStrictSuperset(of: c) // false
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a superset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `other`.
public func isStrictSuperset(of other: Self) -> Bool {
other.isStrictSubset(of: self)
}
/// Returns a Boolean value that indicates whether this set is a strict
/// superset of another set.
///
/// Set *A* is a strict superset of another set *B* if every member of *B* is
/// also a member of *A* and *A* contains at least one element that is *not*
/// a member of *B*.
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if the set is a superset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `other`.
public func isStrictSuperset(of other: BitSet) -> Bool {
other.isStrictSubset(of: self)
}
/// Returns a Boolean value that indicates whether this set is a superset of
/// a given range of integers.
///
/// Set *A* is a superset of another set *B* if every member of *B* is also a
/// member of *A*.
///
/// let a: BitSet.Counted = [0, 1, 2, 3, 4, 10]
/// a.isSuperset(of: 0 ..< 4) // true
/// a.isSuperset(of: -10 ..< 4) // false
///
/// - Parameter other: An arbitrary range of integers.
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: O(`range.count`)
public func isStrictSuperset(of other: Range<Int>) -> Bool {
_bits.isStrictSuperset(of: other)
}
/// Returns a Boolean value that indicates whether this set is a superset of
/// the values in a given sequence of integers.
///
/// Set *A* is a superset of another set *B* if every member of *B* is also a
/// member of *A*.
///
/// let a = [1, 2, 3]
/// let b: BitSet.Counted = [0, 1, 2, 3, 4]
/// let c: BitSet.Counted = [0, 1, 2]
/// b.isSuperset(of: a) // true
/// c.isSuperset(of: a) // false
///
/// - Parameter other: A sequence of arbitrary integers, some of whose members
/// may appear more than once. (Duplicate items are ignored.)
///
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `other`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public func isStrictSuperset(of other: some Sequence<Int>) -> Bool {
_bits.isStrictSuperset(of: other)
}
}
extension BitSet.Counted {
/// Returns a Boolean value that indicates whether the set has no members in
/// common with the given set.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet.Counted = [5, 6]
/// a.isDisjoint(with: b) // true
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if `self` has no elements in common with `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func isDisjoint(with other: Self) -> Bool {
_bits.isDisjoint(with: other._bits)
}
/// Returns a Boolean value that indicates whether the set has no members in
/// common with the given set.
///
/// let a: BitSet.Counted = [1, 2, 3, 4]
/// let b: BitSet = [5, 6]
/// a.isDisjoint(with: b) // true
///
/// - Parameter other: Another bit set.
///
/// - Returns: `true` if `self` has no elements in common with `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in either input.
public func isDisjoint(with other: BitSet) -> Bool {
_bits.isDisjoint(with: other)
}
/// Returns a Boolean value that indicates whether the set has no members in
/// common with the given range of integers.
///
/// let a: BitSet = [1, 2, 3, 4]
/// a.isDisjoint(with: -10 ..< 0) // true
///
/// - Parameter other: A range of arbitrary integers.
///
/// - Returns: `true` if `self` has no elements in common with `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*), where *max* is the largest item in `self`.
public func isDisjoint(with other: Range<Int>) -> Bool {
_bits.isDisjoint(with: other)
}
/// Returns a Boolean value that indicates whether the set has no members in
/// common with the given sequence of integers.
///
/// let a: BitSet = [1, 2, 3, 4]
/// let b: BitSet = [5, 6, -10, 42]
/// a.isDisjoint(with: b) // true
///
/// - Parameter other: A sequence of arbitrary integers.
///
/// - Returns: `true` if `self` has no elements in common with `other`;
/// otherwise, `false`.
///
/// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`,
/// and *k* is the complexity of iterating over all elements in `other`.
@inlinable
public func isDisjoint(with other: some Sequence<Int>) -> Bool {
_bits.isDisjoint(with: other)
}
}
|