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

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.

1 participant