128 lines
4.4 KiB
Plaintext
128 lines
4.4 KiB
Plaintext
#! @file column.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the column, which holds one field of the rows of a table, as the
|
|
#! kind of every type or the owner of every declaration, with a pointer to its
|
|
#! elements that it keeps where they are, so hot code reads and writes them
|
|
#! without a call and never through a pointer gone stale.
|
|
|
|
module sakura::analyzer;
|
|
|
|
|
|
#! One field of the rows of a table, by row: the field's values, one after
|
|
#! another in a block of the program's heap, and where they are.
|
|
#!
|
|
#! Tables keep each field in a column of its own, so a pass reading one field
|
|
#! reads only that, and code that reads them often does so through `p`, as
|
|
#! `kinds.p[id]`, without a call or a check. Whatever moves the values makes
|
|
#! `p` again, so only the column that grew is pointed again, and never another.
|
|
#! A column is no more than the pointer and two counts, so the pointers of a
|
|
#! table's columns, which hot code reads one after another, lie close together.
|
|
#! It owns its block, which it gives back as it closes, so a table that closes,
|
|
#! as a checker the language server makes for each check does, keeps nothing.
|
|
#! @tparam T The type of the field, which closes nothing.
|
|
pub struct column<T> : owning {
|
|
pub var p: *mut T; #! Where the values are, which the column keeps; read and written as `p[row]`, below `length`.
|
|
var count: u64; #! How many rows it holds.
|
|
var room: u64; #! How many rows the block has room for.
|
|
}
|
|
|
|
#! Makes a column holding no rows, which takes memory once one is added.
|
|
#! @returns The column.
|
|
pub def column<T>::new(): column<T> {
|
|
return column::<T> { p = null, count = 0, room = 0 };
|
|
}
|
|
|
|
#! Gives back the column's block, leaving it holding no rows.
|
|
#! @param this The column.
|
|
pub def column<T>::close(&mut this): void {
|
|
dispose::<T>(nothing {}, this.p, this.room);
|
|
this.p = null;
|
|
this.count = 0;
|
|
this.room = 0;
|
|
}
|
|
|
|
#! How many rows the column holds.
|
|
#! @param this The column.
|
|
#! @returns The number of rows.
|
|
pub val column<T>::length: u64 { get => this.count; }
|
|
|
|
#! Adds a row's value after the last.
|
|
#! @param this The column.
|
|
#! @param item The value.
|
|
pub def column<T>::append(&mut this, item: T): void {
|
|
if this.count == this.room {
|
|
this.grow();
|
|
}
|
|
this.p[this.count] = item;
|
|
this.count++;
|
|
}
|
|
|
|
#! Adds the values of a view after the last, in order.
|
|
#! @param this The column.
|
|
#! @param items The values.
|
|
pub def column<T>::extend(&mut this, items: span<T>): void {
|
|
return if items.length == 0;
|
|
val wanted := this.count + items.length;
|
|
if wanted > this.room {
|
|
this.grow(wanted > this.room * 2 ? wanted : this.room * 2);
|
|
}
|
|
memcpy((this.p + this.count) as *mut u8, items.data as *u8, bytes_of::<T>(items.length));
|
|
this.count = wanted;
|
|
}
|
|
|
|
#! Makes the column hold a number of rows, adding zeroed ones or letting go of
|
|
#! those past it.
|
|
#! @param this The column.
|
|
#! @param size How many rows it holds after.
|
|
pub def column<T>::resize(&mut this, size: u64): void {
|
|
# Growing doubles at least, so resizing a little at a time copies little.
|
|
if size > this.room {
|
|
this.grow(size > this.room * 2 ? size : this.room * 2);
|
|
}
|
|
if size > this.count {
|
|
memset((this.p + this.count) as *mut u8, 0, bytes_of::<T>(size - this.count));
|
|
}
|
|
this.count = size;
|
|
}
|
|
|
|
#! The value of a row, or a zero value past the last, as of a column by ID
|
|
#! that grows only as it is written.
|
|
#! @param this The column.
|
|
#! @param row The row.
|
|
#! @returns Its value.
|
|
pub def column<T>::at_or_zero(&this, row: u64): T where T: zeroable {
|
|
if row < this.count {
|
|
return this.p[row];
|
|
}
|
|
var none: T;
|
|
return none;
|
|
}
|
|
|
|
#! Writes the value of a row, growing the column to hold it where it is past
|
|
#! the last, the rows between holding zero values.
|
|
#! @param this The column.
|
|
#! @param row The row.
|
|
#! @param item Its value.
|
|
pub def column<T>::put(&mut this, row: u64, item: T): void {
|
|
if row >= this.count {
|
|
this.resize(row + 1);
|
|
}
|
|
this.p[row] = item;
|
|
}
|
|
|
|
# Gives the column room for twice as many rows, which is apart from `append`, so
|
|
# `append` is small enough to be inlined.
|
|
def column<T>::grow(&mut this): void {
|
|
this.grow(this.room == 0 ? 4 : this.room * 2);
|
|
}
|
|
|
|
# Gives the column room for a number of rows, moving its values, which `p`
|
|
# follows.
|
|
def column<T>::grow(&mut this, size: u64): void {
|
|
this.p = this.p == null ? acquire::<T>(nothing {}, size) : resize::<T>(nothing {}, this.p, this.room, size);
|
|
this.room = size;
|
|
}
|