Skip to content

Commit 9f33b02

Browse files
committed
Use Sequence.Element rather than Sequence.Iterator.Element
1 parent 06d6447 commit 9f33b02

15 files changed

+44
-45
lines changed

Sources/BTreeBuilder.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension BTree {
1717
/// - Complexity: O(count * log(`count`))
1818
/// - SeeAlso: `init(sortedElements:order:fillFactor:)` for a (faster) variant that can be used if the sequence is already sorted.
1919
public init<S: Sequence>(_ elements: S, dropDuplicates: Bool = false, order: Int? = nil)
20-
where S.Iterator.Element == Element {
20+
where S.Element == Element {
2121
let order = order ?? Node.defaultOrder
2222
self.init(Node(order: order))
2323
withCursorAtEnd { cursor in
@@ -47,7 +47,7 @@ extension BTree {
4747
/// If not specified, a value of 1.0 is used, i.e., nodes will be loaded with as many elements as possible.
4848
/// - Complexity: O(count)
4949
/// - SeeAlso: `init(elements:order:fillFactor:)` for a (slower) unsorted variant.
50-
public init<S: Sequence>(sortedElements elements: S, dropDuplicates: Bool = false, order: Int? = nil, fillFactor: Double = 1) where S.Iterator.Element == Element {
50+
public init<S: Sequence>(sortedElements elements: S, dropDuplicates: Bool = false, order: Int? = nil, fillFactor: Double = 1) where S.Element == Element {
5151
var iterator = elements.makeIterator()
5252
self.init(order: order ?? Node.defaultOrder, fillFactor: fillFactor, dropDuplicates: dropDuplicates, next: { iterator.next() })
5353
}

Sources/BTreeCursor.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public final class BTreeCursor<Key: Comparable, Value> {
500500
///
501501
/// - Requires: `self.isValid` and `elements` is sorted by key.
502502
/// - Complexity: O(log(`count`) + *c*), where *c* is the number of elements in the sequence.
503-
public func insert<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
503+
public func insert<S: Sequence>(_ elements: S) where S.Element == Element {
504504
insertWithoutCloning(BTree(sortedElements: elements).root)
505505
}
506506

Sources/BTreeMerger.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ extension BTree {
209209
///
210210
/// - Requires: `sortedKeys` is sorted in ascending order.
211211
/// - Complexity: O(*n* + `self.count`), where *n* is the number of keys in `sortedKeys`.
212-
public func subtracting<S: Sequence>(sortedKeys: S, by strategy: BTreeMatchingStrategy) -> BTree where S.Iterator.Element == Key {
212+
public func subtracting<S: Sequence>(sortedKeys: S, by strategy: BTreeMatchingStrategy) -> BTree where S.Element == Key {
213213
if self.isEmpty { return self }
214214

215215
var b = BTreeBuilder<Key, Value>(order: self.order)
@@ -251,7 +251,7 @@ extension BTree {
251251
///
252252
/// - Requires: `sortedKeys` is sorted in ascending order.
253253
/// - Complexity: O(*n* + `self.count`), where *n* is the number of keys in `sortedKeys`.
254-
public func intersection<S: Sequence>(sortedKeys: S, by strategy: BTreeMatchingStrategy) -> BTree where S.Iterator.Element == Key {
254+
public func intersection<S: Sequence>(sortedKeys: S, by strategy: BTreeMatchingStrategy) -> BTree where S.Element == Key {
255255
if self.isEmpty { return self }
256256

257257
var b = BTreeBuilder<Key, Value>(order: self.order)

Sources/BTreeNode.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal let bTreeNodeSize = 16383
2727
/// A node in an in-memory B-tree data structure, efficiently mapping `Comparable` keys to arbitrary values.
2828
/// Iterating over the elements in a B-tree returns them in ascending order of their keys.
2929
internal final class BTreeNode<Key: Comparable, Value> {
30+
typealias Iterator = BTreeIterator<Key, Value>
3031
typealias Element = Iterator.Element
3132
typealias Node = BTreeNode<Key, Value>
3233

@@ -131,8 +132,6 @@ extension BTreeNode {
131132
//MARK: Sequence
132133

133134
extension BTreeNode: Sequence {
134-
typealias Iterator = BTreeIterator<Key, Value>
135-
136135
var isEmpty: Bool { return count == 0 }
137136

138137
func makeIterator() -> Iterator {

Sources/List.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension List {
4747
/// Initialize a new list from the given elements.
4848
///
4949
/// - Complexity: O(*n*) where *n* is the number of elements in the sequence.
50-
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
50+
public init<S: Sequence>(_ elements: S) where S.Element == Element {
5151
self.init(Tree(sortedElements: elements.lazy.map { (EmptyKey(), $0) }))
5252
}
5353
}
@@ -235,8 +235,8 @@ extension List {
235235
/// Return an `Array` containing the concatenated results of mapping `transform` over `self`.
236236
///
237237
/// - Complexity: O(`result.count`)
238-
public func flatMap<S: Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Iterator.Element] {
239-
var result: [S.Iterator.Element] = []
238+
public func flatMap<S: Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Element] {
239+
var result: [S.Element] = []
240240
try self.forEach { element in
241241
result.append(contentsOf: try transform(element))
242242
}
@@ -398,7 +398,7 @@ extension List {
398398
/// Append the contents of `elements` to the end of this list.
399399
///
400400
/// - Complexity: O(log(`count`) + *n*) where *n* is the number of elements in the sequence.
401-
public mutating func append<S: Sequence>(contentsOf elements: S) where S.Iterator.Element == Element {
401+
public mutating func append<S: Sequence>(contentsOf elements: S) where S.Element == Element {
402402
if let list = elements as? List<Element> {
403403
append(contentsOf: list)
404404
return
@@ -420,7 +420,7 @@ extension List {
420420
/// Insert the contents of `elements` into this list starting at `index`.
421421
///
422422
/// - Complexity: O(log(`self.count`) + *n*) where *n* is the number of elements inserted.
423-
public mutating func insert<S: Sequence>(contentsOf elements: S, at index: Int) where S.Iterator.Element == Element {
423+
public mutating func insert<S: Sequence>(contentsOf elements: S, at index: Int) where S.Element == Element {
424424
if let list = elements as? List<Element> {
425425
insert(contentsOf: list, at: index)
426426
return
@@ -534,7 +534,7 @@ extension List: RangeReplaceableCollection {
534534
/// Replace elements in `range` with `elements`.
535535
///
536536
/// - Complexity: O(log(`count`) + `max(range.count, elements.count)`)
537-
public mutating func replaceSubrange<C: Collection>(_ range: Range<Int>, with elements: C) where C.Iterator.Element == Element {
537+
public mutating func replaceSubrange<C: Collection>(_ range: Range<Int>, with elements: C) where C.Element == Element {
538538
precondition(range.lowerBound >= 0 && range.upperBound <= count)
539539
if let list = elements as? List<Element> {
540540
replaceSubrange(range, with: list)

Sources/Map.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extension Map {
5353
/// If the sequence contains elements with duplicate keys, only the last element is kept in the map.
5454
///
5555
/// - Complexity: O(*n* * log(*n*)) where *n* is the number of items in `elements`.
56-
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
56+
public init<S: Sequence>(_ elements: S) where S.Element == Element {
5757
self.tree = Tree(elements, dropDuplicates: true)
5858
}
5959

@@ -62,7 +62,7 @@ extension Map {
6262
/// If the sequence contains elements with duplicate keys, only the last element is kept in the map.
6363
///
6464
/// - Complexity: O(*n*) where *n* is the number of items in `elements`.
65-
public init<S: Sequence>(sortedElements elements: S) where S.Iterator.Element == Element {
65+
public init<S: Sequence>(sortedElements elements: S) where S.Element == Element {
6666
self.tree = Tree(sortedElements: elements, dropDuplicates: true)
6767
}
6868
}
@@ -258,8 +258,8 @@ extension Map {
258258
/// Return an `Array` containing the concatenated results of mapping `transform` over `self`.
259259
///
260260
/// - Complexity: O(`count`)
261-
public func flatMap<S: Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Iterator.Element] {
262-
var result: [S.Iterator.Element] = []
261+
public func flatMap<S: Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Element] {
262+
var result: [S.Element] = []
263263
try self.forEach { element in
264264
result.append(contentsOf: try transform(element))
265265
}
@@ -546,7 +546,7 @@ extension Map {
546546
/// Return a map that contains all elements in `self` whose keys are in `keys`.
547547
///
548548
/// - Complexity: O(*n* * log(`count`)) where *n* is the number of keys in `keys`.
549-
public func including<S: Sequence>(_ keys: S) -> Map where S.Iterator.Element == Key {
549+
public func including<S: Sequence>(_ keys: S) -> Map where S.Element == Key {
550550
return including(SortedSet(keys))
551551
}
552552

@@ -560,7 +560,7 @@ extension Map {
560560
/// Return a map that contains all elements in `self` whose keys are not in `keys`.
561561
///
562562
/// - Complexity: O(*n* * log(`count`)) where *n* is the number of keys in `keys`.
563-
public func excluding<S: Sequence>(_ keys: S) -> Map where S.Iterator.Element == Key {
563+
public func excluding<S: Sequence>(_ keys: S) -> Map where S.Element == Key {
564564
return excluding(SortedSet(keys))
565565
}
566566
}

Sources/SortedBag.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ extension SortedBag {
5353
/// If the sequence contains duplicate items, all of them are kept, in the same order.
5454
///
5555
/// - Complexity: O(*n* * log(*n*)), where *n* is the number of items in the sequence.
56-
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
56+
public init<S: Sequence>(_ elements: S) where S.Element == Element {
5757
self.init(Tree(sortedElements: elements.sorted().lazy.map { ($0, ()) }, dropDuplicates: false))
5858
}
5959

6060
/// Create a bag from a sorted finite sequence of items.
6161
/// If the sequence contains duplicate items, all of them are kept.
6262
///
6363
/// - Complexity: O(*n*), where *n* is the number of items in the sequence.
64-
public init<S: Sequence>(sortedElements elements: S) where S.Iterator.Element == Element {
64+
public init<S: Sequence>(sortedElements elements: S) where S.Element == Element {
6565
self.init(Tree(sortedElements: elements.lazy.map { ($0, ()) }, dropDuplicates: false))
6666
}
6767

@@ -256,7 +256,7 @@ extension SortedBag {
256256
}
257257

258258
/// Return an `Array` containing the concatenated results of mapping `transform` over `self`.
259-
public func flatMap<S : Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Iterator.Element] {
259+
public func flatMap<S : Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Element] {
260260
return try tree.flatMap { try transform($0.0) }
261261
}
262262

Sources/SortedSet.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ extension SortedSet {
4242
/// If the sequence contains duplicate items, only the last instance will be kept in the set.
4343
///
4444
/// - Complexity: O(*n* * log(*n*)), where *n* is the number of items in the sequence.
45-
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
45+
public init<S: Sequence>(_ elements: S) where S.Element == Element {
4646
self.init(Tree(sortedElements: elements.sorted().lazy.map { ($0, ()) }, dropDuplicates: true))
4747
}
4848

4949
/// Create a set from a sorted finite sequence of items.
5050
/// If the sequence contains duplicate items, only the last instance will be kept in the set.
5151
///
5252
/// - Complexity: O(*n*), where *n* is the number of items in the sequence.
53-
public init<S: Sequence>(sortedElements elements: S) where S.Iterator.Element == Element {
53+
public init<S: Sequence>(sortedElements elements: S) where S.Element == Element {
5454
self.init(Tree(sortedElements: elements.lazy.map { ($0, ()) }, dropDuplicates: true))
5555
}
5656

@@ -245,7 +245,7 @@ extension SortedSet {
245245
}
246246

247247
/// Return an `Array` containing the concatenated results of mapping `transform` over `self`.
248-
public func flatMap<S : Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Iterator.Element] {
248+
public func flatMap<S : Sequence>(_ transform: (Element) throws -> S) rethrows -> [S.Element] {
249249
return try tree.flatMap { try transform($0.0) }
250250
}
251251

Tests/BTreeTests/BTreeBuilderTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BTreeBuilderTests: XCTestCase {
1515
typealias Tree = BTree<Int, String>
1616
typealias Element = (Int, String)
1717

18-
func elements<S: Sequence>(_ range: S) -> [Element] where S.Iterator.Element == Int {
18+
func elements<S: Sequence>(_ range: S) -> [Element] where S.Element == Int {
1919
return range.map { ($0, String($0)) }
2020
}
2121

Tests/BTreeTests/BTreeComparisonTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private typealias Node = BTreeNode<Int, Void>
1414
private typealias Tree = BTree<Int, Void>
1515
private typealias Element = (Int, Void)
1616

17-
private func makeTree<S: Sequence>(_ s: S, order: Int = 5, keysPerNode: Int? = nil) -> Tree where S.Iterator.Element == Int {
17+
private func makeTree<S: Sequence>(_ s: S, order: Int = 5, keysPerNode: Int? = nil) -> Tree where S.Element == Int {
1818
var b = Builder(order: order, keysPerNode: keysPerNode ?? order - 1)
1919
for i in s {
2020
b.append((i, ()))

Tests/BTreeTests/BTreeMergeTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BTreeMergeTests: XCTestCase {
2323
return Tree(order: 5)
2424
}
2525

26-
func makeTree<S: Sequence>(_ s: S, order: Int = 5, keysPerNode: Int? = nil) -> Tree where S.Iterator.Element == Int {
26+
func makeTree<S: Sequence>(_ s: S, order: Int = 5, keysPerNode: Int? = nil) -> Tree where S.Element == Int {
2727
var b = Builder(order: order, keysPerNode: keysPerNode ?? order - 1)
2828
for i in s.sorted() {
2929
b.append((i, ()))

Tests/BTreeTests/BTreeNodeTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class BTreeNodeTests: XCTestCase {
440440
let tree = BTree<Int, String>(order: 5)
441441
return tree.root
442442
}
443-
func createNode<S: Sequence>(_ keys: S) -> Node where S.Iterator.Element == Int {
443+
func createNode<S: Sequence>(_ keys: S) -> Node where S.Element == Int {
444444
let elements = keys.map { ($0, String($0)) }
445445
let tree = BTree(sortedElements: elements, order: 5)
446446
return tree.root

Tests/BTreeTests/BTreeTestSupport.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ struct DictionaryBag<Element: Hashable>: Collection {
272272

273273
init() {}
274274

275-
init<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
275+
init<S: Sequence>(_ elements: S) where S.Element == Element {
276276
self.init()
277277
self.formUnion(elements)
278278
}
@@ -319,13 +319,13 @@ struct DictionaryBag<Element: Hashable>: Collection {
319319
}
320320
}
321321

322-
mutating func subtract<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
322+
mutating func subtract<S: Sequence>(_ elements: S) where S.Element == Element {
323323
for element in elements {
324324
self.remove(element)
325325
}
326326
}
327327

328-
mutating func subtractAll<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
328+
mutating func subtractAll<S: Sequence>(_ elements: S) where S.Element == Element {
329329
for element in elements {
330330
if let c = bag[element] {
331331
bag[element] = nil
@@ -334,25 +334,25 @@ struct DictionaryBag<Element: Hashable>: Collection {
334334
}
335335
}
336336

337-
func subtracting<S: Sequence>(_ elements: S) -> DictionaryBag where S.Iterator.Element == Element {
337+
func subtracting<S: Sequence>(_ elements: S) -> DictionaryBag where S.Element == Element {
338338
var bag = self
339339
bag.subtract(elements)
340340
return bag
341341
}
342342

343-
func subtractingAll<S: Sequence>(_ elements: S) -> DictionaryBag where S.Iterator.Element == Element {
343+
func subtractingAll<S: Sequence>(_ elements: S) -> DictionaryBag where S.Element == Element {
344344
var bag = self
345345
bag.subtractAll(elements)
346346
return bag
347347
}
348348

349-
mutating func formUnion<S: Sequence>(_ elements: S) where S.Iterator.Element == Element {
349+
mutating func formUnion<S: Sequence>(_ elements: S) where S.Element == Element {
350350
for element in elements {
351351
self.insert(element)
352352
}
353353
}
354354

355-
func union<S: Sequence>(_ elements: S) -> DictionaryBag where S.Iterator.Element == Element {
355+
func union<S: Sequence>(_ elements: S) -> DictionaryBag where S.Element == Element {
356356
var bag = self
357357
bag.formUnion(elements)
358358
return bag

Tests/BTreeTests/ListTests.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class ListTests: XCTestCase {
282282
assertEqualElements(l1, 0..<10)
283283

284284
let l3: List<Int> = [10, 11, 12, 13, 14]
285-
func appendAsSequence<E, S: Sequence>(_ list: inout List<E>, _ elements: S) where S.Iterator.Element == E {
285+
func appendAsSequence<E, S: Sequence>(_ list: inout List<E>, _ elements: S) where S.Element == E {
286286
list.append(contentsOf: elements)
287287
}
288288
appendAsSequence(&l1, l3)
@@ -311,7 +311,7 @@ class ListTests: XCTestCase {
311311
assertEqualElements(list, 0..<5)
312312

313313
var copy = list
314-
func insertAsSequence<E, S: Sequence>(_ list: inout List<E>, _ elements: S, at index: Int) where S.Iterator.Element == E {
314+
func insertAsSequence<E, S: Sequence>(_ list: inout List<E>, _ elements: S, at index: Int) where S.Element == E {
315315
list.insert(contentsOf: elements, at: index)
316316
}
317317
insertAsSequence(&copy, l, at: copy.count)
@@ -439,7 +439,7 @@ class ListTests: XCTestCase {
439439
list.replaceSubrange(1..<3, with: [50, 51, 52, 53, 54, 55])
440440
assertEqualElements(list, [0, 50, 51, 52, 53, 54, 55, 20, 30, 8, 9])
441441

442-
func replaceAsSequence<E, C: Collection>(_ list: inout List<E>, range: CountableRange<Int>, with elements: C) where C.Iterator.Element == E {
442+
func replaceAsSequence<E, C: Collection>(_ list: inout List<E>, range: CountableRange<Int>, with elements: C) where C.Element == E {
443443
list.replaceSubrange(range, with: elements)
444444
}
445445
replaceAsSequence(&list, range: 1 ..< 9, with: List(1 ..< 8))

Tests/BTreeTests/XCTest extensions.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ import Foundation
1111
import XCTest
1212
@testable import BTree
1313

14-
func assertEqualElements<Element: Equatable, S1: Sequence, S2: Sequence>(_ a: S1, _ b: S2, file: StaticString = #file, line: UInt = #line) where S1.Iterator.Element == Element, S2.Iterator.Element == Element {
14+
func assertEqualElements<Element: Equatable, S1: Sequence, S2: Sequence>(_ a: S1, _ b: S2, file: StaticString = #file, line: UInt = #line) where S1.Element == Element, S2.Element == Element {
1515
let aa = Array(a)
1616
let ba = Array(b)
1717
if !aa.elementsEqual(ba) {
1818
XCTFail("XCTAssertEqual failed: \"\(aa)\" is not equal to \"\(ba)\"", file: file, line: line)
1919
}
2020
}
2121

22-
func assertEqualElements<T1: Equatable, T2: Equatable, S1: Sequence, S2: Sequence>(_ a: S1, _ b: S2, file: StaticString = #file, line: UInt = #line) where S1.Iterator.Element == (T1, T2), S2.Iterator.Element == (T1, T2) {
22+
func assertEqualElements<T1: Equatable, T2: Equatable, S1: Sequence, S2: Sequence>(_ a: S1, _ b: S2, file: StaticString = #file, line: UInt = #line) where S1.Element == (T1, T2), S2.Element == (T1, T2) {
2323
let aa = Array(a)
2424
let ba = Array(b)
2525
if !aa.elementsEqual(ba, by: { a, b in a.0 == b.0 && a.1 == b.1 }) {
2626
XCTFail("XCTAssertEqual failed: \"\(aa)\" is not equal to \"\(ba)\"", file: file, line: line)
2727
}
2828
}
2929

30-
func assertEqualElements<Element: Equatable, S1: Sequence, S2: Sequence, S1W: Sequence, S2W: Sequence>(_ a: S1, _ b: S2, element: Element.Type = Element.self, file: StaticString = #file, line: UInt = #line) where S1.Iterator.Element == S1W, S2.Iterator.Element == S2W, S1W.Iterator.Element == Element, S2W.Iterator.Element == Element {
30+
func assertEqualElements<Element: Equatable, S1: Sequence, S2: Sequence, S1W: Sequence, S2W: Sequence>(_ a: S1, _ b: S2, element: Element.Type = Element.self, file: StaticString = #file, line: UInt = #line) where S1.Element == S1W, S2.Element == S2W, S1W.Element == Element, S2W.Element == Element {
3131
let aa = a.map { Array($0) }
3232
let ba = b.map { Array($0) }
3333
if !aa.elementsEqual(ba, by: { $0.elementsEqual($1) }) {
@@ -41,14 +41,14 @@ extension BTree {
4141
assertEqualElements(self.map { $0.0 }, other.map { $0.0 }, file: file, line: line)
4242
}
4343

44-
internal func assertKeysEqual<S: Sequence>(_ s: S, file: StaticString = #file, line: UInt = #line) where S.Iterator.Element == Key {
44+
internal func assertKeysEqual<S: Sequence>(_ s: S, file: StaticString = #file, line: UInt = #line) where S.Element == Key {
4545
assertEqualElements(self.map { $0.0 }, s, file: file, line: line)
4646
}
4747
}
4848

4949
internal extension Sequence {
50-
func repeatEach(_ count: Int) -> Array<Iterator.Element> {
51-
var result: [Iterator.Element] = []
50+
func repeatEach(_ count: Int) -> Array<Element> {
51+
var result: [Element] = []
5252
result.reserveCapacity(count * underestimatedCount)
5353
for element in self {
5454
for _ in 0 ..< count {

0 commit comments

Comments
 (0)