///
import { HASH } from "./util/hash";
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
// @ts-ignore: decorator
@inline const INITIAL_CAPACITY = 4;
// @ts-ignore: decorator
@inline const FILL_FACTOR_N = 8;
// @ts-ignore: decorator
@inline const FILL_FACTOR_D = 3;
// @ts-ignore: decorator
@inline const FREE_FACTOR_N = 3;
// @ts-ignore: decorator
@inline const FREE_FACTOR_D = 4;
/** Structure of a set entry. */
@unmanaged class SetEntry {
key: K;
taggedNext: usize; // LSB=1 indicates EMPTY
}
/** Empty bit. */
// @ts-ignore: decorator
@inline const EMPTY: usize = 1 << 0;
/** Size of a bucket. */
// @ts-ignore: decorator
@inline const BUCKET_SIZE = sizeof();
/** Computes the alignment of an entry. */
// @ts-ignore: decorator
@inline
function ENTRY_ALIGN(): usize {
// can align to 4 instead of 8 if 32-bit and K is <= 32-bits
const align = (sizeof() > sizeof() ? sizeof() : sizeof()) - 1;
return align;
}
/** Computes the aligned size of an entry. */
// @ts-ignore: decorator
@inline
function ENTRY_SIZE(): usize {
const align = ENTRY_ALIGN();
const size = (offsetof>() + align) & ~align;
return size;
}
export class Set {
// buckets referencing their respective first entry, usize[bucketsMask + 1]
private buckets: ArrayBuffer = new ArrayBuffer(INITIAL_CAPACITY * BUCKET_SIZE);
private bucketsMask: u32 = INITIAL_CAPACITY - 1;
// entries in insertion order, SetEntry[entriesCapacity]
private entries: ArrayBuffer = new ArrayBuffer(INITIAL_CAPACITY * ENTRY_SIZE());
private entriesCapacity: i32 = INITIAL_CAPACITY;
private entriesOffset: i32 = 0;
private entriesCount: i32 = 0;
constructor() {
/* nop */
}
get size(): i32 {
return this.entriesCount;
}
clear(): void {
this.buckets = new ArrayBuffer(INITIAL_CAPACITY * BUCKET_SIZE);
this.bucketsMask = INITIAL_CAPACITY - 1;
this.entries = new ArrayBuffer(INITIAL_CAPACITY * ENTRY_SIZE());
this.entriesCapacity = INITIAL_CAPACITY;
this.entriesOffset = 0;
this.entriesCount = 0;
}
private find(key: T, hashCode: u32): SetEntry | null {
let entry = load>( // unmanaged!
changetype(this.buckets) + (hashCode & this.bucketsMask) * BUCKET_SIZE
);
while (entry) {
let taggedNext = entry.taggedNext;
if (!(taggedNext & EMPTY) && entry.key == key) return entry;
entry = changetype>(taggedNext & ~EMPTY);
}
return null;
}
@operator("[]")
has(key: T): bool {
return this.find(key, HASH(key)) != null;
}
add(key: T): this {
let hashCode = HASH(key);
let entry = this.find(key, hashCode); // unmanaged!
if (!entry) {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
this.rehash(
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
? this.bucketsMask // just rehash if 1/4+ entries are empty
: (this.bucketsMask << 1) | 1 // grow capacity to next 2^N
);
}
// append new entry
entry = changetype>(changetype(this.entries) + (this.entriesOffset++) * ENTRY_SIZE());
entry.key = key;
if (isManaged()) {
__link(changetype(this), changetype(key), true);
}
++this.entriesCount;
// link with previous entry in bucket
let bucketPtrBase = changetype(this.buckets) + (hashCode & this.bucketsMask) * BUCKET_SIZE;
entry.taggedNext = load(bucketPtrBase);
store(bucketPtrBase, changetype(entry));
}
return this;
}
@operator("[]=")
private __set(key: T, value: bool): void {
if (value) this.add(key);
else this.delete(key);
}
delete(key: T): bool {
let entry = this.find(key, HASH(key)); // unmanaged!
if (!entry) return false;
entry.taggedNext |= EMPTY;
--this.entriesCount;
// check if rehashing is appropriate
let halfBucketsMask = this.bucketsMask >> 1;
if (
halfBucketsMask + 1 >= max(INITIAL_CAPACITY, this.entriesCount) &&
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
) this.rehash(halfBucketsMask);
return true;
}
private rehash(newBucketsMask: u32): void {
let newBucketsCapacity = (newBucketsMask + 1);
let newBuckets = new ArrayBuffer(newBucketsCapacity * BUCKET_SIZE);
let newEntriesCapacity = newBucketsCapacity * FILL_FACTOR_N / FILL_FACTOR_D;
let newEntries = new ArrayBuffer(newEntriesCapacity * ENTRY_SIZE());
// copy old entries to new entries
let oldPtr = changetype(this.entries);
let oldEnd = oldPtr + this.entriesOffset * ENTRY_SIZE();
let newPtr = changetype(newEntries);
while (oldPtr != oldEnd) {
let oldEntry = changetype>(oldPtr); // unmanaged!
if (!(oldEntry.taggedNext & EMPTY)) {
let newEntry = changetype>(newPtr); // unmanaged!
let oldEntryKey = oldEntry.key;
newEntry.key = oldEntryKey;
let newBucketIndex = HASH(oldEntryKey) & newBucketsMask;
let newBucketPtrBase = changetype(newBuckets) + newBucketIndex * BUCKET_SIZE;
newEntry.taggedNext = load(newBucketPtrBase);
store(newBucketPtrBase, newPtr);
newPtr += ENTRY_SIZE();
}
oldPtr += ENTRY_SIZE();
}
this.buckets = newBuckets;
this.bucketsMask = newBucketsMask;
this.entries = newEntries;
this.entriesCapacity = newEntriesCapacity;
this.entriesOffset = this.entriesCount;
}
values(): T[] {
// FIXME: this is preliminary, needs iterators/closures
let start = changetype(this.entries);
let size = this.entriesOffset;
let values = new Array(size);
let length = 0;
for (let i = 0; i < size; ++i) {
let entry = changetype>(start + i * ENTRY_SIZE());
if (!(entry.taggedNext & EMPTY)) {
unchecked(values[length++] = entry.key);
}
}
values.length = length;
return values;
}
toString(): string {
return "[object Set]";
}
// RT integration
@unsafe private __visit(cookie: u32): void {
__visit(changetype(this.buckets), cookie);
let entries = changetype(this.entries);
if (isManaged()) {
let cur = entries;
let end = cur + this.entriesOffset * ENTRY_SIZE();
while (cur < end) {
let entry = changetype>(cur);
if (!(entry.taggedNext & EMPTY)) {
let val = changetype(entry.key);
if (isNullable()) {
if (val) __visit(val, cookie);
} else __visit(val, cookie);
}
cur += ENTRY_SIZE();
}
}
__visit(entries, cookie);
}
}