230 lines
8.2 KiB
Plaintext
230 lines
8.2 KiB
Plaintext
#! @file groups.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Defines an implementation for a `groups` container. Groups hold elements
|
||
|
|
#! gathered by a key numbered from zero, those of each key one after another, so
|
||
|
|
#! that the elements of a key are a view.
|
||
|
|
module std::containers;
|
||
|
|
|
||
|
|
|
||
|
|
#! A `groups` holds elements gathered by key, those of each key one after
|
||
|
|
#! another in one block, so that the elements of a key are a view of it.
|
||
|
|
#!
|
||
|
|
#! They are useful wherever items are listed by what they belong to, such as
|
||
|
|
#! the reads of each value, the edges into each block, or the members of each
|
||
|
|
#! scope, which a vector for each key would scatter over the heap.
|
||
|
|
#!
|
||
|
|
#! Groups are filled in two passes, as compressed sparse rows are: the first
|
||
|
|
#! counts the elements of each key, `seal` works out where those of each key
|
||
|
|
#! begin, and the second puts each element in its place. The elements of a key
|
||
|
|
#! keep the order they were put in. The keys are numbered from zero, up to the
|
||
|
|
#! number the groups are made or reset for.
|
||
|
|
#! @tparam T The type of the elements.
|
||
|
|
pub struct groups<T>
|
||
|
|
: owning
|
||
|
|
, moveable
|
||
|
|
{
|
||
|
|
var _first: vector<u64>; # Where the elements of each key begin, then where the last end: zeros until sealed.
|
||
|
|
var _place: vector<u64>; # How many elements each key has until sealed, then where the next of each goes.
|
||
|
|
var _items: vector<T>;
|
||
|
|
var _sealed: bool;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Provides read only access to how many keys the groups have.
|
||
|
|
#! @param this The groups.
|
||
|
|
#! @returns The number of keys, each of which has a group, empty or not.
|
||
|
|
pub val groups<T>::keys: u64 {
|
||
|
|
get => this._first.length == 0 ? 0 : this._first.length - 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Provides read only access to how many elements the groups hold together.
|
||
|
|
#! @param this The groups.
|
||
|
|
#! @returns The number of elements, which is how many were counted once the
|
||
|
|
#! groups are sealed, and 0 before.
|
||
|
|
pub val groups<T>::size: u64 {
|
||
|
|
get => this._items.length;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Indicates whether the groups are sealed, so that elements are put rather
|
||
|
|
#! than counted.
|
||
|
|
#! @param this The groups.
|
||
|
|
#! @returns Whether `seal` was called since they were made or reset.
|
||
|
|
pub val groups<T>::is_sealed: bool {
|
||
|
|
get => this._sealed;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates groups for a number of keys, with no allocator, ready to count.
|
||
|
|
#! @param keys The number of keys.
|
||
|
|
#! @returns The created groups, each empty.
|
||
|
|
#! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize`
|
||
|
|
#! for memory management.
|
||
|
|
pub def groups<T>::new(keys: u64): groups<T> {
|
||
|
|
return groups::<T>::new(keys, nothing{});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates groups for a number of keys, with an optional allocator, ready to
|
||
|
|
#! count.
|
||
|
|
#! @param keys The number of keys.
|
||
|
|
#! @param alloc The optional allocator for the memory backing of the groups.
|
||
|
|
#! @returns The created groups, each empty.
|
||
|
|
pub def groups<T>::new(keys: u64, alloc: maybe<&mut allocator>): groups<T> {
|
||
|
|
var made := groups::<T> {
|
||
|
|
_first = vector::<u64>::new(alloc),
|
||
|
|
_place = vector::<u64>::new(alloc),
|
||
|
|
_items = vector::<T>::new(alloc),
|
||
|
|
_sealed = false
|
||
|
|
};
|
||
|
|
made._first.resize(keys + 1);
|
||
|
|
made._place.resize(keys);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates new groups by moving the data of others into them.
|
||
|
|
#! @param other The groups to move from, which have no keys after.
|
||
|
|
#! @returns The moved groups, which take the memory and allocator with them.
|
||
|
|
pub def groups<T>::move_of(other: &mut groups<T>): groups<T> {
|
||
|
|
val made := groups::<T> {
|
||
|
|
_first = move(&mut other._first),
|
||
|
|
_place = move(&mut other._place),
|
||
|
|
_items = move(&mut other._items),
|
||
|
|
_sealed = other._sealed
|
||
|
|
};
|
||
|
|
other._first.resize(1);
|
||
|
|
other._sealed = false;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Empties the groups and gives them a number of keys, ready to count again,
|
||
|
|
#! keeping the memory.
|
||
|
|
#! @param this The groups to reset.
|
||
|
|
#! @param keys The number of keys.
|
||
|
|
#! @remarks Elements are closed where they close, or hold what does.
|
||
|
|
pub def groups<T>::reset(&mut this, keys: u64): void {
|
||
|
|
this._items.clear();
|
||
|
|
this._place.clear();
|
||
|
|
this._place.resize(keys);
|
||
|
|
this._first.clear();
|
||
|
|
this._first.resize(keys + 1);
|
||
|
|
this._sealed = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Counts an element of a key, which is put once the groups are sealed.
|
||
|
|
#! @param this The groups, which are not sealed, or else the program stops.
|
||
|
|
#! @param key The key, below `keys`, past which a checked program stops.
|
||
|
|
pub def groups<T>::count(&mut this, key: u64): void {
|
||
|
|
this.count(key, 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Counts a number of elements of a key, which are put once the groups are
|
||
|
|
#! sealed.
|
||
|
|
#! @param this The groups, which are not sealed, or else the program stops.
|
||
|
|
#! @param key The key, below `keys`, past which a checked program stops.
|
||
|
|
#! @param many How many elements.
|
||
|
|
pub def groups<T>::count(&mut this, key: u64, many: u64): void {
|
||
|
|
if this._sealed {
|
||
|
|
fault("The groups are counted after they were sealed.");
|
||
|
|
}
|
||
|
|
this.check(key);
|
||
|
|
this._place.data[key] += many;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Works out where the elements of each key begin, from those counted, so that
|
||
|
|
#! they can be put.
|
||
|
|
#! @param this The groups, which are not sealed, or else the program stops.
|
||
|
|
#! @remarks Every key's elements are made room for at once, zeroed until they
|
||
|
|
#! are put. An allocator that gives no more memory stops the program.
|
||
|
|
pub def groups<T>::seal(&mut this): void {
|
||
|
|
if this._sealed {
|
||
|
|
fault("The groups are sealed a second time.");
|
||
|
|
}
|
||
|
|
|
||
|
|
val keys := this._first.length - 1;
|
||
|
|
val first := this._first.data;
|
||
|
|
val place := this._place.data;
|
||
|
|
for key in 0..keys {
|
||
|
|
first[key + 1] = first[key] + place[key];
|
||
|
|
place[key] = first[key];
|
||
|
|
}
|
||
|
|
this._items.resize(first[keys]);
|
||
|
|
this._sealed = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Puts an element of a key in its place, after those of the key put before it.
|
||
|
|
#! @param this The groups, which are sealed, or else a checked program stops.
|
||
|
|
#! @param key The key, below `keys`, past which a checked program stops, with
|
||
|
|
#! an element counted that is not put yet, or else a checked
|
||
|
|
#! program stops.
|
||
|
|
#! @param item The element.
|
||
|
|
#! @remarks Each check stops a checked program as indexing past an end does,
|
||
|
|
#! which keeps `put` short enough to be written where it is called.
|
||
|
|
pub def groups<T>::put(&mut this, key: u64, item: T): void {
|
||
|
|
if const checking() {
|
||
|
|
if key >= this._place.length {
|
||
|
|
past_end(key, this._place.length);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# Groups not sealed end every key at zero, so no element fits there.
|
||
|
|
val at := this._place.data[key];
|
||
|
|
val end := this._first.data[key + 1];
|
||
|
|
if const checking() {
|
||
|
|
if at >= end {
|
||
|
|
past_end(at, end);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this._items.data[at] = move(item);
|
||
|
|
this._place.data[key] = at + 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many elements a key has.
|
||
|
|
#! @param this The groups.
|
||
|
|
#! @param key The key, below `keys`, past which a checked program stops.
|
||
|
|
#! @returns The number of its elements: those counted, before the groups are
|
||
|
|
#! sealed, and those made room for after.
|
||
|
|
pub def groups<T>::length(&this, key: u64): u64 {
|
||
|
|
this.check(key);
|
||
|
|
if !this._sealed {
|
||
|
|
return this._place.data[key];
|
||
|
|
}
|
||
|
|
return this._first.data[key + 1] - this._first.data[key];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lends a view of the elements of a key, in the order they were put.
|
||
|
|
#! @param this The groups.
|
||
|
|
#! @param key The key, below `keys`, past which a checked program stops.
|
||
|
|
#! @returns The view, which is only good until the groups are reset or closed,
|
||
|
|
#! and whose elements not put yet are zero. Before the groups are
|
||
|
|
#! sealed, it is empty.
|
||
|
|
pub def groups<T>::of(&this, key: u64): span<T> {
|
||
|
|
# Groups not sealed begin and end every key at zero, so its view is empty.
|
||
|
|
val next := key + 1;
|
||
|
|
if const checking() {
|
||
|
|
if next >= this._first.length {
|
||
|
|
past_end(key, this._first.length - 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val start := this._first.data[key];
|
||
|
|
return span::<T>::over(this._items.data + start, this._first.data[next] - start);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes every element and every key, and gives back the memory the groups
|
||
|
|
#! hold.
|
||
|
|
#! @param this The groups to close.
|
||
|
|
#! @remarks Elements are closed where they close, or hold what does.
|
||
|
|
pub def groups<T>::close(&mut this): void {
|
||
|
|
this._items.close();
|
||
|
|
this._place.close();
|
||
|
|
this._first.close();
|
||
|
|
this._sealed = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Stops the program where a key is not one of the groups'.
|
||
|
|
def groups<T>::check(&this, key: u64): void {
|
||
|
|
if const checking() {
|
||
|
|
if key + 1 >= this._first.length {
|
||
|
|
past_end(key, this.keys);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|