A stack in a doubling array over the memory oracle #
The bounded stack Stack.spec w C is implemented by a growing array: a dynamic array
used as a stack. A header of three cells at hdr holds the number of items, the capacity, and the
base address of the item array. A push into a full array first allocates an array of twice the
capacity, copies the items into it and switches the header to it (grow). The old array is
abandoned, since memory is never reused.
So the structure allocates inside its operations. It is a Memory.Framed refinement whose
footprint, the header and the current array, moves into fresh cells as it grows, and whose room,
4 C - 2 · capacity, is what its future growth may still allocate: doubling from capacity c
allocates 2 c cells and lowers the room by exactly 2 c. Its invariant asks that the frontier
plus the room fit a word, which is what makes each allocation admissible. In a product with other
structures that allocate (Memory.Framed.prod), the shared room budget keeps this true.
Every operation costs at most 13 memory units, amortized, with the potential
2 (2 · count - capacity): a push into a full array of n items costs 9 + 2 n and releases
the potential 2 n. The allocator (allocator) sets up an empty stack of capacity one in four
cells, at a cost of seven units, and reserves 4 C + 4 cells in all.
The word RAM's counterpart is DynamicArray.WordRAM, whose append routine is stepped
instruction by instruction and which assumes that it is the only allocator.
Main definitions #
count,capacity,base: the header's cells, read from memory.grow,push,pop,size,methods: the operations as memory programs.abs,Inv,footprint,room,potential: the representation.refinement,framed: the doubling stack as a refinement, and framed with room.create,allocator: allocate an empty doubling stack.
Main results #
push_full,push_not_full,pop_of_ne_zero: the operations' effects on the header and the cells.congr,modifies_methods: the frame, with the footprint growing into fresh cells and the frontier advancing by at most the room given up.amortized: every operation costs at most thirteen memory units, amortized.amortized_allocator: the allocator's cost certificate.
References #
The representation #
The number of items, in the header's first cell.
The capacity of the item array, in the header's second cell.
The base address of the item array, in the header's third cell.
The item array.
The stored stack, top first: the first count items, reversed.
The header's three cells and the current item array.
The cells that growing to capacity 2 C may still allocate.
Four credits per item beyond half the capacity: 2 n on a full array of n items, which pays
for copying them.
The implementation invariant.
The item array lies above the header.
The item array is allocated.
The capacity is positive, so doubling grows it.
The items fit the array.
The frontier plus the room fits a word, so growing is admissible.
The items are words.
The operations #
Double the capacity of a full array of n items: allocate 2 n cells, copy the items, and
switch the header to the new array.
Grow the array if the n items fill its capacity c.
Write the item v after the n items, and count it.
Push: grow a full array, then append.
Pop: an empty stack returns none; otherwise read the top item and decrement the count.
The stack operations as memory programs.
Evaluation #
The memory after growing a full array of n items.
Growing moves the items to a fresh array of twice the size.
Growing costs 4 + 2 n memory units.
Growing is admissible when the old array is allocated and holds words, and the new frontier fits a word.
Appending reads the base, writes the item and the new count.
A push into an array with room reads the count and capacity, then appends.
A push into a full array grows it, then appends.
A pop from an empty stack reads the count only.
A pop from a nonempty stack reads the count, the base and the top item, and writes the new count.
A size query reads the count.
The operations' effects #
The stored stack lists the first count items of the array, top first.
The stack has as many elements as the count.
A state with one more item v on top stores v on top of the stack.
A push into an array with room writes the item and the count, and keeps the rest.
After growing, the array starts at the old frontier.
A push into a full array moves the items to a fresh array of twice the capacity at the frontier, writes the item and the count, and keeps every other allocated cell.
A push into a full array of n items costs 9 + 2 n memory units.
A push of a word into a full array is admissible when twice its capacity fits the room.
A pop from a nonempty stack decrements the count and keeps the rest.
The refinement #
The header is allocated.
The frontier fits a word.
The stack before a pop is the popped item on top of the stack after it.
The doubling array implements the bounded stack.
The frame: footprint and room #
The invariant, the abstraction, the footprint and the room read only the footprint, and survive a growing frontier that leaves the room fitting a word.
Each admissible operation changes no allocated cell outside the footprint, grows the footprint only into fresh cells, and advances the frontier by at most the room it gives up.
The doubling stack, framed by its header and current array, with the room its growth may still allocate.
Every operation costs at most thirteen memory units, amortized: a push into a full array
of n items costs 9 + 2 n and releases the potential 2 n that the last n / 2 pushes
saved.
Allocation #
Allocate an empty doubling stack: a zeroed header of three cells, then an item array of one cell, recorded in the header. Returns the header's address.
Creation allocates the header at the frontier and the array of one cell above it.
The header of a created stack: no items, capacity one, and the array just above.
The doubling stack as an allocator: an empty stack in four cells at a memory cost of
seven units, reserving 4 C + 4 cells for itself and its growth to capacity 2 C.