├── .editorconfig ├── .github ├── ISSUE_TEMPLATE │ ├── BUG_REPORT.md │ ├── FEATURE_REQUEST.md │ └── config.yml ├── PULL_REQUEST_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE │ └── NEW.md └── workflows │ └── pull_request.yml ├── .gitignore ├── .spi.yml ├── .swiftci ├── 5_10_ubuntu2204 ├── 5_7_ubuntu2204 ├── 5_8_ubuntu2204 ├── 5_9_ubuntu2204 ├── nightly_6_0_macos ├── nightly_6_0_ubuntu2204 ├── nightly_main_macos ├── nightly_main_ubuntu2204 └── nightly_main_windows ├── Benchmarks ├── Libraries │ ├── Array.json │ ├── BitSet.json │ ├── Deque.json │ ├── Dictionary.json │ ├── Heap.json │ ├── OrderedDictionary.json │ ├── OrderedSet.json │ ├── Set.json │ ├── TreeDictionary.json │ └── TreeSet.json ├── Package.resolved ├── Package.swift └── Sources │ ├── Benchmarks │ ├── ArrayBenchmarks.swift │ ├── BigStringBenchmarks.swift │ ├── BitsetBenchmarks.swift │ ├── Cpp │ │ ├── CppBenchmarks.swift │ │ ├── CppDequeBenchmarks.swift │ │ ├── CppMapBenchmarks.swift │ │ ├── CppPriorityQueueBenchmarks.swift │ │ ├── CppUnorderedMapBenchmarks.swift │ │ ├── CppUnorderedSetBenchmarks.swift │ │ ├── CppVectorBenchmarks.swift │ │ └── CppVectorBoolBenchmarks.swift │ ├── CustomGenerators.swift │ ├── DequeBenchmarks.swift │ ├── DictionaryBenchmarks.swift │ ├── Foundation │ │ ├── CFBinaryHeapBenchmarks.swift │ │ ├── CFBitVectorBenchmarks.swift │ │ └── FoundationBenchmarks.swift │ ├── HeapBenchmarks.swift │ ├── Kalimba.swift │ ├── OrderedDictionaryBenchmarks.swift │ ├── OrderedSetBenchmarks.swift │ ├── SetBenchmarks.swift │ ├── ShareableDictionaryBenchmarks.swift │ ├── ShareableSetBenchmarks.swift │ ├── SortedDictionaryBenchmarks.swift │ └── SortedSetBenchmarks.swift │ ├── CppBenchmarks │ ├── include │ │ ├── DequeBenchmarks.h │ │ ├── Hashing.h │ │ ├── MapBenchmarks.h │ │ ├── PriorityQueueBenchmarks.h │ │ ├── UnorderedMapBenchmarks.h │ │ ├── UnorderedSetBenchmarks.h │ │ ├── Utils.h │ │ ├── VectorBenchmarks.h │ │ ├── VectorBoolBenchmarks.h │ │ └── module.modulemap │ └── src │ │ ├── CustomHash.h │ │ ├── DequeBenchmarks.cpp │ │ ├── Hashing.cpp │ │ ├── MapBenchmarks.cpp │ │ ├── PriorityQueueBenchmarks.cpp │ │ ├── UnorderedMapBenchmarks.cpp │ │ ├── UnorderedSetBenchmarks.cpp │ │ ├── Utils.cpp │ │ ├── VectorBenchmarks.cpp │ │ └── VectorBoolBenchmarks.cpp │ ├── benchmark-tool │ └── main.swift │ └── memory-benchmark │ ├── DictionaryStatistics.swift │ └── MemoryBenchmarks.swift ├── CMakeLists.txt ├── CODEOWNERS ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Documentation ├── Announcement-benchmarks │ ├── Library.json │ ├── README.md │ ├── Results.md │ ├── Results │ │ ├── 01 Deque - random access lookups.png │ │ ├── 02 Deque - prepending individual integers.png │ │ ├── 03 OrderedSet lookups.png │ │ ├── 04 OrderedSet insertions.png │ │ ├── 05 OrderedDictionary lookups.png │ │ └── 06 OrderedDictionary insertions.png │ ├── Theme.json │ ├── generate-results.sh │ └── results.json ├── Deque.md ├── Heap.md ├── Images │ └── Heap Performance.png ├── Internals │ ├── README.md │ └── ReleaseChecklist.md ├── OrderedDictionary.md ├── OrderedSet.md └── Reviews │ └── 2022-10-31.ShareableHashedCollections │ ├── ShareableHashedCollections.md │ ├── ShareableSet-insert.png │ ├── ShareableSet-model-diffing.png │ └── ShareableSet-sequential iteration.png ├── LICENSE.txt ├── Package.swift ├── README.md ├── Sources ├── BitCollections │ ├── BitArray │ │ ├── BitArray+BitwiseOperations.swift │ │ ├── BitArray+ChunkedBitsIterators.swift │ │ ├── BitArray+Codable.swift │ │ ├── BitArray+Collection.swift │ │ ├── BitArray+Copy.swift │ │ ├── BitArray+CustomReflectable.swift │ │ ├── BitArray+Descriptions.swift │ │ ├── BitArray+Equatable.swift │ │ ├── BitArray+ExpressibleByArrayLiteral.swift │ │ ├── BitArray+ExpressibleByStringLiteral.swift │ │ ├── BitArray+Extras.swift │ │ ├── BitArray+Fill.swift │ │ ├── BitArray+Hashable.swift │ │ ├── BitArray+Initializers.swift │ │ ├── BitArray+Invariants.swift │ │ ├── BitArray+LosslessStringConvertible.swift │ │ ├── BitArray+RandomBits.swift │ │ ├── BitArray+RangeReplaceableCollection.swift │ │ ├── BitArray+Shifts.swift │ │ ├── BitArray+Testing.swift │ │ ├── BitArray._UnsafeHandle.swift │ │ └── BitArray.swift │ ├── BitCollections.docc │ │ ├── BitCollections.md │ │ └── Extensions │ │ │ ├── BitArray.md │ │ │ ├── BitSet.Counted.md │ │ │ └── BitSet.md │ ├── BitSet │ │ ├── BitSet+BidirectionalCollection.swift │ │ ├── BitSet+Codable.swift │ │ ├── BitSet+CustomDebugStringConvertible.swift │ │ ├── BitSet+CustomReflectable.swift │ │ ├── BitSet+CustomStringConvertible.swift │ │ ├── BitSet+Equatable.swift │ │ ├── BitSet+ExpressibleByArrayLiteral.swift │ │ ├── BitSet+Extras.swift │ │ ├── BitSet+Hashable.swift │ │ ├── BitSet+Initializers.swift │ │ ├── BitSet+Invariants.swift │ │ ├── BitSet+Random.swift │ │ ├── BitSet+SetAlgebra basics.swift │ │ ├── BitSet+SetAlgebra conformance.swift │ │ ├── BitSet+SetAlgebra formIntersection.swift │ │ ├── BitSet+SetAlgebra formSymmetricDifference.swift │ │ ├── BitSet+SetAlgebra formUnion.swift │ │ ├── BitSet+SetAlgebra intersection.swift │ │ ├── BitSet+SetAlgebra isDisjoint.swift │ │ ├── BitSet+SetAlgebra isEqualSet.swift │ │ ├── BitSet+SetAlgebra isStrictSubset.swift │ │ ├── BitSet+SetAlgebra isStrictSuperset.swift │ │ ├── BitSet+SetAlgebra isSubset.swift │ │ ├── BitSet+SetAlgebra isSuperset.swift │ │ ├── BitSet+SetAlgebra subtract.swift │ │ ├── BitSet+SetAlgebra subtracting.swift │ │ ├── BitSet+SetAlgebra symmetricDifference.swift │ │ ├── BitSet+SetAlgebra union.swift │ │ ├── BitSet+Sorted Collection APIs.swift │ │ ├── BitSet.Counted.swift │ │ ├── BitSet.Index.swift │ │ ├── BitSet._UnsafeHandle.swift │ │ └── BitSet.swift │ ├── CMakeLists.txt │ └── Shared │ │ ├── Range+Utilities.swift │ │ ├── Slice+Utilities.swift │ │ ├── UInt+Tricks.swift │ │ └── _Word.swift ├── CMakeLists.txt ├── Collections │ ├── CMakeLists.txt │ ├── Collections.docc │ │ ├── Collections.md │ │ └── Extensions │ │ │ ├── BitArray.md │ │ │ ├── BitSet.Counted.md │ │ │ ├── BitSet.md │ │ │ ├── Deque.md │ │ │ ├── Heap.md │ │ │ ├── OrderedDictionary.Elements.md │ │ │ ├── OrderedDictionary.Values.md │ │ │ ├── OrderedDictionary.md │ │ │ ├── OrderedSet.UnorderedView.md │ │ │ ├── OrderedSet.md │ │ │ ├── TreeDictionary.md │ │ │ └── TreeSet.md │ └── Collections.swift ├── DequeModule │ ├── CMakeLists.txt │ ├── Deque+Codable.swift │ ├── Deque+Collection.swift │ ├── Deque+CustomReflectable.swift │ ├── Deque+Descriptions.swift │ ├── Deque+Equatable.swift │ ├── Deque+ExpressibleByArrayLiteral.swift │ ├── Deque+Extras.swift │ ├── Deque+Hashable.swift │ ├── Deque+Testing.swift │ ├── Deque._Storage.swift │ ├── Deque._UnsafeHandle.swift │ ├── Deque.swift │ ├── DequeModule.docc │ │ ├── DequeModule.md │ │ └── Extensions │ │ │ └── Deque.md │ ├── _DequeBuffer.swift │ ├── _DequeBufferHeader.swift │ ├── _DequeSlot.swift │ └── _UnsafeWrappedBuffer.swift ├── HashTreeCollections │ ├── CMakeLists.txt │ ├── HashNode │ │ ├── _AncestorHashSlots.swift │ │ ├── _Bitmap.swift │ │ ├── _Bucket.swift │ │ ├── _Hash.swift │ │ ├── _HashLevel.swift │ │ ├── _HashNode+Builder.swift │ │ ├── _HashNode+Debugging.swift │ │ ├── _HashNode+Initializers.swift │ │ ├── _HashNode+Invariants.swift │ │ ├── _HashNode+Lookups.swift │ │ ├── _HashNode+Primitive Insertions.swift │ │ ├── _HashNode+Primitive Removals.swift │ │ ├── _HashNode+Primitive Replacement.swift │ │ ├── _HashNode+Storage.swift │ │ ├── _HashNode+Structural compactMapValues.swift │ │ ├── _HashNode+Structural filter.swift │ │ ├── _HashNode+Structural intersection.swift │ │ ├── _HashNode+Structural isDisjoint.swift │ │ ├── _HashNode+Structural isEqualSet.swift │ │ ├── _HashNode+Structural isSubset.swift │ │ ├── _HashNode+Structural mapValues.swift │ │ ├── _HashNode+Structural merge.swift │ │ ├── _HashNode+Structural subtracting.swift │ │ ├── _HashNode+Structural symmetricDifference.swift │ │ ├── _HashNode+Structural union.swift │ │ ├── _HashNode+Subtree Insertions.swift │ │ ├── _HashNode+Subtree Modify.swift │ │ ├── _HashNode+Subtree Removals.swift │ │ ├── _HashNode+UnsafeHandle.swift │ │ ├── _HashNode.swift │ │ ├── _HashNodeHeader.swift │ │ ├── _HashSlot.swift │ │ ├── _HashStack.swift │ │ ├── _HashTreeIterator.swift │ │ ├── _HashTreeStatistics.swift │ │ ├── _RawHashNode+UnsafeHandle.swift │ │ ├── _RawHashNode.swift │ │ ├── _UnmanagedHashNode.swift │ │ └── _UnsafePath.swift │ ├── HashTreeCollections.docc │ │ ├── Extensions │ │ │ ├── TreeDictionary.md │ │ │ └── TreeSet.md │ │ └── HashTreeCollections.md │ ├── TreeDictionary │ │ ├── TreeDictionary+Codable.swift │ │ ├── TreeDictionary+Collection.swift │ │ ├── TreeDictionary+CustomReflectable.swift │ │ ├── TreeDictionary+Debugging.swift │ │ ├── TreeDictionary+Descriptions.swift │ │ ├── TreeDictionary+Equatable.swift │ │ ├── TreeDictionary+ExpressibleByDictionaryLiteral.swift │ │ ├── TreeDictionary+Filter.swift │ │ ├── TreeDictionary+Hashable.swift │ │ ├── TreeDictionary+Initializers.swift │ │ ├── TreeDictionary+Keys.swift │ │ ├── TreeDictionary+MapValues.swift │ │ ├── TreeDictionary+Merge.swift │ │ ├── TreeDictionary+Sendable.swift │ │ ├── TreeDictionary+Sequence.swift │ │ ├── TreeDictionary+Values.swift │ │ └── TreeDictionary.swift │ └── TreeSet │ │ ├── TreeSet+Codable.swift │ │ ├── TreeSet+Collection.swift │ │ ├── TreeSet+CustomReflectable.swift │ │ ├── TreeSet+Debugging.swift │ │ ├── TreeSet+Descriptions.swift │ │ ├── TreeSet+Equatable.swift │ │ ├── TreeSet+ExpressibleByArrayLiteral.swift │ │ ├── TreeSet+Extras.swift │ │ ├── TreeSet+Filter.swift │ │ ├── TreeSet+Hashable.swift │ │ ├── TreeSet+Sendable.swift │ │ ├── TreeSet+Sequence.swift │ │ ├── TreeSet+SetAlgebra Initializers.swift │ │ ├── TreeSet+SetAlgebra basics.swift │ │ ├── TreeSet+SetAlgebra formIntersection.swift │ │ ├── TreeSet+SetAlgebra formSymmetricDifference.swift │ │ ├── TreeSet+SetAlgebra formUnion.swift │ │ ├── TreeSet+SetAlgebra intersection.swift │ │ ├── TreeSet+SetAlgebra isDisjoint.swift │ │ ├── TreeSet+SetAlgebra isEqualSet.swift │ │ ├── TreeSet+SetAlgebra isStrictSubset.swift │ │ ├── TreeSet+SetAlgebra isStrictSuperset.swift │ │ ├── TreeSet+SetAlgebra isSubset.swift │ │ ├── TreeSet+SetAlgebra isSuperset.swift │ │ ├── TreeSet+SetAlgebra subtract.swift │ │ ├── TreeSet+SetAlgebra subtracting.swift │ │ ├── TreeSet+SetAlgebra symmetricDifference.swift │ │ ├── TreeSet+SetAlgebra union.swift │ │ └── TreeSet.swift ├── HeapModule │ ├── CMakeLists.txt │ ├── Heap+Descriptions.swift │ ├── Heap+ExpressibleByArrayLiteral.swift │ ├── Heap+Invariants.swift │ ├── Heap+UnsafeHandle.swift │ ├── Heap.swift │ ├── HeapModule.docc │ │ ├── Extensions │ │ │ └── Heap.md │ │ └── HeapModule.md │ └── _HeapNode.swift ├── InternalCollectionsUtilities │ ├── CMakeLists.txt │ ├── Debugging.swift.gyb │ ├── Descriptions.swift.gyb │ ├── IntegerTricks │ │ ├── FixedWidthInteger+roundUpToPowerOfTwo.swift.gyb │ │ ├── Integer rank.swift.gyb │ │ ├── UInt+first and last set bit.swift.gyb │ │ ├── UInt+reversed.swift.gyb │ │ └── autogenerated │ │ │ ├── FixedWidthInteger+roundUpToPowerOfTwo.swift │ │ │ ├── Integer rank.swift │ │ │ ├── UInt+first and last set bit.swift │ │ │ └── UInt+reversed.swift │ ├── RandomAccessCollection+Offsets.swift.gyb │ ├── UnsafeBitSet │ │ ├── _UnsafeBitSet+Index.swift.gyb │ │ ├── _UnsafeBitSet+_Word.swift.gyb │ │ ├── _UnsafeBitSet.swift.gyb │ │ └── autogenerated │ │ │ ├── _UnsafeBitSet+Index.swift │ │ │ ├── _UnsafeBitSet+_Word.swift │ │ │ └── _UnsafeBitSet.swift │ ├── UnsafeBufferPointer+Extras.swift.gyb │ ├── UnsafeMutableBufferPointer+Extras.swift.gyb │ ├── _SortedCollection.swift │ ├── _UniqueCollection.swift │ └── autogenerated │ │ ├── Debugging.swift │ │ ├── Descriptions.swift │ │ ├── RandomAccessCollection+Offsets.swift │ │ ├── UnsafeBufferPointer+Extras.swift │ │ └── UnsafeMutableBufferPointer+Extras.swift ├── OrderedCollections │ ├── CMakeLists.txt │ ├── HashTable │ │ ├── _HashTable+Bucket.swift │ │ ├── _HashTable+BucketIterator.swift │ │ ├── _HashTable+Constants.swift │ │ ├── _HashTable+CustomStringConvertible.swift │ │ ├── _HashTable+Testing.swift │ │ ├── _HashTable+UnsafeHandle.swift │ │ ├── _HashTable.swift │ │ └── _Hashtable+Header.swift │ ├── OrderedCollections.docc │ │ ├── Extensions │ │ │ ├── OrderedDictionary.Elements.md │ │ │ ├── OrderedDictionary.Values.md │ │ │ ├── OrderedDictionary.md │ │ │ ├── OrderedSet.UnorderedView.md │ │ │ └── OrderedSet.md │ │ └── OrderedCollections.md │ ├── OrderedDictionary │ │ ├── OrderedDictionary+Codable.swift │ │ ├── OrderedDictionary+CustomReflectable.swift │ │ ├── OrderedDictionary+Deprecations.swift │ │ ├── OrderedDictionary+Descriptions.swift │ │ ├── OrderedDictionary+Elements.SubSequence.swift │ │ ├── OrderedDictionary+Elements.swift │ │ ├── OrderedDictionary+Equatable.swift │ │ ├── OrderedDictionary+ExpressibleByDictionaryLiteral.swift │ │ ├── OrderedDictionary+Hashable.swift │ │ ├── OrderedDictionary+Initializers.swift │ │ ├── OrderedDictionary+Invariants.swift │ │ ├── OrderedDictionary+Partial MutableCollection.swift │ │ ├── OrderedDictionary+Partial RangeReplaceableCollection.swift │ │ ├── OrderedDictionary+Sendable.swift │ │ ├── OrderedDictionary+Sequence.swift │ │ ├── OrderedDictionary+Values.swift │ │ └── OrderedDictionary.swift │ ├── OrderedSet │ │ ├── OrderedSet+Codable.swift │ │ ├── OrderedSet+CustomReflectable.swift │ │ ├── OrderedSet+Descriptions.swift │ │ ├── OrderedSet+Diffing.swift │ │ ├── OrderedSet+Equatable.swift │ │ ├── OrderedSet+ExpressibleByArrayLiteral.swift │ │ ├── OrderedSet+Hashable.swift │ │ ├── OrderedSet+Initializers.swift │ │ ├── OrderedSet+Insertions.swift │ │ ├── OrderedSet+Invariants.swift │ │ ├── OrderedSet+Partial MutableCollection.swift │ │ ├── OrderedSet+Partial RangeReplaceableCollection.swift │ │ ├── OrderedSet+Partial SetAlgebra formIntersection.swift │ │ ├── OrderedSet+Partial SetAlgebra formSymmetricDifference.swift │ │ ├── OrderedSet+Partial SetAlgebra formUnion.swift │ │ ├── OrderedSet+Partial SetAlgebra intersection.swift │ │ ├── OrderedSet+Partial SetAlgebra isDisjoint.swift │ │ ├── OrderedSet+Partial SetAlgebra isEqualSet.swift │ │ ├── OrderedSet+Partial SetAlgebra isStrictSubset.swift │ │ ├── OrderedSet+Partial SetAlgebra isStrictSuperset.swift │ │ ├── OrderedSet+Partial SetAlgebra isSubset.swift │ │ ├── OrderedSet+Partial SetAlgebra isSuperset.swift │ │ ├── OrderedSet+Partial SetAlgebra subtract.swift │ │ ├── OrderedSet+Partial SetAlgebra subtracting.swift │ │ ├── OrderedSet+Partial SetAlgebra symmetricDifference.swift │ │ ├── OrderedSet+Partial SetAlgebra union.swift │ │ ├── OrderedSet+Partial SetAlgebra+Basics.swift │ │ ├── OrderedSet+RandomAccessCollection.swift │ │ ├── OrderedSet+ReserveCapacity.swift │ │ ├── OrderedSet+Sendable.swift │ │ ├── OrderedSet+SubSequence.swift │ │ ├── OrderedSet+Testing.swift │ │ ├── OrderedSet+UnorderedView.swift │ │ ├── OrderedSet+UnstableInternals.swift │ │ └── OrderedSet.swift │ └── Utilities │ │ └── _UnsafeBitset.swift ├── RopeModule │ ├── BigString │ │ ├── Basics │ │ │ ├── BigString+Builder.swift │ │ │ ├── BigString+Contents.swift │ │ │ ├── BigString+Debugging.swift │ │ │ ├── BigString+Index.swift │ │ │ ├── BigString+Ingester.swift │ │ │ ├── BigString+Invariants.swift │ │ │ ├── BigString+Iterators.swift │ │ │ ├── BigString+Metrics.swift │ │ │ ├── BigString+Summary.swift │ │ │ └── BigString.swift │ │ ├── Chunk │ │ │ ├── BigString+Chunk+Append and Insert.swift │ │ │ ├── BigString+Chunk+Breaks.swift │ │ │ ├── BigString+Chunk+Counts.swift │ │ │ ├── BigString+Chunk+Description.swift │ │ │ ├── BigString+Chunk+Indexing by Characters.swift │ │ │ ├── BigString+Chunk+Indexing by UTF16.swift │ │ │ ├── BigString+Chunk+RopeElement.swift │ │ │ ├── BigString+Chunk+Splitting.swift │ │ │ └── BigString+Chunk.swift │ │ ├── Conformances │ │ │ ├── BigString+BidirectionalCollection.swift │ │ │ ├── BigString+Comparable.swift │ │ │ ├── BigString+CustomDebugStringConvertible.swift │ │ │ ├── BigString+CustomStringConvertible.swift │ │ │ ├── BigString+Equatable.swift │ │ │ ├── BigString+ExpressibleByStringLiteral.swift │ │ │ ├── BigString+Hashing.swift │ │ │ ├── BigString+LosslessStringConvertible.swift │ │ │ ├── BigString+RangeReplaceableCollection.swift │ │ │ ├── BigString+Sequence.swift │ │ │ └── BigString+TextOutputStream.swift │ │ ├── Operations │ │ │ ├── BigString+Append.swift │ │ │ ├── BigString+Initializers.swift │ │ │ ├── BigString+Insert.swift │ │ │ ├── BigString+Managing Breaks.swift │ │ │ ├── BigString+RemoveSubrange.swift │ │ │ ├── BigString+ReplaceSubrange.swift │ │ │ ├── BigString+Split.swift │ │ │ └── Range+BigString.swift │ │ └── Views │ │ │ ├── BigString+UTF16View.swift │ │ │ ├── BigString+UTF8View.swift │ │ │ ├── BigString+UnicodeScalarView.swift │ │ │ ├── BigSubstring+UTF16View.swift │ │ │ ├── BigSubstring+UTF8View.swift │ │ │ ├── BigSubstring+UnicodeScalarView.swift │ │ │ └── BigSubstring.swift │ ├── CMakeLists.txt │ ├── Rope │ │ ├── Basics │ │ │ ├── Rope+Builder.swift │ │ │ ├── Rope+Debugging.swift │ │ │ ├── Rope+Invariants.swift │ │ │ ├── Rope+_Node.swift │ │ │ ├── Rope+_Storage.swift │ │ │ ├── Rope+_UnmanagedLeaf.swift │ │ │ ├── Rope+_UnsafeHandle.swift │ │ │ ├── Rope.swift │ │ │ ├── RopeElement.swift │ │ │ ├── RopeMetric.swift │ │ │ ├── RopeSummary.swift │ │ │ ├── _RopeItem.swift │ │ │ ├── _RopePath.swift │ │ │ └── _RopeVersion.swift │ │ ├── Conformances │ │ │ ├── Rope+Collection.swift │ │ │ ├── Rope+Index.swift │ │ │ └── Rope+Sequence.swift │ │ └── Operations │ │ │ ├── Rope+Append.swift │ │ │ ├── Rope+Extract.swift │ │ │ ├── Rope+Find.swift │ │ │ ├── Rope+ForEachWhile.swift │ │ │ ├── Rope+Insert.swift │ │ │ ├── Rope+Join.swift │ │ │ ├── Rope+MutatingForEach.swift │ │ │ ├── Rope+Remove.swift │ │ │ ├── Rope+RemoveSubrange.swift │ │ │ └── Rope+Split.swift │ └── Utilities │ │ ├── Optional Utilities.swift │ │ ├── String Utilities.swift │ │ ├── String.Index+ABI.swift │ │ └── _CharacterRecognizer.swift └── SortedCollections │ ├── BTree │ ├── _BTree+BidirectionalCollection.swift │ ├── _BTree+CustomDebugStringConvertible.swift │ ├── _BTree+CustomReflectable.swift │ ├── _BTree+Invariants.swift │ ├── _BTree+Partial RangeReplaceableCollection.swift │ ├── _BTree+Sequence.swift │ ├── _BTree+SubSequence.swift │ ├── _BTree+UnsafeCursor.swift │ ├── _BTree.Builder.swift │ ├── _BTree.Index.swift │ ├── _BTree.swift │ ├── _FixedSizeArray.swift │ ├── _Node+CustomDebugString.swift │ ├── _Node+Testing.swift │ ├── _Node.Splinter.swift │ ├── _Node.Storage.swift │ ├── _Node.UnsafeHandle+CustomDebugStringConvertible.swift │ ├── _Node.UnsafeHandle+Deletion.swift │ ├── _Node.UnsafeHandle+Insertion.swift │ ├── _Node.UnsafeHandle.swift │ └── _Node.swift │ ├── SortedDictionary │ ├── SortedDictionary+BidirectionalCollection.swift │ ├── SortedDictionary+Codable.swift │ ├── SortedDictionary+CustomReflectable.swift │ ├── SortedDictionary+CustomStringConvertible.swift │ ├── SortedDictionary+Equatable.swift │ ├── SortedDictionary+ExpressibleByDictionaryLiteral.swift │ ├── SortedDictionary+Hashable.swift │ ├── SortedDictionary+Initializers.swift │ ├── SortedDictionary+Keys.swift │ ├── SortedDictionary+Partial RangeReplaceableCollection.swift │ ├── SortedDictionary+Sendable.swift │ ├── SortedDictionary+Sequence.swift │ ├── SortedDictionary+SubSequence.swift │ ├── SortedDictionary+Subscripts.swift │ ├── SortedDictionary+Values.swift │ ├── SortedDictionary.Index.swift │ └── SortedDictionary.swift │ ├── SortedSet │ ├── SortedSet+BidirectionalCollection.swift │ ├── SortedSet+Codable.swift │ ├── SortedSet+CustomReflectable.swift │ ├── SortedSet+CustomStringConvertible.swift │ ├── SortedSet+Equatable.swift │ ├── SortedSet+ExpressibleByArrayLiteral.swift │ ├── SortedSet+Hashable.swift │ ├── SortedSet+Initializers.swift │ ├── SortedSet+Partial RangeReplaceableCollection.swift │ ├── SortedSet+Sendable.swift │ ├── SortedSet+Sequence.swift │ ├── SortedSet+SetAlgebra.swift │ ├── SortedSet+SubSequence.swift │ ├── SortedSet+Subscripts.swift │ ├── SortedSet.Index.swift │ └── SortedSet.swift │ └── Utilities │ └── Assertions.swift ├── Tests ├── BitCollectionsTests │ ├── BitArrayTests.swift │ ├── BitSet.Counted Tests.swift │ └── BitSetTests.swift ├── CollectionsTestSupportTests │ ├── CombinatoricsChecks.swift │ ├── IndexRangeCollectionTests.swift │ ├── MinimalTypeConformances.swift │ └── UtilitiesTests.swift ├── DequeTests │ ├── DequeInternals.swift │ ├── DequeTests.swift │ ├── MutableCollectionTests.swift │ └── RangeReplaceableCollectionTests.swift ├── HashTreeCollectionsTests │ ├── Colliders.swift │ ├── Hash.swift │ ├── TreeDictionary Smoke Tests.swift │ ├── TreeDictionary Tests.swift │ ├── TreeDictionary.Keys Tests.swift │ ├── TreeDictionary.Values Tests.swift │ ├── TreeHashedCollections Fixtures.swift │ ├── TreeSet Tests.swift │ └── Utilities.swift ├── HeapTests │ ├── HeapNodeTests.swift │ └── HeapTests.swift ├── OrderedCollectionsTests │ ├── HashTable │ │ └── HashTableTests.swift │ ├── OrderedDictionary │ │ ├── OrderedDictionary Tests.swift │ │ ├── OrderedDictionary Utils.swift │ │ ├── OrderedDictionary+Elements Tests.swift │ │ └── OrderedDictionary+Values Tests.swift │ └── OrderedSet │ │ ├── OrderedSet Diffing Tests.swift │ │ ├── OrderedSet.UnorderedView Tests.swift │ │ ├── OrderedSetInternals.swift │ │ ├── OrderedSetTests.swift │ │ └── RandomAccessCollection+Extras.swift ├── README.md ├── RopeModuleTests │ ├── Availability.swift │ ├── SampleStrings.swift │ ├── TestBigString.swift │ └── TestRope.swift ├── SortedCollectionsTests │ ├── BTree │ │ ├── BTree Tests.swift │ │ ├── BTree+Deletion Tests.swift │ │ ├── BTree.Builder Tests.swift │ │ ├── Node Utils.swift │ │ ├── Node+Balancing Tests.swift │ │ ├── Node+Insertion Tests.swift │ │ ├── Node+Join Tests.swift │ │ └── NodeTests.swift │ ├── SortedDictionary │ │ ├── SortedDictionary Tests.swift │ │ └── SortedDictionary Utils.swift │ └── SortedSet │ │ └── SortedSet Tests.swift └── _CollectionsTestSupport │ ├── AssertionContexts │ ├── Assertions.swift │ ├── CollectionTestCase.swift │ ├── Combinatorics.swift │ └── TestContext.swift │ ├── ConformanceCheckers │ ├── CheckBidirectionalCollection.swift │ ├── CheckCollection.swift │ ├── CheckComparable.swift │ ├── CheckEquatable.swift │ ├── CheckHashable.swift │ └── CheckSequence.swift │ ├── MinimalTypes │ ├── MinimalBidirectionalCollection.swift │ ├── MinimalCollection.swift │ ├── MinimalDecoder.swift │ ├── MinimalEncoder.swift │ ├── MinimalIndex.swift │ ├── MinimalIterator.swift │ ├── MinimalMutableRandomAccessCollection.swift │ ├── MinimalMutableRangeReplaceableRandomAccessCollection.swift │ ├── MinimalRandomAccessCollection.swift │ ├── MinimalRangeReplaceableRandomAccessCollection.swift │ ├── MinimalSequence.swift │ ├── ResettableValue.swift │ ├── _CollectionState.swift │ └── _MinimalCollectionCore.swift │ └── Utilities │ ├── AllOnesRandomNumberGenerator.swift │ ├── Box.swift │ ├── DictionaryAPIChecker.swift │ ├── HashableBox.swift │ ├── IndexRangeCollection.swift │ ├── Integer Square Root.swift │ ├── LifetimeTracked.swift │ ├── LifetimeTracker.swift │ ├── RandomStableSample.swift │ ├── RepeatableRandomNumberGenerator.swift │ ├── SetAPIChecker.swift │ ├── SortedCollectionAPIChecker.swift │ └── StringConvertibleValue.swift ├── Utils ├── README.md ├── generate-docs.sh ├── generate-sources.sh ├── gyb ├── gyb.py ├── gyb_utils.py ├── run-benchmarks.sh ├── run-full-tests.sh ├── shuffle-sources.sh └── swift-collections.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ ├── IDETemplateMacros.plist │ ├── IDEWorkspaceChecks.plist │ ├── swiftpm │ └── Package.resolved │ └── xcschemes │ ├── BitCollections.xcscheme │ ├── Collections.xcscheme │ ├── CollectionsBenchmark.xcscheme │ ├── CollectionsTestSupport.xcscheme │ ├── DequeModule.xcscheme │ ├── HashTreeCollections.xcscheme │ ├── HeapModule.xcscheme │ ├── OrderedCollections.xcscheme │ ├── _RopeModule.xcscheme │ ├── benchmark.xcscheme │ ├── memory-benchmark.xcscheme │ └── swift-collections-Package.xcscheme ├── Xcode ├── Collections.xcconfig ├── Collections.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── xcshareddata │ │ └── xcschemes │ │ └── Collections.xcscheme ├── Collections.xctestplan ├── CollectionsTests.xcconfig ├── README.md └── Shared.xcconfig └── cmake └── modules ├── CMakeLists.txt ├── PlatformInfo.cmake ├── SwiftCollectionsConfig.cmake.in └── SwiftSupport.cmake /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | insert_final_newline = true 6 | end_of_line = lf 7 | trim_trailing_whitespace = true 8 | 9 | [*.{swift,swift.gyb,cpp,h,yml}] 10 | indent_style = space 11 | tab_width = 4 12 | indent_size = 2 13 | 14 | [*.{sh,py}] 15 | indent_style = space 16 | indent_size = 4 17 | 18 | [.swiftci/*}] 19 | indent_style = space 20 | indent_size = 4 21 | 22 | [CMakeLists.txt] 23 | indent_style = space 24 | indent_size = 2 25 | 26 | [*.cmake{,.in}] 27 | indent_style = space 28 | indent_size = 2 29 | 30 | [*.{md,txt}] 31 | indent_style = space 32 | indent_size = 2 33 | 34 | [*.xcconfig] 35 | indent_style = space 36 | indent_size = 2 37 | 38 | [*.pbxproj] 39 | indent_style = tab 40 | tab_width = 4 41 | indent_size = 4 42 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/BUG_REPORT.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🐛 Bug Report 3 | about: Something isn't working as expected 4 | labels: bug 5 | --- 6 | 7 | 17 | 18 | Replace this paragraph with a short description of the incorrect behavior. 19 | (If this is a regression, please note the last version of the package that exhibited the correct behavior in addition to your current version.) 20 | 21 | ### Information 22 | 23 | - **Package version:** What tag or branch of swift-collections are you using? 24 | - **Platform version:** Please tell us the version number of your operating system. 25 | - **Swift version:** Paste the output of `swift --version` here. 26 | 27 | ### Checklist 28 | 29 | - [ ] If possible, I've reproduced the issue using the `main` branch of this package. 30 | - [ ] I've searched for [existing GitHub issues](https://github.com/apple/swift-collections/issues). 31 | 32 | ### Steps to Reproduce 33 | Replace this paragraph with an explanation of how to reproduce the incorrect behavior. 34 | Include a simple code example, if possible. 35 | 36 | ### Expected behavior 37 | Describe what you expect to happen. 38 | 39 | ### Actual behavior 40 | Describe or copy/paste the behavior you observe. 41 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 💡 Feature Request 3 | about: A suggestion for a new feature 4 | labels: enhancement 5 | --- 6 | 7 | 13 | 14 | Replace this paragraph with a description of your proposed feature. 15 | Please be sure to describe some concrete use cases for the new feature -- be as specific as possible. 16 | Provide links to existing issues or external references/discussions, if appropriate. 17 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: ❓ Discussion Forum 4 | url: https://forums.swift.org/c/related-projects/collections/ 5 | about: Questions about using Swift Collections? Ask here! 6 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 14 | 15 | Replace this paragraph with a description of your changes and rationale. Provide links to an existing issue or external references/discussions, if appropriate. 16 | 17 | ### Checklist 18 | - [ ] I've read the [Contribution Guidelines](/README.md#contributing-to-swift-collections) 19 | - [ ] My contributions are licensed under the [Swift license](/LICENSE.txt). 20 | - [ ] I've followed the coding style of the rest of the project. 21 | - [ ] I've added tests covering all new code paths my change adds to the project (if appropriate). 22 | - [ ] I've added benchmarks covering new functionality (if appropriate). 23 | - [ ] I've verified that my change does not break any existing tests or introduce unexplained benchmark regressions. 24 | - [ ] I've updated the documentation if necessary. 25 | -------------------------------------------------------------------------------- /.github/workflows/pull_request.yml: -------------------------------------------------------------------------------- 1 | name: Pull request 2 | 3 | on: 4 | pull_request: 5 | types: [opened, reopened, synchronize] 6 | 7 | jobs: 8 | tests: 9 | name: Test 10 | uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main 11 | with: 12 | linux_exclude_swift_versions: '[{"swift_version": "5.8"}, {"swift_version": "5.9"}]' 13 | windows_exclude_swift_versions: '[{"swift_version": "5.9"}]' 14 | soundness: 15 | name: Soundness 16 | uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main 17 | with: 18 | license_header_check_project_name: "Swift.org" 19 | # https://github.com/apple/swift-collections/issues/428 20 | python_lint_check_enabled: false 21 | # https://github.com/apple/swift-collections/issues/429 22 | docs_check_enabled: false 23 | # https://github.com/apple/swift-collections/issues/430 24 | shell_check_enabled: false 25 | # https://github.com/apple/swift-collections/issues/431 26 | format_check_enabled: false 27 | # https://github.com/apple/swift-collections/issues/434 28 | license_header_check_enabled: false 29 | # https://github.com/apple/swift-collections/issues/435 30 | api_breakage_check_enabled: false 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.swiftpm 3 | /.build 4 | /Packages 5 | /*.xcodeproj 6 | xcuserdata/ 7 | .*.sw? 8 | /Benchmarks/.swiftpm 9 | /Benchmarks/.build 10 | .docc-build 11 | __pycache__ 12 | -------------------------------------------------------------------------------- /.spi.yml: -------------------------------------------------------------------------------- 1 | version: 1 2 | builder: 3 | configs: 4 | - documentation_targets: [Collections, BitCollections, DequeModule, HashTreeCollections, HeapModule, OrderedCollections] 5 | -------------------------------------------------------------------------------- /.swiftci/5_10_ubuntu2204: -------------------------------------------------------------------------------- 1 | LinuxSwiftPackageJob { 2 | swift_version_tag = "5.10-jammy" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/5_7_ubuntu2204: -------------------------------------------------------------------------------- 1 | LinuxSwiftPackageJob { 2 | swift_version_tag = "5.7-jammy" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/5_8_ubuntu2204: -------------------------------------------------------------------------------- 1 | LinuxSwiftPackageJob { 2 | swift_version_tag = "5.8-jammy" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/5_9_ubuntu2204: -------------------------------------------------------------------------------- 1 | LinuxSwiftPackageJob { 2 | swift_version_tag = "5.9-jammy" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/nightly_6_0_macos: -------------------------------------------------------------------------------- 1 | macOSSwiftPackageJob { 2 | swift_version = "6.0" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/nightly_6_0_ubuntu2204: -------------------------------------------------------------------------------- 1 | LinuxSwiftPackageJob { 2 | nightly_docker_tag = "nightly-6.0-jammy" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/nightly_main_macos: -------------------------------------------------------------------------------- 1 | macOSSwiftPackageJob { 2 | swift_version = "main" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/nightly_main_ubuntu2204: -------------------------------------------------------------------------------- 1 | LinuxSwiftPackageJob { 2 | nightly_docker_tag = "nightly-jammy" 3 | repo = "swift-collections" 4 | branch = "main" 5 | } 6 | -------------------------------------------------------------------------------- /.swiftci/nightly_main_windows: -------------------------------------------------------------------------------- 1 | WindowsSwiftPackageWithDockerImageJob { 2 | docker_image = "swiftlang/swift:nightly-windowsservercore-1809" 3 | repo = "swift-collections" 4 | branch = "main" 5 | sub_dir = "swift-collections" 6 | label = "windows-server-2019" 7 | } 8 | -------------------------------------------------------------------------------- /Benchmarks/Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "originHash" : "77540dbea8c5b0878a1a745326146f44af61e58620d5484d3d37c7454bf8b088", 3 | "pins" : [ 4 | { 5 | "identity" : "swift-argument-parser", 6 | "kind" : "remoteSourceControl", 7 | "location" : "https://github.com/apple/swift-argument-parser", 8 | "state" : { 9 | "revision" : "41982a3656a71c768319979febd796c6fd111d5c", 10 | "version" : "1.5.0" 11 | } 12 | }, 13 | { 14 | "identity" : "swift-collections-benchmark", 15 | "kind" : "remoteSourceControl", 16 | "location" : "https://github.com/apple/swift-collections-benchmark", 17 | "state" : { 18 | "revision" : "69cd5b456633671456884217688afe53f87efd13", 19 | "version" : "0.0.4" 20 | } 21 | }, 22 | { 23 | "identity" : "swift-system", 24 | "kind" : "remoteSourceControl", 25 | "location" : "https://github.com/apple/swift-system", 26 | "state" : { 27 | "revision" : "a34201439c74b53f0fd71ef11741af7e7caf01e1", 28 | "version" : "1.4.2" 29 | } 30 | } 31 | ], 32 | "version" : 3 33 | } 34 | -------------------------------------------------------------------------------- /Benchmarks/Sources/Benchmarks/Cpp/CppBenchmarks.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import CollectionsBenchmark 13 | import CppBenchmarks 14 | 15 | extension Benchmark { 16 | public mutating func addCppBenchmarks() { 17 | cpp_set_hash_fn { value in value._rawHashValue(seed: 0) } 18 | 19 | self.addSimple( 20 | title: "std::hash", 21 | input: [Int].self 22 | ) { input in 23 | input.withUnsafeBufferPointer { buffer in 24 | cpp_hash(buffer.baseAddress, buffer.count) 25 | } 26 | } 27 | 28 | self.addSimple( 29 | title: "custom_intptr_hash (using Swift.Hasher)", 30 | input: [Int].self 31 | ) { input in 32 | input.withUnsafeBufferPointer { buffer in 33 | cpp_custom_hash(buffer.baseAddress, buffer.count) 34 | } 35 | } 36 | 37 | _addCppVectorBenchmarks() 38 | _addCppDequeBenchmarks() 39 | _addCppUnorderedSetBenchmarks() 40 | _addCppUnorderedMapBenchmarks() 41 | _addCppPriorityQueueBenchmarks() 42 | _addCppVectorBoolBenchmarks() 43 | _addCppMapBenchmarks() 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Benchmarks/Sources/Benchmarks/Cpp/CppMapBenchmarks.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import CollectionsBenchmark 13 | import CppBenchmarks 14 | 15 | internal class CppMap { 16 | var ptr: UnsafeMutableRawPointer? 17 | 18 | init(_ input: [Int]) { 19 | self.ptr = input.withUnsafeBufferPointer { buffer in 20 | cpp_map_create(buffer.baseAddress, buffer.count) 21 | } 22 | } 23 | 24 | deinit { 25 | destroy() 26 | } 27 | 28 | func destroy() { 29 | if let ptr = ptr { 30 | cpp_map_destroy(ptr) 31 | } 32 | ptr = nil 33 | } 34 | } 35 | 36 | extension Benchmark { 37 | internal mutating func _addCppMapBenchmarks() { 38 | self.addSimple( 39 | title: "std::map insert", 40 | input: [Int].self 41 | ) { input in 42 | input.withUnsafeBufferPointer { buffer in 43 | cpp_map_insert_integers(buffer.baseAddress, buffer.count) 44 | } 45 | } 46 | 47 | self.add( 48 | title: "std::map successful find", 49 | input: ([Int], [Int]).self 50 | ) { input, lookups in 51 | let map = CppMap(input) 52 | return { timer in 53 | lookups.withUnsafeBufferPointer { buffer in 54 | cpp_map_lookups(map.ptr, buffer.baseAddress, buffer.count) 55 | } 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Benchmarks/Sources/Benchmarks/Foundation/FoundationBenchmarks.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import CollectionsBenchmark 13 | 14 | extension Benchmark { 15 | public mutating func addFoundationBenchmarks() { 16 | _addCFBinaryHeapBenchmarks() 17 | _addCFBitVectorBenchmarks() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/include/Hashing.h: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #ifndef CPPBENCHMARKS_HASHING_H 13 | #define CPPBENCHMARKS_HASHING_H 14 | 15 | #include 16 | #include 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | typedef size_t (*cpp_hash_fn)(intptr_t); 23 | extern void cpp_set_hash_fn(cpp_hash_fn fn); 24 | 25 | // Benchmarks 26 | void cpp_hash(const intptr_t *start, size_t count); 27 | void cpp_custom_hash(const intptr_t *start, size_t count); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | #endif /* CPPBENCHMARKS_HASHING_H */ 34 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/include/MapBenchmarks.h: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #ifndef CPPBENCHMARKS_MAP_BENCHMARKS_H 13 | #define CPPBENCHMARKS_MAP_BENCHMARKS_H 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | /// Create a std::map, populating it with data from the supplied buffer. 24 | /// Returns an opaque pointer to the created instance. 25 | extern void *cpp_map_create(const intptr_t *start, size_t count); 26 | 27 | /// Destroys an ordered map previously returned by `cpp_map_create`. 28 | extern void cpp_map_destroy(void *ptr); 29 | 30 | extern void cpp_map_insert_integers(const intptr_t *start, size_t count); 31 | 32 | extern void cpp_map_lookups(void *ptr, const intptr_t *start, size_t count); 33 | extern void cpp_map_subscript(void *ptr, const intptr_t *start, size_t count); 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | 40 | #endif /* CPPBENCHMARKS_MAP_BENCHMARKS_H */ 41 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/include/UnorderedMapBenchmarks.h: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #ifndef CPPBENCHMARKS_UNORDERED_MAP_BENCHMARKS_H 13 | #define CPPBENCHMARKS_UNORDERED_MAP_BENCHMARKS_H 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | /// Create a std::unordered_map, populating it with data from the supplied buffer. 24 | /// Returns an opaque pointer to the created instance. 25 | extern void *cpp_unordered_map_create(const intptr_t *start, size_t count); 26 | 27 | /// Destroys an unordered map previously returned by `cpp_unordered_map_create`. 28 | extern void cpp_unordered_map_destroy(void *ptr); 29 | 30 | extern void cpp_unordered_map_iterate(void *ptr); 31 | 32 | extern void cpp_unordered_map_from_int_range(intptr_t count); 33 | extern void cpp_unordered_map_insert_integers(const intptr_t *start, size_t count, bool reserve); 34 | 35 | extern void cpp_unordered_map_lookups(void *ptr, const intptr_t *start, size_t count, bool expectMatch); 36 | extern void cpp_unordered_map_subscript(void *ptr, const intptr_t *start, size_t count); 37 | extern void cpp_unordered_map_removals(void *ptr, const intptr_t *start, size_t count); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | 44 | #endif /* CPPBENCHMARKS_UNORDERED_MAP_BENCHMARKS_H */ 45 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/include/Utils.h: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #ifndef BLACK_HOLE_H 13 | #define BLACK_HOLE_H 14 | 15 | #include 16 | 17 | #ifdef __cplusplus 18 | 19 | // FIXME: Is putting this in a separate compilation unit enough to make 20 | // sure the function call is always emitted? 21 | 22 | extern void black_hole(intptr_t value); 23 | extern void black_hole(void *value); 24 | 25 | extern intptr_t identity(intptr_t value); 26 | extern void *_identity(void *value); 27 | extern const void *_identity(const void *value); 28 | 29 | template 30 | static inline T *identity(T *value) 31 | { 32 | return static_cast(_identity(value)); 33 | } 34 | 35 | template 36 | static inline const T *identity(const T *value) 37 | { 38 | return static_cast(_identity(value)); 39 | } 40 | 41 | #endif /* __cplusplus */ 42 | #endif /* BLACK_HOLE_H */ 43 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/include/module.modulemap: -------------------------------------------------------------------------------- 1 | module CppBenchmarks { 2 | header "Utils.h" 3 | header "Hashing.h" 4 | header "VectorBenchmarks.h" 5 | header "DequeBenchmarks.h" 6 | header "UnorderedSetBenchmarks.h" 7 | header "UnorderedMapBenchmarks.h" 8 | header "MapBenchmarks.h" 9 | header "PriorityQueueBenchmarks.h" 10 | header "VectorBoolBenchmarks.h" 11 | export * 12 | } 13 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/src/CustomHash.h: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #ifndef CUSTOMHASH_H 13 | #define CUSTOMHASH_H 14 | 15 | #include 16 | #include 17 | 18 | #include "Hashing.h" 19 | 20 | extern cpp_hash_fn custom_hash_fn; 21 | 22 | struct custom_intptr_hash: public std::function 23 | { 24 | std::size_t 25 | operator()(intptr_t value) const 26 | { 27 | return static_cast(custom_hash_fn(value)); 28 | } 29 | }; 30 | 31 | #endif /* CUSTOMHASH_H */ 32 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/src/Hashing.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #import "Hashing.h" 13 | #import "CustomHash.h" 14 | #import "Utils.h" 15 | 16 | cpp_hash_fn custom_hash_fn; 17 | 18 | void 19 | cpp_set_hash_fn(cpp_hash_fn fn) 20 | { 21 | custom_hash_fn = fn; 22 | } 23 | 24 | void 25 | cpp_hash(const intptr_t *start, size_t count) 26 | { 27 | for (auto p = start; p < start + count; ++p) { 28 | black_hole(std::hash{}(*p)); 29 | } 30 | } 31 | 32 | void 33 | cpp_custom_hash(const intptr_t *start, size_t count) 34 | { 35 | for (auto p = start; p < start + count; ++p) { 36 | black_hole(custom_intptr_hash{}(*p)); 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/src/PriorityQueueBenchmarks.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #include 13 | #include "PriorityQueueBenchmarks.h" 14 | #include "Utils.h" 15 | 16 | typedef std::priority_queue pqueue; 17 | 18 | void * 19 | cpp_priority_queue_create(const intptr_t *start, size_t count) 20 | { 21 | auto pq = new pqueue(start, start + count); 22 | return pq; 23 | } 24 | 25 | void 26 | cpp_priority_queue_destroy(void *ptr) 27 | { 28 | delete static_cast(ptr); 29 | } 30 | 31 | void 32 | cpp_priority_queue_push(void *ptr, intptr_t value) 33 | { 34 | auto pq = static_cast(ptr); 35 | pq->push(value); 36 | } 37 | 38 | void 39 | cpp_priority_queue_push_loop(void *ptr, const intptr_t *start, size_t count) 40 | { 41 | auto pq = static_cast(ptr); 42 | for (auto p = start; p < start + count; ++p) { 43 | pq->push(*p); 44 | } 45 | } 46 | 47 | intptr_t cpp_priority_queue_pop(void *ptr) 48 | { 49 | auto pq = static_cast(ptr); 50 | auto result = pq->top(); 51 | pq->pop(); 52 | return result; 53 | } 54 | 55 | void 56 | cpp_priority_queue_pop_all(void *ptr) 57 | { 58 | auto pq = static_cast(ptr); 59 | while (!pq->empty()) { 60 | black_hole(pq->top()); 61 | pq->pop(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Benchmarks/Sources/CppBenchmarks/src/Utils.cpp: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #include "Utils.h" 13 | 14 | void 15 | black_hole(intptr_t value) 16 | { 17 | // Do nothing. 18 | } 19 | 20 | void 21 | black_hole(void *value) 22 | { 23 | // Do nothing. 24 | } 25 | 26 | intptr_t 27 | identity(intptr_t value) 28 | { 29 | return value; 30 | } 31 | 32 | void * 33 | _identity(void *value) 34 | { 35 | return value; 36 | } 37 | 38 | const void * 39 | _identity(const void *value) 40 | { 41 | return value; 42 | } 43 | -------------------------------------------------------------------------------- /Benchmarks/Sources/benchmark-tool/main.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import CollectionsBenchmark 13 | import Benchmarks 14 | import DequeModule 15 | 16 | if Deque._isConsistencyCheckingEnabled { 17 | complain(""" 18 | *** INTERNAL CONSISTENCY CHECKING IS ENABLED *** 19 | 20 | Performance guarantees aren't valid in this configuration, 21 | and benchmarking data will be largely useless. Proceed at 22 | your own risk. 23 | 24 | """) 25 | } 26 | 27 | var benchmark = Benchmark(title: "Collection Benchmarks") 28 | benchmark.registerCustomGenerators() 29 | benchmark.addArrayBenchmarks() 30 | benchmark.addSetBenchmarks() 31 | benchmark.addDictionaryBenchmarks() 32 | benchmark.addTreeDictionaryBenchmarks() 33 | benchmark.addDequeBenchmarks() 34 | benchmark.addOrderedSetBenchmarks() 35 | benchmark.addOrderedDictionaryBenchmarks() 36 | #if false 37 | benchmark.addSortedSetBenchmarks() 38 | benchmark.addSortedDictionaryBenchmarks() 39 | #endif 40 | benchmark.addHeapBenchmarks() 41 | benchmark.addBitSetBenchmarks() 42 | benchmark.addTreeSetBenchmarks() 43 | benchmark.addCppBenchmarks() 44 | benchmark.addBigStringBenchmarks() 45 | #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) 46 | benchmark.addFoundationBenchmarks() 47 | #endif 48 | 49 | benchmark.main() 50 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Lines starting with '#' are comments. 2 | # Each line is a case-sensitive file pattern followed by one or more owners. 3 | # Order is important. The last matching pattern has the most precedence. 4 | # More information: https://docs.github.com/en/articles/about-code-owners 5 | # 6 | # Please mirror the repository's file hierarchy in case-sensitive lexicographic 7 | # order. 8 | 9 | # Default owners 10 | * @lorentey 11 | 12 | # Swift CI configuration files 13 | .swiftci/ @shahmishal 14 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | The code of conduct for this project can be found at https://swift.org/code-of-conduct. 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | By submitting a pull request, you represent that you have the right to license 2 | your contribution to Apple and the community, and agree by submitting the patch 3 | that your contributions are licensed under the [Swift 4 | license](https://swift.org/LICENSE.txt). 5 | 6 | --- 7 | 8 | Before submitting the pull request, please make sure you have tested your 9 | changes and that they follow the Swift project [guidelines for contributing 10 | code](https://swift.org/contributing/#contributing-code). 11 | 12 | -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/README.md: -------------------------------------------------------------------------------- 1 | # Benchmark data from the swift.org announcement 2 | 3 | This directory contains the benchmark configuration that we used to collect benchmarking data and render the charts in the [Swift Collections announcement on swift.org][announcement]. 4 | 5 | * [`generate-results.sh`](./generate-results.sh): A script that you can use to run the benchmark on your own machine, and to reproduce the charts. 6 | * [`Library.json`](./Library.json): The benchmark library definition collecting the chart definitions used in the blog post. 7 | * [`Theme.json`](./Theme.json): The chart theme to set up colors, line widths, font sizes etc. (Slightly adapted.) 8 | * [`Results.md`](./Results.md): The generated results summary. 9 | * [`Results/`](./Results/): The generated subdirectory containing the PNG files for the charts. 10 | * [`results.json`](./results.json): The result data we collected. 11 | 12 | Note: If you'd like to try reproducing our results (it's easy!), note that the script is configured to collect 20 rounds of data on up to 16 million items, and this will likely take a very long time. Feel free to reduce the maximum size, or just plan to allow the measurement run overnight. 13 | 14 | For more information on how to use the Swift Collections benchmarking tool, please see [Swift Collections Benchmark][swift-collections-benchmark]. 15 | 16 | [announcement]: https://swift.org/blog/swift-collections 17 | [swift-collections-benchmark]: https://github.com/apple/swift-collections-benchmark 18 | -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/Results.md: -------------------------------------------------------------------------------- 1 | # Benchmark results 2 | 3 | Click to expand individual items below. 4 |
5 | All results 6 |
    7 |
    8 | 01: Deque - random access lookups 9 | 10 |
    11 |
    12 | 02: Deque - prepending individual integers 13 | 14 |
    15 |
    16 | 03: OrderedSet lookups 17 | 18 |
    19 |
    20 | 04: OrderedSet insertions 21 | 22 |
    23 |
    24 | 05: OrderedDictionary lookups 25 | 26 |
    27 |
    28 | 06: OrderedDictionary insertions 29 | 30 |
    31 |
32 |
33 | -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/Results/01 Deque - random access lookups.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Announcement-benchmarks/Results/01 Deque - random access lookups.png -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/Results/02 Deque - prepending individual integers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Announcement-benchmarks/Results/02 Deque - prepending individual integers.png -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/Results/03 OrderedSet lookups.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Announcement-benchmarks/Results/03 OrderedSet lookups.png -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/Results/04 OrderedSet insertions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Announcement-benchmarks/Results/04 OrderedSet insertions.png -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/Results/05 OrderedDictionary lookups.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Announcement-benchmarks/Results/05 OrderedDictionary lookups.png -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/Results/06 OrderedDictionary insertions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Announcement-benchmarks/Results/06 OrderedDictionary insertions.png -------------------------------------------------------------------------------- /Documentation/Announcement-benchmarks/generate-results.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #===----------------------------------------------------------------------===// 3 | # 4 | # This source file is part of the Swift Collections open source project 5 | # 6 | # Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 7 | # Licensed under Apache License v2.0 with Runtime Library Exception 8 | # 9 | # See https://swift.org/LICENSE.txt for license information 10 | # 11 | #===----------------------------------------------------------------------===// 12 | 13 | set -eux 14 | 15 | ../../Utils/run-benchmarks.sh library run results.json --library Library.json --max-size 16M --cycles 20 --mode replace-all 16 | ../../Utils/run-benchmarks.sh library render results.json --library Library.json --max-time 10us --min-time 1ns --theme-file Theme.json --percentile 90 --output . 17 | -------------------------------------------------------------------------------- /Documentation/Images/Heap Performance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Images/Heap Performance.png -------------------------------------------------------------------------------- /Documentation/Reviews/2022-10-31.ShareableHashedCollections/ShareableSet-insert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Reviews/2022-10-31.ShareableHashedCollections/ShareableSet-insert.png -------------------------------------------------------------------------------- /Documentation/Reviews/2022-10-31.ShareableHashedCollections/ShareableSet-model-diffing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Reviews/2022-10-31.ShareableHashedCollections/ShareableSet-model-diffing.png -------------------------------------------------------------------------------- /Documentation/Reviews/2022-10-31.ShareableHashedCollections/ShareableSet-sequential iteration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-collections/d402bec9342aa4ec19ad41a50a4d828454ee6410/Documentation/Reviews/2022-10-31.ShareableHashedCollections/ShareableSet-sequential iteration.png -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitArray: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self, displayStyle: .collection) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitArray: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. 14 | /// 15 | /// Equality is the inverse of inequality. For any values `a` and `b`, 16 | /// `a == b` implies that `a != b` is `false`. 17 | /// 18 | /// - Parameter lhs: A value to compare. 19 | /// - Parameter rhs: Another value to compare. 20 | /// - Complexity: O(left.count) 21 | public static func ==(left: Self, right: Self) -> Bool { 22 | guard left._count == right._count else { return false } 23 | return left._storage == right._storage 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+ExpressibleByArrayLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitArray: ExpressibleByArrayLiteral { 13 | /// Creates an instance initialized with the given elements. 14 | @inlinable 15 | public init(arrayLiteral elements: Bool...) { 16 | self.init(elements) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+ExpressibleByStringLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitArray: ExpressibleByStringLiteral { 13 | /// Creates an instance initialized with the given elements. 14 | @inlinable 15 | public init(stringLiteral value: String) { 16 | guard let bits = Self(value) else { 17 | fatalError("Invalid bit array literal") 18 | } 19 | self = bits 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+Fill.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitArray { 13 | /// Set every bit of this array to `value` (`true` by default). 14 | /// 15 | /// - Parameter value: The Boolean value to which to set the array's elements. 16 | public mutating func fill(with value: Bool = true) { 17 | fill(in: Range(uncheckedBounds: (0, count)), with: value) 18 | } 19 | 20 | /// Set every bit of this array within the specified range to `value` 21 | /// (`true` by default). 22 | /// 23 | /// - Parameter range: The range whose elements to overwrite. 24 | /// - Parameter value: The Boolean value to which to set the array's elements. 25 | public mutating func fill(in range: Range, with value: Bool = true) { 26 | _update { handle in 27 | if value { 28 | handle.fill(in: range) 29 | } else { 30 | handle.clear(in: range) 31 | } 32 | } 33 | } 34 | 35 | /// Set every bit of this array within the specified range to `value` 36 | /// (`true` by default). 37 | /// 38 | /// - Parameter range: The range whose elements to overwrite. 39 | /// - Parameter value: The Boolean value to which to set the array's elements. 40 | public mutating func fill( 41 | in range: some RangeExpression, 42 | with value: Bool = true 43 | ) { 44 | fill(in: range.relative(to: self), with: value) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitArray: Hashable { 13 | /// Hashes the essential components of this value by feeding them into the 14 | /// given hasher. 15 | /// 16 | /// - Parameter hasher: The hasher to use when combining the components 17 | /// of this instance. 18 | public func hash(into hasher: inout Hasher) { 19 | hasher.combine(_count) 20 | for word in _storage { 21 | hasher.combine(word) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+Invariants.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension BitArray { 17 | /// True if consistency checking is enabled in the implementation of this 18 | /// type, false otherwise. 19 | /// 20 | /// Documented performance promises are null and void when this property 21 | /// returns true -- for example, operations that are documented to take 22 | /// O(1) time might take O(*n*) time, or worse. 23 | public static var _isConsistencyCheckingEnabled: Bool { 24 | _isCollectionsInternalCheckingEnabled 25 | } 26 | 27 | #if COLLECTIONS_INTERNAL_CHECKS 28 | @inline(never) 29 | @_effects(releasenone) 30 | public func _checkInvariants() { 31 | precondition(_count <= _storage.count * _Word.capacity) 32 | precondition(_count > (_storage.count - 1) * _Word.capacity) 33 | let p = _BitPosition(_count).split 34 | if p.bit > 0 { 35 | precondition(_storage.last!.subtracting(_Word(upTo: p.bit)) == .empty) 36 | } 37 | } 38 | #else 39 | @inline(__always) @inlinable 40 | public func _checkInvariants() {} 41 | #endif // COLLECTIONS_INTERNAL_CHECKS 42 | } 43 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+RandomBits.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import InternalCollectionsUtilities 13 | 14 | extension BitArray { 15 | /// Create and return a new bit array consisting of `count` random bits, 16 | /// using the system random number generator. 17 | /// 18 | /// - Parameter count: The number of random bits to generate. 19 | public static func randomBits(count: Int) -> BitArray { 20 | var rng = SystemRandomNumberGenerator() 21 | return randomBits(count: count, using: &rng) 22 | } 23 | 24 | /// Create and return a new bit array consisting of `count` random bits, 25 | /// using the given random number generator. 26 | /// 27 | /// - Parameter count: The number of random bits to generate. 28 | /// - Parameter rng: The random number generator to use. 29 | public static func randomBits( 30 | count: Int, 31 | using rng: inout some RandomNumberGenerator 32 | ) -> BitArray { 33 | precondition(count >= 0, "Invalid count") 34 | guard count > 0 else { return BitArray() } 35 | let (w, b) = _BitPosition(count).endSplit 36 | var words = (0 ... w).map { _ in _Word(rng.next() as UInt) } 37 | words[w].formIntersection(_Word(upTo: b)) 38 | return BitArray(_storage: words, count: count) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitArray/BitArray+Testing.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2025 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import InternalCollectionsUtilities 13 | 14 | extension BitArray { 15 | @_spi(Testing) 16 | public var _capacity: Int { 17 | _storage.capacity * _Word.capacity 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitCollections.docc/BitCollections.md: -------------------------------------------------------------------------------- 1 | # ``BitCollections`` 2 | 3 | **Swift Collections** is an open-source package of data structure implementations for the Swift programming language. 4 | 5 | ## Overview 6 | 7 | 8 | 9 | #### Additional Resources 10 | 11 | - [`Swift Collections` on GitHub](https://github.com/apple/swift-collections/) 12 | - [`Swift Collections` on the Swift Forums](https://forums.swift.org/c/related-projects/collections/72) 13 | 14 | 15 | ## Topics 16 | 17 | ### Structures 18 | 19 | - ``BitSet`` 20 | - ``BitArray`` 21 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+Codable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitSet: Codable { 13 | /// Encodes this bit set into the given encoder. 14 | /// 15 | /// Bit sets are encoded as an unkeyed container of `UInt64` values, 16 | /// representing pieces of the underlying bitmap. 17 | /// 18 | /// - Parameter encoder: The encoder to write data to. 19 | public func encode(to encoder: Encoder) throws { 20 | var container = encoder.unkeyedContainer() 21 | try _storage._encodeAsUInt64(to: &container) 22 | } 23 | 24 | /// Creates a new bit set by decoding from the given decoder. 25 | /// 26 | /// Bit sets are encoded as an unkeyed container of `UInt64` values, 27 | /// representing pieces of the underlying bitmap. 28 | /// 29 | /// - Parameter decoder: The decoder to read data from. 30 | public init(from decoder: Decoder) throws { 31 | var container = try decoder.unkeyedContainer() 32 | let words = try [_Word]( 33 | _fromUInt64: &container, reservingCount: container.count) 34 | self.init(_words: words) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+CustomDebugStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitSet: CustomDebugStringConvertible { 13 | /// A textual representation of this instance, suitable for debugging. 14 | public var debugDescription: String { 15 | description 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitSet: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self, displayStyle: .set) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+CustomStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension BitSet: CustomStringConvertible { 17 | // A textual representation of this instance. 18 | public var description: String { 19 | _arrayDescription(for: self) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitSet: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. Two 14 | /// bit sets are considered equal if they contain the same elements. 15 | /// 16 | /// - Note: This simply forwards to the ``isEqualSet(to:)-4xfa9`` method. 17 | /// That method has additional overloads that can be used to compare 18 | /// bit sets with additional types. 19 | /// 20 | /// - Complexity: O(*max*), where *max* is value of the largest member of 21 | /// either set. 22 | public static func ==(left: Self, right: Self) -> Bool { 23 | left.isEqualSet(to: right) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitSet: ExpressibleByArrayLiteral { 13 | /// Creates a new bit set from the contents of an array literal. 14 | /// 15 | /// Do not call this initializer directly. It is used by the compiler when 16 | /// you use an array literal. Instead, create a new bit set using an array 17 | /// literal as its value by enclosing a comma-separated list of values in 18 | /// square brackets. You can use an array literal anywhere a bit set is 19 | /// expected by the type context. 20 | /// 21 | /// - Parameter elements: A variadic list of elements of the new set. 22 | /// - Complexity: O(`elements.count`) 23 | @inlinable 24 | public init(arrayLiteral elements: Int...) { 25 | self.init(elements) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitSet: Hashable { 13 | /// Hashes the essential components of this value by feeding them into the 14 | /// given hasher. 15 | /// 16 | /// Complexity: O(*max*) where *max* is the largest value stored in this set. 17 | public func hash(into hasher: inout Hasher) { 18 | hasher.combine(_storage) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+Invariants.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension BitSet { 17 | /// True if consistency checking is enabled in the implementation of this 18 | /// type, false otherwise. 19 | /// 20 | /// Documented performance promises are null and void when this property 21 | /// returns true -- for example, operations that are documented to take 22 | /// O(1) time might take O(*n*) time, or worse. 23 | public static var _isConsistencyCheckingEnabled: Bool { 24 | _isCollectionsInternalCheckingEnabled 25 | } 26 | 27 | #if COLLECTIONS_INTERNAL_CHECKS 28 | @inline(never) 29 | @_effects(releasenone) 30 | public func _checkInvariants() { 31 | //let actualCount = _storage.reduce(into: 0) { $0 += $1.count } 32 | //precondition(_count == actualCount, "Invalid count") 33 | 34 | precondition(_storage.isEmpty || !_storage.last!.isEmpty, 35 | "Extraneous tail slot") 36 | } 37 | #else 38 | @inline(__always) @inlinable 39 | public func _checkInvariants() {} 40 | #endif // COLLECTIONS_INTERNAL_CHECKS 41 | } 42 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+Random.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import InternalCollectionsUtilities 13 | 14 | extension BitSet { 15 | public static func random(upTo limit: Int) -> BitSet { 16 | var rng = SystemRandomNumberGenerator() 17 | return random(upTo: limit, using: &rng) 18 | } 19 | 20 | public static func random( 21 | upTo limit: Int, 22 | using rng: inout some RandomNumberGenerator 23 | ) -> BitSet { 24 | precondition(limit >= 0, "Invalid limit value") 25 | guard limit > 0 else { return BitSet() } 26 | let (w, b) = _UnsafeHandle.Index(limit).endSplit 27 | var words = (0 ... w).map { _ in _Word(rng.next() as UInt) } 28 | words[w].formIntersection(_Word(upTo: b)) 29 | return BitSet(_words: words) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+SetAlgebra conformance.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension BitSet: SetAlgebra {} 13 | -------------------------------------------------------------------------------- /Sources/BitCollections/BitSet/BitSet+Sorted Collection APIs.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension BitSet: _SortedCollection { 17 | /// Returns the current set (already sorted). 18 | /// 19 | /// - Complexity: O(1) 20 | public func sorted() -> BitSet { self } 21 | 22 | /// Returns the minimum element in this set. 23 | /// 24 | /// Bit sets are sorted, so the minimum element is always at the first 25 | /// position in the set. 26 | /// 27 | /// - Returns: The bit set's minimum element. If the sequence has no 28 | /// elements, returns `nil`. 29 | /// 30 | /// - Complexity: O(1) 31 | @warn_unqualified_access 32 | public func min() -> Element? { 33 | first 34 | } 35 | 36 | /// Returns the maximum element in this set. 37 | /// 38 | /// Bit sets are sorted, so the maximum element is always at the last 39 | /// position in the set. 40 | /// 41 | /// - Returns: The bit set's maximum element. If the sequence has no 42 | /// elements, returns `nil`. 43 | /// 44 | /// - Complexity: O(1) 45 | @warn_unqualified_access 46 | public func max() -> Element? { 47 | last 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Sources/BitCollections/Shared/Range+Utilities.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Range where Bound: FixedWidthInteger { 13 | @inlinable 14 | internal func _clampedToUInt() -> Range { 15 | if upperBound <= 0 { 16 | return Range(uncheckedBounds: (0, 0)) 17 | } 18 | if lowerBound >= UInt.max { 19 | return Range(uncheckedBounds: (UInt.max, UInt.max)) 20 | } 21 | let lower = lowerBound < 0 ? 0 : UInt(lowerBound) 22 | let upper = upperBound > UInt.max ? UInt.max : UInt(upperBound) 23 | return Range(uncheckedBounds: (lower, upper)) 24 | } 25 | 26 | @inlinable 27 | internal func _toUInt() -> Range? { 28 | guard 29 | let lower = UInt(exactly: lowerBound), 30 | let upper = UInt(exactly: upperBound) 31 | else { 32 | return nil 33 | } 34 | return Range(uncheckedBounds: (lower: lower, upper: upper)) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Sources/BitCollections/Shared/Slice+Utilities.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Slice { 13 | internal var _bounds: Range { 14 | Range(uncheckedBounds: (startIndex, endIndex)) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Sources/BitCollections/Shared/UInt+Tricks.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension UInt { 17 | /// Returns the position of the `n`th set bit in `self`. 18 | /// 19 | /// - Parameter n: The to retrieve. This value is 20 | /// decremented by the number of items found in this `self` towards the 21 | /// value we're looking for. (If the function returns non-nil, then `n` 22 | /// is set to `0` on return.) 23 | /// - Returns: If this integer contains enough set bits to satisfy the 24 | /// request, then this function returns the position of the bit found. 25 | /// Otherwise it returns nil. 26 | internal func _rank(ofBit n: inout UInt) -> UInt? { 27 | let c = self.nonzeroBitCount 28 | guard n < c else { 29 | n &-= UInt(bitPattern: c) 30 | return nil 31 | } 32 | let m = Int(bitPattern: n) 33 | n = 0 34 | return _bit(ranked: m)! 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Sources/Collections/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #[[ 2 | This source file is part of the Swift Collections Open Source Project 3 | 4 | Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 5 | Licensed under Apache License v2.0 with Runtime Library Exception 6 | 7 | See https://swift.org/LICENSE.txt for license information 8 | #]] 9 | 10 | add_library(Collections 11 | "Collections.swift") 12 | target_link_libraries(Collections PRIVATE 13 | BitCollections 14 | DequeModule 15 | HeapModule 16 | OrderedCollections 17 | HashTreeCollections 18 | ) 19 | set_target_properties(Collections PROPERTIES 20 | INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) 21 | 22 | _install_target(Collections) 23 | set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS Collections) 24 | -------------------------------------------------------------------------------- /Sources/Collections/Collections.docc/Collections.md: -------------------------------------------------------------------------------- 1 | # ``Collections`` 2 | 3 | **Swift Collections** is an open-source package of data structure implementations for the Swift programming language. 4 | 5 | ## Overview 6 | 7 | 8 | 9 | #### Additional Resources 10 | 11 | - [`Swift Collections` on GitHub](https://github.com/apple/swift-collections/) 12 | - [`Swift Collections` on the Swift Forums](https://forums.swift.org/c/related-projects/collections/72) 13 | 14 | 15 | ## Topics 16 | 17 | ### Bit Collections 18 | 19 | - ``BitSet`` 20 | - ``BitArray`` 21 | 22 | ### Deque Module 23 | 24 | - ``Deque`` 25 | 26 | ### Heap Module 27 | 28 | - ``Heap`` 29 | 30 | ### Ordered Collections 31 | 32 | - ``OrderedSet`` 33 | - ``OrderedDictionary`` 34 | 35 | ### Persistent Hashed Collections 36 | 37 | - ``TreeSet`` 38 | - ``TreeDictionary`` 39 | -------------------------------------------------------------------------------- /Sources/Collections/Collections.docc/Extensions/Deque.md: -------------------------------------------------------------------------------- 1 | # ``Collections/Deque`` 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ## Topics 11 | -------------------------------------------------------------------------------- /Sources/Collections/Collections.docc/Extensions/Heap.md: -------------------------------------------------------------------------------- 1 | # ``Collections/Heap`` 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ## Topics 11 | -------------------------------------------------------------------------------- /Sources/Collections/Collections.docc/Extensions/OrderedDictionary.Elements.md: -------------------------------------------------------------------------------- 1 | # ``Collections/OrderedDictionary/Elements-swift.struct`` 2 | 3 | 4 | 5 | 6 | ## Topics 7 | 8 | ### Inspecting an Elements View 9 | 10 | - ``isEmpty`` 11 | - ``count`` 12 | 13 | ### Accessing Elements 14 | 15 | - ``subscript(_:)-4xwc2`` 16 | - ``keys`` 17 | - ``values`` 18 | - ``index(forKey:)`` 19 | 20 | ### Removing Elements 21 | 22 | - ``remove(at:)`` 23 | - ``removeSubrange(_:)-5x7oo`` 24 | - ``removeSubrange(_:)-7wdak`` 25 | - ``removeAll(keepingCapacity:)`` 26 | - ``removeAll(where:)`` 27 | - ``removeFirst()`` 28 | - ``removeFirst(_:)`` 29 | - ``removeLast()`` 30 | - ``removeLast(_:)`` 31 | 32 | ### Reordering Elements 33 | 34 | - ``swapAt(_:_:)`` 35 | - ``reverse()`` 36 | - ``sort()`` 37 | - ``sort(by:)`` 38 | - ``partition(by:)`` 39 | - ``shuffle()`` 40 | - ``shuffle(using:)`` 41 | -------------------------------------------------------------------------------- /Sources/Collections/Collections.docc/Extensions/OrderedDictionary.Values.md: -------------------------------------------------------------------------------- 1 | # ``Collections/OrderedDictionary/Values-swift.struct`` 2 | 3 | 4 | 5 | 6 | ## Topics 7 | 8 | ### Inspecting a Values Collection 9 | 10 | - ``isEmpty`` 11 | - ``count`` 12 | 13 | ### Accessing Elements 14 | 15 | - ``subscript(_:)-25vfz`` 16 | - ``elements`` 17 | - ``withUnsafeBufferPointer(_:)`` 18 | - ``withUnsafeMutableBufferPointer(_:)`` 19 | 20 | ### Reordering Elements 21 | 22 | - ``swapAt(_:_:)-77eiy`` 23 | - ``partition(by:)-9x0i5`` 24 | - ``sort()`` 25 | - ``sort(by:)`` 26 | - ``shuffle()`` 27 | - ``shuffle(using:)`` 28 | -------------------------------------------------------------------------------- /Sources/Collections/Collections.docc/Extensions/OrderedSet.UnorderedView.md: -------------------------------------------------------------------------------- 1 | # ``Collections/OrderedSet/UnorderedView`` 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ## Topics 11 | 12 | ### Binary Set Operations 13 | 14 | - ``intersection(_:)-3q45l`` 15 | - ``intersection(_:)-6ee3o`` 16 | 17 | - ``union(_:)-79uk3`` 18 | - ``union(_:)-23dm1`` 19 | 20 | - ``subtracting(_:)-3ct1b`` 21 | - ``subtracting(_:)-8e6mw`` 22 | 23 | - ``symmetricDifference(_:)-6aed7`` 24 | - ``symmetricDifference(_:)-7r79p`` 25 | 26 | - ``formIntersection(_:)-4ow38`` 27 | - ``formIntersection(_:)-80iht`` 28 | 29 | - ``formUnion(_:)-6ijb`` 30 | - ``formUnion(_:)-8tuol`` 31 | 32 | - ``subtract(_:)-627eq`` 33 | - ``subtract(_:)-4pjhu`` 34 | 35 | - ``formSymmetricDifference(_:)-8pkt5`` 36 | - ``formSymmetricDifference(_:)-75z52`` 37 | 38 | ### Binary Set Predicates 39 | 40 | - ``==(_:_:)`` 41 | - ``isEqualSet(to:)-1szq`` 42 | - ``isEqualSet(to:)-9djqq`` 43 | 44 | - ``isSubset(of:)-2dx31`` 45 | - ``isSubset(of:)-801lo`` 46 | - ``isSubset(of:)-952h5`` 47 | 48 | - ``isSuperset(of:)-9t33p`` 49 | - ``isSuperset(of:)-2vtig`` 50 | - ``isSuperset(of:)-9krpz`` 51 | 52 | - ``isStrictSubset(of:)-9o6mg`` 53 | - ``isStrictSubset(of:)-91par`` 54 | - ``isStrictSubset(of:)-7n66e`` 55 | 56 | - ``isStrictSuperset(of:)-89ig3`` 57 | - ``isStrictSuperset(of:)-1e0xt`` 58 | - ``isStrictSuperset(of:)-5dsfd`` 59 | 60 | - ``isDisjoint(with:)-3wuso`` 61 | - ``isDisjoint(with:)-25vmx`` 62 | - ``isDisjoint(with:)-8nfqs`` 63 | -------------------------------------------------------------------------------- /Sources/Collections/Collections.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | @_exported import BitCollections 14 | @_exported import DequeModule 15 | @_exported import HashTreeCollections 16 | @_exported import HeapModule 17 | @_exported import OrderedCollections 18 | // Note: _RopeModule is very intentionally not reexported, as its contents 19 | // aren't part of this package's stable API surface (yet). 20 | #endif 21 | -------------------------------------------------------------------------------- /Sources/DequeModule/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #[[ 2 | This source file is part of the Swift Collections Open Source Project 3 | 4 | Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 5 | Licensed under Apache License v2.0 with Runtime Library Exception 6 | 7 | See https://swift.org/LICENSE.txt for license information 8 | #]] 9 | 10 | if(COLLECTIONS_SINGLE_MODULE) 11 | set(module_name ${COLLECTIONS_MODULE_NAME}) 12 | else() 13 | set(module_name DequeModule) 14 | add_library(DequeModule 15 | ${COLLECTIONS_DEQUE_SOURCES}) 16 | target_link_libraries(DequeModule PRIVATE 17 | InternalCollectionsUtilities) 18 | set_target_properties(DequeModule PROPERTIES 19 | INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) 20 | 21 | _install_target(DequeModule) 22 | set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS DequeModule) 23 | endif() 24 | 25 | target_sources(${module_name} PRIVATE 26 | "Deque+Codable.swift" 27 | "Deque+Collection.swift" 28 | "Deque+CustomReflectable.swift" 29 | "Deque+Descriptions.swift" 30 | "Deque+Equatable.swift" 31 | "Deque+ExpressibleByArrayLiteral.swift" 32 | "Deque+Extras.swift" 33 | "Deque+Hashable.swift" 34 | "Deque+Testing.swift" 35 | "Deque._Storage.swift" 36 | "Deque._UnsafeHandle.swift" 37 | "Deque.swift" 38 | "_DequeBuffer.swift" 39 | "_DequeBufferHeader.swift" 40 | "_DequeSlot.swift" 41 | "_UnsafeWrappedBuffer.swift" 42 | ) 43 | -------------------------------------------------------------------------------- /Sources/DequeModule/Deque+Codable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Deque: Encodable where Element: Encodable { 13 | /// Encodes the elements of this deque into the given encoder in an unkeyed 14 | /// container. 15 | /// 16 | /// This function throws an error if any values are invalid for the given 17 | /// encoder's format. 18 | /// 19 | /// - Parameter encoder: The encoder to write data to. 20 | @inlinable 21 | public func encode(to encoder: Encoder) throws { 22 | var container = encoder.unkeyedContainer() 23 | for element in self { 24 | try container.encode(element) 25 | } 26 | } 27 | } 28 | 29 | extension Deque: Decodable where Element: Decodable { 30 | /// Creates a new deque by decoding from the given decoder. 31 | /// 32 | /// This initializer throws an error if reading from the decoder fails, or 33 | /// if the data read is corrupted or otherwise invalid. 34 | /// 35 | /// - Parameter decoder: The decoder to read data from. 36 | @inlinable 37 | public init(from decoder: Decoder) throws { 38 | self.init() 39 | 40 | var container = try decoder.unkeyedContainer() 41 | if let count = container.count { 42 | self.reserveCapacity(count) 43 | } 44 | while !container.isAtEnd { 45 | let element = try container.decode(Element.self) 46 | self.append(element) 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Sources/DequeModule/Deque+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Deque: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self, displayStyle: .collection) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/DequeModule/Deque+Descriptions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension Deque: CustomStringConvertible { 17 | /// A textual representation of this instance. 18 | public var description: String { 19 | _arrayDescription(for: self) 20 | } 21 | } 22 | 23 | extension Deque: CustomDebugStringConvertible { 24 | /// A textual representation of this instance, suitable for debugging. 25 | public var debugDescription: String { 26 | description 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/DequeModule/Deque+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Deque: Equatable where Element: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. Two 14 | /// deques are considered equal if they contain the same elements in the same 15 | /// order. 16 | /// 17 | /// - Complexity: O(`min(left.count, right.count)`) 18 | @inlinable 19 | public static func ==(left: Self, right: Self) -> Bool { 20 | let lhsCount = left.count 21 | if lhsCount != right.count { 22 | return false 23 | } 24 | 25 | // Test referential equality. 26 | if lhsCount == 0 || left._storage.isIdentical(to: right._storage) { 27 | return true 28 | } 29 | 30 | return left.elementsEqual(right) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Sources/DequeModule/Deque+ExpressibleByArrayLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Deque: ExpressibleByArrayLiteral { 13 | /// Creates a new deque from the contents of an array literal. 14 | /// 15 | /// Do not call this initializer directly. It is used by the compiler when 16 | /// you use an array literal. Instead, create a new deque using an array 17 | /// literal as its value by enclosing a comma-separated list of values in 18 | /// square brackets. You can use an array literal anywhere a deque is expected 19 | /// by the type context. 20 | /// 21 | /// - Parameter elements: A variadic list of elements of the new deque. 22 | @inlinable 23 | @inline(__always) 24 | public init(arrayLiteral elements: Element...) { 25 | self.init(elements) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Sources/DequeModule/Deque+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Deque: Hashable where Element: Hashable { 13 | /// Hashes the essential components of this value by feeding them into the 14 | /// given hasher. 15 | /// 16 | /// Complexity: O(`count`) 17 | @inlinable 18 | public func hash(into hasher: inout Hasher) { 19 | hasher.combine(count) // discriminator 20 | for element in self { 21 | hasher.combine(element) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Sources/DequeModule/DequeModule.docc/DequeModule.md: -------------------------------------------------------------------------------- 1 | # ``DequeModule`` 2 | 3 | Summary 4 | 5 | ## Overview 6 | 7 | Text 8 | 9 | -------------------------------------------------------------------------------- /Sources/DequeModule/DequeModule.docc/Extensions/Deque.md: -------------------------------------------------------------------------------- 1 | # ``DequeModule/Deque`` 2 | 3 | 4 | 5 | 6 | 7 | ## Topics 8 | -------------------------------------------------------------------------------- /Sources/DequeModule/_DequeBufferHeader.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @usableFromInline 13 | internal struct _DequeBufferHeader { 14 | @usableFromInline 15 | var capacity: Int 16 | 17 | @usableFromInline 18 | var count: Int 19 | 20 | @usableFromInline 21 | var startSlot: _DequeSlot 22 | 23 | @usableFromInline 24 | init(capacity: Int, count: Int, startSlot: _DequeSlot) { 25 | self.capacity = capacity 26 | self.count = count 27 | self.startSlot = startSlot 28 | _checkInvariants() 29 | } 30 | 31 | #if COLLECTIONS_INTERNAL_CHECKS 32 | @usableFromInline @inline(never) @_effects(releasenone) 33 | internal func _checkInvariants() { 34 | precondition(capacity >= 0) 35 | precondition(count >= 0 && count <= capacity) 36 | precondition(startSlot.position >= 0 && startSlot.position <= capacity) 37 | } 38 | #else 39 | @inlinable @inline(__always) 40 | internal func _checkInvariants() {} 41 | #endif // COLLECTIONS_INTERNAL_CHECKS 42 | } 43 | 44 | extension _DequeBufferHeader: CustomStringConvertible { 45 | @usableFromInline 46 | internal var description: String { 47 | "(capacity: \(capacity), count: \(count), startSlot: \(startSlot))" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/HashNode/_HashNode+Structural compactMapValues.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension _HashNode { 13 | @inlinable 14 | internal func compactMapValues( 15 | _ level: _HashLevel, 16 | _ transform: (Value) throws -> T? 17 | ) rethrows -> _HashNode.Builder { 18 | return try self.read { 19 | var result: _HashNode.Builder = .empty(level) 20 | 21 | if isCollisionNode { 22 | let items = $0.reverseItems 23 | for i in items.indices { 24 | if let v = try transform(items[i].value) { 25 | result.addNewCollision(level, (items[i].key, v), $0.collisionHash) 26 | } 27 | } 28 | return result 29 | } 30 | 31 | for (bucket, slot) in $0.itemMap { 32 | let p = $0.itemPtr(at: slot) 33 | if let v = try transform(p.pointee.value) { 34 | result.addNewItem(level, (p.pointee.key, v), at: bucket) 35 | } 36 | } 37 | 38 | for (bucket, slot) in $0.childMap { 39 | let branch = try $0[child: slot] 40 | .compactMapValues(level.descend(), transform) 41 | result.addNewChildBranch(level, branch, at: bucket) 42 | } 43 | return result 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/HashTreeCollections.docc/HashTreeCollections.md: -------------------------------------------------------------------------------- 1 | # ``HashTreeCollections`` 2 | 3 | **Swift Collections** is an open-source package of data structure implementations for the Swift programming language. 4 | 5 | ## Overview 6 | 7 | 8 | 9 | #### Additional Resources 10 | 11 | - [`Swift Collections` on GitHub](https://github.com/apple/swift-collections/) 12 | - [`Swift Collections` on the Swift Forums](https://forums.swift.org/c/related-projects/collections/72) 13 | 14 | 15 | ## Topics 16 | 17 | ### Persistent Collections 18 | 19 | - ``TreeSet`` 20 | - ``TreeDictionary`` 21 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeDictionary/TreeDictionary+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeDictionary: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self, displayStyle: .dictionary) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Debugging.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension TreeDictionary { 17 | /// True if consistency checking is enabled in the implementation of this 18 | /// type, false otherwise. 19 | /// 20 | /// Documented performance promises are null and void when this property 21 | /// returns true -- for example, operations that are documented to take 22 | /// O(1) time might take O(*n*) time, or worse. 23 | public static var _isConsistencyCheckingEnabled: Bool { 24 | _isCollectionsInternalCheckingEnabled 25 | } 26 | 27 | @inlinable 28 | public func _invariantCheck() { 29 | _root._fullInvariantCheck() 30 | } 31 | 32 | public func _dump(iterationOrder: Bool = false) { 33 | _root.dump(iterationOrder: iterationOrder) 34 | } 35 | 36 | public static var _maxDepth: Int { 37 | _HashLevel.limit 38 | } 39 | 40 | public var _statistics: _HashTreeStatistics { 41 | var stats = _HashTreeStatistics() 42 | _root.gatherStatistics(.top, &stats) 43 | return stats 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Descriptions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension TreeDictionary: CustomStringConvertible { 17 | // A textual representation of this instance. 18 | public var description: String { 19 | _dictionaryDescription(for: self) 20 | } 21 | } 22 | 23 | extension TreeDictionary: CustomDebugStringConvertible { 24 | /// A textual representation of this instance, suitable for debugging. 25 | public var debugDescription: String { 26 | description 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2019 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeDictionary: Equatable where Value: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. 14 | /// 15 | /// Two persistent dictionaries are considered equal if they contain the same 16 | /// key-value pairs, but not necessarily in the same order. 17 | /// 18 | /// - Complexity: O(`min(left.count, right.count)`) 19 | @inlinable 20 | public static func == (left: Self, right: Self) -> Bool { 21 | left._root.isEqualSet(to: right._root, by: { $0 == $1 }) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeDictionary/TreeDictionary+ExpressibleByDictionaryLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2019 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeDictionary: ExpressibleByDictionaryLiteral { 13 | /// Creates a new dictionary from the contents of a dictionary literal. 14 | /// 15 | /// The literal must not contain duplicate elements. 16 | /// 17 | /// Do not call this initializer directly. It is used by the compiler when 18 | /// you use a dictionary literal. Instead, create a new dictionary using a 19 | /// dictionary literal as its value by enclosing a comma-separated list of 20 | /// values in square brackets. You can use a dictionary literal anywhere a 21 | /// persistent dictionary is expected by the type context. 22 | /// 23 | /// Like the standard `Dictionary`, persistent dictionaries do not preserve 24 | /// the order of elements inside the array literal. 25 | /// 26 | /// - Parameter elements: A variadic list of elements of the new set. 27 | /// 28 | /// - Complexity: O(`elements.count`) if `Element` properly implements 29 | /// hashing. 30 | @inlinable 31 | @inline(__always) 32 | public init(dictionaryLiteral elements: (Key, Value)...) { 33 | self.init(uniqueKeysWithValues: elements) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2019 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeDictionary: Hashable where Value: Hashable { 13 | /// Hashes the essential components of this value by feeding them into the 14 | /// given hasher. 15 | /// 16 | /// Complexity: O(`count`) 17 | @inlinable 18 | public func hash(into hasher: inout Hasher) { 19 | var commutativeHash = 0 20 | for (key, value) in self { 21 | var elementHasher = hasher 22 | elementHasher.combine(key) 23 | elementHasher.combine(value) 24 | commutativeHash ^= elementHasher.finalize() 25 | } 26 | hasher.combine(commutativeHash) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Sendable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeDictionary: @unchecked Sendable 13 | where Key: Sendable, Value: Sendable {} 14 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeSet/TreeSet+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeSet: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self, displayStyle: .set) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeSet/TreeSet+Debugging.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension TreeSet { 17 | /// True if consistency checking is enabled in the implementation of this 18 | /// type, false otherwise. 19 | /// 20 | /// Documented performance promises are null and void when this property 21 | /// returns true -- for example, operations that are documented to take 22 | /// O(1) time might take O(*n*) time, or worse. 23 | public static var _isConsistencyCheckingEnabled: Bool { 24 | _isCollectionsInternalCheckingEnabled 25 | } 26 | 27 | @inlinable 28 | public func _invariantCheck() { 29 | _root._fullInvariantCheck() 30 | } 31 | 32 | public func _dump(iterationOrder: Bool = false) { 33 | _root.dump(iterationOrder: iterationOrder) 34 | } 35 | 36 | public static var _maxDepth: Int { 37 | _HashLevel.limit 38 | } 39 | 40 | public var _statistics: _HashTreeStatistics { 41 | var stats = _HashTreeStatistics() 42 | _root.gatherStatistics(.top, &stats) 43 | return stats 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeSet/TreeSet+Descriptions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension TreeSet: CustomStringConvertible { 17 | /// A textual representation of this instance. 18 | public var description: String { 19 | _arrayDescription(for: self) 20 | } 21 | } 22 | 23 | extension TreeSet: CustomDebugStringConvertible { 24 | /// A textual representation of this instance, suitable for debugging. 25 | public var debugDescription: String { 26 | description 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeSet/TreeSet+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension TreeSet: Equatable { 17 | /// Returns a Boolean value indicating whether two values are equal. 18 | /// 19 | /// Two persistent sets are considered equal if they contain the same 20 | /// elements, but not necessarily in the same order. 21 | /// 22 | /// - Note: This simply forwards to the ``isEqualSet(to:)-4bc1i`` method. 23 | /// That method has additional overloads that can be used to compare 24 | /// persistent sets with additional types. 25 | /// 26 | /// - Complexity: O(`min(left.count, right.count)`) 27 | @inlinable @inline(__always) 28 | public static func == (left: Self, right: Self) -> Bool { 29 | left.isEqualSet(to: right) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeSet/TreeSet+ExpressibleByArrayLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeSet: ExpressibleByArrayLiteral { 13 | /// Creates a new set from the contents of an array literal. 14 | /// 15 | /// Duplicate elements in the literal are allowed, but the resulting 16 | /// persistent set will only contain the first occurrence of each. 17 | /// 18 | /// Do not call this initializer directly. It is used by the compiler when 19 | /// you use an array literal. Instead, create a new persistent set using an 20 | /// array literal as its value by enclosing a comma-separated list of values 21 | /// in square brackets. You can use an array literal anywhere a persistent set 22 | /// is expected by the type context. 23 | /// 24 | /// Like the standard `Set`, persistent sets do not preserve the order of 25 | /// elements inside the array literal. 26 | /// 27 | /// - Parameter elements: A variadic list of elements of the new set. 28 | /// 29 | /// - Complexity: O(`elements.count`) if `Element` properly implements 30 | /// hashing. 31 | public init(arrayLiteral elements: Element...) { 32 | self.init(elements) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeSet/TreeSet+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeSet: Hashable { 13 | /// Hashes the essential components of this value by feeding them into the 14 | /// given hasher. 15 | /// 16 | /// Complexity: O(`count`) 17 | @inlinable 18 | public func hash(into hasher: inout Hasher) { 19 | let copy = hasher 20 | let seed = copy.finalize() 21 | 22 | var hash = 0 23 | for member in self { 24 | hash ^= member._rawHashValue(seed: seed) 25 | } 26 | hasher.combine(hash) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/HashTreeCollections/TreeSet/TreeSet+Sendable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension TreeSet: @unchecked Sendable where Element: Sendable {} 13 | -------------------------------------------------------------------------------- /Sources/HeapModule/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #[[ 2 | This source file is part of the Swift Collections Open Source Project 3 | 4 | Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 5 | Licensed under Apache License v2.0 with Runtime Library Exception 6 | 7 | See https://swift.org/LICENSE.txt for license information 8 | #]] 9 | 10 | if(COLLECTIONS_SINGLE_MODULE) 11 | set(module_name ${COLLECTIONS_MODULE_NAME}) 12 | else() 13 | set(module_name HeapModule) 14 | add_library(HeapModule 15 | ${COLLECTIONS_HEAP_SOURCES}) 16 | target_link_libraries(HeapModule PRIVATE 17 | InternalCollectionsUtilities) 18 | set_target_properties(HeapModule PROPERTIES 19 | INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) 20 | 21 | _install_target(HeapModule) 22 | set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS HeapModule) 23 | endif() 24 | 25 | target_sources(${module_name} PRIVATE 26 | "_HeapNode.swift" 27 | "Heap.swift" 28 | "Heap+Descriptions.swift" 29 | "Heap+ExpressibleByArrayLiteral.swift" 30 | "Heap+Invariants.swift" 31 | "Heap+UnsafeHandle.swift") 32 | -------------------------------------------------------------------------------- /Sources/HeapModule/Heap+Descriptions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Heap: CustomStringConvertible { 13 | /// A textual representation of this instance. 14 | public var description: String { 15 | "<\(count) item\(count == 1 ? "" : "s") @\(_idString)>" 16 | } 17 | 18 | internal var _idString: String { 19 | // "<32 items @0x2787abcf>" 20 | _storage.withUnsafeBytes { 21 | guard let p = $0.baseAddress else { 22 | return "nil" 23 | } 24 | return String(UInt(bitPattern: p), radix: 16) 25 | } 26 | } 27 | } 28 | 29 | extension Heap: CustomDebugStringConvertible { 30 | /// A textual representation of this instance, suitable for debugging. 31 | public var debugDescription: String { 32 | description 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Sources/HeapModule/Heap+ExpressibleByArrayLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Heap: ExpressibleByArrayLiteral { 13 | /// Creates a new heap from the contents of an array literal. 14 | /// 15 | /// **Do not call this initializer directly.** It is used by the compiler when 16 | /// you use an array literal. Instead, create a new heap using an array 17 | /// literal as its value by enclosing a comma-separated list of values in 18 | /// square brackets. You can use an array literal anywhere a heap is expected 19 | /// by the type context. 20 | /// 21 | /// - Parameter elements: A variadic list of elements of the new heap. 22 | public init(arrayLiteral elements: Element...) { 23 | self.init(elements) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/HeapModule/HeapModule.docc/Extensions/Heap.md: -------------------------------------------------------------------------------- 1 | # ``HeapModule/Heap`` 2 | 3 | 4 | 5 | 6 | 7 | ## Topics 8 | -------------------------------------------------------------------------------- /Sources/HeapModule/HeapModule.docc/HeapModule.md: -------------------------------------------------------------------------------- 1 | # ``HeapModule`` 2 | 3 | Summary 4 | 5 | ## Overview 6 | 7 | Text 8 | 9 | ## Topics 10 | 11 | ### Structures 12 | 13 | - ``Heap`` 14 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #[[ 2 | This source file is part of the Swift Collections Open Source Project 3 | 4 | Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 5 | Licensed under Apache License v2.0 with Runtime Library Exception 6 | 7 | See https://swift.org/LICENSE.txt for license information 8 | #]] 9 | 10 | if(COLLECTIONS_SINGLE_MODULE) 11 | set(module_name ${COLLECTIONS_MODULE_NAME}) 12 | else() 13 | set(module_name InternalCollectionsUtilities) 14 | add_library(InternalCollectionsUtilities 15 | ${COLLECTIONS_UTILITIES_SOURCES}) 16 | set_target_properties(InternalCollectionsUtilities PROPERTIES 17 | INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) 18 | 19 | _install_target(InternalCollectionsUtilities) 20 | set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS InternalCollectionsUtilities) 21 | endif() 22 | 23 | target_sources(${module_name} PRIVATE 24 | "autogenerated/Debugging.swift" 25 | "autogenerated/Descriptions.swift" 26 | "autogenerated/RandomAccessCollection+Offsets.swift" 27 | "autogenerated/UnsafeBufferPointer+Extras.swift" 28 | "autogenerated/UnsafeMutableBufferPointer+Extras.swift" 29 | "IntegerTricks/autogenerated/FixedWidthInteger+roundUpToPowerOfTwo.swift" 30 | "IntegerTricks/autogenerated/Integer rank.swift" 31 | "IntegerTricks/autogenerated/UInt+first and last set bit.swift" 32 | "IntegerTricks/autogenerated/UInt+reversed.swift" 33 | "UnsafeBitSet/autogenerated/_UnsafeBitSet+Index.swift" 34 | "UnsafeBitSet/autogenerated/_UnsafeBitSet+_Word.swift" 35 | "UnsafeBitSet/autogenerated/_UnsafeBitSet.swift" 36 | "_SortedCollection.swift" 37 | "_UniqueCollection.swift" 38 | ) 39 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/Debugging.swift.gyb: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | %{ 13 | from gyb_utils import * 14 | }% 15 | ${autogenerated_warning()} 16 | 17 | % for modifier in visibility_levels: 18 | ${visibility_boilerplate(modifier)} 19 | /// True if consistency checking is enabled in the implementation of the 20 | /// Swift Collections package, false otherwise. 21 | /// 22 | /// Documented performance promises are null and void when this property 23 | /// returns true -- for example, operations that are documented to take 24 | /// O(1) time might take O(*n*) time, or worse. 25 | @inlinable @inline(__always) 26 | ${modifier} var _isCollectionsInternalCheckingEnabled: Bool { 27 | #if COLLECTIONS_INTERNAL_CHECKS 28 | return true 29 | #else 30 | return false 31 | #endif 32 | } 33 | % end 34 | ${visibility_boilerplate("end")} 35 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/IntegerTricks/FixedWidthInteger+roundUpToPowerOfTwo.swift.gyb: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | %{ 13 | from gyb_utils import * 14 | }% 15 | ${autogenerated_warning()} 16 | 17 | % for modifier in visibility_levels: 18 | ${visibility_boilerplate(modifier)} 19 | extension FixedWidthInteger { 20 | /// Round up `self` to the nearest power of two, assuming it's representable. 21 | /// Returns 0 if `self` isn't positive. 22 | @inlinable 23 | ${modifier} func _roundUpToPowerOfTwo() -> Self { 24 | guard self > 0 else { return 0 } 25 | let l = Self.bitWidth - (self &- 1).leadingZeroBitCount 26 | return 1 << l 27 | } 28 | } 29 | % end 30 | ${visibility_boilerplate("end")} 31 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/IntegerTricks/UInt+first and last set bit.swift.gyb: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | %{ 13 | from gyb_utils import * 14 | }% 15 | ${autogenerated_warning()} 16 | 17 | % for modifier in visibility_levels: 18 | ${visibility_boilerplate(modifier)} 19 | extension UInt { 20 | @inlinable @inline(__always) 21 | ${modifier} var _firstSetBit: UInt? { 22 | guard self != 0 else { return nil } 23 | let v = UInt.bitWidth &- 1 &- self.leadingZeroBitCount 24 | return UInt(truncatingIfNeeded: v) 25 | } 26 | 27 | @inlinable @inline(__always) 28 | ${modifier} var _lastSetBit: UInt? { 29 | guard self != 0 else { return nil } 30 | return UInt(truncatingIfNeeded: self.trailingZeroBitCount) 31 | } 32 | } 33 | % end 34 | ${visibility_boilerplate("end")} 35 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/IntegerTricks/UInt+reversed.swift.gyb: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | %{ 13 | from gyb_utils import * 14 | }% 15 | ${autogenerated_warning()} 16 | 17 | % for modifier in visibility_levels: 18 | ${visibility_boilerplate(modifier)} 19 | extension UInt { 20 | @inlinable 21 | ${modifier} var _reversed: UInt { 22 | // https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel 23 | var shift: UInt = UInt(UInt.bitWidth) 24 | var mask: UInt = ~0; 25 | var result = self 26 | while true { 27 | shift &>>= 1 28 | guard shift > 0 else { break } 29 | mask ^= mask &<< shift 30 | result = ((result &>> shift) & mask) | ((result &<< shift) & ~mask) 31 | } 32 | return result 33 | } 34 | } 35 | % end 36 | ${visibility_boilerplate("end")} 37 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/RandomAccessCollection+Offsets.swift.gyb: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | %{ 13 | from gyb_utils import * 14 | }% 15 | ${autogenerated_warning()} 16 | 17 | % for modifier in visibility_levels: 18 | ${visibility_boilerplate(modifier)} 19 | extension RandomAccessCollection { 20 | @_alwaysEmitIntoClient @inline(__always) 21 | ${modifier} func _index(at offset: Int) -> Index { 22 | index(startIndex, offsetBy: offset) 23 | } 24 | 25 | @_alwaysEmitIntoClient @inline(__always) 26 | ${modifier} func _offset(of index: Index) -> Int { 27 | distance(from: startIndex, to: index) 28 | } 29 | 30 | @_alwaysEmitIntoClient @inline(__always) 31 | ${modifier} subscript(_offset offset: Int) -> Element { 32 | self[_index(at: offset)] 33 | } 34 | } 35 | % end 36 | ${visibility_boilerplate("end")} 37 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/UnsafeBufferPointer+Extras.swift.gyb: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | %{ 13 | from gyb_utils import * 14 | }% 15 | ${autogenerated_warning()} 16 | 17 | % for modifier in visibility_levels: 18 | ${visibility_boilerplate(modifier)} 19 | extension UnsafeBufferPointer { 20 | @inlinable 21 | @inline(__always) 22 | ${modifier} func _ptr(at index: Int) -> UnsafePointer { 23 | assert(index >= 0 && index < count) 24 | return baseAddress.unsafelyUnwrapped + index 25 | } 26 | } 27 | % end 28 | ${visibility_boilerplate("end")} 29 | -------------------------------------------------------------------------------- /Sources/InternalCollectionsUtilities/_SortedCollection.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | /// A Collection type that is guaranteed to contain elements in monotonically 13 | /// increasing order. (Duplicates are still allowed unless the collection 14 | /// also conforms to `_UniqueCollection`.) 15 | /// 16 | /// Types conforming to this protocol must also conform to `Collection`, 17 | /// with an `Element` type that conforms to `Comparable`. 18 | /// (However, this protocol does not specify these as explicit requirements, 19 | /// to allow simple conformance tests such as `someValue is _SortedCollection` 20 | /// to be possible.) 21 | /// 22 | /// For any two valid indices `i` and `j` for a conforming collection `c` 23 | /// (both below the end index), it must hold true that if `i < j`, then 24 | /// `c[i] <= c[j]`. 25 | public protocol _SortedCollection {} 26 | 27 | extension Slice: _SortedCollection where Base: _SortedCollection {} 28 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/HashTable/_HashTable+Bucket.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension _HashTable { 13 | /// Identifies a particular bucket within a hash table by its offset. 14 | /// Having a dedicated wrapper type for this prevents passing a bucket number 15 | /// to a function that expects a word index, or vice versa. 16 | @usableFromInline 17 | @frozen 18 | internal struct Bucket { 19 | /// The distance of this bucket from the first bucket in the hash table. 20 | @usableFromInline 21 | internal var offset: Int 22 | 23 | @inlinable 24 | @inline(__always) 25 | internal init(offset: Int) { 26 | assert(offset >= 0) 27 | self.offset = offset 28 | } 29 | } 30 | } 31 | 32 | extension _HashTable.Bucket: Equatable { 33 | @_transparent 34 | public static func == (left: Self, right: Self) -> Bool { 35 | left.offset == right.offset 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedCollections.docc/Extensions/OrderedDictionary.Elements.md: -------------------------------------------------------------------------------- 1 | # ``OrderedCollections/OrderedDictionary/Elements-swift.struct`` 2 | 3 | ## Topics 4 | 5 | ### Inspecting an Elements View 6 | 7 | - ``isEmpty`` 8 | - ``count`` 9 | 10 | ### Accessing Elements 11 | 12 | - ``subscript(_:)-4xwc2`` 13 | - ``keys`` 14 | - ``values`` 15 | - ``index(forKey:)`` 16 | 17 | ### Removing Elements 18 | 19 | - ``remove(at:)`` 20 | - ``removeSubrange(_:)-5x7oo`` 21 | - ``removeSubrange(_:)-7wdak`` 22 | - ``removeAll(keepingCapacity:)`` 23 | - ``removeAll(where:)`` 24 | - ``removeFirst()`` 25 | - ``removeFirst(_:)`` 26 | - ``removeLast()`` 27 | - ``removeLast(_:)`` 28 | 29 | ### Reordering Elements 30 | 31 | - ``swapAt(_:_:)`` 32 | - ``reverse()`` 33 | - ``sort()`` 34 | - ``sort(by:)`` 35 | - ``partition(by:)`` 36 | - ``shuffle()`` 37 | - ``shuffle(using:)`` 38 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedCollections.docc/Extensions/OrderedDictionary.Values.md: -------------------------------------------------------------------------------- 1 | # ``OrderedCollections/OrderedDictionary/Values-swift.struct`` 2 | 3 | ## Topics 4 | 5 | ### Inspecting a Values Collection 6 | 7 | - ``isEmpty`` 8 | - ``count`` 9 | 10 | ### Accessing Elements 11 | 12 | - ``subscript(_:)-25vfz`` 13 | - ``elements`` 14 | - ``withUnsafeBufferPointer(_:)`` 15 | - ``withUnsafeMutableBufferPointer(_:)`` 16 | 17 | ### Reordering Elements 18 | 19 | - ``swapAt(_:_:)-77eiy`` 20 | - ``partition(by:)-9x0i5`` 21 | - ``sort()`` 22 | - ``sort(by:)`` 23 | - ``shuffle()`` 24 | - ``shuffle(using:)`` 25 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedCollections.docc/Extensions/OrderedSet.UnorderedView.md: -------------------------------------------------------------------------------- 1 | # ``OrderedCollections/OrderedSet/UnorderedView`` 2 | 3 | 4 | 5 | 6 | 7 | ## Topics 8 | 9 | ### Binary Set Operations 10 | 11 | - ``intersection(_:)-3q45l`` 12 | - ``intersection(_:)-6ee3o`` 13 | 14 | - ``union(_:)-79uk3`` 15 | - ``union(_:)-23dm1`` 16 | 17 | - ``subtracting(_:)-3ct1b`` 18 | - ``subtracting(_:)-8e6mw`` 19 | 20 | - ``symmetricDifference(_:)-6aed7`` 21 | - ``symmetricDifference(_:)-7r79p`` 22 | 23 | - ``formIntersection(_:)-4ow38`` 24 | - ``formIntersection(_:)-80iht`` 25 | 26 | - ``formUnion(_:)-6ijb`` 27 | - ``formUnion(_:)-8tuol`` 28 | 29 | - ``subtract(_:)-627eq`` 30 | - ``subtract(_:)-4pjhu`` 31 | 32 | - ``formSymmetricDifference(_:)-8pkt5`` 33 | - ``formSymmetricDifference(_:)-75z52`` 34 | 35 | ### Binary Set Predicates 36 | 37 | - ``==(_:_:)`` 38 | - ``isEqualSet(to:)-1szq`` 39 | - ``isEqualSet(to:)-9djqq`` 40 | 41 | - ``isSubset(of:)-2dx31`` 42 | - ``isSubset(of:)-801lo`` 43 | - ``isSubset(of:)-952h5`` 44 | 45 | - ``isSuperset(of:)-9t33p`` 46 | - ``isSuperset(of:)-2vtig`` 47 | - ``isSuperset(of:)-9krpz`` 48 | 49 | - ``isStrictSubset(of:)-9o6mg`` 50 | - ``isStrictSubset(of:)-91par`` 51 | - ``isStrictSubset(of:)-7n66e`` 52 | 53 | - ``isStrictSuperset(of:)-89ig3`` 54 | - ``isStrictSuperset(of:)-1e0xt`` 55 | - ``isStrictSuperset(of:)-5dsfd`` 56 | 57 | - ``isDisjoint(with:)-3wuso`` 58 | - ``isDisjoint(with:)-25vmx`` 59 | - ``isDisjoint(with:)-8nfqs`` 60 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedCollections.docc/OrderedCollections.md: -------------------------------------------------------------------------------- 1 | # ``OrderedCollections`` 2 | 3 | **Swift Collections** is an open-source package of data structure implementations for the Swift programming language. 4 | 5 | ## Overview 6 | 7 | 8 | 9 | #### Additional Resources 10 | 11 | - [`Swift Collections` on GitHub](https://github.com/apple/swift-collections/) 12 | - [`Swift Collections` on the Swift Forums](https://forums.swift.org/c/related-projects/collections/72) 13 | 14 | 15 | ## Topics 16 | 17 | ### Structures 18 | 19 | - ``OrderedSet`` 20 | - ``OrderedDictionary`` 21 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedDictionary: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self.elements, displayStyle: .dictionary) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Descriptions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension OrderedDictionary: CustomStringConvertible { 17 | /// A textual representation of this instance. 18 | public var description: String { 19 | _dictionaryDescription(for: self.elements) 20 | } 21 | } 22 | 23 | extension OrderedDictionary: CustomDebugStringConvertible { 24 | /// A textual representation of this instance, suitable for debugging. 25 | public var debugDescription: String { 26 | description 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedDictionary: Equatable where Value: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. 14 | /// 15 | /// Two ordered dictionaries are considered equal if they contain the same 16 | /// key-value pairs, in the same order. 17 | /// 18 | /// - Complexity: O(`min(left.count, right.count)`) 19 | @inlinable 20 | public static func ==(left: Self, right: Self) -> Bool { 21 | left._keys == right._keys && left._values == right._values 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+ExpressibleByDictionaryLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedDictionary: ExpressibleByDictionaryLiteral { 13 | /// Creates a new ordered dictionary from the contents of a dictionary 14 | /// literal. 15 | /// 16 | /// Do not call this initializer directly. It is used by the compiler when you 17 | /// use a dictionary literal. Instead, create a new ordered dictionary using a 18 | /// dictionary literal as its value by enclosing a comma-separated list of 19 | /// key-value pairs in square brackets. You can use a dictionary literal 20 | /// anywhere an ordered dictionary is expected by the type context. 21 | /// 22 | /// - Parameter elements: A variadic list of key-value pairs for the new 23 | /// ordered dictionary. 24 | /// 25 | /// - Complexity: O(`elements.count`) if `Key` implements 26 | /// high-quality hashing. 27 | @inlinable 28 | public init(dictionaryLiteral elements: (Key, Value)...) { 29 | self.init(uniqueKeysWithValues: elements) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedDictionary: Hashable where Value: Hashable { 13 | /// Hashes the essential components of this value by feeding them into the 14 | /// given hasher. 15 | /// 16 | /// Complexity: O(`count`) 17 | @inlinable 18 | public func hash(into hasher: inout Hasher) { 19 | hasher.combine(count) // Discriminator 20 | for (key, value) in self { 21 | hasher.combine(key) 22 | hasher.combine(value) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Invariants.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension OrderedDictionary { 17 | /// True if consistency checking is enabled in the implementation of this 18 | /// type, false otherwise. 19 | /// 20 | /// Documented performance promises are null and void when this property 21 | /// returns true -- for example, operations that are documented to take 22 | /// O(1) time might take O(*n*) time, or worse. 23 | public static var _isConsistencyCheckingEnabled: Bool { 24 | _isCollectionsInternalCheckingEnabled 25 | } 26 | 27 | #if COLLECTIONS_INTERNAL_CHECKS 28 | @inline(never) @_effects(releasenone) 29 | public func _checkInvariants() { 30 | precondition(_keys.count == _values.count) 31 | self._keys._checkInvariants() 32 | } 33 | #else 34 | @inline(__always) @inlinable 35 | public func _checkInvariants() {} 36 | #endif // COLLECTIONS_INTERNAL_CHECKS 37 | } 38 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Sendable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedDictionary: @unchecked Sendable 13 | where Key: Sendable, Value: Sendable {} 14 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedSet/OrderedSet+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedSet: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: _elements, displayStyle: .set) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedSet/OrderedSet+Descriptions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | #endif 15 | 16 | extension OrderedSet: CustomStringConvertible { 17 | /// A textual representation of this instance. 18 | public var description: String { 19 | _arrayDescription(for: self) 20 | } 21 | } 22 | 23 | extension OrderedSet: CustomDebugStringConvertible { 24 | /// A textual representation of this instance, suitable for debugging. 25 | public var debugDescription: String { 26 | description 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedSet/OrderedSet+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedSet: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. 14 | /// 15 | /// Two ordered sets are considered equal if they contain the same 16 | /// elements in the same order. 17 | /// 18 | /// - Note: This operator implements different behavior than the 19 | /// `isEqualSet(to:)` method -- the latter implements an unordered 20 | /// comparison, to match the behavior of members like `isSubset(of:)`, 21 | /// `isStrictSuperset(of:)` etc. 22 | /// 23 | /// - Complexity: O(`min(left.count, right.count)`) 24 | @inlinable 25 | public static func ==(left: Self, right: Self) -> Bool { 26 | left._elements == right._elements 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedSet/OrderedSet+ExpressibleByArrayLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedSet: ExpressibleByArrayLiteral { 13 | /// Creates a new ordered set from the contents of an array literal. 14 | /// 15 | /// Duplicate elements in the literal are allowed, but the resulting ordered 16 | /// set will only contain the first occurrence of each. 17 | /// 18 | /// Do not call this initializer directly. It is used by the compiler when 19 | /// you use an array literal. Instead, create a new ordered set using an array 20 | /// literal as its value by enclosing a comma-separated list of values in 21 | /// square brackets. You can use an array literal anywhere an ordered set is 22 | /// expected by the type context. 23 | /// 24 | /// - Parameter elements: A variadic list of elements of the new ordered set. 25 | /// 26 | /// - Complexity: O(`elements.count`) if `Element` implements 27 | /// high-quality hashing. 28 | @inlinable 29 | public init(arrayLiteral elements: Element...) { 30 | self.init(elements) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedSet/OrderedSet+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedSet: Hashable { 13 | /// Hashes the essential components of this value by feeding them into the 14 | /// given hasher. 15 | /// 16 | /// Complexity: O(`count`) 17 | @inlinable 18 | public func hash(into hasher: inout Hasher) { 19 | hasher.combine(count) // Discriminator 20 | for item in _elements { 21 | hasher.combine(item) 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedSet/OrderedSet+Sendable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedSet: @unchecked Sendable where Element: Sendable {} 13 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/OrderedSet/OrderedSet+UnstableInternals.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension OrderedSet { 13 | /// Exposes some private implementation details and low-level unsafe 14 | /// operations, primarily to allow clear box testing. 15 | /// 16 | /// This struct is a private implementation detail, therefore it and its 17 | /// members are not covered by any source compatibility promises -- they 18 | /// may disappear in any new release. 19 | @frozen 20 | public struct _UnstableInternals { 21 | @usableFromInline 22 | internal typealias _Bucket = _HashTable.Bucket 23 | 24 | @usableFromInline 25 | internal var base: OrderedSet 26 | 27 | @inlinable 28 | init(_ base: OrderedSet) { 29 | self.base = base 30 | } 31 | } 32 | 33 | @inlinable 34 | public var __unstable: _UnstableInternals { 35 | @inline(__always) 36 | get { 37 | _UnstableInternals(self) 38 | } 39 | 40 | @inline(__always) // https://github.com/apple/swift-collections/issues/164 41 | _modify { 42 | var view = _UnstableInternals(self) 43 | self = OrderedSet() 44 | defer { self = view.base } 45 | yield &view 46 | } 47 | } 48 | } 49 | 50 | extension OrderedSet._UnstableInternals: Sendable 51 | where Element: Sendable {} 52 | -------------------------------------------------------------------------------- /Sources/OrderedCollections/Utilities/_UnsafeBitset.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if !COLLECTIONS_SINGLE_MODULE 13 | import InternalCollectionsUtilities 14 | 15 | @usableFromInline 16 | internal typealias _UnsafeBitSet = InternalCollectionsUtilities._UnsafeBitSet 17 | #endif 18 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Basics/BigString+Debugging.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString { 14 | public func _dump(heightLimit: Int = .max) { 15 | _rope._dump(heightLimit: heightLimit) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Basics/BigString+Invariants.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString { 14 | public func _invariantCheck() { 15 | #if COLLECTIONS_INTERNAL_CHECKS 16 | _rope._invariantCheck() 17 | let allowUndersize = _rope.isSingleton 18 | 19 | var state = _CharacterRecognizer() 20 | for chunk in _rope { 21 | precondition(allowUndersize || !chunk.isUndersized, "Undersized chunk") 22 | let (characters, prefix, suffix) = state.edgeCounts(consuming: chunk.string) 23 | precondition( 24 | chunk.prefixCount == prefix, 25 | "Inconsistent position of first grapheme break in chunk") 26 | precondition( 27 | chunk.suffixCount == suffix, 28 | "Inconsistent position of last grapheme break in chunk") 29 | precondition( 30 | chunk.characterCount == characters, 31 | "Inconsistent character count in chunk") 32 | } 33 | #endif 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Basics/BigString+Iterators.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString { 14 | struct ChunkIterator { 15 | var base: _Rope.Iterator 16 | 17 | init(base: _Rope.Iterator) { 18 | self.base = base 19 | } 20 | } 21 | 22 | func makeChunkIterator() -> ChunkIterator { 23 | ChunkIterator(base: _rope.makeIterator()) 24 | } 25 | } 26 | 27 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 28 | extension BigString.ChunkIterator: IteratorProtocol { 29 | typealias Element = String 30 | 31 | mutating func next() -> String? { 32 | base.next()?.string 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Basics/BigString.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | /// The core of a B-tree based String implementation. 13 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 14 | public struct BigString: Sendable { 15 | typealias _Rope = Rope<_Chunk> 16 | 17 | var _rope: _Rope 18 | 19 | internal init(_rope: _Rope) { 20 | self._rope = _rope 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Chunk/BigString+Chunk+Indexing by UTF16.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString._Chunk { 14 | /// UTF-16 index lookup. 15 | func index(at utf8Offset: Int, utf16TrailingSurrogate: Bool) -> String.Index { 16 | var index = string._utf8Index(at: utf8Offset) 17 | if 18 | utf16TrailingSurrogate, 19 | index < string.endIndex, 20 | string.utf8[index]._isUTF8NonBMPLeadingCodeUnit 21 | { 22 | index = string.utf16.index(after: index) 23 | } 24 | return index 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Conformances/BigString+CustomDebugStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString: CustomDebugStringConvertible { 14 | public var debugDescription: String { 15 | description.debugDescription 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Conformances/BigString+CustomStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString: CustomStringConvertible { 14 | public var description: String { 15 | String(self) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Conformances/BigString+ExpressibleByStringLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString: ExpressibleByStringLiteral { 14 | public init(stringLiteral value: String) { 15 | self.init(value) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Conformances/BigString+LosslessStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString: LosslessStringConvertible { 14 | // init?(_: String) is implemented by RangeReplaceableCollection.init(_:) 15 | } 16 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Conformances/BigString+TextOutputStream.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString: TextOutputStream { 14 | public mutating func write(_ string: String) { 15 | append(contentsOf: string) 16 | } 17 | } 18 | 19 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 20 | extension BigString: TextOutputStreamable { 21 | public func write(to target: inout some TextOutputStream) { 22 | for chunk in _rope { 23 | chunk.string.write(to: &target) 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Operations/BigString+Split.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension BigString { 14 | mutating func split( 15 | at index: Index 16 | ) -> Builder { 17 | let b = _ropeBuilder(at: index) 18 | let state = b._breakState() 19 | let builder = Builder(base: b, prefixEndState: state, suffixStartState: state) 20 | return builder 21 | } 22 | 23 | mutating func split( 24 | at index: Index, 25 | state: _CharacterRecognizer 26 | ) -> Builder { 27 | let b = _ropeBuilder(at: index) 28 | let builder = Builder(base: b, prefixEndState: state, suffixStartState: state) 29 | return builder 30 | } 31 | 32 | mutating func _ropeBuilder(at index: Index) -> _Rope.Builder { 33 | if let ri = index._rope, _rope.isValid(ri) { 34 | return _rope.split(at: ri, index._chunkIndex) 35 | } 36 | return _rope.builder(splittingAt: index.utf8Offset, in: _UTF8Metric()) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Sources/RopeModule/BigString/Operations/Range+BigString.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) 13 | extension Range { 14 | internal var _isEmptyUTF8: Bool { 15 | lowerBound.utf8Offset == upperBound.utf8Offset 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/RopeModule/Rope/Basics/Rope+_UnmanagedLeaf.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Rope { 13 | @frozen // Not really! This module isn't ABI stable. 14 | @usableFromInline 15 | internal struct _UnmanagedLeaf { 16 | @usableFromInline internal typealias _Item = Rope._Item 17 | @usableFromInline internal typealias _Leaf = _Storage<_Item> 18 | @usableFromInline internal typealias _UnsafeHandle = Rope._UnsafeHandle 19 | 20 | @usableFromInline var _ref: Unmanaged<_Leaf> 21 | 22 | @inlinable 23 | internal init(_ leaf: __shared _Leaf) { 24 | _ref = .passUnretained(leaf) 25 | } 26 | } 27 | } 28 | 29 | extension Rope._UnmanagedLeaf: Equatable { 30 | @inlinable 31 | internal static func ==(left: Self, right: Self) -> Bool { 32 | left._ref.toOpaque() == right._ref.toOpaque() 33 | } 34 | } 35 | 36 | extension Rope._UnmanagedLeaf { 37 | @inlinable 38 | internal func read( 39 | body: (_UnsafeHandle<_Item>) -> R 40 | ) -> R { 41 | _ref._withUnsafeGuaranteedRef { leaf in 42 | leaf.withUnsafeMutablePointers { h, p in 43 | let handle = _UnsafeHandle(isMutable: false, header: h, start: p) 44 | return body(handle) 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Sources/RopeModule/Rope/Basics/RopeMetric.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | public protocol RopeMetric: Sendable { 13 | associatedtype Element: RopeElement 14 | 15 | /// Returns the size of a summarized rope element in this metric. 16 | func size(of summary: Element.Summary) -> Int 17 | 18 | /// Returns an index addressing the content at the given offset from 19 | /// the start of the specified rope element. 20 | /// 21 | /// - Parameter offset: An integer offset from the start of `element` in this 22 | /// metric, not exceeding `size(of: element.summary)`. 23 | /// - Parameter element: An arbitrary rope element. 24 | /// - Returns: The index addressing the desired position in the input element. 25 | func index(at offset: Int, in element: Element) -> Element.Index 26 | } 27 | 28 | extension RopeMetric { 29 | @inlinable @inline(__always) 30 | internal func _nonnegativeSize(of summary: Element.Summary) -> Int { 31 | let r = size(of: summary) 32 | assert(r >= 0) 33 | return r 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/RopeModule/Rope/Basics/_RopeVersion.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | @usableFromInline 13 | @frozen // Not really! This module isn't ABI stable. 14 | internal struct _RopeVersion { 15 | // FIXME: Replace this probabilistic mess with atomics when Swift gets its act together. 16 | @usableFromInline internal var _value: UInt 17 | 18 | @inlinable 19 | internal init() { 20 | var rng = SystemRandomNumberGenerator() 21 | _value = rng.next() 22 | } 23 | 24 | @inlinable 25 | internal init(_ value: UInt) { 26 | self._value = value 27 | } 28 | } 29 | 30 | extension _RopeVersion: Equatable { 31 | @inlinable 32 | internal static func ==(left: Self, right: Self) -> Bool { 33 | left._value == right._value 34 | } 35 | } 36 | 37 | extension _RopeVersion { 38 | @inlinable 39 | internal mutating func bump() { 40 | _value &+= 1 41 | } 42 | 43 | @inlinable 44 | internal mutating func reset() { 45 | var rng = SystemRandomNumberGenerator() 46 | _value = rng.next() 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Sources/RopeModule/Rope/Conformances/Rope+Sequence.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Rope: Sequence { 13 | @inlinable 14 | public func makeIterator() -> Iterator { 15 | Iterator(self, from: self.startIndex) 16 | } 17 | 18 | @inlinable 19 | public func makeIterator(from start: Index) -> Iterator { 20 | Iterator(self, from: start) 21 | } 22 | 23 | @frozen // Not really! This module isn't ABI stable. 24 | public struct Iterator: IteratorProtocol { 25 | @usableFromInline 26 | internal let _rope: Rope 27 | 28 | @usableFromInline 29 | internal var _index: Index 30 | 31 | @inlinable 32 | internal init(_ rope: Rope, from start: Index) { 33 | rope.validate(start) 34 | self._rope = rope 35 | self._index = start 36 | self._rope.grease(&_index) 37 | } 38 | 39 | @inlinable 40 | public mutating func next() -> Element? { 41 | guard let leaf = _index._leaf else { return nil } 42 | let item = leaf.read { $0.children[_index._path[0]].value } 43 | _rope.formIndex(after: &_index) 44 | return item 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Sources/RopeModule/Utilities/Optional Utilities.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension Optional { 13 | @inlinable 14 | internal mutating func _take() -> Self { 15 | let r = self 16 | self = nil 17 | return r 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Sources/SortedCollections/BTree/_BTree+CustomDebugStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension _BTree: CustomDebugStringConvertible { 13 | #if DEBUG 14 | /// A textual representation of this instance, suitable for debugging. 15 | public var debugDescription: String { 16 | return "BTree<\(Key.self), \(Value.self)>\n" + 17 | self.root.read { String(reflecting: $0) } 18 | } 19 | #else 20 | /// A textual representation of this instance, suitable for debugging. 21 | public var debugDescription: String { 22 | return "BTree<\(Key.self), \(Value.self)>(\(self.root))" 23 | } 24 | #endif // DEBUG 25 | } 26 | -------------------------------------------------------------------------------- /Sources/SortedCollections/BTree/_BTree+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension _BTree: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | @inlinable 15 | internal var customMirror: Mirror { 16 | Mirror(self, unlabeledChildren: self, displayStyle: .dictionary) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Sources/SortedCollections/BTree/_Node+CustomDebugString.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension _Node: CustomDebugStringConvertible { 13 | /// A textual representation of this instance, suitable for debugging. 14 | public var debugDescription: String { 15 | self.read { $0.debugDescription } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedDictionary/SortedDictionary+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedDictionary: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self, displayStyle: .dictionary) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedDictionary/SortedDictionary+CustomStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedDictionary: CustomStringConvertible, CustomDebugStringConvertible { 13 | @inlinable 14 | public var description: String { 15 | if isEmpty { return "[:]" } 16 | var result = "[" 17 | var first = true 18 | for (key, value) in self { 19 | if first { 20 | first = false 21 | } else { 22 | result += ", " 23 | } 24 | result += "\(key): \(value)" 25 | } 26 | result += "]" 27 | return result 28 | } 29 | 30 | @inlinable 31 | public var debugDescription: String { 32 | var result = "SortedDictionary<\(Key.self), \(Value.self)>(" 33 | if isEmpty { 34 | result += "[:]" 35 | } else { 36 | result += "[" 37 | var first = true 38 | for (key, value) in self { 39 | if first { 40 | first = false 41 | } else { 42 | result += ", " 43 | } 44 | 45 | debugPrint(key, value, separator: ": ", terminator: "", to: &result) 46 | } 47 | result += "]" 48 | } 49 | result += ")" 50 | return result 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedDictionary/SortedDictionary+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedDictionary: Equatable where Value: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. 14 | /// 15 | /// Equality is the inverse of inequality. For any values `a` and `b`, 16 | /// `a == b` implies that `a != b` is false. 17 | /// 18 | /// - Parameters: 19 | /// - lhs: A value to compare. 20 | /// - rhs: Another value to compare. 21 | /// - Complexity: O(`self.count`) 22 | @inlinable 23 | public static func ==(lhs: SortedDictionary, rhs: SortedDictionary) -> Bool { 24 | // TODO: optimize/benchmarking by comparing node identity/shared subtrees. 25 | if lhs.count != rhs.count { return false } 26 | for ((k1, v1), (k2, v2)) in zip(lhs, rhs) { 27 | if k1 != k2 || v1 != v2 { 28 | return false 29 | } 30 | } 31 | return true 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedDictionary/SortedDictionary+ExpressibleByDictionaryLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedDictionary: ExpressibleByDictionaryLiteral { 13 | /// Creates a new sorted dictionary from the contents of a dictionary 14 | /// literal. 15 | /// 16 | /// Duplicate elements in the literal are allowed, but the resulting 17 | /// set will only contain the first occurrence of each. 18 | /// 19 | /// Do not call this initializer directly. It is used by the compiler when you 20 | /// use a dictionary literal. Instead, create a new ordered dictionary using a 21 | /// dictionary literal as its value by enclosing a comma-separated list of 22 | /// values in square brackets. You can use an array literal anywhere a set is 23 | /// expected by the type context. 24 | /// 25 | /// - Parameter elements: A variadic list of key-value pairs for the new 26 | /// dictionary. 27 | /// 28 | /// - Complexity: O(`n log n`) 29 | @inlinable 30 | public init(dictionaryLiteral elements: (Key, Value)...) { 31 | self.init(keysWithValues: elements) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedDictionary/SortedDictionary+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedDictionary: Hashable where Key: Hashable, Value: Hashable { 13 | /// Hashes the essential components of this value by feeding them 14 | /// into the given hasher. 15 | /// - Parameter hasher: The hasher to use when combining 16 | /// the components of this instance. 17 | /// - Complexity: O(`self.count`) 18 | @inlinable 19 | public func hash(into hasher: inout Hasher) { 20 | hasher.combine(self.count) 21 | for (key, value) in self { 22 | hasher.combine(key) 23 | hasher.combine(value) 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedDictionary/SortedDictionary+Sendable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedDictionary: @unchecked Sendable 13 | where Key: Sendable, Value: Sendable {} 14 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet+CustomReflectable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedSet: CustomReflectable { 13 | /// The custom mirror for this instance. 14 | public var customMirror: Mirror { 15 | Mirror(self, unlabeledChildren: self, displayStyle: .set) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet+CustomStringConvertible.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedSet: CustomStringConvertible, CustomDebugStringConvertible { 13 | @inlinable 14 | public var description: String { 15 | var result = "[" 16 | var first = true 17 | for element in self { 18 | if first { 19 | first = false 20 | } else { 21 | result += ", " 22 | } 23 | print(element, terminator: "", to: &result) 24 | } 25 | result += "]" 26 | return result 27 | } 28 | 29 | @inlinable 30 | public var debugDescription: String { 31 | var result = "SortedSet<\(Element.self)>([" 32 | var first = true 33 | for element in self { 34 | if first { 35 | first = false 36 | } else { 37 | result += ", " 38 | } 39 | 40 | debugPrint(element, terminator: "", to: &result) 41 | } 42 | result += "])" 43 | return result 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet+Equatable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedSet: Equatable { 13 | /// Returns a Boolean value indicating whether two values are equal. 14 | /// 15 | /// Equality is the inverse of inequality. For any values `a` and `b`, 16 | /// `a == b` implies that `a != b` is false. 17 | /// 18 | /// - Parameters: 19 | /// - lhs: A value to compare. 20 | /// - rhs: Another value to compare. 21 | /// - Complexity: O(`self.count`) 22 | @inlinable 23 | public static func ==(lhs: Self, rhs: Self) -> Bool { 24 | // TODO: optimize/benchmarking by comparing node identity. 25 | if lhs.count != rhs.count { return false } 26 | for (k1, k2) in zip(lhs, rhs) { 27 | if k1 != k2 { 28 | return false 29 | } 30 | } 31 | return true 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet+ExpressibleByArrayLiteral.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedSet: ExpressibleByArrayLiteral { 13 | /// Creates a new sorted set from the contents of an array literal. 14 | /// 15 | /// Duplicate elements in the literal are allowed, but the resulting 16 | /// set will only contain the last occurrence of each. 17 | /// 18 | /// Do not call this initializer directly. It is used by the compiler when 19 | /// you use an array literal. Instead, create a new set using an array 20 | /// literal as its value by enclosing a comma-separated list of values in 21 | /// square brackets. You can use an array literal anywhere a set is expected 22 | /// by the type context. 23 | /// 24 | /// - Parameter elements: A variadic list of elements of the new set. 25 | /// 26 | /// - Complexity: O(`n log n`) where `n` is the number of elements 27 | /// in `elements`. 28 | @inlinable 29 | public init(arrayLiteral elements: Element...) { 30 | self.init(elements) 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet+Hashable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedSet: Hashable where Element: Hashable { 13 | /// Hashes the essential components of this value by feeding them 14 | /// into the given hasher. 15 | /// - Parameter hasher: The hasher to use when combining 16 | /// the components of this instance. 17 | /// - Complexity: O(`self.count`) 18 | @inlinable 19 | public func hash(into hasher: inout Hasher) { 20 | hasher.combine(self.count) 21 | for element in self { 22 | hasher.combine(element) 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet+Sendable.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedSet: @unchecked Sendable where Element: Sendable {} 13 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet.Index.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension SortedSet { 13 | /// Returns the index for a given element, if it exists 14 | /// - Complexity: O(`log n`) 15 | @inlinable 16 | public func index(of element: Element) -> Index? { 17 | if let index = self._root.findAnyIndex(forKey: element) { 18 | return Index(index) 19 | } else { 20 | return nil 21 | } 22 | } 23 | 24 | /// The position of an element within a sorted set 25 | public struct Index { 26 | @usableFromInline 27 | internal var _index: _Tree.Index 28 | 29 | @inlinable 30 | @inline(__always) 31 | internal init(_ _index: _Tree.Index) { 32 | self._index = _index 33 | } 34 | } 35 | } 36 | 37 | extension SortedSet.Index: @unchecked Sendable 38 | where Element: Sendable {} 39 | 40 | // MARK: Equatable 41 | extension SortedSet.Index: Equatable { 42 | @inlinable 43 | public static func ==(lhs: SortedSet.Index, rhs: SortedSet.Index) -> Bool { 44 | lhs._index.ensureValid(with: rhs._index) 45 | return lhs._index == rhs._index 46 | } 47 | } 48 | 49 | // MARK: Comparable 50 | extension SortedSet.Index: Comparable { 51 | @inlinable 52 | public static func <(lhs: SortedSet.Index, rhs: SortedSet.Index) -> Bool { 53 | lhs._index.ensureValid(with: rhs._index) 54 | return lhs._index < rhs._index 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Sources/SortedCollections/SortedSet/SortedSet.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | /// A collection which maintains unique members in ascending sorted order. 13 | public struct SortedSet { 14 | @usableFromInline 15 | internal typealias _Tree = _BTree 16 | 17 | @usableFromInline 18 | internal var _root: _Tree 19 | 20 | //// Creates an empty set. 21 | /// 22 | /// This initializer is equivalent to initializing with an empty array 23 | /// literal. 24 | /// 25 | /// - Complexity: O(1) 26 | @inlinable 27 | @inline(__always) 28 | public init() { 29 | self._root = _Tree() 30 | } 31 | 32 | /// Creates a set rooted at a given B-Tree. 33 | @inlinable 34 | internal init(_rootedAt tree: _Tree) { 35 | self._root = tree 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sources/SortedCollections/Utilities/Assertions.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | 13 | -------------------------------------------------------------------------------- /Tests/CollectionsTestSupportTests/IndexRangeCollectionTests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import XCTest 13 | #if !COLLECTIONS_SINGLE_MODULE 14 | import _CollectionsTestSupport 15 | #endif 16 | 17 | final class IndexRangeCollectionTests: CollectionTestCase { 18 | func testCollection() { 19 | withEvery("b", in: [0, 1]) { b in 20 | withEvery("c", in: 0 ..< 3) { c in 21 | let expected = (b ... b + c).flatMap { end in 22 | (b ... end).lazy.map { start in start ..< end } 23 | } 24 | let actual = IndexRangeCollection(bounds: b ..< b + c) 25 | checkBidirectionalCollection(actual, expectedContents: expected) 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Tests/CollectionsTestSupportTests/MinimalTypeConformances.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import XCTest 13 | #if !COLLECTIONS_SINGLE_MODULE 14 | import _CollectionsTestSupport 15 | #endif 16 | 17 | final class MinimalTypeTests: CollectionTestCase { 18 | func testMinimalSequence() { 19 | withEvery( 20 | "behavior", 21 | in: [ 22 | UnderestimatedCountBehavior.precise, 23 | UnderestimatedCountBehavior.half, 24 | UnderestimatedCountBehavior.value(0) 25 | ] 26 | ) { behavior in 27 | withEvery("isContiguous", in: [false, true]) { isContiguous in 28 | func make() -> MinimalSequence { 29 | MinimalSequence( 30 | elements: 0 ..< 50, 31 | underestimatedCount: behavior, 32 | isContiguous: isContiguous) 33 | } 34 | checkSequence(make, expectedContents: 0 ..< 50) 35 | } 36 | } 37 | } 38 | 39 | func testMinimalCollection() { 40 | checkCollection(MinimalCollection(0 ..< 50), expectedContents: 0 ..< 50) 41 | } 42 | 43 | func testMinimalBidirectionalCollection() { 44 | checkBidirectionalCollection(MinimalBidirectionalCollection(0 ..< 50), expectedContents: 0 ..< 50) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Tests/CollectionsTestSupportTests/UtilitiesTests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import XCTest 13 | 14 | #if !COLLECTIONS_SINGLE_MODULE && DEBUG 15 | @testable import _CollectionsTestSupport 16 | #endif 17 | 18 | #if COLLECTIONS_SINGLE_MODULE || DEBUG 19 | final class UtilitiesTests: CollectionTestCase { 20 | func testIntegerSquareRoot() { 21 | withSome("i", in: 0 ..< Int.max, maxSamples: 100_000) { i in 22 | let s = i._squareRoot() 23 | expectLessThanOrEqual(s * s, i) 24 | let next = (s + 1).multipliedReportingOverflow(by: s + 1) 25 | if !next.overflow { 26 | expectGreaterThan(next.partialValue, i) 27 | } 28 | } 29 | } 30 | } 31 | #endif 32 | -------------------------------------------------------------------------------- /Tests/HashTreeCollectionsTests/TreeDictionary.Values Tests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | import XCTest 13 | #if COLLECTIONS_SINGLE_MODULE 14 | import Collections 15 | #else 16 | import _CollectionsTestSupport 17 | import HashTreeCollections 18 | #endif 19 | 20 | class TreeDictionaryValuesTests: CollectionTestCase { 21 | func test_BidirectionalCollection_fixtures() { 22 | withEachFixture { fixture in 23 | withLifetimeTracking { tracker in 24 | let (d, ref) = tracker.shareableDictionary(for: fixture) 25 | let v = ref.map { $0.value } 26 | checkCollection(d.values, expectedContents: v, by: ==) 27 | _checkBidirectionalCollection_indexOffsetBy( 28 | d.values, expectedContents: v, by: ==) 29 | } 30 | } 31 | } 32 | 33 | func test_descriptions() { 34 | let d: TreeDictionary = [ 35 | "a": 1, 36 | "b": 2 37 | ] 38 | 39 | if d.first!.key == "a" { 40 | expectEqual(d.values.description, "[1, 2]") 41 | } else { 42 | expectEqual(d.values.description, "[2, 1]") 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Tests/HeapTests/HeapNodeTests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if DEBUG // These unit tests need access to HeapModule internals 13 | import XCTest 14 | #if COLLECTIONS_SINGLE_MODULE 15 | @testable import Collections 16 | #else 17 | @testable import HeapModule 18 | #endif 19 | 20 | class HeapNodeTests: XCTestCase { 21 | func test_levelCalculation() { 22 | // Check alternating min and max levels in the heap 23 | var isMin = true 24 | for exp in 0...12 { 25 | // Check [2^exp, 2^(exp + 1)) 26 | for offset in Int(pow(2, Double(exp)) - 1)..( 21 | keys: Keys 22 | ) -> ( 23 | dictionary: OrderedDictionary, LifetimeTracked>, 24 | expected: [(key: LifetimeTracked, value: LifetimeTracked)] 25 | ) 26 | where Keys.Element == Int 27 | { 28 | let k = Array(keys) 29 | let keys = self.instances(for: k) 30 | let values = self.instances(for: k.map { $0 + 100 }) 31 | let dictionary = OrderedDictionary(uniqueKeys: keys, values: values) 32 | return (dictionary, (0 ..< k.count).map { (key: keys[$0], value: values[$0]) }) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Tests/README.md: -------------------------------------------------------------------------------- 1 | # Unit tests 2 | 3 | Beware! The contents of this directory are not source stable. They are provided as is, with no compatibility promises across package releases. Future versions of this package can arbitrarily change these files or remove them, without any advance notice. (This can include patch releases.) 4 | -------------------------------------------------------------------------------- /Tests/RopeModuleTests/Availability.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | var isRunningOnSwiftStdlib5_8: Bool { 13 | if #available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *) { 14 | return true 15 | } 16 | return false 17 | } 18 | -------------------------------------------------------------------------------- /Tests/SortedCollectionsTests/BTree/BTree.Builder Tests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if DEBUG 13 | import _CollectionsTestSupport 14 | @_spi(Testing) @testable import SortedCollections 15 | 16 | final class BTreeBuilderTests: CollectionTestCase { 17 | func test_append() { 18 | withEvery("size", in: 0..<1000) { size in 19 | var builder = _BTree.Builder(capacity: 4) 20 | 21 | for i in 0..( 17 | keys: Keys 18 | ) -> ( 19 | dictionary: SortedDictionary, LifetimeTracked>, 20 | kvs: [(LifetimeTracked, LifetimeTracked)] 21 | ) 22 | where Keys.Element == Int 23 | { 24 | let k = Array(keys) 25 | let keys = self.instances(for: k) 26 | let values = self.instances(for: k.map { -($0 + 1) }) 27 | 28 | let kvs = Array(zip(keys, values)) 29 | 30 | let dictionary = SortedDictionary(keysWithValues: kvs) 31 | 32 | return (dictionary, kvs) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/AssertionContexts/CollectionTestCase.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift.org open source project 4 | // 5 | // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | // 11 | //===----------------------------------------------------------------------===// 12 | 13 | import XCTest 14 | 15 | open class CollectionTestCase: XCTestCase { 16 | internal var _context: TestContext? 17 | 18 | public var context: TestContext { _context! } 19 | 20 | open var isAvailable: Bool { true } 21 | 22 | #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) 23 | open override func invokeTest() { 24 | guard isAvailable else { 25 | print("\(Self.self) unavailable; skipping") 26 | return 27 | } 28 | return super.invokeTest() 29 | } 30 | #endif 31 | 32 | open override func setUp() { 33 | super.setUp() 34 | _context = TestContext.pushNew() 35 | } 36 | 37 | open override func tearDown() { 38 | if let context = _context { 39 | TestContext.pop(context) 40 | _context = nil 41 | } 42 | super.tearDown() 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/MinimalTypes/ResettableValue.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift.org open source project 4 | // 5 | // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | // 11 | //===----------------------------------------------------------------------===// 12 | 13 | // Loosely adapted from https://github.com/apple/swift/tree/main/stdlib/private/StdlibUnittest 14 | 15 | public class ResettableValue { 16 | public init(_ value: Value) { 17 | self.defaultValue = value 18 | self.value = value 19 | } 20 | 21 | public func reset() { 22 | value = defaultValue 23 | } 24 | 25 | public let defaultValue: Value 26 | public var value: Value 27 | } 28 | 29 | extension ResettableValue where Value: Strideable { 30 | public func increment(by delta: Value.Stride = 1) { 31 | value = value.advanced(by: delta) 32 | } 33 | } 34 | 35 | extension ResettableValue: CustomStringConvertible { 36 | public var description: String { "\(value)" } 37 | } 38 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/Utilities/AllOnesRandomNumberGenerator.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | /// A terrible random number generator that always returns values with all 13 | /// bits set to true. 14 | public struct AllOnesRandomNumberGenerator: RandomNumberGenerator { 15 | 16 | public init() {} 17 | 18 | public mutating func next() -> UInt64 { 19 | UInt64.max 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/Utilities/Box.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift.org open source project 4 | // 5 | // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | // 11 | //===----------------------------------------------------------------------===// 12 | 13 | public final class Box { 14 | public init(_ value: T) { self.value = value } 15 | public var value: T 16 | } 17 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/Utilities/HashableBox.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift.org open source project 4 | // 5 | // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | // 11 | //===----------------------------------------------------------------------===// 12 | 13 | public final class HashableBox: Hashable { 14 | public init(_ value: T) { self.value = value } 15 | public var value: T 16 | 17 | public static func ==(left: HashableBox, right: HashableBox) -> Bool { 18 | left.value == right.value 19 | } 20 | 21 | public func hash(into hasher: inout Hasher) { 22 | hasher.combine(value) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/Utilities/Integer Square Root.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | extension FixedWidthInteger { 13 | internal func _squareRoot() -> Self { 14 | // Newton's method 15 | precondition(self >= 0) 16 | guard self != 0 else { return 0 } 17 | var x: Self = 1 &<< ((self.bitWidth + 1) / 2) 18 | var y: Self = 0 19 | while true { 20 | y = (self / x + x) &>> 1 21 | if x == y || x == y - 1 { break } 22 | x = y 23 | } 24 | return x 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/Utilities/RepeatableRandomNumberGenerator.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | public struct RepeatableRandomNumberGenerator: RandomNumberGenerator { 13 | public static var globalSeed: Int { 14 | #if COLLECTIONS_RANDOMIZED_TESTING 15 | 42.hashValue 16 | #else 17 | 0 18 | #endif 19 | } 20 | 21 | // This uses the same linear congruential generator as rand48. 22 | // FIXME: Replace with something better. 23 | internal static let _m: UInt64 = 1 << 48 24 | internal static let _a: UInt64 = 25214903917 25 | internal static let _c: UInt64 = 11 26 | 27 | internal var _state: UInt64 28 | 29 | public init(seed: Int) { 30 | self.init(seed: UInt64(truncatingIfNeeded: seed)) 31 | } 32 | 33 | public init(seed: UInt64) { 34 | // Perturb the seed a little so that the sequence doesn't start with a 35 | // zero value in the common case of seed == 0. (Using a zero seed is a 36 | // rather silly thing to do, but it's the easy thing.) 37 | let global = UInt64(truncatingIfNeeded: Self.globalSeed) 38 | _state = seed ^ global ^ 0x536f52616e646f6d // "SoRandom" 39 | } 40 | 41 | private mutating func _next() -> UInt64 { 42 | _state = (Self._a &* _state &+ Self._c) & (Self._m - 1) 43 | return _state &>> 16 44 | } 45 | 46 | public mutating func next() -> UInt64 { 47 | return (_next() &<< 32) | _next() 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/Utilities/SortedCollectionAPIChecker.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | #if COLLECTIONS_SINGLE_MODULE 13 | import Collections 14 | #else 15 | import InternalCollectionsUtilities 16 | #endif 17 | 18 | /// This protocol simply lists Collection/Sequence extensions that ought to be 19 | /// customized for sorted collections. 20 | /// 21 | /// Conforming to this protocol admittedly doesn't do much, as the default 22 | /// implementations already exist for most of these requirements 23 | /// (but they aren't doing the right thing). 24 | public protocol SortedCollectionAPIChecker: Collection, _SortedCollection 25 | where Element: Comparable { 26 | // This one actually does not come with a default implementation. 27 | func sorted() -> Self 28 | 29 | // These are also defined on `Sequence`, but the default implementation does 30 | // a linear search, which isn't what we want. 31 | func min() -> Element? 32 | func max() -> Element? 33 | } 34 | -------------------------------------------------------------------------------- /Tests/_CollectionsTestSupport/Utilities/StringConvertibleValue.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift.org open source project 4 | // 5 | // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | // 11 | //===----------------------------------------------------------------------===// 12 | 13 | public struct StringConvertibleValue { 14 | public var value: Int 15 | public init(_ value: Int) { self.value = value } 16 | } 17 | 18 | extension StringConvertibleValue: ExpressibleByIntegerLiteral { 19 | public init(integerLiteral value: Int) { 20 | self.init(value) 21 | } 22 | } 23 | 24 | extension StringConvertibleValue: CustomStringConvertible { 25 | public var description: String { 26 | "description(\(value))" 27 | } 28 | } 29 | 30 | extension StringConvertibleValue: CustomDebugStringConvertible { 31 | public var debugDescription: String { 32 | "debugDescription(\(value))" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Utils/README.md: -------------------------------------------------------------------------------- 1 | # Maintenance Scripts 2 | 3 | This directory contains scripts that are used to maintain this package. 4 | 5 | Beware! The contents of this directory are not source stable. They are provided as is, with no compatibility promises across package releases. Future versions of this package can arbitrarily change these files or remove them, without any advance notice. (This can include patch releases.) 6 | 7 | - `generate-docs.sh`: A shell scripts that automates the generation of API documentation. 8 | 9 | - `generate-sources.sh`: A shell script that invokes `gyb` to regenerate the contents of autogenerated/ directories. This needs to be run whenever a file with a `.swift.gyb` extension is updated. 10 | 11 | - `gyb`, `gyb.py`: Generate Your Boilerplate. A rudimentary source code generation utility. 12 | 13 | - `gyb_utils.py`: A Python module containing code generation utility definitions that are shared across multiple `.swift.gyb` files in this repository. 14 | 15 | - `run-full-tests.sh`: A shell script that exercises many common configurations of this package in a semi-automated way. This is used before tagging a release to avoid accidentally shipping a package version that breaks some setups. 16 | 17 | - `shuffle-sources.sh`: A legacy utility that randomly reorders Swift source files in a given directory. This is used to avoid reoccurrances of issue #7. (This is hopefully only relevant with compilers that the package no longer supports.) 18 | -------------------------------------------------------------------------------- /Utils/gyb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import gyb 3 | gyb.main() 4 | -------------------------------------------------------------------------------- /Utils/gyb_utils.py: -------------------------------------------------------------------------------- 1 | #===-----------------------------------------------------------------------===// 2 | # 3 | # This source file is part of the Swift.org open source project 4 | # 5 | # Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | # Licensed under Apache License v2.0 with Runtime Library Exception 7 | # 8 | # See https://swift.org/LICENSE.txt for license information 9 | # See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors 10 | # 11 | #===-----------------------------------------------------------------------===// 12 | 13 | def autogenerated_warning(): 14 | return """ 15 | // ############################################################################# 16 | // # # 17 | // # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. # 18 | // # # 19 | // ############################################################################# 20 | """ 21 | 22 | visibility_levels = ["internal", "public"] 23 | def visibility_boilerplate(part): 24 | if part == "internal": 25 | return """ 26 | // In single module mode, we need these declarations to be internal, 27 | // but in regular builds we want them to be public. Unfortunately 28 | // the current best way to do this is to duplicate all definitions. 29 | #if COLLECTIONS_SINGLE_MODULE""" 30 | 31 | if part == "public": 32 | return "#else // !COLLECTIONS_SINGLE_MODULE" 33 | if part == "end": 34 | return "#endif // COLLECTIONS_SINGLE_MODULE" 35 | -------------------------------------------------------------------------------- /Utils/run-benchmarks.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -eu 4 | 5 | srcroot="$(dirname "$0")/../Benchmarks" 6 | results="batch.results" 7 | 8 | command="${1:-help}" 9 | shift || : 10 | 11 | run() { 12 | local flags 13 | flags="-c release" 14 | flags="$flags -Xswiftc -Xllvm -Xswiftc -align-module-to-page-size" 15 | swift run --package-path "$srcroot" $flags benchmark "$@" 16 | } 17 | 18 | case "$command" in 19 | batch-run) 20 | revision="$(git rev-parse HEAD)" 21 | run library run "$results" \ 22 | --source-url "https://github.com/apple/swift-collections/tree/$revision" \ 23 | --mode replace-all \ 24 | --max-size 4M --cycles 3 \ 25 | --amortized-cutoff 10us \ 26 | "$@" 27 | ;; 28 | batch-render) 29 | run library render "$results" \ 30 | --min-time 100ps --max-time 10us \ 31 | --min-size 1 --max-size 4M \ 32 | "$@" 33 | ;; 34 | batch-clean) 35 | rm -rf "$out"/results 36 | ;; 37 | *) 38 | run "$command" "$@" 39 | ;; 40 | esac 41 | -------------------------------------------------------------------------------- /Utils/shuffle-sources.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Randomly reorder Swift source files in the specified directory, then 4 | # try rebuilding the package, in a loop. 5 | # 6 | # This script is useful to help reproducing nondeterministic issues 7 | # with the Swift compiler's MergeModules phase, as in 8 | # https://github.com/apple/swift-collections/issues/7 9 | # 10 | 11 | set -eu 12 | 13 | 14 | function shuffle() { 15 | input="$1" 16 | 17 | tmp="$(dirname "$input")/tmp" 18 | mkdir "$tmp" 19 | local i=0 20 | ls "$input"/*.swift | sort -R | while read file; do 21 | mv "$file" "$tmp/$i.swift" 22 | i=$(($i + 1)) 23 | done 24 | 25 | mv "$tmp"/*.swift "$input" 26 | rmdir "$tmp" 27 | } 28 | 29 | if [[ -z "${1+set}" || ! -d "$1" ]]; then 30 | echo "Usage: $0 ..." >&2 31 | exit 1 32 | fi 33 | 34 | dir="$1" 35 | shift 36 | 37 | cd "$(dirname "$0")/.." 38 | 39 | i=0 40 | while true; do 41 | echo 42 | echo "$i" 43 | 44 | rm -rf .build 45 | shuffle "$dir" 46 | xcrun swift build "$@" 47 | i="$(($i + 1))" 48 | done 49 | -------------------------------------------------------------------------------- /Utils/swift-collections.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Utils/swift-collections.xcworkspace/xcshareddata/IDETemplateMacros.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | FILEHEADER 6 | ===----------------------------------------------------------------------===// 7 | // 8 | // This source file is part of the Swift Collections open source project 9 | // 10 | // Copyright (c) 2022 - 2024 Apple Inc. and the Swift project authors 11 | // Licensed under Apache License v2.0 with Runtime Library Exception 12 | // 13 | // See https://swift.org/LICENSE.txt for license information 14 | // 15 | //===----------------------------------------------------------------------===// 16 | 17 | 18 | -------------------------------------------------------------------------------- /Utils/swift-collections.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Utils/swift-collections.xcworkspace/xcshareddata/swiftpm/Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "pins" : [ 3 | { 4 | "identity" : "swift-argument-parser", 5 | "kind" : "remoteSourceControl", 6 | "location" : "https://github.com/apple/swift-argument-parser", 7 | "state" : { 8 | "revision" : "41982a3656a71c768319979febd796c6fd111d5c", 9 | "version" : "1.5.0" 10 | } 11 | }, 12 | { 13 | "identity" : "swift-system", 14 | "kind" : "remoteSourceControl", 15 | "location" : "https://github.com/apple/swift-system", 16 | "state" : { 17 | "revision" : "a34201439c74b53f0fd71ef11741af7e7caf01e1", 18 | "version" : "1.4.2" 19 | } 20 | } 21 | ], 22 | "version" : 2 23 | } 24 | -------------------------------------------------------------------------------- /Xcode/Collections.xcconfig: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | PRODUCT_NAME = Collections 13 | PRODUCT_BUNDLE_IDENTIFIER = org.swift.Collections 14 | 15 | SUPPORTED_PLATFORMS = macosx iphoneos iphonesimulator watchos watchsimulator appletvos appletvsimulator 16 | ARCHS = $(ARCHS_STANDARD) 17 | 18 | MACOSX_DEPLOYMENT_TARGET = 12.0 19 | IPHONEOS_DEPLOYMENT_TARGET = 15.0 20 | WATCHOS_DEPLOYMENT_TARGET = 8.0 21 | TVOS_DEPLOYMENT_TARGET = 15.0 22 | 23 | MARKETING_VERSION = 1.1 24 | 25 | CURRENT_PROJECT_VERSION = 1 26 | VERSIONING_SYSTEM = apple-generic 27 | VERSION_INFO_PREFIX = 28 | 29 | DYLIB_COMPATIBILITY_VERSION = $(CURRENT_PROJECT_VERSION) 30 | DYLIB_CURRENT_VERSION = $(CURRENT_PROJECT_VERSION) 31 | 32 | INSTALL_PATH = $(LOCAL_LIBRARY_DIR)/Frameworks 33 | SKIP_INSTALL = YES 34 | DYLIB_INSTALL_NAME_BASE = @rpath 35 | LD_RUNPATH_SEARCH_PATHS = $(inherited) @executable_path/../Frameworks @loader_path/Frameworks 36 | 37 | ENABLE_TESTABILITY = NO 38 | ENABLE_TESTABILITY[config=Debug] = YES 39 | 40 | GENERATE_INFOPLIST_FILE = YES 41 | 42 | CODE_SIGN_STYLE = Automatic 43 | CODE_SIGN_IDENTITY = - 44 | -------------------------------------------------------------------------------- /Xcode/Collections.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Xcode/Collections.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Xcode/Collections.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "18C3508F-98DB-45BE-9CFC-5159D79BA600", 5 | "name" : "Test Scheme Action", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | 13 | }, 14 | "testTargets" : [ 15 | { 16 | "parallelizable" : true, 17 | "target" : { 18 | "containerPath" : "container:Collections.xcodeproj", 19 | "identifier" : "7DE91B2D29CA6721004483EB", 20 | "name" : "CollectionsTests" 21 | } 22 | } 23 | ], 24 | "version" : 1 25 | } 26 | -------------------------------------------------------------------------------- /Xcode/CollectionsTests.xcconfig: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the Swift Collections open source project 4 | // 5 | // Copyright (c) 2023 - 2024 Apple Inc. and the Swift project authors 6 | // Licensed under Apache License v2.0 with Runtime Library Exception 7 | // 8 | // See https://swift.org/LICENSE.txt for license information 9 | // 10 | //===----------------------------------------------------------------------===// 11 | 12 | PRODUCT_NAME = CollectionsTests 13 | PRODUCT_BUNDLE_IDENTIFIER = org.swift.CollectionsTests 14 | 15 | SUPPORTED_PLATFORMS = macosx iphoneos iphonesimulator watchos watchsimulator appletvos appletvsimulator 16 | ARCHS = $(ARCHS_STANDARD) 17 | 18 | MACOSX_DEPLOYMENT_TARGET = 12.0 19 | IPHONEOS_DEPLOYMENT_TARGET = 15.0 20 | WATCHOS_DEPLOYMENT_TARGET = 8.0 21 | TVOS_DEPLOYMENT_TARGET = 15.0 22 | 23 | CURRENT_PROJECT_VERSION = 1 24 | MARKETING_VERSION = 1.0 25 | 26 | GENERATE_INFOPLIST_FILE = YES 27 | 28 | CODE_SIGN_STYLE = Automatic 29 | CODE_SIGN_IDENTITY = - 30 | 31 | ENABLE_TESTABILITY = NO 32 | 33 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) COLLECTIONS_RANDOMIZED_TESTING 34 | -------------------------------------------------------------------------------- /Xcode/README.md: -------------------------------------------------------------------------------- 1 | # Xcode build files 2 | 3 | The project file here is used to build a variant of this package with Xcode. The project file is a regular Xcode project that builds the code base using the COLLECTIONS_SINGLE_MODULE configuration, producing a single framework bundle. Builds settings are entirely configured via the provided xcconfig files. 4 | 5 | Beware! The contents of this directory are not source stable. They are provided as is, with no compatibility promises across package releases. Future versions of this package can arbitrarily change these files or remove them, without any advance notice. (This can include patch releases.) 6 | -------------------------------------------------------------------------------- /cmake/modules/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #[[ 2 | This source file is part of the Swift Collections Open Source Project 3 | 4 | Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 5 | Licensed under Apache License v2.0 with Runtime Library Exception 6 | 7 | See https://swift.org/LICENSE.txt for license information 8 | #]] 9 | 10 | set(SWIFT_COLLECTIONS_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftCollectionsExports.cmake) 11 | 12 | configure_file(SwiftCollectionsConfig.cmake.in 13 | ${CMAKE_CURRENT_BINARY_DIR}/SwiftCollectionsConfig.cmake) 14 | 15 | get_property(SWIFT_COLLECTIONS_EXPORTS GLOBAL PROPERTY SWIFT_COLLECTIONS_EXPORTS) 16 | export(TARGETS ${SWIFT_COLLECTIONS_EXPORTS} 17 | NAMESPACE SwiftCollections:: 18 | FILE ${SWIFT_COLLECTIONS_EXPORTS_FILE} 19 | EXPORT_LINK_INTERFACE_LIBRARIES) 20 | -------------------------------------------------------------------------------- /cmake/modules/PlatformInfo.cmake: -------------------------------------------------------------------------------- 1 | #[[ 2 | This source file is part of the Swift Collections Open Source Project 3 | 4 | Copyright (c) 2025 Apple Inc. and the Swift project authors 5 | Licensed under Apache License v2.0 with Runtime Library Exception 6 | 7 | See https://swift.org/LICENSE.txt for license information 8 | #]] 9 | 10 | set(target_info_cmd "${CMAKE_Swift_COMPILER}" -print-target-info) 11 | if(CMAKE_Swfit_COMPILER_TARGET) 12 | list(APPEND target_info_cmd -target ${CMAKE_Swift_COMPILER_TARGET}) 13 | endif() 14 | execute_process(COMMAND ${target_info_cmd} OUTPUT_VARIABLE target_info_json) 15 | message(CONFIGURE_LOG "Swift target info: ${target_info_cmd}\n" 16 | "${target_info_json}") 17 | 18 | if(NOT SwiftCollections_MODULE_TRIPLE) 19 | string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") 20 | set(SwiftCollections_MODULE_TRIPLE "${module_triple}" CACHE STRING "Triple used for installed swift{doc,module, interface} files") 21 | mark_as_advanced(SwiftCollections_MODULE_TRIPLE) 22 | 23 | message(CONFIGURE_LOG "Swift module triple: ${module_triple}") 24 | endif() 25 | -------------------------------------------------------------------------------- /cmake/modules/SwiftCollectionsConfig.cmake.in: -------------------------------------------------------------------------------- 1 | #[[ 2 | This source file is part of the Swift Collections Open Source Project 3 | 4 | Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors 5 | Licensed under Apache License v2.0 with Runtime Library Exception 6 | 7 | See https://swift.org/LICENSE.txt for license information 8 | #]] 9 | 10 | if(NOT TARGET SwiftCollections) 11 | include(@SWIFT_COLLECTIONS_EXPORTS_FILE@) 12 | endif() 13 | --------------------------------------------------------------------------------