187 lines
5.9 KiB
TypeScript
Raw Normal View History

2019-03-15 09:26:31 +01:00
import { LINK } from "./runtime";
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 {
var entry = load<SetEntry<K>>(
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(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(key);
2018-06-21 16:55:51 +02:00
var entry = this.find(key, hashCode);
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
let entries = this.entries;
entry = changetype<SetEntry<K>>(
2019-03-13 03:47:35 +01:00
changetype<usize>(entries) + this.entriesOffset++ * ENTRY_SIZE<K>()
2018-06-21 16:55:51 +02:00
);
entry.key = key;
++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));
2019-03-15 09:26:31 +01:00
if (isManaged<K>()) LINK(key, this);
2018-01-15 00:08:06 +01:00
}
}
2018-06-21 16:55:51 +02:00
delete(key: K): bool {
var entry = this.find(key, HASH<K>(key));
2018-06-21 16:55:51 +02:00
if (!entry) return false;
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) {
let oldEntry = changetype<SetEntry<K>>(oldPtr);
if (!(oldEntry.taggedNext & EMPTY)) {
let newEntry = changetype<SetEntry<K>>(newPtr);
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-03-13 03:47:35 +01:00
// private __gc(): void {
// __gc_mark(changetype<usize>(this.buckets)); // tslint:disable-line
// var entries = this.entries;
// __gc_mark(changetype<usize>(entries)); // tslint:disable-line
// if (isManaged<K>()) {
// let offset: usize = 0;
// let end: usize = this.entriesOffset * ENTRY_SIZE<K>();
// while (offset < end) {
// let entry = changetype<SetEntry<K>>(
// changetype<usize>(entries) + HEADER_SIZE_AB + offset * ENTRY_SIZE<K>()
// );
// if (!(entry.taggedNext & EMPTY)) __gc_mark(changetype<usize>(entry.key)); // tslint:disable-line
// offset += ENTRY_SIZE<K>();
// }
// }
// }
2017-12-16 17:54:53 +01:00
}