Embed std.deque with eleven generic operations implemented entirely in safe Aji over initialized Option<T> buffer slots. Bounded pushes and moving pops work at both ends; full pushes, out-of-bounds replacements and nonempty free return ownership to the caller. Explicit reserve preserves order and all state on allocation failure, charges the old and new slot capacities at the allocation peak, and rotates whole values after successful growth. No AIR, verifier or runtime change. Specify the visible representation invariant, tag-inclusive allocation accounting and empty-only cleanup. Add RFC 0025, a runnable owning example, source/CLI conformance and CI coverage. Complete STD-6c; HashMap, HashSet and BinaryHeap remain STD-6d. Validation: unit tests and 785 full conformance cases; 28 deque fixtures; focused collection after every instruction and 12 schedule seeds over four quanta; 136 source/package/hosted CLI checks; an independent Python deque oracle for 128 operations and 71 small growth configurations; 93/93 source coverage; Zig formatting, staged whitespace and bundles. Reference cost: full source excluding AIR grows 52,384 -> 53,993 cl100k tokens (+3.1%); Quick Reference 7,667 -> 7,747 (+1.0%). AIR stays at 175,736 bytes. Construction/growth still incur interpreter prefix-copy overhead; no native performance or empirical MCC benefit is claimed.
201 lines
7.5 KiB
Rust
201 lines
7.5 KiB
Rust
module std.deque
|
|
import std.option;
|
|
|
|
struct Deque<T> { storage: buffer<std.option.Option<T>>, head: usize, length: usize }
|
|
enum NewResult<T> { ok(Deque<T>), err(alloc_error) }
|
|
enum PushResult<T> { ok, full(T) }
|
|
enum ReserveResult { ok, err(alloc_error) }
|
|
enum FreeResult<T> { ok, not_empty(Deque<T>) }
|
|
enum ReplaceResult<T> { ok(T), out_of_bounds(T) }
|
|
|
|
fn new<T>(capacity: usize, heap: cap<mem.alloc>) -> NewResult<T>
|
|
effects [mem.alloc]
|
|
{
|
|
match buffer.new<std.option.Option<T>>(capacity, heap) {
|
|
ok(storage) => {
|
|
{
|
|
mut slots = storage;
|
|
for index in 0usize..capacity {
|
|
let empty = std.option.Option::<T>::none;
|
|
buffer.push(slots, move empty) or trap;
|
|
}
|
|
}
|
|
return NewResult::<T>::ok(Deque::<T> { storage: move storage, head: 0, length: 0 });
|
|
}
|
|
err(error) => { return NewResult::<T>::err(error); }
|
|
}
|
|
}
|
|
|
|
fn len<T>(value: ref<Deque<T>>) -> usize effects [] {
|
|
return value.length;
|
|
}
|
|
|
|
fn capacity<T>(value: ref<Deque<T>>) -> usize effects [] {
|
|
ref slots = value.storage;
|
|
return buffer.capacity(slots);
|
|
}
|
|
|
|
fn reserve<T>(value: mut<Deque<T>>, capacity: usize, heap: cap<mem.alloc>) -> ReserveResult
|
|
effects [mem.alloc]
|
|
{
|
|
let head = value.head;
|
|
mut storage = value.storage;
|
|
let old_capacity = buffer.capacity(storage);
|
|
if capacity <= old_capacity { return ReserveResult::ok; }
|
|
match buffer.reserve(storage, capacity, heap) {
|
|
ok() => {}
|
|
err(error) => { return ReserveResult::err(error); }
|
|
}
|
|
for index in old_capacity..capacity {
|
|
let empty = std.option.Option::<T>::none;
|
|
buffer.push(storage, move empty) or trap;
|
|
}
|
|
if head != 0usize {
|
|
// Three reversals rotate the original slots left by head.
|
|
mut slots = buffer.view(storage);
|
|
for pass in 0usize..3usize {
|
|
var first: usize = 0;
|
|
var stop: usize = head;
|
|
if pass == 1usize { first = head; stop = old_capacity; }
|
|
if pass == 2usize { stop = old_capacity; }
|
|
while first < stop {
|
|
stop = stop - 1;
|
|
if first < stop {
|
|
let empty = std.option.Option::<T>::none;
|
|
let left = value.swap(mut slots[first], move empty);
|
|
let right = value.swap(mut slots[stop], move left);
|
|
let hole = value.swap(mut slots[first], move right);
|
|
match move hole {
|
|
std.option.Option::<T>::none => {}
|
|
std.option.Option::<T>::some(unexpected) => { trap; }
|
|
}
|
|
}
|
|
first = first + 1;
|
|
}
|
|
}
|
|
}
|
|
value.head = 0;
|
|
return ReserveResult::ok;
|
|
}
|
|
|
|
fn push_back<T>(value: mut<Deque<T>>, item: T) -> PushResult<T> effects [] {
|
|
let length = value.length;
|
|
let head = value.head;
|
|
mut storage = value.storage;
|
|
let capacity = buffer.capacity(storage);
|
|
if length == capacity { return PushResult::<T>::full(move item); }
|
|
let room = capacity - head;
|
|
let index = if length < room { head + length } else { length - room };
|
|
mut slots = buffer.view(storage);
|
|
let next = std.option.Option::<T>::some(move item);
|
|
let previous = value.swap(mut slots[index], move next);
|
|
match move previous {
|
|
std.option.Option::<T>::none => {}
|
|
std.option.Option::<T>::some(unexpected) => { trap; }
|
|
}
|
|
value.length = length + 1;
|
|
return PushResult::<T>::ok;
|
|
}
|
|
|
|
fn push_front<T>(value: mut<Deque<T>>, item: T) -> PushResult<T> effects [] {
|
|
let length = value.length;
|
|
let head = value.head;
|
|
mut storage = value.storage;
|
|
let capacity = buffer.capacity(storage);
|
|
if length == capacity { return PushResult::<T>::full(move item); }
|
|
let index = if head == 0usize { capacity - 1 } else { head - 1 };
|
|
mut slots = buffer.view(storage);
|
|
let next = std.option.Option::<T>::some(move item);
|
|
let previous = value.swap(mut slots[index], move next);
|
|
match move previous {
|
|
std.option.Option::<T>::none => {}
|
|
std.option.Option::<T>::some(unexpected) => { trap; }
|
|
}
|
|
value.head = index;
|
|
value.length = length + 1;
|
|
return PushResult::<T>::ok;
|
|
}
|
|
|
|
fn pop_front<T>(value: mut<Deque<T>>) -> std.option.Option<T> effects [] {
|
|
let length = value.length;
|
|
if length == 0usize { return std.option.Option::<T>::none; }
|
|
let head = value.head;
|
|
mut storage = value.storage;
|
|
let capacity = buffer.capacity(storage);
|
|
mut slots = buffer.view(storage);
|
|
let empty = std.option.Option::<T>::none;
|
|
let previous = value.swap(mut slots[head], move empty);
|
|
value.length = length - 1;
|
|
if length == 1usize { value.head = 0; }
|
|
else { value.head = if head == capacity - 1 { 0usize } else { head + 1 }; }
|
|
match move previous {
|
|
std.option.Option::<T>::some(item) => { return std.option.Option::<T>::some(move item); }
|
|
std.option.Option::<T>::none => { trap; }
|
|
}
|
|
}
|
|
|
|
fn pop_back<T>(value: mut<Deque<T>>) -> std.option.Option<T> effects [] {
|
|
let length = value.length;
|
|
if length == 0usize { return std.option.Option::<T>::none; }
|
|
let head = value.head;
|
|
mut storage = value.storage;
|
|
let capacity = buffer.capacity(storage);
|
|
let offset = length - 1;
|
|
let room = capacity - head;
|
|
let index = if offset < room { head + offset } else { offset - room };
|
|
mut slots = buffer.view(storage);
|
|
let empty = std.option.Option::<T>::none;
|
|
let previous = value.swap(mut slots[index], move empty);
|
|
value.length = offset;
|
|
if length == 1usize { value.head = 0; }
|
|
match move previous {
|
|
std.option.Option::<T>::some(item) => { return std.option.Option::<T>::some(move item); }
|
|
std.option.Option::<T>::none => { trap; }
|
|
}
|
|
}
|
|
|
|
fn free<T>(value: Deque<T>) -> FreeResult<T> effects [] {
|
|
if value.length != 0usize { return FreeResult::<T>::not_empty(move value); }
|
|
let (storage, head, length) = struct.unpack(move value);
|
|
{
|
|
mut slots = storage;
|
|
while buffer.len(slots) != 0usize {
|
|
let empty = buffer.pop(slots) or trap;
|
|
match move empty {
|
|
std.option.Option::<T>::none => {}
|
|
std.option.Option::<T>::some(unexpected) => { trap; }
|
|
}
|
|
}
|
|
}
|
|
buffer.free(move storage) or trap;
|
|
return FreeResult::<T>::ok;
|
|
}
|
|
|
|
fn get<T: Copy>(value: ref<Deque<T>>, index: usize) -> std.option.Option<T> effects [] {
|
|
if index >= value.length { return std.option.Option::<T>::none; }
|
|
let head = value.head;
|
|
ref storage = value.storage;
|
|
let room = buffer.capacity(storage) - head;
|
|
let position = if index < room { head + index } else { index - room };
|
|
ref slots = buffer.view(storage);
|
|
match slots[position] {
|
|
std.option.Option::<T>::some(item) => { return std.option.Option::<T>::some(item); }
|
|
std.option.Option::<T>::none => { trap; }
|
|
}
|
|
}
|
|
|
|
fn replace<T>(value: mut<Deque<T>>, index: usize, item: T) -> ReplaceResult<T> effects [] {
|
|
if index >= value.length { return ReplaceResult::<T>::out_of_bounds(move item); }
|
|
let head = value.head;
|
|
mut storage = value.storage;
|
|
let room = buffer.capacity(storage) - head;
|
|
let position = if index < room { head + index } else { index - room };
|
|
mut slots = buffer.view(storage);
|
|
let next = std.option.Option::<T>::some(move item);
|
|
let previous = value.swap(mut slots[position], move next);
|
|
match move previous {
|
|
std.option.Option::<T>::some(old) => { return ReplaceResult::<T>::ok(move old); }
|
|
std.option.Option::<T>::none => { trap; }
|
|
}
|
|
}
|