#! @file runs.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Defines an implementation for a `runs` container. Runs hold many short lists, #! each a run of elements one after another in one block, named by an ID. module std::containers; #! A `runs` holds many lists of elements, each a run of them one after another #! in one block, which a `u32` ID names. #! #! They are useful wherever many short lists are made and kept, such as the #! parts of each type, the parameters of each function, or the children of each #! node, which a vector each would scatter over the heap. #! #! The elements of every run are held in one vector, in the order the runs were #! made, and where each run begins in another. A run is made by adding a view of #! its elements, or by pushing them one at a time and then ending it. Runs are #! never changed, and none is removed but by `rewind` and `clear`, so an ID is #! good for as long as its run is. ID 0 is the empty run, which every empty view #! added is given. #! @tparam T The type of the elements. pub struct runs : owning , copyable , moveable { var _items: vector; var _start: vector; } #! Provides read only access to how many runs there are. #! @param this The runs to count. #! @returns The number of runs, the empty one included, which is one more than #! the newest's ID. pub val runs::count: u64 { get => this._start.length - 1; } #! Provides read only access to how many elements the runs hold together. #! @param this The runs to count the elements of. #! @returns The number of elements, those of a run still being pushed included. pub val runs::size: u64 { get => this._items.length; } #! Creates new runs with no allocator, holding only the empty run. #! @returns The created runs. #! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize` #! for memory management. pub def runs::new(): runs { return runs::::new(nothing{}); } #! Creates new runs with an optional allocator, holding only the empty run. #! @param alloc The optional allocator for the memory backing of the runs. #! @returns The created runs. pub def runs::new(alloc: maybe<&mut allocator>): runs { var made := runs:: { _items = vector::::new(alloc), _start = vector::::new(alloc) }; made._start.resize(2); return made; } #! Builds runs holding copies of another's, each with the same ID, with its #! allocator. #! @param other The runs to copy from. #! @returns The copy, which owns its own memory and elements. #! @remarks Elements are copied by `copyable` where they provide one. pub def runs::copy_of(other: &runs): runs { return runs:: { _items = vector::::copy_of(&other._items), _start = vector::::copy_of(&other._start) }; } #! Creates new runs by moving the data of others into them. #! @param other The runs to move from, which hold nothing after, not even the #! empty run, until they are cleared. #! @returns The moved runs, which take the memory and allocator with them. pub def runs::move_of(other: &mut runs): runs { return runs:: { _items = move(&mut other._items), _start = move(&mut other._start) }; } #! Adds a run of copies of elements. #! @param this The runs to add to. #! @param items The elements of the run, in order. #! @returns The run's ID, or 0, the empty run's, where there are no elements. #! @remarks Elements are copied as a vector's `extend` copies them. Elements #! pushed and not yet ended begin the run. An allocator that gives no #! more memory stops the program. pub def runs::add(&mut this, items: span): u32 { this._items.extend(items); return this.end(); } #! Adds an element to the run being made, which `end` ends. #! @param this The runs to add to. #! @param item The element, which follows those pushed before it. pub def runs::push(&mut this, item: T): void { this._items.append(move(item)); } #! Ends the run being made, of the elements pushed since the last run ended. #! @param this The runs to end the run of. #! @returns The run's ID, or 0, the empty run's, where none was pushed. pub def runs::end(&mut this): u32 { val open := this._start.data[this._start.length - 1]; return 0 if this._items.length == open; val id := this._start.length - 1; this._start.append(this._items.length); return id as u32; } #! How many elements a run holds. #! @param this The runs. #! @param id The run's ID, below `count`, past which a checked program #! stops. #! @returns The number of its elements. pub def runs::length(&this, id: u32): u64 { if const checking() { if id as u64 + 1 >= this._start.length { past_end(id as u64, this._start.length - 1); } } return this._start.data[id as u64 + 1] - this._start.data[id]; } #! Reads an element of a run. #! @param this The runs. #! @param id The run's ID, below `count`, past which a checked program #! stops. #! @param index The element's place in the run, below its length, past which a #! checked program stops. #! @returns The element, which is only good until the runs grow or close. pub def runs::at(&this, id: u32, index: u64): &T { if const checking() { if id as u64 + 1 >= this._start.length { past_end(id as u64, this._start.length - 1); } } val start := this._start.data[id]; val length := this._start.data[id as u64 + 1] - start; if const checking() { if index >= length { past_end(index, length); } } return &this._items.data[start + index]; } #! Lends a view of a run's elements. #! @param this The runs. #! @param id The run's ID, below `count`, past which a checked program #! stops. #! @returns The view, which is only good until the runs grow or close, so a loop #! that adds runs while it walks one reads it by `at` instead. pub def runs::view(&this, id: u32): span { val next := id as u64 + 1; if const checking() { if next >= this._start.length { past_end(id as u64, this._start.length - 1); } } val start := this._start.data[id]; return span::::over(this._items.data + start, this._start.data[next] - start); } #! Removes the runs from an ID on, so that the next run made is given it. #! @param this The runs to remove from. #! @param count How many runs to keep, from 1, which keeps only the empty one, #! to `count`, which keeps them all. #! @remarks Elements removed are closed where they close, or hold what does, and #! so are those pushed and not yet ended. pub def runs::rewind(&mut this, count: u64): void { if const checking() { if count == 0 || count > this.count { past_end(count, this.count + 1); } } this._items.resize(this._start.data[count]); this._start.resize(count + 1); } #! Removes every run but the empty one, keeping the memory. #! @param this The runs to empty. #! @remarks Elements are closed where they close, or hold what does. pub def runs::clear(&mut this): void { this._items.clear(); this._start.clear(); this._start.resize(2); } #! Removes every run and gives back the memory the runs hold. #! @param this The runs to close. #! @remarks Elements are closed where they close, or hold what does. Closed runs #! hold not even the empty run, until they are cleared. pub def runs::close(&mut this): void { this._items.close(); this._start.close(); } # Stops the program where an ID names no run. def runs::check(&this, id: u32): void { if const checking() { if id as u64 + 1 >= this._start.length { past_end(id as u64, this._start.length - 1); } } }