Skip to content

Commit 5c0958b

Browse files
committed
Beginning of curation for Algorithms
1 parent bfcaaa8 commit 5c0958b

17 files changed

+396
-6
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.1
1+
// swift-tools-version:5.5
22
//===----------------------------------------------------------------------===//
33
//
44
// This source file is part of the Swift Algorithms open source project
@@ -21,6 +21,7 @@ let package = Package(
2121
],
2222
dependencies: [
2323
.package(url: "https://github.com/apple/swift-numerics.git", from: "1.0.0"),
24+
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"),
2425
],
2526
targets: [
2627
.target(

Sources/Algorithms/AdjacentPairs.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
extension Sequence {
13-
/// Creates a sequence of adjacent pairs of elements from this sequence.
13+
/// Returns a sequence of overlapping adjacent pairs of the elements of this
14+
/// sequence.
1415
///
1516
/// In the `AdjacentPairsSequence` returned by this method, the elements of
1617
/// the *i*th pair are the *i*th and *(i+1)*th elements of the underlying
@@ -24,14 +25,19 @@ extension Sequence {
2425
/// // Prints "(2, 3)"
2526
/// // Prints "(3, 4)"
2627
/// // Prints "(4, 5)"
28+
///
29+
/// The resulting sequence is empty when called on an empty or single-element
30+
/// sequence.
31+
///
32+
/// - Complexity: O(1)
2733
@inlinable
2834
public func adjacentPairs() -> AdjacentPairsSequence<Self> {
2935
AdjacentPairsSequence(base: self)
3036
}
3137
}
3238

3339
extension Collection {
34-
/// A collection of adjacent pairs of elements built from an underlying
40+
/// Returns a collection of overlapping adjacent pairs of the elements of this
3541
/// collection.
3642
///
3743
/// In an `AdjacentPairsCollection`, the elements of the *i*th pair are the
@@ -46,6 +52,11 @@ extension Collection {
4652
/// // Prints "(2, 3)"
4753
/// // Prints "(3, 4)"
4854
/// // Prints "(4, 5)"
55+
///
56+
/// The resulting collection is empty when called on an empty or
57+
/// single-element collection.
58+
///
59+
/// - Complexity: O(1)
4960
@inlinable
5061
public func adjacentPairs() -> AdjacentPairsCollection<Self> {
5162
AdjacentPairsCollection(base: self)

Sources/Algorithms/Combinations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ extension Collection {
289289
/// - Parameter k: The number of elements to include in each combination.
290290
///
291291
/// - Complexity: O(1) for random-access base collections. O(*n*) where *n*
292-
/// is the number of elements in the base collection, since
293-
/// `CombinationsSequence` accesses the `count` of the base collection.
292+
/// is the number of elements in the base collection, since
293+
/// `CombinationsSequence` accesses the `count` of the base collection.
294294
@inlinable
295295
public func combinations(ofCount k: Int) -> CombinationsSequence<Self> {
296296
precondition(k >= 0, "Can't have combinations with a negative number of elements.")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# ``Algorithms``
2+
3+
**Swift Algorithms** is an open-source package of sequence and collection algorithms, along with their related types.
4+
5+
## Overview
6+
7+
This library provides a bunch of new `Sequence` and `Collection` methods, plus some global functions.
8+
9+
## Topics
10+
11+
- <doc:CombinationsPermutations>
12+
- <doc:Slices>
13+
- <doc:Chunked>
14+
- <doc:Joining>
15+
- <doc:Extending>
16+
- <doc:Trim>
17+
- <doc:Sampling>
18+
- <doc:Selection>
19+
- <doc:Filtering>
20+
- <doc:Reductions>
21+
- <doc:Partitioning>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Chunked
2+
3+
Break collections into consecutive chunks by length or based on closure-based logic.
4+
5+
## Overview
6+
7+
## Topics
8+
9+
### Chunking a Collection by Count
10+
11+
- ``Swift/Collection/chunks(ofCount:)``
12+
- ``Swift/Collection/evenlyChunked(in:)``
13+
14+
### Chunking a Collection by Predicate
15+
16+
- ``Swift/Collection/chunked(on:)``
17+
- ``Swift/Collection/chunked(by:)``
18+
- ``Swift/LazySequenceProtocol/chunked(by:)``
19+
- ``Swift/LazySequenceProtocol/chunked(by:)``
20+
- ``Swift/LazySequenceProtocol/chunked(on:)``
21+
22+
### Supporting Types
23+
24+
- ``ChunkedByCollection``
25+
- ``ChunkedOnCollection``
26+
- ``ChunksOfCountCollection``
27+
- ``EvenlyChunkedCollection``
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Combinations and Permutations
2+
3+
You can call the `combinations(ofCount:)`, `permutations(ofCount:)`, and `uniquePermutations(ofCount:)` methods on any collection, or combine two collections using the `product(_:_:)` function.
4+
5+
## Overview
6+
7+
...
8+
9+
## Topics
10+
11+
### Combinations
12+
13+
- ``Swift/Collection/combinations(ofCount:)-26o4x``
14+
- ``Swift/Collection/combinations(ofCount:)-53jql``
15+
16+
### Permutations
17+
18+
- ``Swift/Collection/permutations(ofCount:)-7rc99``
19+
- ``Swift/Collection/permutations(ofCount:)-5zvhn``
20+
21+
### Unique Permutations
22+
23+
- ``Swift/Collection/uniquePermutations(ofCount:)-2extq``
24+
- ``Swift/Collection/uniquePermutations(ofCount:)-48r1k``
25+
26+
### Product
27+
28+
- ``product(_:_:)``
29+
30+
### Supporting Types
31+
32+
- ``CombinationsSequence``
33+
- ``PermutationsSequence``
34+
- ``UniquePermutationsSequence``
35+
- ``Product2Sequence``
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# DeprecatedScan
2+
3+
<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@-->
4+
5+
## Overview
6+
7+
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
8+
9+
## Topics
10+
11+
- ``Swift/Sequence/scan(_:)``
12+
- ``Swift/Sequence/scan(_:_:)``
13+
- ``Swift/Sequence/scan(into:_:)``
14+
- ``Swift/LazySequenceProtocol/scan(_:)``
15+
- ``Swift/LazySequenceProtocol/scan(_:_:)``
16+
- ``Swift/LazySequenceProtocol/scan(into:_:)``
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Extending a Collection
2+
3+
...
4+
5+
## Overview
6+
7+
...
8+
9+
## Topics
10+
11+
### Cycling a Collection
12+
13+
- ``Swift/Collection/cycled()``
14+
- ``Swift/Collection/cycled(times:)``
15+
16+
### Chaining Two Collections
17+
18+
- ``chain(_:_:)``
19+
20+
### Supporting Types
21+
22+
- ``Chain2Sequence``
23+
- ``CycledSequence``
24+
- ``CycledTimesCollection``
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Filtering
2+
3+
<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@-->
4+
5+
## Overview
6+
7+
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
8+
9+
## Topics
10+
11+
### Uniqueing Elements
12+
13+
- ``Swift/Sequence/uniqued()``
14+
- ``Swift/Sequence/uniqued(on:)``
15+
- ``Swift/LazySequenceProtocol/uniqued(on:)``
16+
17+
### Filtering out nil Elements
18+
19+
- ``Swift/Collection/compacted()``
20+
- ``Swift/Sequence/compacted()``
21+
22+
### Supporting Types
23+
24+
- ``UniquedSequence``
25+
- ``CompactedSequence``
26+
- ``CompactedCollection``
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Joining
2+
3+
...
4+
5+
## Overview
6+
7+
...
8+
9+
## Topics
10+
11+
### Joining by an Element
12+
13+
- ``Swift/Sequence/joined(by:)-6mrf9``
14+
- ``Swift/Sequence/joined(by:)-9hyaf``
15+
- ``Swift/Collection/joined(by:)-430ue``
16+
- ``Swift/LazySequenceProtocol/joined(by:)-3yjw0``
17+
- ``Swift/LazySequenceProtocol/joined(by:)-47xvy``
18+
19+
### Joining by a Collection
20+
21+
- ``Swift/Sequence/joined(by:)-62j1h``
22+
- ``Swift/Sequence/joined(by:)-9b108``
23+
- ``Swift/Collection/joined(by:)-28n3b``
24+
- ``Swift/LazySequenceProtocol/joined(by:)-4neii``
25+
- ``Swift/LazySequenceProtocol/joined(by:)-49xws``
26+
27+
### Interspersing Elements
28+
29+
- ``Swift/Sequence/interspersed(with:)``
30+
31+
### Supporting Types
32+
33+
- ``JoinedBySequence``
34+
- ``JoinedByCollection``
35+
- ``JoinedByClosureSequence``
36+
- ``JoinedByClosureCollection``
37+
- ``InterspersedSequence``
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Partitioning and Rotating
2+
3+
...
4+
5+
## Overview
6+
7+
...
8+
9+
## Topics
10+
11+
### Partition
12+
13+
- ``Swift/MutableCollection/partition(subrange:by:)-5vdh7``
14+
- ``Swift/MutableCollection/partition(subrange:by:)-4gpqz``
15+
16+
### Stable Partition
17+
18+
- ``Swift/MutableCollection/stablePartition(by:)``
19+
- ``Swift/MutableCollection/stablePartition(subrange:by:)``
20+
- ``Swift/Sequence/partitioned(by:)``
21+
- ``Swift/Collection/partitioned(by:)``
22+
23+
### Finding a Partition Index
24+
25+
- ``Swift/Collection/partitioningIndex(where:)``
26+
27+
### Rotation
28+
29+
- ``Swift/MutableCollection/rotate(toStartAt:)-9fp48``
30+
- ``Swift/MutableCollection/rotate(toStartAt:)-2r55j``
31+
- ``Swift/MutableCollection/rotate(subrange:toStartAt:)-ov6a``
32+
- ``Swift/MutableCollection/rotate(subrange:toStartAt:)-5teoq``
33+
34+
### Reversing
35+
36+
- ``Swift/MutableCollection/reverse(subrange:)``
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Reductions
2+
3+
<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@-->
4+
5+
## Overview
6+
7+
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
8+
9+
## Topics
10+
11+
- ``Swift/Sequence/reductions(_:)``
12+
- ``Swift/Sequence/reductions(_:_:)``
13+
- ``Swift/Sequence/reductions(into:_:)``
14+
- ``Swift/LazySequenceProtocol/reductions(_:)``
15+
- ``Swift/LazySequenceProtocol/reductions(_:_:)``
16+
- ``Swift/LazySequenceProtocol/reductions(into:_:)``
17+
18+
### Supporting Types
19+
20+
- ``InclusiveReductionsSequence``
21+
- ``ExclusiveReductionsSequence``
22+
23+
### Deprecated Methods
24+
25+
- <doc:DeprecatedScan>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Random Sampling
2+
3+
Choose a specified number of random elements from a sequence or collection.
4+
5+
## Overview
6+
7+
...
8+
9+
## Topics
10+
11+
### Random Sampling
12+
13+
- ``Swift/Sequence/randomSample(count:)``
14+
- ``Swift/Collection/randomSample(count:)``
15+
- ``Swift/Collection/randomStableSample(count:)``
16+
17+
### Random Sampling with a Generator
18+
19+
- ``Swift/Sequence/randomSample(count:using:)``
20+
- ``Swift/Collection/randomSample(count:using:)``
21+
- ``Swift/Collection/randomStableSample(count:using:)``
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Selecting Collection Elements
2+
3+
<!--@START_MENU_TOKEN@-->Summary<!--@END_MENU_TOKEN@-->
4+
5+
## Overview
6+
7+
<!--@START_MENU_TOKEN@-->Text<!--@END_MENU_TOKEN@-->
8+
9+
## Topics
10+
11+
### Selecting Elements at an Interval
12+
13+
- ``Swift/Sequence/striding(by:)``
14+
- ``Swift/Collection/striding(by:)``
15+
16+
### Finding Minimum or Maximum Elements
17+
18+
- ``Swift/Sequence/min(count:)``
19+
- ``Swift/Collection/min(count:)``
20+
- ``Swift/Sequence/min(count:sortedBy:)``
21+
- ``Swift/Collection/min(count:sortedBy:)``
22+
- ``Swift/Sequence/max(count:)``
23+
- ``Swift/Collection/max(count:)``
24+
- ``Swift/Sequence/max(count:sortedBy:)``
25+
- ``Swift/Collection/max(count:sortedBy:)``
26+
27+
### Finding the Minimum and Maximum Elements Simulataneously
28+
29+
- ``Swift/Sequence/minAndMax()``
30+
- ``Swift/Sequence/minAndMax(by:)``
31+
32+
### Selecting the Suffix of a Collection
33+
34+
- ``Swift/BidirectionalCollection/suffix(while:)``
35+
36+
### Conditionally Finding the First Mapped Value
37+
38+
- ``Swift/Sequence/firstNonNil(_:)``
39+
40+
### Supporting Types
41+
42+
- ``StridingSequence``
43+
- ``StridingCollection``

0 commit comments

Comments
 (0)