Add fixed-capacity support to Deque with ring buffer semantics#519
Add fixed-capacity support to Deque with ring buffer semantics#519ekansh28 wants to merge 1 commit intoapple:mainfrom
Conversation
Implements a complete fixed-capacity Deque feature (issue apple#309), providing ring buffer behavior backed by contiguous memory. Core Features: - Added `Deque<T>(fixedCapacity:)` initializer for bounded deques. - Implemented ring buffer semantics: • `append(_:)` removes the oldest element when full. • `prepend(_:)` removes the newest element when full. - Introduced helper properties: • `maxCapacity` • `isFixedCapacity` • `isFull` • `remainingCapacity` - Ensured full backward compatibility — existing unbounded deques continue to function unchanged. Behavior: - Example: [1, 2, 3] → append(4) → [2, 3, 4] - Example: [1, 2, 3] → prepend(0) → [0, 1, 2] Test Coverage: - Property validation (capacity, fullness, etc.) - Append/prepend ring buffer behavior - Precondition validation - Integration tests with existing Deque functionality This change delivers the requested feature for high-performance systems that need bounded memory collections. It follows Swift Collections’ coding and documentation conventions, while maintaining O(1) amortized performance guarantees.
|
Thanks for working on this. Unfortunately it is unlikely we will land this in this form -- we're preparing to ship new, ownership aware A (somewhat outdated) work-in-progress draft of The impending 1.3.0 release will ship |
| /// | ||
| /// - Complexity: O(1) | ||
| @inlinable | ||
| public var remainingCapacity: Int { |
There was a problem hiding this comment.
For RigidArray and RigidDeque (and later RigidSet, RigidDictionary etc) I've been using the name freeCapacity for the same property.
It feels like your name may be a better choice! I'll sleep on it -- we may want RigidArray to adopt this before we ship 1.3.0.
There was a problem hiding this comment.
D'oh, we have already established the freeCapacity name in the OutputSpan type that has shipped in Swift 6.2's stdlib.
Oh well; remainingCapacity feels better, but it's better to be consistent than perfect.
| @inlinable | ||
| public init(_ elements: some Collection<Element>) { | ||
| let c = elements.count | ||
| guard c > 0 else { |
There was a problem hiding this comment.
could it better using !elements.isEmpty in the guard condition?
There was a problem hiding this comment.
No; we need to know the count to allocate enough storage.
When the collection happens to be empty, it does not matter whether we call count or isEmpty -- both operations have constant complexity in that case.
As foretold above, version 1.4.0 of swift-collections has shipped Thanks again for pushing this! |
Implements a complete fixed-capacity Deque feature (issue #309), providing ring buffer behavior backed by contiguous memory.
Core Features:
Deque<T>(fixedCapacity:)initializer for bounded deques.append(_:)removes the oldest element when full. •prepend(_:)removes the newest element when full.maxCapacity•isFixedCapacity•isFull•remainingCapacityBehavior:
Test Coverage:
This change delivers the requested feature for high-performance systems that need bounded memory collections. It follows Swift Collections’ coding and documentation conventions, while maintaining O(1) amortized performance guarantees.
Replace this paragraph with a description of your changes and rationale. Provide links to an existing issue or external references/discussions, if appropriate.
Checklist