Skip to content

Enable CI format check #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 5, 2025
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
16 changes: 14 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ on:
types: [opened, reopened, synchronize]

jobs:
validate_format_config:
name: Validate Format Config
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install apt dependencies
run: sudo apt-get -qq update && sudo apt-get -qq -y install curl
- name: Compare against swift-mmio swift-format config
run: |
curl -sL https://raw.githubusercontent.com/apple/swift-mmio/refs/heads/main/.swift-format -o .swift-format-mmio
diff .swift-format .swift-format-mmio

tests:
name: Test
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main

soundness:
name: Soundness
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
Expand All @@ -16,5 +30,3 @@ jobs:
license_header_check_enabled: false
# https://github.com/apple/swift-algorithms/issues/251
docs_check_enabled: false
# https://github.com/apple/swift-algorithms/issues/252
format_check_enabled: false
71 changes: 71 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"indentation" : {
"spaces" : 2
},
"indentConditionalCompilationBlocks" : false,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : false,
"lineBreakBeforeEachGenericRequirement" : false,
"lineLength" : 80,
"maximumBlankLines" : 1,
"multiElementCollectionTrailingCommas" : true,
"noAssignmentInExpressions" : {
"allowedFunctions" : [
"XCTAssertNoThrow"
]
},
"prioritizeKeepingFunctionOutputTogether" : false,
"respectsExistingLineBreaks" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : true,
"AlwaysUseLowerCamelCase" : true,
"AmbiguousTrailingClosureOverload" : false,
"BeginDocumentationCommentWithOneLineSummary" : true,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : true,
"NeverUseForceTry" : true,
"NeverUseImplicitlyUnwrappedOptionals" : true,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoPlaygroundLiterals" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"TypeNamesShouldBeCapitalized" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : true,
"UseLetInEveryBoundCaseVariable" : true,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : true,
"UseSynthesizedInitializer" : true,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : true
},
"spacesBeforeEndOfLineComments": 2,
"spacesAroundRangeFormationOperators" : false,
"tabWidth" : 2,
"version" : 1
}
38 changes: 19 additions & 19 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
import PackageDescription

let package = Package(
name: "swift-algorithms",
products: [
.library(
name: "Algorithms",
targets: ["Algorithms"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics.git", from: "1.0.0"),
],
targets: [
.target(
name: "Algorithms",
dependencies: [
.product(name: "RealModule", package: "swift-numerics"),
]),
.testTarget(
name: "SwiftAlgorithmsTests",
dependencies: ["Algorithms"]),
]
name: "swift-algorithms",
products: [
.library(
name: "Algorithms",
targets: ["Algorithms"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics.git", from: "1.0.0")
],
targets: [
.target(
name: "Algorithms",
dependencies: [
.product(name: "RealModule", package: "swift-numerics")
]),
.testTarget(
name: "SwiftAlgorithmsTests",
dependencies: ["Algorithms"]),
]
)
64 changes: 39 additions & 25 deletions Sources/Algorithms/AdjacentPairs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extension AdjacentPairsSequence: Sequence {
}

extension AdjacentPairsSequence: LazySequenceProtocol
where Base: LazySequenceProtocol {}
where Base: LazySequenceProtocol {}

/// A collection of adjacent pairs of elements built from an underlying
/// collection.
Expand All @@ -143,7 +143,8 @@ public struct AdjacentPairsCollection<Base: Collection> {
@inlinable
internal init(base: Base) {
self.base = base
self.secondBaseIndex = base.isEmpty
self.secondBaseIndex =
base.isEmpty
? base.endIndex
: base.index(after: base.startIndex)
}
Expand All @@ -154,7 +155,7 @@ extension AdjacentPairsCollection {
public struct Index: Comparable {
@usableFromInline
internal var first: Base.Index

@usableFromInline
internal var second: Base.Index

Expand All @@ -168,7 +169,7 @@ extension AdjacentPairsCollection {
public static func == (lhs: Index, rhs: Index) -> Bool {
lhs.first == rhs.first
}

@inlinable
public static func < (lhs: Index, rhs: Index) -> Bool {
lhs.first < rhs.first
Expand All @@ -183,7 +184,7 @@ extension AdjacentPairsCollection: Collection {
first: secondBaseIndex == base.endIndex ? base.endIndex : base.startIndex,
second: secondBaseIndex)
}

@inlinable
public var endIndex: Index {
Index(first: base.endIndex, second: base.endIndex)
Expand All @@ -207,20 +208,23 @@ extension AdjacentPairsCollection: Collection {
public func index(_ i: Index, offsetBy distance: Int) -> Index {
guard distance != 0 else { return i }

guard let result = distance > 0
? offsetForward(i, by: distance, limitedBy: endIndex)
: offsetBackward(i, by: -distance, limitedBy: startIndex)
guard
let result = distance > 0
? offsetForward(i, by: distance, limitedBy: endIndex)
: offsetBackward(i, by: -distance, limitedBy: startIndex)
else { fatalError("Index out of bounds") }
return result
}

@inlinable
public func index(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
_ i: Index,
offsetBy distance: Int,
limitedBy limit: Index
) -> Index? {
guard distance != 0 else { return i }
guard limit != i else { return nil }

if distance > 0 {
let limit = limit > i ? limit : endIndex
return offsetForward(i, by: distance, limitedBy: limit)
Expand All @@ -229,38 +233,48 @@ extension AdjacentPairsCollection: Collection {
return offsetBackward(i, by: -distance, limitedBy: limit)
}
}

@inlinable
internal func offsetForward(
_ i: Index, by distance: Int, limitedBy limit: Index
_ i: Index,
by distance: Int,
limitedBy limit: Index
) -> Index? {
assert(distance > 0)
assert(limit > i)

guard let newFirst = base.index(i.second, offsetBy: distance - 1, limitedBy: limit.first),
newFirst != base.endIndex

let newFirst = base.index(
i.second,
offsetBy: distance - 1,
limitedBy: limit.first)
guard let newFirst, newFirst != base.endIndex
else { return nil }

let newSecond = base.index(after: newFirst)

precondition(newSecond <= base.endIndex, "Can't advance beyond endIndex")
return newSecond == base.endIndex
? endIndex
: Index(first: newFirst, second: newSecond)
}

@inlinable
internal func offsetBackward(
_ i: Index, by distance: Int, limitedBy limit: Index
_ i: Index,
by distance: Int,
limitedBy limit: Index
) -> Index? {
assert(distance > 0)
assert(limit < i)

let offset = i == endIndex ? 0 : 1
guard let newSecond = base.index(
let newSecond = base.index(
i.first,
offsetBy: -(distance - offset),
limitedBy: limit.second)
guard let newSecond
else { return nil }

let newFirst = base.index(newSecond, offsetBy: -1)
precondition(newFirst >= base.startIndex, "Can't move before startIndex")
return Index(first: newFirst, second: newSecond)
Expand All @@ -282,12 +296,12 @@ extension AdjacentPairsCollection: Collection {
}

extension AdjacentPairsCollection: BidirectionalCollection
where Base: BidirectionalCollection
{
where Base: BidirectionalCollection {
@inlinable
public func index(before i: Index) -> Index {
precondition(i != startIndex, "Can't offset before startIndex")
let second = i == endIndex
let second =
i == endIndex
? base.index(before: base.endIndex)
: i.first
let first = base.index(before: second)
Expand All @@ -296,10 +310,10 @@ extension AdjacentPairsCollection: BidirectionalCollection
}

extension AdjacentPairsCollection: RandomAccessCollection
where Base: RandomAccessCollection {}
where Base: RandomAccessCollection {}

extension AdjacentPairsCollection: LazySequenceProtocol, LazyCollectionProtocol
where Base: LazySequenceProtocol {}
where Base: LazySequenceProtocol {}

extension AdjacentPairsCollection.Index: Hashable where Base.Index: Hashable {
@inlinable
Expand Down
Loading