@@ -599,6 +599,27 @@ extension _NativeDictionary {
599
599
( _values + a. offset) . moveInitialize ( from: _values + b. offset, count: 1 )
600
600
( _values + b. offset) . initialize ( to: value)
601
601
}
602
+
603
+ @inlinable
604
+ internal func extractDictionary(
605
+ using bitset: _UnsafeBitset ,
606
+ count: Int
607
+ ) -> _NativeDictionary < Key , Value > {
608
+ var count = count
609
+ if count == 0 { return _NativeDictionary < Key , Value > ( ) }
610
+ if count == self . count { return self }
611
+ let result = _NativeDictionary < Key , Value > ( capacity: count)
612
+ for offset in bitset {
613
+ let key = self . uncheckedKey ( at: Bucket ( offset: offset) )
614
+ let value = self . uncheckedValue ( at: Bucket ( offset: offset) )
615
+ result. _unsafeInsertNew ( key: key, value: value)
616
+ // The hash table can have set bits after the end of the bitmap.
617
+ // Ignore them.
618
+ count -= 1
619
+ if count == 0 { break }
620
+ }
621
+ return result
622
+ }
602
623
}
603
624
604
625
extension _NativeDictionary where Value: Equatable {
@@ -771,6 +792,26 @@ extension _NativeDictionary { // High-level operations
771
792
}
772
793
}
773
794
}
795
+
796
+ @inlinable
797
+ internal func filter(
798
+ _ isIncluded: ( Element ) throws -> Bool
799
+ ) rethrows -> _NativeDictionary < Key , Value > {
800
+ try _UnsafeBitset. withTemporaryBitset (
801
+ capacity: _storage. _bucketCount
802
+ ) { bitset in
803
+ var count = 0
804
+ for bucket in hashTable {
805
+ if try isIncluded (
806
+ ( uncheckedKey ( at: bucket) , uncheckedValue ( at: bucket) )
807
+ ) {
808
+ bitset. uncheckedInsert ( bucket. offset)
809
+ count += 1
810
+ }
811
+ }
812
+ return extractDictionary ( using: bitset, count: count)
813
+ }
814
+ }
774
815
}
775
816
776
817
extension _NativeDictionary : Sequence {
0 commit comments