Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Scripts/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift Algorithms open source project
##
## Copyright (c) 2025 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
##
##===----------------------------------------------------------------------===##

# Move to the project root
cd "$(dirname "$0")" || exit
cd ..
echo "Formatting Swift sources in $(pwd)"

# Run the format / lint commands
git ls-files -z '*.swift' | xargs -0 swift format format --parallel --in-place
git ls-files -z '*.swift' | xargs -0 swift format lint --strict --parallel
7 changes: 4 additions & 3 deletions Sources/Algorithms/Chunked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,10 @@ extension EvenlyChunkedCollection: Collection {
@usableFromInline
internal var baseRange: Range<Base.Index>

/// The offset corresponding to the chunk at this position. The first chunk
/// has offset `0` and all other chunks have an offset `1` greater than the
/// previous.
/// The offset corresponding to the chunk at this position.
///
/// The first chunk has offset `0` and all other chunks have an offset
/// `1` greater than the previous.
@usableFromInline
internal var offset: Int

Expand Down
2 changes: 1 addition & 1 deletion Sources/Algorithms/Combinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension CombinationsSequence: Sequence {
@usableFromInline
internal var kRange: Range<Int>

/// Whether or not iteration is finished (`kRange` is empty)
/// Whether or not iteration is finished (`kRange` is empty).
@inlinable
internal var isFinished: Bool {
kRange.isEmpty
Expand Down
2 changes: 1 addition & 1 deletion Sources/Algorithms/Permutations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ extension PermutationsSequence: Sequence {
@usableFromInline
internal var kRange: Range<Int>

/// Whether or not iteration is finished (`kRange` is empty)
/// Whether or not iteration is finished (`kRange` is empty).
@inlinable
internal var isFinished: Bool {
kRange.isEmpty
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftAlgorithmsTests/PartitionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ final class PartitionTests: XCTestCase {
XCTAssertTrue(s0.1.isEmpty)
}

/// Test the example given in the `partitioned(by:)` documentation
/// Test the example given in the `partitioned(by:)` documentation.
func testPartitionedExample() throws {
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let (longNames, shortNames) = cast.partitioned(by: { $0.count < 5 })
Expand Down
21 changes: 11 additions & 10 deletions Tests/SwiftAlgorithmsTests/RotateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import XCTest
@testable import Algorithms

final class RotateTests: XCTestCase {
/// Tests the example given in `_reverse(subrange:until:)`’s documentation
/// Tests the example given in the `_reverse(subrange:until:)` documentation.
func testUnderscoreReverse() {
var input = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
Expand All @@ -33,56 +33,57 @@ final class RotateTests: XCTestCase {
XCTAssertEqual(upper, input.endIndex.advanced(by: -limit))
}

/// Tests the example given in `reverse(subrange:)`’s documentation
/// Tests the example given in the `reverse(subrange:)` documentation.
func testReverse() {
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
numbers.reverse(subrange: 0..<4)
XCTAssertEqual(numbers, [40, 30, 20, 10, 50, 60, 70, 80])
}

/// Tests `rotate(subrange:toStartAt:)` with an empty subrange
/// The order of elements are unchanged
/// Tests `rotate(subrange:toStartAt:)` with an empty subrange.
func testRotateEmptySubrange() {
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let oldStart = numbers.rotate(subrange: 3..<3, toStartAt: 3)
// The order of elements are unchanged
XCTAssertEqual(numbers, [10, 20, 30, 40, 50, 60, 70, 80])
XCTAssertEqual(numbers[oldStart], 40)
}

/// Tests `rotate(subrange:toStartAt:)` with an empty collection
/// Tests `rotate(subrange:toStartAt:)` with an empty collection.
func testRotateSubrangeOnEmptyCollection() {
var numbers: [Int] = []
let oldStart = numbers.rotate(subrange: 0..<0, toStartAt: 0)
XCTAssertEqual(numbers, [])
XCTAssertEqual(oldStart, numbers.startIndex)
}

/// Tests `rotate(subrange:toStartAt:)` with the full range of the collection
/// Tests `rotate(subrange:toStartAt:)` with the full range of the collection.
func testRotateFullRange() {
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let oldStart = numbers.rotate(subrange: 0..<8, toStartAt: 1)
XCTAssertEqual(numbers, [20, 30, 40, 50, 60, 70, 80, 10])
XCTAssertEqual(numbers[oldStart], 10)
}

/// Tests the example given in `rotate(subrange:toStartAt:)`’s documentation
/// Tests the example given in the `rotate(subrange:toStartAt:)`
/// documentation.
func testRotateSubrange() {
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let oldStart = numbers.rotate(subrange: 0..<4, toStartAt: 2)
XCTAssertEqual(numbers, [30, 40, 10, 20, 50, 60, 70, 80])
XCTAssertEqual(numbers[oldStart], 10)
}

/// Tests the example given in `rotate(toStartAt:)`’s documentation
/// Tests the example given in the `rotate(toStartAt:)` documentation.
func testRotateExample() {
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let oldStart = numbers.rotate(toStartAt: 3)
XCTAssertEqual(numbers, [40, 50, 60, 70, 80, 10, 20, 30])
XCTAssertEqual(numbers[oldStart], 10)
}

/// Tests `rotate(toStartAt:)` on collections of varying lengths, at different
/// starting points
/// Tests `rotate(toStartAt:)` on collections of varying lengths, at
/// different starting points.
func testRotate() {
for length in 0...15 {
let a = Array(0..<length)
Expand Down