206 lines
6.4 KiB
TypeScript
Raw Normal View History

2019-05-12 13:50:28 +02:00
/// <reference path="./rt/index.d.ts" />
2019-03-26 23:35:08 +01:00
2019-03-13 03:47:35 +01:00
import { HASH } from "./util/hash";
2018-01-15 00:08:06 +01:00
2018-06-21 16:55:51 +02:00
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
2018-01-15 00:08:06 +01:00
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@inline
const INITIAL_CAPACITY = 4;
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@inline
const FILL_FACTOR: f64 = 8 / 3;
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@inline
const FREE_FACTOR: f64 = 3 / 4;
2018-01-15 00:08:06 +01:00
2018-06-21 16:55:51 +02:00
/** Structure of a set entry. */
@unmanaged class SetEntry<K> {
key: K;
taggedNext: usize; // LSB=1 indicates EMPTY
}
2018-01-15 00:08:06 +01:00
2018-06-21 16:55:51 +02:00
/** Empty bit. */
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@inline
const EMPTY: usize = 1 << 0;
2018-01-15 00:08:06 +01:00
2018-06-21 16:55:51 +02:00
/** Size of a bucket. */
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@inline
const BUCKET_SIZE = sizeof<usize>();
2018-01-28 06:18:27 +01:00
2018-06-21 16:55:51 +02:00
/** Computes the alignment of an entry. */
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@inline
2019-06-01 05:12:30 +02:00
function ENTRY_ALIGN<T>(): usize {
2018-06-21 16:55:51 +02:00
// can align to 4 instead of 8 if 32-bit and K is <= 32-bits
2019-06-01 05:12:30 +02:00
const align = (sizeof<T>() > sizeof<usize>() ? sizeof<T>() : sizeof<usize>()) - 1;
2018-06-21 16:55:51 +02:00
return align;
}
/** Computes the aligned size of an entry. */
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@inline
2019-06-01 05:12:30 +02:00
function ENTRY_SIZE<T>(): usize {
const align = ENTRY_ALIGN<T>();
const size = (offsetof<SetEntry<T>>() + align) & ~align;
2018-06-21 16:55:51 +02:00
return size;
}
2019-06-01 05:12:30 +02:00
export class Set<T> {
2018-06-21 16:55:51 +02:00
// buckets holding references to the respective first entry within
private buckets: ArrayBuffer; // usize[bucketsMask + 1]
private bucketsMask: u32;
// entries in insertion order
private entries: ArrayBuffer; // SetEntry<K>[entriesCapacity]
private entriesCapacity: i32;
private entriesOffset: i32;
private entriesCount: i32;
get size(): i32 { return this.entriesCount; }
constructor() { this.clear(); }
clear(): void {
const bucketsSize = INITIAL_CAPACITY * <i32>BUCKET_SIZE;
this.buckets = new ArrayBuffer(bucketsSize);
this.bucketsMask = INITIAL_CAPACITY - 1;
2019-06-01 05:12:30 +02:00
const entriesSize = INITIAL_CAPACITY * <i32>ENTRY_SIZE<T>();
2019-03-14 12:46:36 +01:00
this.entries = new ArrayBuffer(entriesSize);
2018-06-21 16:55:51 +02:00
this.entriesCapacity = INITIAL_CAPACITY;
this.entriesOffset = 0;
this.entriesCount = 0;
}
2019-06-01 05:12:30 +02:00
private find(key: T, hashCode: u32): SetEntry<T> | null {
var entry = load<SetEntry<T>>( // unmanaged!
2019-03-13 03:47:35 +01:00
changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE
2018-06-21 16:55:51 +02:00
);
while (entry) {
if (!(entry.taggedNext & EMPTY) && entry.key == key) return entry;
2019-06-01 05:12:30 +02:00
entry = changetype<SetEntry<T>>(entry.taggedNext & ~EMPTY);
2018-02-25 23:21:32 +01:00
}
2018-06-21 16:55:51 +02:00
return null;
2018-01-15 00:08:06 +01:00
}
2019-06-01 05:12:30 +02:00
has(key: T): bool {
return this.find(key, HASH<T>(key)) !== null;
2018-06-21 16:55:51 +02:00
}
2018-01-28 06:18:27 +01:00
2019-06-01 05:12:30 +02:00
add(key: T): void {
var hashCode = HASH<T>(key);
2019-03-20 17:12:33 +01:00
var entry = this.find(key, hashCode); // unmanaged!
2018-06-21 16:55:51 +02:00
if (!entry) {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
this.rehash(
this.entriesCount < <i32>(this.entriesCapacity * FREE_FACTOR)
? this.bucketsMask // just rehash if 1/4+ entries are empty
: (this.bucketsMask << 1) | 1 // grow capacity to next 2^N
);
2018-01-15 00:08:06 +01:00
}
2018-06-21 16:55:51 +02:00
// append new entry
2019-06-01 05:12:30 +02:00
entry = changetype<SetEntry<T>>(changetype<usize>(this.entries) + this.entriesOffset++ * ENTRY_SIZE<T>());
entry.key = isManaged<T>()
? changetype<T>(__retain(changetype<usize>(key)))
2019-05-12 13:50:28 +02:00
: key;
2018-06-21 16:55:51 +02:00
++this.entriesCount;
// link with previous entry in bucket
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
2019-03-13 03:47:35 +01:00
entry.taggedNext = load<usize>(bucketPtrBase);
store<usize>(bucketPtrBase, changetype<usize>(entry));
2018-01-15 00:08:06 +01:00
}
}
2019-06-01 05:12:30 +02:00
delete(key: T): bool {
var entry = this.find(key, HASH<T>(key)); // unmanaged!
2018-06-21 16:55:51 +02:00
if (!entry) return false;
2019-06-01 05:12:30 +02:00
if (isManaged<T>()) __release(changetype<usize>(entry.key)); // exact 'key'
2018-06-21 16:55:51 +02:00
entry.taggedNext |= EMPTY;
--this.entriesCount;
// check if rehashing is appropriate
var halfBucketsMask = this.bucketsMask >> 1;
if (
halfBucketsMask + 1 >= max<u32>(INITIAL_CAPACITY, this.entriesCount) &&
this.entriesCount < <i32>(this.entriesCapacity * FREE_FACTOR)
) this.rehash(halfBucketsMask);
return true;
2018-01-15 00:08:06 +01:00
}
2018-06-21 16:55:51 +02:00
private rehash(newBucketsMask: u32): void {
var newBucketsCapacity = <i32>(newBucketsMask + 1);
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
2019-06-01 05:12:30 +02:00
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<T>());
2018-06-21 16:55:51 +02:00
// copy old entries to new entries
2019-03-13 03:47:35 +01:00
var oldPtr = changetype<usize>(this.entries);
2019-06-01 05:12:30 +02:00
var oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<T>();
2019-03-13 03:47:35 +01:00
var newPtr = changetype<usize>(newEntries);
2018-06-21 16:55:51 +02:00
while (oldPtr != oldEnd) {
2019-06-01 05:12:30 +02:00
let oldEntry = changetype<SetEntry<T>>(oldPtr); // unmanaged!
2018-06-21 16:55:51 +02:00
if (!(oldEntry.taggedNext & EMPTY)) {
2019-06-01 05:12:30 +02:00
let newEntry = changetype<SetEntry<T>>(newPtr); // unmanaged!
2018-06-21 16:55:51 +02:00
newEntry.key = oldEntry.key;
2019-06-01 05:12:30 +02:00
let newBucketIndex = HASH<T>(oldEntry.key) & newBucketsMask;
2018-06-21 16:55:51 +02:00
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
2019-03-13 03:47:35 +01:00
newEntry.taggedNext = load<usize>(newBucketPtrBase);
store<usize>(newBucketPtrBase, newPtr);
2019-06-01 05:12:30 +02:00
newPtr += ENTRY_SIZE<T>();
2018-06-21 16:55:51 +02:00
}
2019-06-01 05:12:30 +02:00
oldPtr += ENTRY_SIZE<T>();
2018-06-21 16:55:51 +02:00
}
2018-01-28 06:18:27 +01:00
2018-06-21 16:55:51 +02:00
this.buckets = newBuckets;
this.bucketsMask = newBucketsMask;
this.entries = newEntries;
this.entriesCapacity = newEntriesCapacity;
this.entriesOffset = this.entriesCount;
2018-01-15 00:08:06 +01:00
}
2018-08-02 18:23:02 +02:00
2019-06-01 05:12:30 +02:00
values(): T[] {
// FIXME: this is preliminary, needs iterators/closures
var start = changetype<usize>(this.entries);
var size = this.entriesOffset;
2019-06-01 05:12:30 +02:00
var values = Array.create<T>(size);
for (let i = 0; i < size; ++i) {
2019-06-01 05:12:30 +02:00
let entry = changetype<SetEntry<T>>(start + <usize>i * ENTRY_SIZE<T>());
if (!(entry.taggedNext & EMPTY)) {
values.push(entry.key);
}
}
return values;
}
toString(): string {
return "[object Set]";
}
2019-05-20 23:10:06 +02:00
// RT integration
2019-03-21 10:44:14 +01:00
2019-05-20 23:10:06 +02:00
@unsafe private __visit_impl(cookie: u32): void {
2019-05-12 13:50:28 +02:00
__visit(changetype<usize>(this.buckets), cookie);
2019-05-20 23:10:06 +02:00
var entries = changetype<usize>(this.entries);
2019-06-01 05:12:30 +02:00
if (isManaged<T>()) {
2019-05-20 23:10:06 +02:00
let cur = entries;
2019-06-01 05:12:30 +02:00
let end = cur + <usize>this.entriesOffset * ENTRY_SIZE<T>();
2019-03-21 10:44:14 +01:00
while (cur < end) {
2019-06-01 05:12:30 +02:00
let entry = changetype<SetEntry<T>>(cur);
2019-03-30 13:58:20 +01:00
if (!(entry.taggedNext & EMPTY)) {
let val = changetype<usize>(entry.key);
2019-06-01 05:12:30 +02:00
if (isNullable<T>()) {
2019-05-20 23:10:06 +02:00
if (val) __visit(val, cookie);
} else __visit(val, cookie);
2019-03-30 13:58:20 +01:00
}
2019-06-01 05:12:30 +02:00
cur += ENTRY_SIZE<T>();
2019-03-21 10:44:14 +01:00
}
}
2019-05-20 23:10:06 +02:00
__visit(entries, cookie);
2019-03-21 10:44:14 +01:00
}
2017-12-16 17:54:53 +01:00
}