227 lines
7.3 KiB
TypeScript
Raw Normal View History

2019-03-26 23:35:08 +01:00
/// <reference path="./collector/index.d.ts" />
2019-03-13 03:47:35 +01:00
import { HASH } from "./util/hash";
2019-04-04 02:25:22 +02:00
import { __runtime_id, __gc_mark_members } from "./runtime";
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<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
let entries = this.entries;
2019-03-26 23:35:08 +01:00
entry = changetype<SetEntry<K>>(changetype<usize>(entries) + this.entriesOffset++ * ENTRY_SIZE<K>());
entry.key = key;
2019-03-20 17:12:33 +01:00
// link with the set
2019-03-26 23:35:08 +01:00
if (isManaged<K>()) {
if (isNullable<K>()) {
if (key !== null) {
if (isDefined(__ref_link)) __ref_link(changetype<usize>(key), changetype<usize>(this));
else if (isDefined(__ref_retain)) __ref_retain(changetype<usize>(key));
else assert(false);
}
} else {
if (isDefined(__ref_link)) __ref_link(changetype<usize>(key), changetype<usize>(this));
else if (isDefined(__ref_retain)) __ref_retain(changetype<usize>(key));
else assert(false);
}
}
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 {
var entry = this.find(key, HASH<K>(key));
2018-06-21 16:55:51 +02:00
if (!entry) return false;
2019-03-26 23:35:08 +01:00
if (isManaged<K>()) {
key = entry.key; // exact, e.g. string
if (isNullable<K>()) {
if (key !== null) {
if (isDefined(__ref_link)) {
if (isDefined(__ref_unlink)) __ref_unlink(changetype<usize>(key), changetype<usize>(this));
} else if (isDefined(__ref_retain)) __ref_release(changetype<usize>(key));
2019-03-26 23:35:08 +01:00
else assert(false);
}
} else {
if (isDefined(__ref_link)) {
if (isDefined(__ref_unlink)) __ref_unlink(changetype<usize>(key), changetype<usize>(this));
} else if (isDefined(__ref_retain)) __ref_release(changetype<usize>(key));
2019-03-26 23:35:08 +01:00
else assert(false);
}
}
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) {
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-21 10:44:14 +01:00
// GC integration
2019-04-02 10:12:57 +02:00
@unsafe private __traverse(): void {
__ref_mark(changetype<usize>(this.buckets));
2019-03-21 10:44:14 +01:00
var entries = this.entries;
2019-04-02 10:12:57 +02:00
__ref_mark(changetype<usize>(entries));
2019-03-21 10:44:14 +01:00
if (isManaged<K>()) {
let cur = changetype<usize>(entries);
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>()) {
if (val) {
2019-04-02 10:12:57 +02:00
__ref_mark(val);
2019-04-02 16:18:44 +02:00
__gc_mark_members(__runtime_id<K>(), val);
2019-03-30 13:58:20 +01:00
}
} else {
2019-04-02 10:12:57 +02:00
__ref_mark(val);
2019-04-02 16:18:44 +02:00
__gc_mark_members(__runtime_id<K>(), val);
2019-03-30 13:58:20 +01:00
}
}
2019-03-21 10:44:14 +01:00
cur += ENTRY_SIZE<K>();
}
}
}
2017-12-16 17:54:53 +01:00
}