-
Notifications
You must be signed in to change notification settings - Fork 356
Add fixed-capacity support to Deque with ring buffer semantics #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could it better using !elements.isEmpty in the guard condition?
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