#! @file chain.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Defines an implementation for a `chain` container. Chains are a structured #! container that stores each element in a node of its own, linked to the nodes #! before and after it. module std::containers; #! A `chain` is a structured data container, doubly linked, that stores each of #! its elements in a node of its own. #! #! They are useful where elements are added and removed at either end, and where #! an element must never move once it is added, since each keeps its own node #! until it is removed. Reaching an element by index walks the chain from #! whichever end is nearer. #! @tparam T The type of its elements. pub struct chain : owning , hashable , indexable , iterable , copyable , moveable , equatable { var _alc: maybe<&mut allocator>; var _hed: *mut chain::node; var _tal: *mut chain::node; var _len: u64; } #! An element of a chain, with the nodes before and after it. struct chain::node { var value: T; var prev: *mut chain::node; var next: *mut chain::node; } #! Walks the elements of a chain 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 chain::cursor : sized_iterator<&T>, backward_iterator<&T> { var _at: *mut chain::node; var _end: *mut chain::node; var _left: u64; } #! Gives the next element of the chain. #! @param this The cursor to advance. #! @returns Maybe a reference to the next element; otherwise, `nothing`. pub def chain::cursor::next(&mut this): maybe<&T> { if this._left == 0 { return nothing {}; } val here := this._at; this._at = (*here).next; this._left = this._left - 1; return &(*here).value; } #! How many elements the cursor has left to give. #! @param this The cursor. #! @returns How many. pub def chain::cursor::remaining(&this): u64 { return this._left; } #! Gives the last element the cursor has left, from the back of the chain. #! @param this The cursor to advance from its far end. #! @returns Maybe a reference to the element; otherwise, `nothing`. pub def chain::cursor::next_back(&mut this): maybe<&T> { if this._left == 0 { return nothing {}; } val here := this._end; this._end = (*here).prev; this._left = this._left - 1; return &(*here).value; } #! Walks the elements of a chain 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 chain::mut_cursor : sized_iterator<&mut T>, backward_iterator<&mut T> { var _at: *mut chain::node; var _end: *mut chain::node; var _left: u64; } #! Gives the next element of the chain, to be written. #! @param this The cursor to advance. #! @returns Maybe a reference to the next element; otherwise, `nothing`. pub def chain::mut_cursor::next(&mut this): maybe<&mut T> { if this._left == 0 { return nothing {}; } val here := this._at; this._at = (*here).next; this._left = this._left - 1; return &mut (*here).value; } #! How many elements the cursor has left to give. #! @param this The cursor. #! @returns How many. pub def chain::mut_cursor::remaining(&this): u64 { return this._left; } #! Gives the last element the cursor has left, from the back of the chain, to #! be written. #! @param this The cursor to advance from its far end. #! @returns Maybe a reference to the element; otherwise, `nothing`. pub def chain::mut_cursor::next_back(&mut this): maybe<&mut T> { if this._left == 0 { return nothing {}; } val here := this._end; this._end = (*here).prev; this._left = this._left - 1; return &mut (*here).value; } #! Provides read only access to the length of a chain. #! @param this The chain to get the length of. #! @returns The length of the chain. pub val chain::length: u64 { get => this._len; } #! Indicates if a chain is empty. #! @param this The chain to check. #! @returns Whether the chain is empty. pub val chain::is_empty: bool { get => this._len == 0; } #! Creates a new chain with no allocator. #! @returns The created chain container. #! @remarks Will use the built in `raw_allocate` and `raw_release` for memory #! management. pub def chain::new(): chain { return chain::::new(nothing{}); } #! Creates a new chain with an optional allocator. #! @param alloc The optional allocator for the nodes of the chain. #! @returns The created chain container, which holds no elements yet. pub def chain::new(alloc: maybe<&mut allocator>): chain { return chain:: { _alc = alloc, _hed = null, _tal = null, _len = 0 }; } #! Creates a copy of the provided chain. #! @param other The chain to copy. #! @returns The copy, which owns its own nodes 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 chain::copy_of(other: &chain): chain { var made := chain::::new(other._alc); var at := other._hed; while at != null { made.push_back(copy::(&(*at).value)); at = (*at).next; } return made; } #! Creates a new chain by moving the nodes of another chain into it. #! @param other The chain to move elements from, which holds none after. #! @returns The moved chain, which takes the nodes and allocator with it. #! @remarks Nothing is copied and no element is touched: the nodes move as they #! stand, and the other is left empty rather than dead, so closing it #! afterwards is harmless. pub def chain::move_of(other: &mut chain): chain { val made: chain = chain { _alc = other._alc, _hed = other._hed, _tal = other._tal, _len = other._len }; other._hed = null; other._tal = null; other._len = 0; return made; } #! Gets the element at the provided index, walking from the nearer end. #! @param this The chain 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 chain::at(&this, index: u64): &T { if const checking() { if index >= this._len { past_end(index, this._len); } } return &(*this.node_at(index)).value; } #! Gets the element at the provided index, walking from the nearer end. #! @param this The chain 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 chain::at(&mut this, index: u64): &mut T { if const checking() { if index >= this._len { past_end(index, this._len); } } return &mut (*this.node_at(index)).value; } #! Gets the element at an index, if the chain holds one there, for an index #! that may be past the end in a program that is right. #! @param this The chain to read. #! @param index The index of the element. #! @returns Maybe the element; otherwise, `nothing`. pub def chain::get(&this, index: u64): maybe<&T> { return nothing {} if index >= this._len; return &(*this.node_at(index)).value; } #! Gets the element at an index to write it, if the chain holds one there. #! @param this The chain to write. #! @param index The index of the element. #! @returns Maybe the element; otherwise, `nothing`. pub def chain::get(&mut this, index: u64): maybe<&mut T> { return nothing {} if index >= this._len; return &mut (*this.node_at(index)).value; } #! Gets the element at the front of a chain. #! @param this The chain to get the front of, which stops a checked program #! where it is empty. #! @returns A reference to an immutable element. pub def chain::front(&this): &T { if const checking() { if this._len == 0 { fault("The chain is empty."); } } return &(*this._hed).value; } #! Gets the element at the front of a chain. #! @param this The chain to get the front of, which stops a checked program #! where it is empty. #! @returns A reference to a mutable element. pub def chain::front(&mut this): &mut T { if const checking() { if this._len == 0 { fault("The chain is empty."); } } return &mut (*this._hed).value; } #! Gets the element at the back of a chain. #! @param this The chain to get the back of, which stops a checked program #! where it is empty. #! @returns A reference to an immutable element. pub def chain::back(&this): &T { if const checking() { if this._len == 0 { fault("The chain is empty."); } } return &(*this._tal).value; } #! Gets the element at the back of a chain. #! @param this The chain to get the back of, which stops a checked program #! where it is empty. #! @returns A reference to a mutable element. pub def chain::back(&mut this): &mut T { if const checking() { if this._len == 0 { fault("The chain is empty."); } } return &mut (*this._tal).value; } #! Pushes a new element onto the front of a chain. #! @param this The chain to push the value onto. #! @param value The value to push onto the chain. #! @remarks An allocator that gives no more memory stops the program. pub def chain::push_front(&mut this, value: T): void { this.link(null, this._hed, move(value)); } #! Pushes a new element onto the back of a chain. #! @param this The chain to push the value onto. #! @param value The value to push onto the chain. #! @remarks An allocator that gives no more memory stops the program. pub def chain::push_back(&mut this, value: T): void { this.link(this._tal, null, move(value)); } #! Pops the element off the front of a chain. #! @param this The chain 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 chain::pop_front(&mut this): void { if const checking() { if this._len == 0 { fault("The chain is empty."); } } this.unlink(this._hed); } #! Pops the element off the back of a chain. #! @param this The chain 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 chain::pop_back(&mut this): void { if const checking() { if this._len == 0 { fault("The chain is empty."); } } this.unlink(this._tal); } #! Adds an element at an index, after the elements before it. #! @param this The chain to insert into. #! @param index Where the element goes, from `0` to `length`, where `length` #! appends. An index past that stops a checked program. #! @param value The element to insert. #! @remarks An allocator that gives no more memory stops the program. pub def chain::insert_at(&mut this, index: u64, value: T): void { if const checking() { if index > this._len { past_end(index, this._len); } } if index == this._len { this.push_back(move(value)); return; } val next := this.node_at(index); this.link((*next).prev, next, move(value)); } #! Removes the element at an index. #! @param this The chain to remove from. #! @param index The index of the element, below `length`, past which a checked #! program stops. #! @remarks The element is closed where it closes, or holds what does. pub def chain::remove_at(&mut this, index: u64): void { if const checking() { if index >= this._len { past_end(index, this._len); } } this.unlink(this.node_at(index)); } #! Removes the first element equal to the one given. #! @param this The chain to remove from. #! @param elem The element to look for. #! @returns Whether an element equalled it and was removed. #! @remarks The element is closed where it closes, or holds what does. pub def chain::remove(&mut this, elem: &T): bool { var at := this._hed; while at != null { if equals::(&(*at).value, elem) { this.unlink(at); return true; } at = (*at).next; } return false; } #! Tells whether the chain holds an element equal to the one given. #! @param this The chain to search. #! @param elem The element to look for. #! @returns Whether any element equals it. pub def chain::contains(&this, elem: &T): bool { var at := this._hed; while at != null { return true if equals::(&(*at).value, elem); at = (*at).next; } return false; } #! Removes all elements from a chain, disposing their nodes. #! @param this The chain to clear. #! @remarks Elements that close, or hold what does, are closed. pub def chain::clear(&mut this): void { var at := this._hed; while at != null { val next := (*at).next; this.release(at); at = next; } this._hed = null; this._tal = null; this._len = 0; } #! Removes all elements of a chain, disposing their nodes. #! @param this The chain to close. pub def chain::close(&mut this): void { this.clear(); } #! Compares one chain to another by equality. #! @param this The chain to compare against. #! @param other The chain to compare with. #! @returns Whether both hold equal elements, in the same order. pub def chain::equals(&this, other: &chain): bool { if this._len != other._len { return false; } var mine := this._hed; var theirs := other._hed; while mine != null { return false if !equals::(&(*mine).value, &(*theirs).value); mine = (*mine).next; theirs = (*theirs).next; } return true; } #! Lends a cursor that reads the elements from the front of the chain, which #! `for x in c` walks. #! @param this The chain to walk. #! @returns The cursor, which is only good until the chain changes or closes. pub def chain::iter(&this): chain::cursor { return chain::::cursor { _at = this._hed, _end = this._tal, _left = this._len }; } #! Lends a cursor that writes the elements where they are, from the front of the #! chain, which `for &mut x in c` walks. #! @param this The chain to walk. #! @returns The cursor, which is only good until the chain changes or closes. pub def chain::iter(&mut this): chain::mut_cursor { return chain::::mut_cursor { _at = this._hed, _end = this._tal, _left = this._len }; } #! Hashes the provided chain. #! @param this The chain to hash. #! @returns The hashed value of the chain. #! @remarks Elements are hashed by their `hash`, so they are `hashable`. pub def chain::hash(&this): u64 { var code := this._len; var at := this._hed; while at != null { code = mix(code, hash::(&(*at).value)); at = (*at).next; } return code; } # The node at an index, which is below the length, walked to from the nearer end. def chain::node_at(&this, index: u64): *mut chain::node { if index < this._len / 2 { var at := this._hed; for _ in 0..index { at = (*at).next; } return at; } var at := this._tal; for _ in 0..(this._len - 1 - index) { at = (*at).prev; } return at; } def chain::link( &mut this, prev: *mut chain::node, next: *mut chain::node, value: T ): void { # An allocator that gives no memory stops the program, as `acquire` does. val made := acquire::::node>(this._alc, 1); (*made).value = move(value); (*made).prev = prev; (*made).next = next; if prev == null { this._hed = made; } else { (*prev).next = made; } if next == null { this._tal = made; } else { (*next).prev = made; } this._len++; } def chain::unlink(&mut this, node: *mut chain::node): void { if (*node).prev == null { this._hed = (*node).next; } else { (*(*node).prev).next = (*node).next; } if (*node).next == null { this._tal = (*node).prev; } else { (*(*node).next).prev = (*node).prev; } this.release(node); this._len--; } def chain::release(&mut this, node: *mut chain::node): void { close(&mut (*node).value); dispose::::node>(this._alc, node, 1); }