#! @file queue.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Defines an implementation for a `queue` container. Queues are a structured #! container where data is pushed onto the back and popped off the front, in a #! first-in-first-out (FIFO) manner. module std::containers; #! A `queue` is a structured data container that stores its content in a first- #! in-first-out (FIFO) manner. #! #! They are useful wherever work is handled in the order it arrives, such as #! event loops, breadth-first searches, and buffers between a producer and a #! consumer. #! #! 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 queue : owning , hashable , 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 queue. #! @param this The queue to get the length of. #! @returns The length of the queue. pub val queue::length: u64 { get => this._len; } #! Provides read only access to the capacity of a queue. #! @param this The queue to get the capacity of. #! @returns The capacity of the queue. pub val queue::capacity: u64 { get => this._cap; } #! Indicates if a queue is empty. #! @param this The queue to check. #! @returns Whether the queue is empty. pub val queue::is_empty: bool { get => this._len == 0; } #! Indicates if a queue is full. #! @param this The queue to check. #! @returns Whether the queue is full. pub val queue::is_full: bool { get => this._len == this._cap; } #! Creates a new queue with no allocator and a default size. #! @returns The created queue container. #! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize` #! for memory management. pub def queue::new(): queue { return queue::::new(4, nothing{}); } #! Creates a new queue with an optional allocator and a default size. #! @param alloc The optional allocator for the memory backing of the queue. #! @returns The created queue container. pub def queue::new(alloc: maybe<&mut allocator>): queue { return queue::::new(4, alloc); } #! Creates a new queue with an optional allocator and a provided size. #! @param size The initial capacity of the queue, which may be none. #! @param alloc The optional allocator for the memory backing of the queue. #! @returns The created queue container, which holds no elements yet. pub def queue::new(size: u64, alloc: maybe<&mut allocator>): queue { var made: queue = queue { _alc = alloc, _dat = null, _hed = 0, _len = 0, _cap = 0 }; made.reserve(size); return made; } #! Creates a copy of the provided queue. #! @param other The queue 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 queue::copy_of(other: &queue): queue { var made := queue::::new(other._len, other._alc); for index in 0..other._len { made.push(copy::(&other._dat[other.slot(index)])); } return made; } #! Creates a new queue by moving the data of another queue into it. #! @param other The queue to move elements from, which holds none after. #! @returns The moved queue, 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 queue::move_of(other: &mut queue): queue { val made: queue = queue { _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; } #! Pushes a new element onto the back of a queue. #! @param this The queue to push the value onto. #! @param value The value to push onto the queue. #! @remarks An allocator that gives no more memory stops the program. pub def queue::push(&mut this, value: T): void { if this.is_full { this.grow(); } this._dat[this.slot(this._len)] = move(value); this._len++; } #! Pops the element off the front of a queue. #! @param this The queue 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 queue::pop(&mut this): void { if const checking() { if this._len == 0 { fault("The queue is empty."); } } this.release(this._hed); this._hed = (this._hed + 1) % this._cap; this._len--; } #! Peeks the element at the front of a queue without removing it. #! @param this The queue to peek the front of, which stops a checked program #! where it is empty. #! @returns A reference to an immutable element. pub def queue::peek(&this): &T { if const checking() { if this._len == 0 { fault("The queue is empty."); } } return &this._dat[this._hed]; } #! Peeks the element at the front of a queue without removing it. #! @param this The queue to peek the front of, which stops a checked program #! where it is empty. #! @returns A reference to a mutable element. pub def queue::peek(&mut this): &mut T { if const checking() { if this._len == 0 { fault("The queue is empty."); } } return &mut this._dat[this._hed]; } #! Reserves an amount of memory for the number of elements requested for the #! backing memory of the queue provided. #! @param this The queue to reserve elements for. #! @param count The number of elements to reserve for a queue. pub def queue::reserve(&mut this, count: u64): void { this.grow(count); } #! Removes all elements from a queue. #! @param this The queue to clear. #! @remarks Elements that close, or hold what does, are closed. The backing data #! is kept but not disposed. pub def queue::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 queue then disposes the backing memory. #! @param this The queue to close. pub def queue::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 queue to another by equality. #! @param this The queue to compare against. #! @param other The queue to compare with. #! @returns Whether both hold equal elements, in the same order. pub def queue::equals(&this, other: &queue): 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; } #! Walks the elements of a queue from its front, giving a reference that reads #! each in turn, which is what `iter(&this)` lends and `for x` and `for &x` #! walk. pub struct queue::cursor : sized_iterator<&T>, backward_iterator<&T> { var _dat: *mut T; var _at: u64; var _cap: u64; var _left: u64; } #! Gives the next element of the queue. #! @param this The cursor to advance. #! @returns Maybe a reference to the next element; otherwise, `nothing`. pub def queue::cursor::next(&mut this): maybe<&T> { if this._left == 0 { return nothing {}; } val here := this._at; this._at = (here + 1) % this._cap; this._left = this._left - 1; return &this._dat[here]; } #! How many elements the cursor has left to give. #! @param this The cursor. #! @returns How many. pub def queue::cursor::remaining(&this): u64 { return this._left; } #! Gives the last element the cursor has left, from the back of the queue. #! @param this The cursor to advance from its far end. #! @returns Maybe a reference to the element; otherwise, `nothing`. pub def queue::cursor::next_back(&mut this): maybe<&T> { if this._left == 0 { return nothing {}; } this._left = this._left - 1; return &this._dat[(this._at + this._left) % this._cap]; } #! Walks the elements of a queue from its front, giving a reference that writes #! each in turn, which is what `iter(&mut this)` lends and `for &mut x` walks. pub struct queue::mut_cursor : sized_iterator<&mut T>, backward_iterator<&mut T> { var _dat: *mut T; var _at: u64; var _cap: u64; var _left: u64; } #! Gives the next element of the queue, to be written. #! @param this The cursor to advance. #! @returns Maybe a reference to the next element; otherwise, `nothing`. pub def queue::mut_cursor::next(&mut this): maybe<&mut T> { if this._left == 0 { return nothing {}; } val here := this._at; this._at = (here + 1) % this._cap; this._left = this._left - 1; return &mut this._dat[here]; } #! How many elements the cursor has left to give. #! @param this The cursor. #! @returns How many. pub def queue::mut_cursor::remaining(&this): u64 { return this._left; } #! Gives the last element the cursor has left, from the back of the queue, to #! be written. #! @param this The cursor to advance from its far end. #! @returns Maybe a reference to the element; otherwise, `nothing`. pub def queue::mut_cursor::next_back(&mut this): maybe<&mut T> { if this._left == 0 { return nothing {}; } this._left = this._left - 1; return &mut this._dat[(this._at + this._left) % this._cap]; } #! Lends a cursor that reads the elements from the front of the queue, which #! `for x in q` walks. #! @param this The queue to walk. #! @returns The cursor, which is only good until the queue changes or closes. pub def queue::iter(&this): queue::cursor { return queue::::cursor { _dat = this._dat, _at = this._hed, _cap = this._cap, _left = this._len }; } #! Lends a cursor that writes the elements where they are, from the front of the #! queue, which `for &mut x in q` walks. #! @param this The queue to walk. #! @returns The cursor, which is only good until the queue changes or closes. pub def queue::iter(&mut this): queue::mut_cursor { return queue::::mut_cursor { _dat = this._dat, _at = this._hed, _cap = this._cap, _left = this._len }; } #! Hashes the provided queue. #! @param this The queue to hash. #! @returns The hashed value of the queue. #! @remarks Elements are hashed by their `hash`, so they are `hashable`. pub def queue::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 queue::slot(&this, index: u64): u64 { return (this._hed + index) % this._cap; } def queue::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 queue room for more elements, doubling it. def queue::grow(&mut this): void { this.grow( this._cap != 0 ? this._cap * 2 : 4 ); } # Gives the queue 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 queue::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; }