ArrayDeque
Implement the same deque with a circular array: constant-time ends, doubling and shrinking storage, a full RandomAccessCollection, equality that ignores layout, value semantics for free, and a randomized test of the resizing rules.
This is CS61B’s Project 2, in Swift. You will implement the Deque protocol
from Project 6 again, this time in a circular array that grows and shrinks
as items come and go, the design from Lesson 30. Then you will make it a full
Swift collection, so that for–in, sort(), map, and slicing all work on
it, as Lesson 32 showed.
Getting started
Work in the arraydeque folder of the
swift-projects repository:
cd swift-projects/arraydeque
swift test
The Deque protocol is the same as in Project 6. ArrayDeque.swift is yours,
including its extensions.
The rules
- Store the items in an array of optionals,
[Element?], used as fixed-size storage: create it with a number of slots, and afterwards only read and write slots. Noappend,insert, orremoveon it; those would hide the very work this project is about. - Start with 8 slots. When the storage is full, double it. When fewer than a quarter of the slots are used and there are more than 8, halve it.
- Slots that do not hold an item must be
nil, so removed items are released (Lesson 30’s loitering). addFirst,addLast,removeFirst,removeLast,get, and the subscript take constant time, apart from the occasional resize.
Choose your representation, write its invariants as a comment above the
stored properties, and keep them true in every method. Lesson 30 uses first
and count; CS61B uses nextFirst and nextLast, with a size. Either works.
Task 1: Adding
Write the stored properties, count, capacity (the number of slots), and
addFirst and addLast, without resizing for now: the tests for this task
never add more than 8 items. Adding at the front moves the front index one
step back, wrapping around from 0 to the last slot.
Task 2: Reading
Write get(_:) and toArray(). Item i is at slot (first + i) % capacity.
Task 3: Removing
Write removeFirst() and removeLast(), clearing the slot each removed item
occupied.
Task 4: Resizing
Write a private resize(to:) and call it when adding to a full deque and when
removing leaves it sparse. Copy item i to slot i of the new storage, not
slot to slot, so that a deque that had wrapped around is laid out in order.
Lesson 27’s randomized test found exactly this bug in a queue that copied slot
to slot.
Task 5: A real collection
ArrayDeque declares conformance to RandomAccessCollection and
MutableCollection, with integer indices from 0 to count. Implement the
subscript’s getter and setter, with a precondition that the index is in range.
That is all: the standard library then provides iteration, first, last,
map, reduce, firstIndex(of:), slicing, sort(), reverse(), and more.
The provided tests call several of them.
The starter also includes isEmpty. Both Deque and Collection offer a
default isEmpty, and Swift refuses to guess between them, so the deque
declares its own.
Task 6: Equality and description
descriptionprints the items like an array:[1, 2, 3].==compares items, in order. Two deques with the same items can have different layouts in their storage, one wrapped around and one not, so the synthesized==, which would compare the storage, is wrong here. Think about why before writing yours.hasSameItems(as:), in an extension on theDequeprotocol, compares any two deques, even of different types, such as anArrayDequeand Project 6’sLinkedListDeque, using only the protocol’s requirements.
Finally, notice a test you did not need to write any code for:
valueSemantics. ArrayDeque is a structure whose storage is a Swift array,
and arrays are copy-on-write (Lesson 33), so a copy of a deque is independent
and cheap until one of them changes.
Task 7: Your own tests
Write, in MyTests.swift, a randomized comparison against [Int] that runs in
phases, mostly adding and then mostly removing, so the deque grows and shrinks
many times. After every operation, check the items, the count, and the
resizing rules: capacity is at least 8 and at least count, and whenever it
is above 8, count >= capacity / 4, since a removal that left fewer items than
that would have shrunk it.