Skip to content

Commit 7670fd4

Browse files
committed
Move Collection.partition(intoNumberOfBatches:) to SKSupport
This way we’ll be able to use it from the semantic indexer.
1 parent cbc51ef commit 7670fd4

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

Sources/SKSupport/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_library(SKSupport STATIC
55
BuildConfiguration.swift
66
ByteString.swift
77
Collection+Only.swift
8+
Collection+PartitionIntoBatches.swift
89
Connection+Send.swift
910
dlopen.swift
1011
DocumentURI+CustomLogStringConvertible.swift
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 extension Collection {
14+
/// Partition the elements of the collection into `numberOfBatches` roughly equally sized batches.
15+
///
16+
/// Elements are assigned to the batches round-robin. This ensures that elements that are close to each other in the
17+
/// original collection end up in different batches. This is important because eg. test files will live close to each
18+
/// other in the file system and test scanning wants to scan them in different batches so we don't end up with one
19+
/// batch only containing source files and the other only containing test files.
20+
func partition(intoNumberOfBatches numberOfBatches: Int) -> [[Element]] {
21+
var batches: [[Element]] = Array(
22+
repeating: {
23+
var batch: [Element] = []
24+
batch.reserveCapacity(self.count / numberOfBatches)
25+
return batch
26+
}(),
27+
count: numberOfBatches
28+
)
29+
30+
for (index, element) in self.enumerated() {
31+
batches[index % numberOfBatches].append(element)
32+
}
33+
return batches.filter { !$0.isEmpty }
34+
}
35+
}

Sources/SourceKitLSP/Swift/SyntacticTestIndex.swift

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -211,27 +211,3 @@ actor SyntacticTestIndex {
211211
return await readTask.value
212212
}
213213
}
214-
215-
fileprivate extension Collection {
216-
/// Partition the elements of the collection into `numberOfBatches` roughly equally sized batches.
217-
///
218-
/// Elements are assigned to the batches round-robin. This ensures that elements that are close to each other in the
219-
/// original collection end up in different batches. This is important because eg. test files will live close to each
220-
/// other in the file system and test scanning wants to scan them in different batches so we don't end up with one
221-
/// batch only containing source files and the other only containing test files.
222-
func partition(intoNumberOfBatches numberOfBatches: Int) -> [[Element]] {
223-
var batches: [[Element]] = Array(
224-
repeating: {
225-
var batch: [Element] = []
226-
batch.reserveCapacity(self.count / numberOfBatches)
227-
return batch
228-
}(),
229-
count: numberOfBatches
230-
)
231-
232-
for (index, element) in self.enumerated() {
233-
batches[index % numberOfBatches].append(element)
234-
}
235-
return batches.filter { !$0.isEmpty }
236-
}
237-
}

0 commit comments

Comments
 (0)