1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// An automatically expanding ring buffer implementation backed by a `ContiguousArray`. Even though this implementation
/// will automatically expand if more elements than `initialCapacity` are stored, it's advantageous to prevent
/// expansions from happening frequently. Expansions will always force an allocation and a copy to happen.
public struct CircularBuffer<Element>: CustomStringConvertible {
@usableFromInline
internal var _buffer: ContiguousArray<Element?>
@usableFromInline
internal var headBackingIndex: Int
@usableFromInline
internal var tailBackingIndex: Int
@inlinable
internal var mask: Int {
return self._buffer.count &- 1
}
@inlinable
internal mutating func advanceHeadIdx(by: Int) {
self.headBackingIndex = indexAdvanced(index: self.headBackingIndex, by: by)
}
@inlinable
internal mutating func advanceTailIdx(by: Int) {
self.tailBackingIndex = indexAdvanced(index: self.tailBackingIndex, by: by)
}
@inlinable
internal func indexBeforeHeadIdx() -> Int {
return self.indexAdvanced(index: self.headBackingIndex, by: -1)
}
@inlinable
internal func indexBeforeTailIdx() -> Int {
return self.indexAdvanced(index: self.tailBackingIndex, by: -1)
}
@inlinable
internal func indexAdvanced(index: Int, by: Int) -> Int {
return (index &+ by) & self.mask
}
/// An opaque `CircularBuffer` index.
///
/// You may get indices offset from other indices by using `CircularBuffer.index(:offsetBy:)`,
/// `CircularBuffer.index(before:)`, or `CircularBuffer.index(after:)`.
///
/// - note: Every index is invalidated as soon as you perform a length-changing operating on the `CircularBuffer`
/// but remains valid when you replace one item by another using the subscript.
public struct Index: Comparable {
@usableFromInline var _backingIndex: UInt32
@usableFromInline var _backingCheck: _UInt24
@usableFromInline var isIndexGEQHeadIndex: Bool
@inlinable
internal var backingIndex: Int {
return Int(self._backingIndex)
}
@inlinable
internal init(backingIndex: Int, backingCount: Int, backingIndexOfHead: Int) {
self.isIndexGEQHeadIndex = backingIndex >= backingIndexOfHead
self._backingCheck = .max
self._backingIndex = UInt32(backingIndex)
debugOnly {
// if we can, we store the check for the backing here
self._backingCheck = backingCount < Int(_UInt24.max) ? _UInt24(UInt32(backingCount)) : .max
}
}
@inlinable
public static func == (lhs: Index, rhs: Index) -> Bool {
return lhs._backingIndex == rhs._backingIndex &&
lhs._backingCheck == rhs._backingCheck &&
lhs.isIndexGEQHeadIndex == rhs.isIndexGEQHeadIndex
}
@inlinable
public static func < (lhs: Index, rhs: Index) -> Bool {
if lhs.isIndexGEQHeadIndex && rhs.isIndexGEQHeadIndex {
return lhs.backingIndex < rhs.backingIndex
} else if lhs.isIndexGEQHeadIndex && !rhs.isIndexGEQHeadIndex {
return true
} else if !lhs.isIndexGEQHeadIndex && rhs.isIndexGEQHeadIndex {
return false
} else {
return lhs.backingIndex < rhs.backingIndex
}
}
@usableFromInline
internal func isValidIndex(for ring: CircularBuffer<Element>) -> Bool {
return self._backingCheck == _UInt24.max || Int(self._backingCheck) == ring.count
}
}
}
// MARK: Collection/MutableCollection implementation
extension CircularBuffer: Collection, MutableCollection {
public typealias Element = Element
public typealias Indices = DefaultIndices<CircularBuffer<Element>>
public typealias RangeType<Bound> = Range<Bound> where Bound: Strideable, Bound.Stride: SignedInteger
public typealias SubSequence = CircularBuffer<Element>
/// Returns the position immediately after the given index.
///
/// The successor of an index must be well defined. For an index `i` into a
/// collection `c`, calling `c.index(after: i)` returns the same index every
/// time.
///
/// - Parameter i: A valid index of the collection. `i` must be less than
/// `endIndex`.
/// - Returns: The index value immediately after `i`.
@inlinable
public func index(after: Index) -> Index {
return self.index(after, offsetBy: 1)
}
/// Returns the index before `index`.
@inlinable
public func index(before: Index) -> Index {
return self.index(before, offsetBy: -1)
}
/// Accesses the element at the specified index.
///
/// You can subscript `CircularBuffer` with any valid index other than the
/// `CircularBuffer`'s end index. The end index refers to the position one
/// past the last element of a collection, so it doesn't correspond with an
/// element.
///
/// - Parameter position: The position of the element to access. `position`
/// must be a valid index of the collection that is not equal to the
/// `endIndex` property.
///
/// - Complexity: O(1)
@inlinable
public subscript(position: Index) -> Element {
get {
assert(position.isValidIndex(for: self),
"illegal index used, index was for CircularBuffer with count \(position._backingCheck), " +
"but actual count is \(self.count)")
return self._buffer[position.backingIndex]!
}
set {
assert(position.isValidIndex(for: self),
"illegal index used, index was for CircularBuffer with count \(position._backingCheck), " +
"but actual count is \(self.count)")
self._buffer[position.backingIndex] = newValue
}
}
/// The position of the first element in a nonempty `CircularBuffer`.
///
/// If the `CircularBuffer` is empty, `startIndex` is equal to `endIndex`.
@inlinable
public var startIndex: Index {
return .init(backingIndex: self.headBackingIndex,
backingCount: self.count,
backingIndexOfHead: self.headBackingIndex)
}
/// The `CircularBuffer`'s "past the end" position---that is, the position one
/// greater than the last valid subscript argument.
///
/// When you need a range that includes the last element of a collection, use
/// the half-open range operator (`..<`) with `endIndex`. The `..<` operator
/// creates a range that doesn't include the upper bound, so it's always
/// safe to use with `endIndex`.
///
/// If the `CircularBuffer` is empty, `endIndex` is equal to `startIndex`.
@inlinable
public var endIndex: Index {
return .init(backingIndex: self.tailBackingIndex,
backingCount: self.count,
backingIndexOfHead: self.headBackingIndex)
}
/// Returns the distance between two indices.
///
/// Unless the collection conforms to the `BidirectionalCollection` protocol,
/// `start` must be less than or equal to `end`.
///
/// - Parameters:
/// - start: A valid index of the collection.
/// - end: Another valid index of the collection. If `end` is equal to
/// `start`, the result is zero.
/// - Returns: The distance between `start` and `end`. The result can be
/// negative only if the collection conforms to the
/// `BidirectionalCollection` protocol.
///
/// - Complexity: O(1) if the collection conforms to
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the
/// resulting distance.
@inlinable
public func distance(from start: CircularBuffer<Element>.Index, to end: CircularBuffer<Element>.Index) -> Int {
let backingCount = self._buffer.count
switch (start.isIndexGEQHeadIndex, end.isIndexGEQHeadIndex) {
case (true, true):
return end.backingIndex &- start.backingIndex
case (true, false):
return backingCount &- (start.backingIndex &- end.backingIndex)
case (false, true):
return -(backingCount &- (end.backingIndex &- start.backingIndex))
case (false, false):
return end.backingIndex &- start.backingIndex
}
}
}
// MARK: RandomAccessCollection implementation
extension CircularBuffer: RandomAccessCollection {
/// Returns the index offset by `distance` from `index`.
///
/// The following example obtains an index advanced four positions from a
/// string's starting index and then prints the character at that position.
///
/// let s = "Swift"
/// let i = s.index(s.startIndex, offsetBy: 4)
/// print(s[i])
/// // Prints "t"
///
/// The value passed as `distance` must not offset `i` beyond the bounds of
/// the collection.
///
/// - Parameters:
/// - i: A valid index of the collection.
/// - distance: The distance to offset `i`. `distance` must not be negative
/// unless the collection conforms to the `BidirectionalCollection`
/// protocol.
/// - Returns: An index offset by `distance` from the index `i`. If
/// `distance` is positive, this is the same value as the result of
/// `distance` calls to `index(after:)`. If `distance` is negative, this
/// is the same value as the result of `abs(distance)` calls to
/// `index(before:)`.
///
/// - Complexity: O(1) if the collection conforms to
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the absolute
/// value of `distance`.
@inlinable
public func index(_ i: Index, offsetBy distance: Int) -> Index {
return .init(backingIndex: (i.backingIndex &+ distance) & self.mask,
backingCount: self.count,
backingIndexOfHead: self.headBackingIndex)
}
@inlinable
public subscript(bounds: Range<Index>) -> SubSequence {
get {
precondition(self.distance(from: self.startIndex, to: bounds.lowerBound) >= 0)
precondition(self.distance(from: bounds.upperBound, to: self.endIndex) >= 0)
var newRing = self
newRing.headBackingIndex = bounds.lowerBound.backingIndex
newRing.tailBackingIndex = bounds.upperBound.backingIndex
return newRing
}
set {
precondition(self.distance(from: self.startIndex, to: bounds.lowerBound) >= 0)
precondition(self.distance(from: bounds.upperBound, to: self.endIndex) >= 0)
self.replaceSubrange(bounds, with: newValue)
}
}
}
extension CircularBuffer {
/// Allocates a buffer that can hold up to `initialCapacity` elements and initialise an empty ring backed by
/// the buffer. When the ring grows to more than `initialCapacity` elements the buffer will be expanded.
@inlinable
public init(initialCapacity: Int) {
let capacity = Int(UInt32(initialCapacity).nextPowerOf2())
self.headBackingIndex = 0
self.tailBackingIndex = 0
self._buffer = ContiguousArray<Element?>(repeating: nil, count: capacity)
assert(self._buffer.count == capacity)
}
/// Allocates an empty buffer.
@inlinable
public init() {
self = .init(initialCapacity: 16)
}
/// Append an element to the end of the ring buffer.
///
/// Amortized *O(1)*
@inlinable
public mutating func append(_ value: Element) {
self._buffer[self.tailBackingIndex] = value
self.advanceTailIdx(by: 1)
if self.headBackingIndex == self.tailBackingIndex {
// No more room left for another append so grow the buffer now.
self._doubleCapacity()
}
}
/// Prepend an element to the front of the ring buffer.
///
/// Amortized *O(1)*
@inlinable
public mutating func prepend(_ value: Element) {
let idx = self.indexBeforeHeadIdx()
self._buffer[idx] = value
self.advanceHeadIdx(by: -1)
if self.headBackingIndex == self.tailBackingIndex {
// No more room left for another append so grow the buffer now.
self._doubleCapacity()
}
}
/// Double the capacity of the buffer and adjust the headIdx and tailIdx.
///
/// Must only be called when buffer is full.
@inlinable
internal mutating func _doubleCapacity() {
// Double the storage. This can't use _resizeAndFlatten because the buffer is
// full at this stage. That's ok: we have some optimised code paths for this use-case.
let newCapacity = self.capacity << 1
assert(self.headBackingIndex == self.tailBackingIndex)
var newBacking: ContiguousArray<Element?> = []
precondition(newCapacity > 0, "Can't change capacity to \(newCapacity)")
assert(newCapacity % 2 == 0)
assert(newCapacity > self.capacity)
newBacking.reserveCapacity(newCapacity)
newBacking.append(contentsOf: self._buffer[self.headBackingIndex...])
newBacking.append(contentsOf: self._buffer[..<self.tailBackingIndex])
let newTailIndex = newBacking.count
let paddingCount = newCapacity &- newTailIndex
newBacking.append(contentsOf: repeatElement(nil, count: paddingCount))
self.headBackingIndex = 0
self.tailBackingIndex = newTailIndex
self._buffer = newBacking
assert(self.verifyInvariants())
}
/// Resizes and flatten this buffer.
///
/// Capacities are always powers of 2.
@inlinable
internal mutating func _resizeAndFlatten(newCapacity: Int) {
var newBacking: ContiguousArray<Element?> = []
precondition(newCapacity > 0, "Can't change capacity to \(newCapacity)")
assert(newCapacity % 2 == 0)
assert(newCapacity > self.capacity)
newBacking.reserveCapacity(newCapacity)
if self.tailBackingIndex >= self.headBackingIndex {
newBacking.append(contentsOf: self._buffer[self.headBackingIndex..<self.tailBackingIndex])
} else {
newBacking.append(contentsOf: self._buffer[self.headBackingIndex...])
newBacking.append(contentsOf: self._buffer[..<self.tailBackingIndex])
}
let newTailIndex = newBacking.count
let paddingCount = newCapacity &- newTailIndex
newBacking.append(contentsOf: repeatElement(nil, count: paddingCount))
self.headBackingIndex = 0
self.tailBackingIndex = newTailIndex
self._buffer = newBacking
assert(self.verifyInvariants())
}
/// Return element `offset` from first element.
///
/// *O(1)*
@inlinable
public subscript(offset offset: Int) -> Element {
get {
return self[self.index(self.startIndex, offsetBy: offset)]
}
set {
self[self.index(self.startIndex, offsetBy: offset)] = newValue
}
}
/// Returns whether the ring is empty.
@inlinable
public var isEmpty: Bool {
return self.headBackingIndex == self.tailBackingIndex
}
/// Returns the number of element in the ring.
@inlinable
public var count: Int {
if self.tailBackingIndex >= self.headBackingIndex {
return self.tailBackingIndex &- self.headBackingIndex
} else {
return self._buffer.count &- (self.headBackingIndex &- self.tailBackingIndex)
}
}
/// The total number of elements that the ring can contain without allocating new storage.
@inlinable
public var capacity: Int {
return self._buffer.count
}
/// Removes all members from the circular buffer whist keeping the capacity.
@inlinable
public mutating func removeAll(keepingCapacity: Bool = false) {
if keepingCapacity {
self.removeFirst(self.count)
} else {
self._buffer.removeAll(keepingCapacity: false)
self._buffer.append(nil)
}
self.headBackingIndex = 0
self.tailBackingIndex = 0
assert(self.verifyInvariants())
}
/// Modify the element at `index`.
///
/// This function exists to provide a method of modifying the element in its underlying backing storage, instead
/// of copying it out, modifying it, and copying it back in. This emulates the behaviour of the `_modify` accessor
/// that is part of the generalized accessors work. That accessor is currently underscored and not safe to use, so
/// this is the next best thing.
///
/// Note that this function is not guaranteed to be fast. In particular, as it is both generic and accepts a closure
/// it is possible that it will be slower than using the get/modify/set path that occurs with the subscript. If you
/// are interested in using this function for performance you *must* test and verify that the optimisation applies
/// correctly in your situation.
///
/// - parameters:
/// - index: The index of the object that should be modified. If this index is invalid this function will trap.
/// - modifyFunc: The function to apply to the modified object.
@inlinable
public mutating func modify<Result>(_ index: Index, _ modifyFunc: (inout Element) throws -> Result) rethrows -> Result {
return try modifyFunc(&self._buffer[index.backingIndex]!)
}
// MARK: CustomStringConvertible implementation
/// Returns a human readable description of the ring.
public var description: String {
var desc = "[ "
for el in self._buffer.enumerated() {
if el.0 == self.headBackingIndex {
desc += "<"
} else if el.0 == self.tailBackingIndex {
desc += ">"
}
desc += el.1.map { "\($0) " } ?? "_ "
}
desc += "]"
desc += " (bufferCapacity: \(self._buffer.count), ringLength: \(self.count))"
return desc
}
}
// MARK: - RangeReplaceableCollection
extension CircularBuffer: RangeReplaceableCollection {
/// Removes and returns the first element of the `CircularBuffer`.
///
/// Calling this method may invalidate all saved indices of this
/// `CircularBuffer`. Do not rely on a previously stored index value after
/// altering a `CircularBuffer` with any operation that can change its length.
///
/// - Returns: The first element of the `CircularBuffer` if the `CircularBuffer` is not
/// empty; otherwise, `nil`.
///
/// - Complexity: O(1)
@inlinable
public mutating func popFirst() -> Element? {
if count > 0 {
return self.removeFirst()
} else {
return nil
}
}
/// Removes and returns the last element of the `CircularBuffer`.
///
/// Calling this method may invalidate all saved indices of this
/// `CircularBuffer`. Do not rely on a previously stored index value after
/// altering a `CircularBuffer` with any operation that can change its length.
///
/// - Returns: The last element of the `CircularBuffer` if the `CircularBuffer` is not
/// empty; otherwise, `nil`.
///
/// - Complexity: O(1)
@inlinable
public mutating func popLast() -> Element? {
if count > 0 {
return self.removeLast()
} else {
return nil
}
}
/// Removes the specified number of elements from the end of the
/// `CircularBuffer`.
///
/// Attempting to remove more elements than exist in the `CircularBuffer`
/// triggers a runtime error.
///
/// Calling this method may invalidate all saved indices of this
/// `CircularBuffer`. Do not rely on a previously stored index value after
/// altering a `CircularBuffer` with any operation that can change its length.
///
/// - Parameter k: The number of elements to remove from the `CircularBuffer`.
/// `k` must be greater than or equal to zero and must not exceed the
/// number of elements in the `CircularBuffer`.
///
/// - Complexity: O(*k*), where *k* is the specified number of elements.
@inlinable
public mutating func removeLast(_ k: Int) {
precondition(k <= self.count, "Number of elements to drop bigger than the amount of elements in the buffer.")
var idx = self.tailBackingIndex
for _ in 0 ..< k {
idx = self.indexAdvanced(index: idx, by: -1)
self._buffer[idx] = nil
}
self.tailBackingIndex = idx
}
/// Removes the specified number of elements from the beginning of the
/// `CircularBuffer`.
///
/// Calling this method may invalidate any existing indices for use with this
/// `CircularBuffer`.
///
/// - Parameter k: The number of elements to remove.
/// `k` must be greater than or equal to zero and must not exceed the
/// number of elements in the `CircularBuffer`.
///
/// - Complexity: O(*k*), where *k* is the specified number of elements.
@inlinable
public mutating func removeFirst(_ k: Int) {
precondition(k <= self.count, "Number of elements to drop bigger than the amount of elements in the buffer.")
var idx = self.headBackingIndex
for _ in 0 ..< k {
self._buffer[idx] = nil
idx = self.indexAdvanced(index: idx, by: 1)
}
self.headBackingIndex = idx
}
/// Removes and returns the first element of the `CircularBuffer`.
///
/// The `CircularBuffer` must not be empty.
///
/// Calling this method may invalidate any existing indices for use with this
/// `CircularBuffer`.
///
/// - Returns: The removed element.
///
/// - Complexity: O(*1*)
@discardableResult
@inlinable
public mutating func removeFirst() -> Element {
defer {
self.removeFirst(1)
}
return self.first!
}
/// Removes and returns the last element of the `CircularBuffer`.
///
/// The `CircularBuffer` must not be empty.
///
/// Calling this method may invalidate all saved indices of this
/// `CircularBuffer`. Do not rely on a previously stored index value after
/// altering the `CircularBuffer` with any operation that can change its length.
///
/// - Returns: The last element of the `CircularBuffer`.
///
/// - Complexity: O(*1*)
@discardableResult
@inlinable
public mutating func removeLast() -> Element {
defer {
self.removeLast(1)
}
return self.last!
}
/// Replaces the specified subrange of elements with the given `CircularBuffer`.
///
/// - Parameter subrange: The subrange of the collection to replace. The bounds of the range must be valid indices
/// of the `CircularBuffer`.
///
/// - Parameter newElements: The new elements to add to the `CircularBuffer`.
///
/// *O(n)* where _n_ is the length of the new elements collection if the subrange equals to _n_
///
/// *O(m)* where _m_ is the combined length of the collection and _newElements_
@inlinable
public mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C) where Element == C.Element {
precondition(subrange.lowerBound >= self.startIndex && subrange.upperBound <= self.endIndex,
"Subrange out of bounds")
assert(subrange.lowerBound.isValidIndex(for: self),
"illegal index used, index was for CircularBuffer with count \(subrange.lowerBound._backingCheck), " +
"but actual count is \(self.count)")
assert(subrange.upperBound.isValidIndex(for: self),
"illegal index used, index was for CircularBuffer with count \(subrange.upperBound._backingCheck), " +
"but actual count is \(self.count)")
let subrangeCount = self.distance(from: subrange.lowerBound, to: subrange.upperBound)
if subrangeCount == newElements.count {
var index = subrange.lowerBound
for element in newElements {
self._buffer[index.backingIndex] = element
index = self.index(after: index)
}
} else if subrangeCount == self.count && newElements.isEmpty {
self.removeSubrange(subrange)
} else {
var newBuffer: ContiguousArray<Element?> = []
let neededNewCapacity = self.count + newElements.count - subrangeCount + 1 /* always one spare */
let newCapacity = Swift.max(self.capacity, neededNewCapacity.nextPowerOf2())
newBuffer.reserveCapacity(newCapacity)
// This mapping is required due to an inconsistent ability to append sequences of non-optional
// to optional sequences.
// https://bugs.swift.org/browse/SR-7921
newBuffer.append(contentsOf: self[self.startIndex ..< subrange.lowerBound].lazy.map { $0 })
newBuffer.append(contentsOf: newElements.lazy.map { $0 })
newBuffer.append(contentsOf: self[subrange.upperBound..<self.endIndex].lazy.map { $0 })
let repetitionCount = newCapacity &- newBuffer.count
if repetitionCount > 0 {
newBuffer.append(contentsOf: repeatElement(nil, count: repetitionCount))
}
self._buffer = newBuffer
self.headBackingIndex = 0
self.tailBackingIndex = newBuffer.count &- repetitionCount
}
assert(self.verifyInvariants())
}
/// Removes the elements in the specified subrange from the circular buffer.
///
/// - Parameter bounds: The range of the circular buffer to be removed. The bounds of the range must be valid indices of the collection.
@inlinable
public mutating func removeSubrange(_ bounds: Range<Index>) {
precondition(bounds.upperBound >= self.startIndex && bounds.upperBound <= self.endIndex, "Invalid bounds.")
let boundsCount = self.distance(from: bounds.lowerBound, to: bounds.upperBound)
switch boundsCount {
case 1:
remove(at: bounds.lowerBound)
case self.count:
self = .init(initialCapacity: self._buffer.count)
default:
replaceSubrange(bounds, with: [])
}
assert(self.verifyInvariants())
}
/// Removes & returns the item at `position` from the buffer
///
/// - Parameter position: The index of the item to be removed from the buffer.
///
/// *O(1)* if the position is `headIdx` or `tailIdx`.
/// otherwise
/// *O(n)* where *n* is the number of elements between `position` and `tailIdx`.
@discardableResult
@inlinable
public mutating func remove(at position: Index) -> Element {
assert(position.isValidIndex(for: self),
"illegal index used, index was for CircularBuffer with count \(position._backingCheck), " +
"but actual count is \(self.count)")
defer {
assert(self.verifyInvariants())
}
precondition(self.indices.contains(position), "Position out of bounds.")
var bufferIndex = position.backingIndex
let element = self._buffer[bufferIndex]!
switch bufferIndex {
case self.headBackingIndex:
self.advanceHeadIdx(by: 1)
self._buffer[bufferIndex] = nil
case self.indexBeforeTailIdx():
self.advanceTailIdx(by: -1)
self._buffer[bufferIndex] = nil
default:
self._buffer[bufferIndex] = nil
var nextIndex = self.indexAdvanced(index: bufferIndex, by: 1)
while nextIndex != self.tailBackingIndex {
self._buffer.swapAt(bufferIndex, nextIndex)
bufferIndex = nextIndex
nextIndex = self.indexAdvanced(index: bufferIndex, by: 1)
}
self.advanceTailIdx(by: -1)
}
return element
}
/// The first `Element` of the `CircularBuffer` (or `nil` if empty).
@inlinable
public var first: Element? {
// We implement this here to work around https://bugs.swift.org/browse/SR-14516
guard !self.isEmpty else {
return nil
}
return self[self.startIndex]
}
/// Prepares the `CircularBuffer` to store the specified number of elements.
@inlinable
public mutating func reserveCapacity(_ minimumCapacity: Int) {
if self.capacity >= minimumCapacity {
// Already done, do nothing.
return
}
// We need to allocate a larger buffer. We take this opportunity to make ourselves contiguous
// again as needed.
let targetCapacity = minimumCapacity.nextPowerOf2()
self._resizeAndFlatten(newCapacity: targetCapacity)
}
}
extension CircularBuffer {
@usableFromInline
internal func verifyInvariants() -> Bool {
var index = self.headBackingIndex
while index != self.tailBackingIndex {
if self._buffer[index] == nil {
return false
}
index = self.indexAdvanced(index: index, by: 1)
}
return true
}
// this is not a general invariant (not true for CircularBuffer that have been sliced)
private func unreachableAreNil() -> Bool {
var index = self.tailBackingIndex
while index != self.headBackingIndex {
if self._buffer[index] != nil {
return false
}
index = self.indexAdvanced(index: index, by: 1)
}
return true
}
internal func testOnly_verifyInvariantsForNonSlices() -> Bool {
return self.verifyInvariants() && self.unreachableAreNil()
}
}
extension CircularBuffer: Equatable where Element: Equatable {
public static func ==(lhs: CircularBuffer, rhs: CircularBuffer) -> Bool {
return lhs.count == rhs.count && zip(lhs, rhs).allSatisfy(==)
}
}
extension CircularBuffer: Hashable where Element: Hashable {
public func hash(into hasher: inout Hasher) {
for element in self {
hasher.combine(element)
}
}
}
extension CircularBuffer: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Element...) {
self.init(elements)
}
}
|