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-06-21 16:55:51 +02:00
|
|
|
|
|
|
|
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
|
|
|
|
|
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-15 09:26:31 +01:00
|
|
|
@inline
|
|
|
|
const FILL_FACTOR: f64 = 8 / 3;
|
2019-03-14 05:11:03 +01:00
|
|
|
|
|
|
|
// @ts-ignore: decorator
|
2019-03-15 09:26:31 +01:00
|
|
|
@inline
|
|
|
|
const FREE_FACTOR: f64 = 3 / 4;
|
2018-06-21 16:55:51 +02:00
|
|
|
|
|
|
|
/** Structure of a map entry. */
|
|
|
|
@unmanaged class MapEntry<K,V> {
|
|
|
|
key: K;
|
|
|
|
value: V;
|
|
|
|
taggedNext: usize; // LSB=1 indicates EMPTY
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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-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-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,V>(): usize {
|
2018-06-21 16:55:51 +02:00
|
|
|
// can align to 4 instead of 8 if 32-bit and K/V is <= 32-bits
|
|
|
|
const maxkv = sizeof<K>() > sizeof<V>() ? sizeof<K>() : sizeof<V>();
|
|
|
|
const align = (maxkv > sizeof<usize>() ? maxkv : 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,V>(): usize {
|
2018-06-21 16:55:51 +02:00
|
|
|
const align = ENTRY_ALIGN<K,V>();
|
|
|
|
const size = (offsetof<MapEntry<K,V>>() + align) & ~align;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:54:53 +01:00
|
|
|
export class Map<K,V> {
|
2018-01-29 22:36:07 +01:00
|
|
|
|
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; // MapEntry<K,V>[entriesCapacity]
|
|
|
|
private entriesCapacity: i32;
|
|
|
|
private entriesOffset: i32;
|
|
|
|
private entriesCount: i32;
|
|
|
|
|
|
|
|
get size(): i32 { return this.entriesCount; }
|
2018-01-29 22:36:07 +01:00
|
|
|
|
2019-03-15 13:13:48 +01:00
|
|
|
constructor() {
|
|
|
|
this.clear();
|
|
|
|
}
|
2018-01-29 22:36:07 +01:00
|
|
|
|
2018-06-21 16:55:51 +02:00
|
|
|
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,V>();
|
2019-03-14 07:45:59 +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;
|
2018-01-29 22:36:07 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 16:55:51 +02:00
|
|
|
private find(key: K, hashCode: u32): MapEntry<K,V> | null {
|
2019-05-12 13:50:28 +02:00
|
|
|
var entry = load<MapEntry<K,V>>( // 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<MapEntry<K,V>>(entry.taggedNext & ~EMPTY);
|
2018-02-25 23:21:32 +01:00
|
|
|
}
|
2018-01-29 22:36:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
has(key: K): bool {
|
2018-12-08 23:38:49 +01:00
|
|
|
return this.find(key, HASH<K>(key)) !== null;
|
2018-06-21 16:55:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get(key: K): V {
|
2018-12-08 23:38:49 +01:00
|
|
|
var entry = this.find(key, HASH<K>(key));
|
2018-06-21 16:55:51 +02:00
|
|
|
return entry ? entry.value : <V>unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
set(key: K, value: V): void {
|
2018-12-08 23:38:49 +01:00
|
|
|
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) {
|
2019-05-25 19:16:09 +02:00
|
|
|
if (isManaged<V>()) {
|
|
|
|
let oldRef = changetype<usize>(entry.value);
|
|
|
|
if (changetype<usize>(value) != oldRef) {
|
|
|
|
entry.value = changetype<V>(__retain(changetype<usize>(value)));
|
|
|
|
__release(oldRef);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entry.value = value;
|
|
|
|
}
|
2018-06-21 16:55:51 +02:00
|
|
|
} else {
|
|
|
|
// 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-02-25 23:21:32 +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<MapEntry<K,V>>(changetype<usize>(entries) + this.entriesOffset++ * ENTRY_SIZE<K,V>());
|
2019-03-20 17:12:33 +01:00
|
|
|
// link with the map
|
2019-05-12 13:50:28 +02:00
|
|
|
entry.key = isManaged<K>()
|
|
|
|
? changetype<K>(__retain(changetype<usize>(key)))
|
|
|
|
: key;
|
|
|
|
entry.value = isManaged<V>()
|
|
|
|
? changetype<V>(__retain(changetype<usize>(value)))
|
|
|
|
: value;
|
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-02-25 23:21:32 +01:00
|
|
|
}
|
2018-01-29 22:36:07 +01:00
|
|
|
}
|
|
|
|
|
2018-06-21 16:55:51 +02:00
|
|
|
delete(key: K): bool {
|
2018-12-08 23:38:49 +01:00
|
|
|
var entry = this.find(key, HASH<K>(key));
|
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));
|
|
|
|
if (isManaged<V>()) __release(changetype<usize>(entry.value));
|
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-29 22:36:07 +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 07:45:59 +01:00
|
|
|
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K,V>());
|
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,V>();
|
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<MapEntry<K,V>>(oldPtr);
|
|
|
|
if (!(oldEntry.taggedNext & EMPTY)) {
|
|
|
|
let newEntry = changetype<MapEntry<K,V>>(newPtr);
|
|
|
|
newEntry.key = oldEntry.key;
|
|
|
|
newEntry.value = oldEntry.value;
|
2018-12-08 23:38:49 +01:00
|
|
|
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,V>();
|
|
|
|
}
|
|
|
|
oldPtr += ENTRY_SIZE<K,V>();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buckets = newBuckets;
|
|
|
|
this.bucketsMask = newBucketsMask;
|
|
|
|
this.entries = newEntries;
|
|
|
|
this.entriesCapacity = newEntriesCapacity;
|
|
|
|
this.entriesOffset = this.entriesCount;
|
2018-01-29 22:36:07 +01:00
|
|
|
}
|
2019-05-28 15:49:56 +02:00
|
|
|
|
|
|
|
keys(): K[] {
|
|
|
|
// FIXME: this is preliminary, needs iterators/closures
|
|
|
|
var start = changetype<usize>(this.entries);
|
|
|
|
var size = this.entriesOffset;
|
|
|
|
var keys = Array.create<K>(size);
|
|
|
|
for (let i = 0; i < size; ++i) {
|
|
|
|
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
|
|
|
|
if (!(entry.taggedNext & EMPTY)) {
|
|
|
|
keys.push(entry.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
values(): V[] {
|
|
|
|
// FIXME: this is preliminary, needs iterators/closures
|
|
|
|
var start = changetype<usize>(this.entries);
|
|
|
|
var size = this.entriesOffset;
|
|
|
|
var values = Array.create<V>(size);
|
|
|
|
for (let i = 0; i < size; ++i) {
|
|
|
|
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
|
|
|
|
if (!(entry.taggedNext & EMPTY)) {
|
|
|
|
values.push(entry.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
2018-08-02 18:23:02 +02:00
|
|
|
|
2018-11-18 12:43:44 +02:00
|
|
|
toString(): string {
|
|
|
|
return "[object Map]";
|
|
|
|
}
|
|
|
|
|
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>() || isManaged<V>()) {
|
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,V>();
|
|
|
|
while (cur < end) {
|
|
|
|
let entry = changetype<MapEntry<K,V>>(cur);
|
|
|
|
if (!(entry.taggedNext & EMPTY)) {
|
2019-03-30 13:58:20 +01:00
|
|
|
if (isManaged<K>()) {
|
|
|
|
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
|
|
|
}
|
|
|
|
if (isManaged<V>()) {
|
|
|
|
let val = changetype<usize>(entry.value);
|
|
|
|
if (isNullable<V>()) {
|
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,V>();
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|