158 lines
4.7 KiB
Plaintext
158 lines
4.7 KiB
Plaintext
#! @file keyed.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the two ways a node is told apart from the nodes of other files,
|
|
#! whose indexes count from 0 as its own file's do: a `file_node`, its file and
|
|
#! itself, and a `table_node`, the result table it was checked in and itself,
|
|
#! which tells a node of a template checked for one instance from the same node
|
|
#! checked for another. What is kept of nodes across files or tables is keyed
|
|
#! by one of these, in a `keyed` map, which takes no bare node.
|
|
|
|
module sakura::analyzer;
|
|
|
|
|
|
#! What a `keyed` map's keys are: values each one word.
|
|
pub trait keyable {
|
|
#! The word the value is, which no other value of its type is.
|
|
#! @param this The value.
|
|
#! @returns The word.
|
|
def key(&this): u64;
|
|
}
|
|
|
|
#! A node of a file.
|
|
pub struct file_node : keyable, hashable, equatable {
|
|
pub var file: u32; #! The file's index.
|
|
pub var node: u32; #! The node, among the file's.
|
|
}
|
|
|
|
#! The word a file's node is: its file, then the node.
|
|
#! @param this The file's node.
|
|
#! @returns The word.
|
|
pub def file_node::key(&this): u64 {
|
|
return pair_key(this.file, this.node);
|
|
}
|
|
|
|
#! Hashes a file's node, as a map keyed by them finds it.
|
|
#! @param this The file's node.
|
|
#! @returns The hash.
|
|
pub def file_node::hash(&this): u64 {
|
|
return mix(this.key(), 0);
|
|
}
|
|
|
|
#! Whether two are the same node of the same file.
|
|
#! @param this The file's node.
|
|
#! @param other The other.
|
|
#! @returns Whether they are.
|
|
pub def file_node::equals(&this, other: &file_node): bool {
|
|
return this.file == other.file && this.node == other.node;
|
|
}
|
|
|
|
#! A node as a result table holds it: the table of its file, of an instance of
|
|
#! a generic, or of a pass of `for const`, and the node.
|
|
pub struct table_node : keyable, hashable, equatable {
|
|
pub var table: u32; #! The result table.
|
|
pub var node: u32; #! The node, among its file's.
|
|
}
|
|
|
|
#! The word a table's node is: its table, then the node.
|
|
#! @param this The table's node.
|
|
#! @returns The word.
|
|
pub def table_node::key(&this): u64 {
|
|
return pair_key(this.table, this.node);
|
|
}
|
|
|
|
#! Hashes a table's node, as a map keyed by them finds it.
|
|
#! @param this The table's node.
|
|
#! @returns The hash.
|
|
pub def table_node::hash(&this): u64 {
|
|
return mix(this.key(), 0);
|
|
}
|
|
|
|
#! Whether two are the same node of the same table.
|
|
#! @param this The table's node.
|
|
#! @param other The other.
|
|
#! @returns Whether they are.
|
|
pub def table_node::equals(&this, other: &table_node): bool {
|
|
return this.table == other.table && this.node == other.node;
|
|
}
|
|
|
|
#! A map from keys of one kind, as nodes of files or of tables, to IDs, held in a `keymap` by
|
|
#! the word each key is, which takes no key of another kind.
|
|
pub struct keyed<K> : owning where K: keyable {
|
|
var map: keymap; #! The IDs, by the word each key is.
|
|
}
|
|
|
|
#! Makes a map holding nothing.
|
|
#! @returns The map.
|
|
pub def keyed<K>::new(): keyed<K> {
|
|
return keyed::<K> { map = keymap::new() };
|
|
}
|
|
|
|
#! Lets go of what the map holds.
|
|
#! @param this The map.
|
|
pub def keyed<K>::close(&mut this): void {
|
|
this.map.close();
|
|
}
|
|
|
|
#! How many keys the map holds.
|
|
#! @param this The map.
|
|
#! @returns How many.
|
|
pub val keyed<K>::length: u64 { get => this.map.length; }
|
|
|
|
#! The ID a key has.
|
|
#! @param this The map.
|
|
#! @param at The key.
|
|
#! @returns The ID, or nothing where the key has none.
|
|
pub def keyed<K>::get(&this, at: &K): maybe<u32> {
|
|
return this.map.get(&at.key());
|
|
}
|
|
|
|
#! The ID a key has, or another where it has none.
|
|
#! @param this The map.
|
|
#! @param at The key.
|
|
#! @param missing What is given where the key has none.
|
|
#! @returns The ID.
|
|
pub def keyed<K>::get_or(&this, at: &K, missing: u32): u32 {
|
|
return this.map.get_or(&at.key(), missing);
|
|
}
|
|
|
|
#! Whether a key has an ID.
|
|
#! @param this The map.
|
|
#! @param at The key.
|
|
#! @returns Whether it has.
|
|
pub def keyed<K>::contains(&this, at: &K): bool {
|
|
return this.map.contains(&at.key());
|
|
}
|
|
|
|
#! Gives a key an ID, in place of any it had.
|
|
#! @param this The map.
|
|
#! @param at The key.
|
|
#! @param value The ID.
|
|
pub def keyed<K>::set(&mut this, at: K, value: u32): void {
|
|
this.map.set(at.key(), value);
|
|
}
|
|
|
|
#! Gives a key an ID, where it has none.
|
|
#! @param this The map.
|
|
#! @param at The key.
|
|
#! @param value The ID.
|
|
#! @returns Whether the key had none, and has the ID now.
|
|
pub def keyed<K>::insert(&mut this, at: K, value: u32): bool {
|
|
return this.map.insert(at.key(), value);
|
|
}
|
|
|
|
#! Takes a key's ID away, where it has one.
|
|
#! @param this The map.
|
|
#! @param at The key.
|
|
pub def keyed<K>::remove(&mut this, at: &K): void {
|
|
this.map.remove(&at.key());
|
|
}
|
|
|
|
#! Takes every key's ID away.
|
|
#! @param this The map.
|
|
pub def keyed<K>::clear(&mut this): void {
|
|
this.map.clear();
|
|
}
|