#! @file deque.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Defines an implementation for a `deque` container. Deques are a structured #! container where data is pushed and popped at either end. module std::containers; #! A `deque` is a structured data container, a double-ended queue, that stores #! its content in order and is pushed and popped at either end. #! #! They are useful wherever work is taken from both ends, such as undo #! histories, sliding windows, and work-stealing schedulers, and wherever a #! queue must also be indexed. #! #! Its elements live in one block used as a ring: the front is at `_hed`, and #! each element after it is at the next index, wrapping to the start of the #! block. #! @tparam T The type of its elements. pub struct deque : owning , hashable , indexable , assignable , iterable , copyable , moveable , equatable { var _alc: maybe<&mut allocator>; var _dat: *mut T; var _hed: u64; var _len: u64; var _cap: u64; } #! Provides read only access to the length of a deque. #! @param this The deque to get the length of. #! @returns The length of the deque. pub val deque::length: u64 { get => this._len; } #! Provides read only access to the capacity of a deque. #! @param this The deque to get the capacity of. #! @returns The capacity of the deque. pub val deque::capacity: u64 { get => this._cap; } #! Indicates if a deque is empty. #! @param this The deque to check. #! @returns Whether the deque is empty. pub val deque::empty: bool { get => this._len == 0; } #! Indicates if a deque is full, so the next push makes it grow. #! @param this The deque to check. #! @returns Whether the deque is full. pub val deque::full: bool { get => this._len == this._cap; } #! Creates a new deque with no allocator and a default size. #! @returns The created deque container. #! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize` #! for memory management. pub def deque::new(): deque { return deque::::new(4, nothing{}); } #! Creates a new deque with an optional allocator and a default size. #! @param alloc The optional allocator for the memory backing of the deque. #! @returns The created deque container. pub def deque::new(alloc: maybe<&mut allocator>): deque { return deque::::new(4, alloc); } #! Creates a new deque with an optional allocator and a provided size. #! @param size The initial capacity of the deque, which may be none. #! @param alloc The optional allocator for the memory backing of the deque. #! @returns The created deque container, which holds no elements yet. pub def deque::new(size: u64, alloc: maybe<&mut allocator>): deque { var made: deque = deque { _alc = alloc, _dat = null, _hed = 0, _len = 0, _cap = 0 }; made.reserve(size); return made; } #! Creates a copy of the provided deque. #! @param other The deque to copy. #! @returns The copy, which owns its own memory and its own elements. #! @remarks Each element is copied by `copyable` where it provides one, so an #! element owning memory is copied rather than shared. pub def deque::copy_of(other: &deque): deque { var made := deque::::new(other._len, other._alc); for index in 0..other._len { made.push_back(copy::(&other._dat[other.slot(index)])); } return made; } #! Creates a new deque by moving the data of another deque into it. #! @param other The deque to move elements from, which holds none after. #! @returns The moved deque, which takes the memory and allocator with it. #! @remarks Nothing is copied and no element is touched: the memory moves as it #! stands, and the other is left empty rather than dead, so closing it #! afterwards is harmless. pub def deque::move_of(other: &mut deque): deque { val made: deque = deque { _alc = other._alc, _dat = other._dat, _hed = other._hed, _len = other._len, _cap = other._cap }; other._dat = null; other._hed = 0; other._len = 0; other._cap = 0; return made; } #! Gets the element at the provided index, counted from the front. #! @param this The deque to get the element of. #! @param index The index of the element, below `length`, past which a checked #! program stops. #! @returns The element at the given index. pub def deque::at(&this, index: u64): &T { if const checking() { if index >= this._len { past_end(index, this._len); } } return &this._dat[this.slot(index)]; } #! Gets the element at the provided index, counted from the front. #! @param this The deque to get the element of. #! @param index The index of the element, below `length`, past which a checked #! program stops. #! @returns The element at the given index. pub def deque::at(&mut this, index: u64): &mut T { if const checking() { if index >= this._len { past_end(index, this._len); } } return &mut this._dat[this.slot(index)]; } #! Gets the element at an index, if the deque holds one there, for an index #! that may be past the end in a program that is right. #! @param this The deque to read. #! @param index The index of the element. #! @returns Maybe the element; otherwise, `nothing`. pub def deque::get(&this, index: u64): maybe<&T> { return nothing {} if index >= this._len; return &this._dat[this.slot(index)]; } #! Gets the element at an index to write it, if the deque holds one there. #! @param this The deque to write. #! @param index The index of the element. #! @returns Maybe the element; otherwise, `nothing`. pub def deque::get(&mut this, index: u64): maybe<&mut T> { return nothing {} if index >= this._len; return &mut this._dat[this.slot(index)]; } #! The element at the front of a deque, which a deque that can be written #! writes through. #! @param this The deque to read, which stops a checked program where it is #! empty. #! @returns A reference to the element. pub val deque::first: &T { get { if const checking() { if this._len == 0 { fault("The deque is empty."); } } return &this._dat[this._hed]; } get mut { if const checking() { if this._len == 0 { fault("The deque is empty."); } } return &mut this._dat[this._hed]; } } #! The element at the back of a deque, which a deque that can be written writes #! through. #! @param this The deque to read, which stops a checked program where it is #! empty. #! @returns A reference to the element. pub val deque::last: &T { get { if const checking() { if this._len == 0 { fault("The deque is empty."); } } return &this._dat[this.slot(this._len - 1)]; } get mut { if const checking() { if this._len == 0 { fault("The deque is empty."); } } return &mut this._dat[this.slot(this._len - 1)]; } } #! Pushes a new element onto the front of a deque. #! @param this The deque to push the value onto. #! @param value The value to push onto the deque. #! @remarks An allocator that gives no more memory stops the program. pub def deque::push_front(&mut this, value: T): void { if this.full { this.grow(); } this._hed = (this._hed + this._cap - 1) % this._cap; this._dat[this._hed] = move(value); this._len++; } #! Pushes a new element onto the back of a deque. #! @param this The deque to push the value onto. #! @param value The value to push onto the deque. #! @remarks An allocator that gives no more memory stops the program. pub def deque::push_back(&mut this, value: T): void { if this.full { this.grow(); } this._dat[this.slot(this._len)] = move(value); this._len++; } #! Pops the element off the front of a deque. #! @param this The deque to pop the value off of, which stops a checked #! program where it is empty. #! @remarks The value is closed where it closes, or holds what does. pub def deque::pop_front(&mut this): void { if const checking() { if this._len == 0 { fault("The deque is empty."); } } this.release(this._hed); this._hed = (this._hed + 1) % this._cap; this._len--; } #! Pops the element off the back of a deque. #! @param this The deque to pop the value off of, which stops a checked #! program where it is empty. #! @remarks The value is closed where it closes, or holds what does. pub def deque::pop_back(&mut this): void { if const checking() { if this._len == 0 { fault("The deque is empty."); } } this.release(this.slot(this._len - 1)); this._len--; } #! Reserves an amount of memory for the number of elements requested for the #! backing memory of the deque provided. #! @param this The deque to reserve elements for. #! @param count The number of elements to reserve for a deque. pub def deque::reserve(&mut this, count: u64): void { this.grow(count); } #! Removes all elements from a deque. #! @param this The deque to clear. #! @remarks Elements that close, or hold what does, are closed. The backing data #! is kept but not disposed. pub def deque::clear(&mut this): void { for index in 0..this._len { this.release(this.slot(index)); } this._hed = 0; this._len = 0; } #! Removes all elements of a deque then disposes the backing memory. #! @param this The deque to close. pub def deque::close(&mut this): void { if const T is closeable { for index in 0..this._len { close(&mut this._dat[this.slot(index)]); } } this._hed = 0; this._len = 0; if this._dat != null { dispose::(this._alc, this._dat, this._cap); } this._dat = null; this._cap = 0; } #! Compares one deque to another by equality. #! @param this The deque to compare against. #! @param other The deque to compare with. #! @returns Whether both hold equal elements, in the same order. pub def deque::equals(&this, other: &deque): bool { if this._len != other._len { return false; } for index in 0..this._len { return false if !equals::( &this._dat[this.slot(index)], &other._dat[other.slot(index)] ); } return true; } #! What `iter(&this)` lends, which reads the elements from the front of the #! deque through its `at`, and which `for x` and `for &x` walk. pub type deque::iterator = positions_iterator, T>; #! What `iter(&mut this)` lends, which writes the elements where they are, from #! the front of the deque, and which `for &mut x` walks. pub type deque::mutator = position_mutator, T>; #! Lends an iterator that reads the elements from the front of the deque, which #! `for x in d` walks. #! @param this The deque to walk. #! @returns The iterator, which is only good until the deque changes, moves, or #! closes. pub def deque::iter(&this): deque::iterator { return positions_iterator::, T>::over(this, this._len); } #! Lends a mutator that writes the elements where they are, from the front of #! the deque, which `for &mut x in d` walks. #! @param this The deque to walk. #! @returns The mutator, which is only good until the deque changes, moves, or #! closes. pub def deque::iter(&mut this): deque::mutator { return position_mutator::, T>::over(this, this._len); } #! An iterator that reads the elements from the front of the deque, as `iter` #! lends. #! @param this The deque to walk. #! @returns The iterator, which is only good until the deque changes, moves, or #! closes. pub val deque::iterator: deque::iterator { get => this.iter(); } #! A mutator that writes the elements where they are, read of a deque that can #! be written. #! @param this The deque to walk. #! @returns The mutator, which is only good until the deque changes, moves, or #! closes. pub val deque::mutator: deque::mutator { get mut => this.iter(); } #! Hashes the provided deque. #! @param this The deque to hash. #! @returns The hashed value of the deque. #! @remarks Elements are hashed by their `hash`, so they are `hashable`. pub def deque::hash(&this): u64 { var code := this._len; for index in 0..this._len { code = mix(code, hash::(&this._dat[this.slot(index)])); } return code; } def deque::slot(&this, index: u64): u64 { return (this._hed + index) % this._cap; } def deque::release(&mut this, slot: u64): void { close(&mut this._dat[slot]); # The slot is emptied where writing over it would close what it held. if const T is closeable { memset((this._dat + slot) as *mut u8, 0, bytes_of::(1)); } } # Gives the deque room for more elements, doubling it. def deque::grow(&mut this): void { this.grow( this._cap != 0 ? this._cap * 2 : 4 ); } # Gives the deque room for a number of elements, where it has less, with them # from its first slot. An allocator that gives no memory stops the program, as # `acquire` does. def deque::grow(&mut this, size: u64): void { if size <= this._cap { return; } val block := acquire::(this._alc, size); val first := this._cap - this._hed; val lead := this._len < first ? this._len : first; memcpy(block as *mut u8, (this._dat + this._hed) as *mut u8, bytes_of::(lead)); memcpy((block + lead) as *mut u8, this._dat as *mut u8, bytes_of::(this._len - lead)); if this._dat != null { dispose::(this._alc, this._dat, this._cap); } this._dat = block; this._hed = 0; this._cap = size; }