#! @file list.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Defines an implementation for a `list` container. Lists are a structured #! container that stores each element in a node of its own, linked to the next. module std::containers; #! A `list` is a structured data container, singly linked, that stores each of #! its elements in a node of its own. #! #! They are useful where elements are added and removed at the front, 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 list from the #! front. #! #! It holds its first and last nodes, so pushing onto either end takes the same #! time however long it is, but only the front is popped without walking. #! @tparam T The type of its elements. pub struct list : owning , hashable , indexable , assignable , iterable , copyable , moveable , equatable { var _alc: maybe<&mut allocator>; var _hed: *mut list::node; var _tal: *mut list::node; var _len: u64; } #! An element of a list, with the node after it. struct list::node { var value: T; var next: *mut list::node; } #! Walks the elements of a list 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 list::iterator : sized_iterator<&T> { var _at: *mut list::node; var _left: u64; } #! Gives the next element of the list. #! @param this The iterator to advance. #! @returns Maybe a reference to the next element; otherwise, `nothing`. pub def list::iterator::next(&mut this): maybe<&T> { if this._at == null { return nothing {}; } val here := this._at; this._at = (*here).next; this._left = this._left - 1; return &(*here).value; } #! How many elements the iterator has left to give. #! @param this The iterator. #! @returns How many. pub def list::iterator::remaining(&this): u64 { return this._left; } #! Walks the elements of a list 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 list::mutator : sized_iterator<&mut T> { var _at: *mut list::node; var _left: u64; } #! Gives the next element of the list, to be written. #! @param this The mutator to advance. #! @returns Maybe a reference to the next element; otherwise, `nothing`. pub def list::mutator::next(&mut this): maybe<&mut T> { if this._at == null { return nothing {}; } val here := this._at; this._at = (*here).next; this._left = this._left - 1; return &mut (*here).value; } #! How many elements the mutator has left to give. #! @param this The mutator. #! @returns How many. pub def list::mutator::remaining(&this): u64 { return this._left; } #! Provides read only access to the length of a list. #! @param this The list to get the length of. #! @returns The length of the list. pub val list::length: u64 { get => this._len; } #! Indicates if a list is empty. #! @param this The list to check. #! @returns Whether the list is empty. pub val list::empty: bool { get => this._len == 0; } #! Creates a new list with no allocator. #! @returns The created list container. #! @remarks Will use the built in `raw_allocate` and `raw_release` for memory #! management. pub def list::new(): list { return list::::new(nothing{}); } #! Creates a new list with an optional allocator. #! @param alloc The optional allocator for the nodes of the list. #! @returns The created list container, which holds no elements yet. pub def list::new(alloc: maybe<&mut allocator>): list { return list:: { _alc = alloc, _hed = null, _tal = null, _len = 0 }; } #! Creates a copy of the provided list. #! @param other The list 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 list::copy_of(other: &list): list { var made := list::::new(other._alc); var at := other._hed; while at != null { made.push_back(copy::(&(*at).value)); at = (*at).next; } return made; } #! Creates a new list by moving the nodes of another list into it. #! @param other The list to move elements from, which holds none after. #! @returns The moved list, 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 list::move_of(other: &mut list): list { val made: list = list { _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 front. #! @param this The list 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 list::at(&this, index: u64): &T { expects(index < this._len); return &(*this.node_at(index)).value; } #! Gets the element at the provided index, walking from the front. #! @param this The list 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 list::at(&mut this, index: u64): &mut T { expects(index < this._len); return &mut (*this.node_at(index)).value; } #! Gets the element at an index, if the list holds one there, for an index #! that may be past the end in a program that is right. #! @param this The list to read. #! @param index The index of the element. #! @returns Maybe the element; otherwise, `nothing`. pub def list::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 list holds one there. #! @param this The list to write. #! @param index The index of the element. #! @returns Maybe the element; otherwise, `nothing`. pub def list::get(&mut this, index: u64): maybe<&mut T> { return nothing {} if index >= this._len; return &mut (*this.node_at(index)).value; } #! The element at the front of a list, which a list that can be written writes #! through. #! @param this The list to read, which stops a checked program where it is #! empty. #! @returns A reference to the element. pub val list::first: &T { get { expects(!this.empty, "The list is empty."); return &(*this._hed).value; } get mut { expects(!this.empty, "The list is empty."); return &mut (*this._hed).value; } } #! The element at the back of a list, which a list that can be written writes #! through. #! @param this The list to read, which stops a checked program where it is #! empty. #! @returns A reference to the element. pub val list::last: &T { get { expects(!this.empty, "The list is empty."); return &(*this._tal).value; } get mut { expects(!this.empty, "The list is empty."); return &mut (*this._tal).value; } } #! Pushes a new element onto the front of a list. #! @param this The list to push the value onto. #! @param value The value to push onto the list. #! @remarks An allocator that gives no more memory stops the program. pub def list::push_front(&mut this, value: T): void { val made := this.make(move(value)); (*made).next = this._hed; this._hed = made; if this._tal == null { this._tal = made; } this._len++; } #! Pushes a new element onto the back of a list. #! @param this The list to push the value onto. #! @param value The value to push onto the list. #! @remarks An allocator that gives no more memory stops the program. pub def list::push_back(&mut this, value: T): void { val made := this.make(move(value)); if this._tal == null { this._hed = made; } else { (*this._tal).next = made; } this._tal = made; this._len++; } #! Pops the element off the front of a list. #! @param this The list 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 list::pop_front(&mut this): void { expects(!this.empty, "The list is empty."); val gone := this._hed; this._hed = (*gone).next; if this._hed == null { this._tal = null; } this.release(gone); this._len--; } #! Adds an element at an index, after the elements before it. #! @param this The list 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 list::insert_at(&mut this, index: u64, value: T): void { expects(index <= this._len); if index == 0 { this.push_front(move(value)); return; } if index == this._len { this.push_back(move(value)); return; } val made := this.make(move(value)); val prev := this.node_at(index - 1); (*made).next = (*prev).next; (*prev).next = made; this._len++; } #! Removes the element at an index. #! @param this The list 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 list::remove_at(&mut this, index: u64): void { expects(index < this._len); if index == 0 { this.pop_front(); return; } this.unlink(this.node_at(index - 1)); } #! Removes the first element equal to the one given. #! @param this The list 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 list::remove(&mut this, elem: &T): bool { if this._len == 0 { return false; } if equals::(&(*this._hed).value, elem) { this.pop_front(); return true; } var prev := this._hed; while (*prev).next != null { if equals::(&(*(*prev).next).value, elem) { this.unlink(prev); return true; } prev = (*prev).next; } return false; } #! Tells whether the list holds an element equal to the one given. #! @param this The list to search. #! @param elem The element to look for. #! @returns Whether any element equals it. pub def list::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 list, disposing their nodes. #! @param this The list to clear. #! @remarks Elements that close, or hold what does, are closed. pub def list::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 list, disposing their nodes. #! @param this The list to close. pub def list::close(&mut this): void { this.clear(); } #! Compares one list to another by equality. #! @param this The list to compare against. #! @param other The list to compare with. #! @returns Whether both hold equal elements, in the same order. pub def list::equals(&this, other: &list): 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 an iterator that reads the elements from the front of the list, which #! `for x in l` walks. #! @param this The list to walk. #! @returns The iterator, which is only good until the list changes or closes. pub def list::iter(&this): list::iterator { return list::::iterator { _at = this._hed, _left = this._len }; } #! Lends a mutator that writes the elements where they are, from the front of #! the list, which `for &mut x in l` walks. #! @param this The list to walk. #! @returns The mutator, which is only good until the list changes or closes. pub def list::iter(&mut this): list::mutator { return list::::mutator { _at = this._hed, _left = this._len }; } #! An iterator that reads the elements from the front of the list, as `iter` #! lends. #! @param this The list to walk. #! @returns The iterator, which is only good until the list changes or closes. pub val list::iterator: list::iterator { get => this.iter(); } #! A mutator that writes the elements where they are, read of a list that can #! be written. #! @param this The list to walk. #! @returns The mutator, which is only good until the list changes or closes. pub val list::mutator: list::mutator { get mut => this.iter(); } #! Hashes the provided list. #! @param this The list to hash. #! @returns The hashed value of the list. #! @remarks Elements are hashed by their `hash`, so they are `hashable`. pub def list::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. def list::node_at(&this, index: u64): *mut list::node { var at := this._hed; for _ in 0..index { at = (*at).next; } return at; } # A node holding the value, from the list's allocator, which stops the program # where it gives no memory, as `acquire` does. def list::make(&mut this, value: T): *mut list::node { val made := acquire::::node>(this._alc, 1); (*made).value = move(value); (*made).next = null; return made; } def list::unlink(&mut this, prev: *mut list::node): void { val gone := (*prev).next; (*prev).next = (*gone).next; if gone == this._tal { this._tal = prev; } this.release(gone); this._len--; } def list::release(&mut this, node: *mut list::node): void { close(&mut (*node).value); dispose::::node>(this._alc, node, 1); }