Skip to content

Commit d3a68aa

Browse files
committedSep 24, 2016
Improve collection protocol conformances.
List now conforms to RandomAccessCollection. Map and SortedSet now conforms to BidirectionalCollection.
1 parent 59089a6 commit d3a68aa

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed
 

‎Sources/List.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
/// - Note: `List` implements all formal requirements of `CollectionType`, but it violates the semantic requirement
2323
/// that indexing has O(1) complexity: subscripting a `List` costs `O(log(count))`, since it requires an offset
2424
/// lookup in the B-tree. However, B-trees for typical element sizes and counts are extremely shallow
25-
/// (~5-10 levels for a billion elements), so in practice offset lookup behaves more like `O(1)` than `O(log(count))`.
25+
/// (less than 5 levels for a billion elements), so with practical element counts, offset lookup typically
26+
/// behaves more like `O(1)` than `O(log(count))`.
2627
///
2728
public struct List<Element> {
2829
internal typealias Tree = BTree<EmptyKey, Element>
@@ -78,10 +79,11 @@ extension List: CustomDebugStringConvertible {
7879
}
7980
}
8081

81-
extension List: MutableCollection, BidirectionalCollection {
82+
extension List: MutableCollection, RandomAccessCollection {
8283
//MARK: CollectionType
8384

8485
public typealias Index = Int
86+
public typealias Indices = CountableRange<Int>
8587
public typealias Iterator = BTreeValueIterator<Element>
8688
public typealias SubSequence = List<Element>
8789

‎Sources/Map.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ extension Map: CustomDebugStringConvertible {
100100
}
101101
}
102102

103-
extension Map: Collection {
103+
extension Map: BidirectionalCollection {
104104
//MARK: CollectionType
105105

106106
public typealias Index = BTreeIndex<Key, Value>

‎Sources/SortedSet.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension SortedSet {
5959
}
6060
}
6161

62-
extension SortedSet: Collection {
62+
extension SortedSet: BidirectionalCollection {
6363
//MARK: CollectionType
6464

6565
public typealias Index = BTreeIndex<Element, Void>

2 commit comments

Comments
 (2)

natecook1000 commented on Sep 24, 2016

@natecook1000

Nice 👍

SortedSet could benefit from providing _customContainsEquatableElement and _customIndexOfEquatableElement instead of contains(_:) and index(of:), so that it will still have good performance in a generic context.

lorentey commented on Sep 25, 2016

@lorentey
CollaboratorAuthor

I try very hard not to use any underscored API from stdlib — they are private implementation detail that stdlib engineers can (and do) change at whim. On the other hand, the forbidden fruit looks so sweet; I miss out on a lot of optimization opportunities by taking the high road. :-(

Perhaps these could be added hidden behind an optional conditional compilation switch that is disabled by default. (-DTurbo?)

Please sign in to comment.