192 lines
6.0 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
function ENTRY_ALIGN<K>(): usize {
2018-06-21 16:55:51 +02:00
// can align to 4 instead of 8 if 32-bit and K is <= 32-bits
const align = (sizeof<K>() > sizeof<usize>() ? sizeof<K>() : sizeof<usize>()) - 1;
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
function ENTRY_SIZE<K>(): usize {
2018-06-21 16:55:51 +02:00
const align = ENTRY_ALIGN<K>();
const size = (offsetof<SetEntry<K>>() + align) & ~align;
return size;
}
export class Set<K> {
// 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;
const entriesSize = INITIAL_CAPACITY * <i32>ENTRY_SIZE<K>();
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;
}
private find(key: K, hashCode: u32): SetEntry<K> | null {
2019-05-12 13:50:28 +02:00
var entry = load<SetEntry<K>>( // 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;
entry = changetype<SetEntry<K>>(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
}
2018-06-21 16:55:51 +02:00
has(key: K): bool {
return this.find(key, HASH<K>(key)) !== null;
2018-06-21 16:55:51 +02:00
}
2018-01-28 06:18:27 +01:00
2018-06-21 16:55:51 +02:00
add(key: K): void {
var hashCode = HASH<K>(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-05-12 13:50:28 +02:00
entry = changetype<SetEntry<K>>(changetype<usize>(this.entries) + this.entriesOffset++ * ENTRY_SIZE<K>());
entry.key = isManaged<K>()
? changetype<K>(__retain(changetype<usize>(key)))
: 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
}
}
2018-06-21 16:55:51 +02:00
delete(key: K): bool {
2019-05-12 13:50:28 +02:00
var entry = this.find(key, HASH<K>(key)); // unmanaged!
2018-06-21 16:55:51 +02:00
if (!entry) return false;
2019-05-12 13:50:28 +02:00
if (isManaged<K>()) __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-03-14 12:46:36 +01:00
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K>());
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);
2018-06-21 16:55:51 +02:00
var oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<K>();
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-05-12 13:50:28 +02:00
let oldEntry = changetype<SetEntry<K>>(oldPtr); // unmanaged!
2018-06-21 16:55:51 +02:00
if (!(oldEntry.taggedNext & EMPTY)) {
2019-05-12 13:50:28 +02:00
let newEntry = changetype<SetEntry<K>>(newPtr); // unmanaged!
2018-06-21 16:55:51 +02:00
newEntry.key = oldEntry.key;
let newBucketIndex = HASH<K>(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);
2018-06-21 16:55:51 +02:00
newPtr += ENTRY_SIZE<K>();
}
oldPtr += ENTRY_SIZE<K>();
}
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
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-03-21 10:44:14 +01:00
if (isManaged<K>()) {
2019-05-20 23:10:06 +02:00
let cur = entries;
2019-03-21 10:44:14 +01:00
let end = cur + <usize>this.entriesOffset * ENTRY_SIZE<K>();
while (cur < end) {
let entry = changetype<SetEntry<K>>(cur);
2019-03-30 13:58:20 +01:00
if (!(entry.taggedNext & EMPTY)) {
let val = changetype<usize>(entry.key);
if (isNullable<K>()) {
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-03-21 10:44:14 +01:00
cur += ENTRY_SIZE<K>();
}
}
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
}