@@ -227,14 +227,6 @@ extension CircularBuffer: Collection, MutableCollection {
227
227
// MARK: RandomAccessCollection implementation
228
228
extension CircularBuffer : RandomAccessCollection {
229
229
/// Returns the index offset by `distance` from `index`.
230
- @inlinable
231
- public func index( _ i: Index , offsetBy distance: Int ) -> Index {
232
- return . init( backingIndex: ( i. backingIndex &+ distance) & self . mask,
233
- backingCount: self . count,
234
- backingIndexOfHead: self . headBackingIndex)
235
- }
236
-
237
- /// Returns an index that is the specified distance from the given index.
238
230
///
239
231
/// The following example obtains an index advanced four positions from a
240
232
/// string's starting index and then prints the character at that position.
@@ -261,15 +253,30 @@ extension CircularBuffer: RandomAccessCollection {
261
253
/// - Complexity: O(1) if the collection conforms to
262
254
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the absolute
263
255
/// value of `distance`.
256
+ @inlinable
257
+ public func index( _ i: Index , offsetBy distance: Int ) -> Index {
258
+ return . init( backingIndex: ( i. backingIndex &+ distance) & self . mask,
259
+ backingCount: self . count,
260
+ backingIndexOfHead: self . headBackingIndex)
261
+ }
262
+
264
263
@inlinable
265
264
public subscript( bounds: Range < Index > ) -> SubSequence {
266
- precondition ( self . distance ( from: self . startIndex, to: bounds. lowerBound) >= 0 )
267
- precondition ( self . distance ( from: bounds. upperBound, to: self . endIndex) >= 0 )
265
+ get {
266
+ precondition ( self . distance ( from: self . startIndex, to: bounds. lowerBound) >= 0 )
267
+ precondition ( self . distance ( from: bounds. upperBound, to: self . endIndex) >= 0 )
268
268
269
- var newRing = self
270
- newRing. headBackingIndex = bounds. lowerBound. backingIndex
271
- newRing. tailBackingIndex = bounds. upperBound. backingIndex
272
- return newRing
269
+ var newRing = self
270
+ newRing. headBackingIndex = bounds. lowerBound. backingIndex
271
+ newRing. tailBackingIndex = bounds. upperBound. backingIndex
272
+ return newRing
273
+ }
274
+ set {
275
+ precondition ( self . distance ( from: self . startIndex, to: bounds. lowerBound) >= 0 )
276
+ precondition ( self . distance ( from: bounds. upperBound, to: self . endIndex) >= 0 )
277
+
278
+ self . replaceSubrange ( bounds, with: newValue)
279
+ }
273
280
}
274
281
}
275
282
0 commit comments