126 lines
3.9 KiB
Plaintext
126 lines
3.9 KiB
Plaintext
#! @file idindex.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the ID index, which interning finds values by their hash with: the
|
||
|
|
#! type table its types, and the checker its known values and constants.
|
||
|
|
|
||
|
|
module sakura::analyzer;
|
||
|
|
|
||
|
|
|
||
|
|
#! The IDs of interned values by their hash, held open: a power of two of
|
||
|
|
#! slots, a quarter of them free at least, each holding the top half of a
|
||
|
|
#! value's hash beside one more than its ID, or 0 where it is free. A value's
|
||
|
|
#! search begins at the slot its hash's top half says, and goes on to the next
|
||
|
|
#! until a free one, so it reads only eight bytes for each value it passes, and
|
||
|
|
#! compares only those whose hash shares its top half.
|
||
|
|
#!
|
||
|
|
#! What is interned keeps its own fields, which the one interning compares for
|
||
|
|
#! each ID a walk gives, and gives its values IDs in the order they are made,
|
||
|
|
#! which no hash decides.
|
||
|
|
pub struct id_index : owning {
|
||
|
|
var slots: *mut u64; #! Each slot's hash's top half and one more than its ID, or 0.
|
||
|
|
var count: u64; #! How many IDs it holds.
|
||
|
|
var room: u64; #! How many slots there are, a power of two, or 0.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where a walk over the IDs of one hash is.
|
||
|
|
pub struct id_walk {
|
||
|
|
var at: u64; #! The slot it reads next.
|
||
|
|
var tag: u64; #! The top half of the hash, as each slot holds it.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Makes an index holding no ID, which takes no memory until it holds one.
|
||
|
|
#! @returns The index.
|
||
|
|
pub def id_index::new(): id_index {
|
||
|
|
return id_index { slots = null, count = 0, room = 0 };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives back the index's block, leaving it empty.
|
||
|
|
#! @param this The index.
|
||
|
|
pub def id_index::close(&mut this): void {
|
||
|
|
dispose::<u64>(nothing {}, this.slots, this.room);
|
||
|
|
this.slots = null;
|
||
|
|
this.count = 0;
|
||
|
|
this.room = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many IDs the index holds.
|
||
|
|
#! @param this The index.
|
||
|
|
#! @returns How many.
|
||
|
|
pub val id_index::length: u64 { get => this.count; }
|
||
|
|
|
||
|
|
#! How many slots the index has, which is more than it holds.
|
||
|
|
#! @param this The index.
|
||
|
|
#! @returns How many.
|
||
|
|
pub val id_index::capacity: u64 { get => this.room; }
|
||
|
|
|
||
|
|
#! A walk over the IDs whose values may have the hash, which `next` gives.
|
||
|
|
#! @param this The index.
|
||
|
|
#! @param hash The hash.
|
||
|
|
#! @returns The walk.
|
||
|
|
@inline
|
||
|
|
pub def id_index::walk(&this, hash: u64): id_walk {
|
||
|
|
val tag := hash >> 32;
|
||
|
|
return id_walk { at = this.room == 0 ? 0 : tag & (this.room - 1), tag = tag };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! One more than the next ID whose value may have the walk's hash, or 0 where
|
||
|
|
#! there are no more.
|
||
|
|
#! @param this The index.
|
||
|
|
#! @param walk The walk.
|
||
|
|
#! @returns One more than the ID, or 0.
|
||
|
|
@inline
|
||
|
|
pub def id_index::next(&this, walk: &mut id_walk): u32 {
|
||
|
|
return 0 if this.room == 0;
|
||
|
|
val mask := this.room - 1;
|
||
|
|
while true {
|
||
|
|
val held := this.slots[walk.at];
|
||
|
|
return 0 if held == 0;
|
||
|
|
walk.at = (walk.at + 1) & mask;
|
||
|
|
if (held >> 32) == walk.tag {
|
||
|
|
return (held & 0xFFFFFFFF) as u32;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds the ID of a value of the hash, which it does not hold yet.
|
||
|
|
#! @param this The index.
|
||
|
|
#! @param hash The value's hash.
|
||
|
|
#! @param id Its ID.
|
||
|
|
pub def id_index::add(&mut this, hash: u64, id: u32): void {
|
||
|
|
if (this.count + 1) * 4 > this.room * 3 {
|
||
|
|
this.grow(this.room == 0 ? 64 : this.room * 2);
|
||
|
|
}
|
||
|
|
this.put((hash >> 32) << 32 | (id as u64 + 1));
|
||
|
|
this.count++;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Puts a slot's word in the first free slot from where its hash's top half
|
||
|
|
# says, which there is.
|
||
|
|
def id_index::put(&mut this, word: u64): void {
|
||
|
|
val mask := this.room - 1;
|
||
|
|
var at := (word >> 32) & mask;
|
||
|
|
while this.slots[at] != 0 {
|
||
|
|
at = (at + 1) & mask;
|
||
|
|
}
|
||
|
|
this.slots[at] = word;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Moves the IDs into a block of a number of slots, a power of two.
|
||
|
|
def id_index::grow(&mut this, size: u64): void {
|
||
|
|
val old := this.slots;
|
||
|
|
val old_room := this.room;
|
||
|
|
this.slots = acquire_zeroed::<u64>(nothing {}, size);
|
||
|
|
this.room = size;
|
||
|
|
for i in 0..old_room {
|
||
|
|
if old[i] != 0 {
|
||
|
|
this.put(old[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if old_room != 0 {
|
||
|
|
dispose::<u64>(nothing {}, old, old_room);
|
||
|
|
}
|
||
|
|
}
|