Skip to content

Commit a954964

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

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

0 commit comments

Comments
 (0)