Skip to content

Commit dd819cc

Browse files
committed
Add docs for undocumented methods. Increase unit test coverage.
1 parent 41e7eee commit dd819cc

9 files changed

+422
-43
lines changed

Sources/BTree.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ extension BTree: BidirectionalCollection {
148148

149149
/// Returns the successor of the given index.
150150
///
151-
/// - Requires: `index` is valid of this tree and it is not equal to `endIndex`.
151+
/// - Requires: `index` is a valid index of this tree and it is not equal to `endIndex`.
152152
/// - Complexity: Amortized O(1).
153153
public func index(after index: Index) -> Index {
154154
checkIndex(index)
@@ -159,7 +159,7 @@ extension BTree: BidirectionalCollection {
159159

160160
/// Replaces the given index with its successor.
161161
///
162-
/// - Requires: `index` is valid of this tree and it is not equal to `endIndex`.
162+
/// - Requires: `index` is a valid index of this tree and it is not equal to `endIndex`.
163163
/// - Complexity: Amortized O(1).
164164
public func formIndex(after index: inout Index) {
165165
checkIndex(index)
@@ -168,7 +168,7 @@ extension BTree: BidirectionalCollection {
168168

169169
/// Returns the predecessor of the given index.
170170
///
171-
/// - Requires: `index` is valid of this tree and it is not equal to `startIndex`.
171+
/// - Requires: `index` is a valid index of this tree and it is not equal to `startIndex`.
172172
/// - Complexity: Amortized O(1).
173173
public func index(before index: Index) -> Index {
174174
checkIndex(index)
@@ -179,7 +179,7 @@ extension BTree: BidirectionalCollection {
179179

180180
/// Replaces the given index with its predecessor.
181181
///
182-
/// - Requires: `index` is valid of this tree and it is not equal to `startIndex`.
182+
/// - Requires: `index` is a valid index of this tree and it is not equal to `startIndex`.
183183
/// - Complexity: Amortized O(1).
184184
public func formIndex(before index: inout Index) {
185185
checkIndex(index)
@@ -212,7 +212,7 @@ extension BTree: BidirectionalCollection {
212212

213213
/// Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
214214
///
215-
/// - Requires: `index` must be a valid index in this tree. The operation must not advance the index beyond `endIndex` or before `startIndex`.
215+
/// - Requires: `index` and `limit` must be valid indices of this tree. The operation must not advance the index beyond `endIndex` or before `startIndex`.
216216
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the tree.
217217
public func index(_ i: Index, offsetBy n: Int, limitedBy limit: Index) -> Index? {
218218
checkIndex(i)
@@ -228,7 +228,7 @@ extension BTree: BidirectionalCollection {
228228

229229
/// Offsets the given index by the specified distance, or so that it equals the given limiting index.
230230
///
231-
/// - Requires: `index` must be a valid index in this tree. The operation must not advance the index beyond `endIndex` or before `startIndex`.
231+
/// - Requires: `index` and `limit` must be valid indices of this tree. The operation must not advance the index beyond `endIndex` or before `startIndex`.
232232
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the tree.
233233
@discardableResult
234234
public func formIndex(_ i: inout Index, offsetBy n: Int, limitedBy limit: Index) -> Bool {
@@ -240,6 +240,7 @@ extension BTree: BidirectionalCollection {
240240

241241
/// Returns the distance between two indices.
242242
///
243+
/// - Requires: `start` and `end` must be valid indices in this tree.
243244
/// - Complexity: O(1)
244245
public func distance(from start: Index, to end: Index) -> Int {
245246
checkIndex(start)

Sources/BTreeCursor.swift

+9-10
Original file line numberDiff line numberDiff line change
@@ -692,15 +692,14 @@ public final class BTreeCursor<Key: Comparable, Value> {
692692
split.suffix.insert(split.separator, atOffset: 0)
693693
return split.suffix
694694
}
695-
else {
696-
let (left, sep1, tail) = state.split()
697-
state = State(root: tail.root, offset: n - 1)
698-
var (mid, sep2, right) = state.split()
699-
state.invalidate()
700-
let j = Node.join(left: left.root, separator: sep2, right: right.root)
701-
state = State(root: j, offset: offset)
702-
mid.insert(sep1, atOffset: 0)
703-
return mid
704-
}
695+
696+
let (left, sep1, tail) = state.split()
697+
state = State(root: tail.root, offset: n - 1)
698+
var (mid, sep2, right) = state.split()
699+
state.invalidate()
700+
let j = Node.join(left: left.root, separator: sep2, right: right.root)
701+
state = State(root: j, offset: offset)
702+
mid.insert(sep1, atOffset: 0)
703+
return mid
705704
}
706705
}

Sources/List.swift

+27-8
Original file line numberDiff line numberDiff line change
@@ -134,41 +134,52 @@ extension List: MutableCollection, BidirectionalCollection {
134134
return Iterator(tree.makeIterator())
135135
}
136136

137+
/// Return the successor value of `index`. This method does not verify that the given index or the result are valid in this list.
137138
public func index(after index: Index) -> Index {
138139
return index + 1
139140
}
140141

142+
/// Replace `index` by its successor.
143+
/// This method does not verify that the given index or the result are valid in this list.
141144
public func formIndex(after index: inout Index) {
142145
index += 1
143146
}
144147

148+
/// Return the predecessor value of `index`.
149+
/// This method does not verify that the given index or the result are valid in this list.
145150
public func index(before index: Index) -> Index {
146151
return index - 1
147152
}
148153

154+
/// Replace `index` by its predecessor.
155+
/// This method does not verify that the given index or the result are valid in this list.
149156
public func formIndex(before index: inout Index) {
150157
index -= 1
151158
}
152159

160+
/// Add `n` to `i` and return the result.
161+
/// This method does not verify that the given index or the result are valid in this list.
153162
public func index(_ i: Index, offsetBy n: Int) -> Index {
154163
return i + n
155164
}
156165

166+
/// Add `n` to `i` in place.
167+
/// This method does not verify that the given index or the result are valid in this list.
168+
public func formIndex(_ i: inout Index, offsetBy n: Int) {
169+
i += n
170+
}
171+
172+
/// Add `n` to `i` and return the result, unless the result would exceed (or, in case of negative `n`, go under) `limit`, in which case return `nil`.
173+
/// This method does not verify that the given indices (or the resulting value) are valid in this list.
157174
public func index(_ i: Index, offsetBy n: Int, limitedBy limit: Index) -> Index? {
158175
if (n >= 0 && i + n > limit) || (n < 0 && i + n < limit) {
159176
return nil
160177
}
161178
return i + n
162179
}
163180

164-
public func distance(from start: Index, to end: Index) -> Int {
165-
return end - start
166-
}
167-
168-
public func formIndex(_ i: inout Index, offsetBy n: Int) {
169-
i += n
170-
}
171-
181+
/// Add `n` to `i` in place, unless the result would exceed (or, in case of negative `n`, go under) `limit`, in which case set `i` to `limit`.
182+
/// This method does not verify that the given indices (or the resulting value) are valid in this list.
172183
@discardableResult
173184
public func formIndex(_ i: inout Index, offsetBy n: Int, limitedBy limit: Index) -> Bool {
174185
if (n >= 0 && i + n > limit) || (n < 0 && i + n < limit) {
@@ -179,10 +190,18 @@ extension List: MutableCollection, BidirectionalCollection {
179190
return true
180191
}
181192

193+
/// Return the difference between `end` and `start`.
194+
/// This method does not verify that the given indices are valid in this list.
195+
public func distance(from start: Index, to end: Index) -> Int {
196+
return end - start
197+
}
198+
199+
/// Return the first element in this list, or `nil` if the list is empty.
182200
public var first: Element? {
183201
return tree.first?.1
184202
}
185203

204+
/// Return the last element in this list, or `nil` if the list is empty.
186205
public var last: Element? {
187206
return tree.last?.1
188207
}

Sources/Map.swift

+40
Original file line numberDiff line numberDiff line change
@@ -153,39 +153,79 @@ extension Map: Collection {
153153
return tree.makeIterator()
154154
}
155155

156+
/// Returns the successor of the given index.
157+
///
158+
/// - Requires: `index` is a valid index of this map and it is not equal to `endIndex`.
159+
/// - Complexity: Amortized O(1).
156160
public func index(after index: Index) -> Index {
157161
return tree.index(after: index)
158162
}
159163

164+
/// Replaces the given index with its successor.
165+
///
166+
/// - Requires: `index` is a valid index of this map and it is not equal to `endIndex`.
167+
/// - Complexity: Amortized O(1).
160168
public func formIndex(after index: inout Index) {
161169
tree.formIndex(after: &index)
162170
}
163171

172+
/// Returns the predecessor of the given index.
173+
///
174+
/// - Requires: `index` is a valid index of this map and it is not equal to `startIndex`.
175+
/// - Complexity: Amortized O(1).
164176
public func index(before index: Index) -> Index {
165177
return tree.index(before: index)
166178
}
167179

180+
/// Replaces the given index with its predecessor.
181+
///
182+
/// - Requires: `index` is a valid index of this map and it is not equal to `startIndex`.
183+
/// - Complexity: Amortized O(1).
168184
public func formIndex(before index: inout Index) {
169185
tree.formIndex(before: &index)
170186
}
171187

188+
/// Returns an index that is the specified distance from the given index.
189+
///
190+
/// - Requires: `index` must be a valid index of this map.
191+
/// If `n` is positive, it must not exceed the distance from `index` to `endIndex`.
192+
/// If `n` is negative, it must not be less than the distance from `index` to `startIndex`.
193+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the map.
172194
public func index(_ i: Index, offsetBy n: Int) -> Index {
173195
return tree.index(i, offsetBy: n)
174196
}
175197

198+
/// Offsets the given index by the specified distance.
199+
///
200+
/// - Requires: `index` must be a valid index of this map.
201+
/// If `n` is positive, it must not exceed the distance from `index` to `endIndex`.
202+
/// If `n` is negative, it must not be less than the distance from `index` to `startIndex`.
203+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the map.
176204
public func formIndex(_ i: inout Index, offsetBy n: Int) {
177205
tree.formIndex(&i, offsetBy: n)
178206
}
179207

208+
/// Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
209+
///
210+
/// - Requires: `index` and `limit` must be valid indices in this map. The operation must not advance the index beyond `endIndex` or before `startIndex`.
211+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the map.
180212
public func index(_ i: Index, offsetBy n: Int, limitedBy limit: Index) -> Index? {
181213
return tree.index(i, offsetBy: n, limitedBy: limit)
182214
}
183215

216+
/// Offsets the given index by the specified distance, or so that it equals the given limiting index.
217+
///
218+
/// - Requires: `index` and `limit` must be valid indices in this map. The operation must not advance the index beyond `endIndex` or before `startIndex`.
219+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the map.
184220
@discardableResult
185221
public func formIndex(_ i: inout Index, offsetBy n: Int, limitedBy limit: Index) -> Bool {
186222
return tree.formIndex(&i, offsetBy: n, limitedBy: limit)
187223
}
188224

225+
/// Returns the distance between two indices.
226+
///
227+
/// - Requires: `start` and `end` must be valid indices in this map.
228+
/// - Complexity: O(1)
189229
public func distance(from start: Index, to end: Index) -> Int {
190230
return tree.distance(from: start, to: end)
191231
}

Sources/SortedSet.swift

+66-10
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,79 @@ extension SortedSet: Collection {
111111
return Iterator(tree.makeIterator())
112112
}
113113

114+
/// Returns the successor of the given index.
115+
///
116+
/// - Requires: `index` is a valid index of this set and it is not equal to `endIndex`.
117+
/// - Complexity: Amortized O(1).
114118
public func index(after index: Index) -> Index {
115119
return tree.index(after: index)
116120
}
117121

122+
/// Replaces the given index with its successor.
123+
///
124+
/// - Requires: `index` is a valid index of this set and it is not equal to `endIndex`.
125+
/// - Complexity: Amortized O(1).
118126
public func formIndex(after index: inout Index) {
119127
tree.formIndex(after: &index)
120128
}
121129

130+
/// Returns the predecessor of the given index.
131+
///
132+
/// - Requires: `index` is a valid index of this set and it is not equal to `startIndex`.
133+
/// - Complexity: Amortized O(1).
122134
public func index(before index: Index) -> Index {
123135
return tree.index(before: index)
124136
}
125137

138+
/// Replaces the given index with its predecessor.
139+
///
140+
/// - Requires: `index` is a valid index of this set and it is not equal to `startIndex`.
141+
/// - Complexity: Amortized O(1).
126142
public func formIndex(before index: inout Index) {
127143
tree.formIndex(before: &index)
128144
}
129145

146+
/// Returns an index that is the specified distance from the given index.
147+
///
148+
/// - Requires: `index` must be a valid index of this set.
149+
/// If `n` is positive, it must not exceed the distance from `index` to `endIndex`.
150+
/// If `n` is negative, it must not be less than the distance from `index` to `startIndex`.
151+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the set.
130152
public func index(_ i: Index, offsetBy n: Int) -> Index {
131153
return tree.index(i, offsetBy: n)
132154
}
133155

156+
/// Offsets the given index by the specified distance.
157+
///
158+
/// - Requires: `index` must be a valid index of this set.
159+
/// If `n` is positive, it must not exceed the distance from `index` to `endIndex`.
160+
/// If `n` is negative, it must not be less than the distance from `index` to `startIndex`.
161+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the set.
134162
public func formIndex(_ i: inout Index, offsetBy n: Int) {
135163
tree.formIndex(&i, offsetBy: n)
136164
}
137165

166+
/// Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
167+
///
168+
/// - Requires: `index` and `limit` must be valid indices in this set. The operation must not advance the index beyond `endIndex` or before `startIndex`.
169+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the set.
138170
public func index(_ i: Index, offsetBy n: Int, limitedBy limit: Index) -> Index? {
139171
return tree.index(i, offsetBy: n, limitedBy: limit)
140172
}
141173

174+
/// Offsets the given index by the specified distance, or so that it equals the given limiting index.
175+
///
176+
/// - Requires: `index` and `limit` must be valid indices in this set. The operation must not advance the index beyond `endIndex` or before `startIndex`.
177+
/// - Complexity: O(log(*count*)) where *count* is the number of elements in the set.
142178
@discardableResult
143179
public func formIndex(_ i: inout Index, offsetBy n: Int, limitedBy limit: Index) -> Bool {
144180
return tree.formIndex(&i, offsetBy: n, limitedBy: limit)
145181
}
146182

183+
/// Returns the distance between two indices.
184+
///
185+
/// - Requires: `start` and `end` must be valid indices in this set.
186+
/// - Complexity: O(1)
147187
public func distance(from start: Index, to end: Index) -> Int {
148188
return tree.distance(from: start, to: end)
149189
}
@@ -152,6 +192,21 @@ extension SortedSet: Collection {
152192
extension SortedSet {
153193
//MARK: Offset-based access
154194

195+
/// Returns the offset of the element at `index`.
196+
///
197+
/// - Complexity: O(log(`count`))
198+
public func index(ofOffset offset: Int) -> Index {
199+
return tree.index(ofOffset: offset)
200+
}
201+
202+
/// Returns the index of the element at `offset`.
203+
///
204+
/// - Requires: `offset >= 0 && offset < count`
205+
/// - Complexity: O(log(`count`))
206+
public func offset(of index: Index) -> Int {
207+
return tree.offset(of: index)
208+
}
209+
155210
/// Returns the element at `offset` from the start of the set.
156211
///
157212
/// - Complexity: O(log(`count`))
@@ -449,27 +504,28 @@ extension SortedSet {
449504
extension SortedSet {
450505
//MARK: Insertion
451506

452-
/// Insert a member into the set.
507+
/// Insert a member into the set if it is not already present.
453508
///
509+
/// - Returns: `(true, newMember)` if `newMember` was not contained in the set.
510+
/// If an element equal to `newMember` was already contained in the set, the method returns `(false, oldMember)`,
511+
/// where `oldMember` is the element that was equal to `newMember`. In some cases, `oldMember` may be distinguishable
512+
/// from `newMember` by identity comparison or some other means.
454513
/// - Complexity: O(log(`count`))
455514
@discardableResult
456-
public mutating func insert(_ element: Element) -> (inserted: Bool, memberAfterInsert: Element) {
457-
if let old = tree.insertOrFind((element, ())) {
458-
return (false, old.0)
459-
}
460-
else {
461-
return (true, element)
515+
public mutating func insert(_ newMember: Element) -> (inserted: Bool, memberAfterInsert: Element) {
516+
guard let old = tree.insertOrFind((newMember, ())) else {
517+
return (true, newMember)
462518
}
519+
return (false, old.0)
463520
}
464521

465522
/// Inserts the given element into the set unconditionally.
466523
///
467524
/// If an element equal to `newMember` is already contained in the set,
468-
/// `newMember` replaces the existing element. In this example, an existing
469-
/// element is inserted into `classDays`, a set of days of the week.
525+
/// `newMember` replaces the existing element.
470526
///
471527
/// - Parameter newMember: An element to insert into the set.
472-
/// - Returns: The element equal to `newMember` that was originally in the set, if exists; otherwise, nil.
528+
/// - Returns: The element equal to `newMember` that was originally in the set, if exists; otherwise, `nil`.
473529
/// In some cases, the returned element may be distinguishable from `newMember` by identity
474530
/// comparison or some other means.
475531
public mutating func update(with newMember: Element) -> Element? {

0 commit comments

Comments
 (0)