Skip to content

Conversation

ekansh28
Copy link

@ekansh28 ekansh28 commented Sep 20, 2025

Implements a complete fixed-capacity Deque feature (issue #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: • maxCapacityisFixedCapacityisFullremainingCapacity
  • 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.

Replace this paragraph with a description of your changes and rationale. Provide links to an existing issue or external references/discussions, if appropriate.

Checklist

  • I've read the Contribution Guidelines
  • My contributions are licensed under the Swift license.
  • I've followed the coding style of the rest of the project.
  • I've added tests covering all new code paths my change adds to the project (if appropriate).
  • I've added benchmarks covering new functionality (if appropriate).
  • I've verified that my change does not break any existing tests or introduce unexplained benchmark regressions.
  • I've updated the documentation if necessary.

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.
@ekansh28 ekansh28 requested a review from lorentey as a code owner September 20, 2025 16:55
@ekansh28
Copy link
Author

@lorentey

@lorentey
Copy link
Member

Thanks for working on this. Unfortunately it is unlikely we will land this in this form -- we're preparing to ship new, ownership aware Deque variants, including a fixed-capacity RigidDeque type that is likely to be a better approach overall.

A (somewhat outdated) work-in-progress draft of RigidDeque is available on the future branch; this type is designed to provide clients with full control over all aspects of allocation behavior.

The impending 1.3.0 release will ship RigidArray as the first step towards fleshing out a full suite of these Rigid* types; I hope a production-ready RigidDeque will quickly follow in a subsequent update.

///
/// - Complexity: O(1)
@inlinable
public var remainingCapacity: Int {
Copy link
Member

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.

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants