mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-26 07:22:21 +00:00
Move Map and Set to stdlib, fixes #17
This commit is contained in:
parent
7ed55f7ea6
commit
1626e50b0f
2
dist/asc.js
vendored
2
dist/asc.js
vendored
File diff suppressed because one or more lines are too long
2
dist/asc.js.map
vendored
2
dist/asc.js.map
vendored
File diff suppressed because one or more lines are too long
@ -27,16 +27,4 @@ export class ArrayBuffer {
|
|||||||
move_memory(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
|
move_memory(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// internals
|
|
||||||
|
|
||||||
static readonly HEADER_SIZE: usize = HEADER_SIZE;
|
|
||||||
|
|
||||||
@inline load<T>(index: usize): T {
|
|
||||||
return load<T>(changetype<usize>(this) + index * sizeof<T>(), HEADER_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@inline store<T>(index: usize, value: T): void {
|
|
||||||
store<T>(changetype<usize>(this) + index * sizeof<T>(), value, HEADER_SIZE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,41 +1,165 @@
|
|||||||
|
import {
|
||||||
|
HEADER_SIZE as HEADER_SIZE_AB
|
||||||
|
} from "./internal/arraybuffer";
|
||||||
|
|
||||||
|
import {
|
||||||
|
hash
|
||||||
|
} from "./internal/hash";
|
||||||
|
|
||||||
|
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
|
||||||
|
|
||||||
|
const INITIAL_CAPACITY = 4;
|
||||||
|
const FILL_FACTOR: f64 = 8 / 3;
|
||||||
|
const FREE_FACTOR: f64 = 3 / 4;
|
||||||
|
|
||||||
|
/** Structure of a map entry. */
|
||||||
|
@unmanaged class MapEntry<K,V> {
|
||||||
|
key: K;
|
||||||
|
value: V;
|
||||||
|
taggedNext: usize; // LSB=1 indicates EMPTY
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Empty bit. */
|
||||||
|
const EMPTY: usize = 1 << 0;
|
||||||
|
|
||||||
|
/** Size of a bucket. */
|
||||||
|
const BUCKET_SIZE = sizeof<usize>();
|
||||||
|
|
||||||
|
/** Computes the alignment of an entry. */
|
||||||
|
@inline function ENTRY_ALIGN<K,V>(): usize {
|
||||||
|
// 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. */
|
||||||
|
@inline function ENTRY_SIZE<K,V>(): usize {
|
||||||
|
const align = ENTRY_ALIGN<K,V>();
|
||||||
|
const size = (offsetof<MapEntry<K,V>>() + align) & ~align;
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
export class Map<K,V> {
|
export class Map<K,V> {
|
||||||
|
|
||||||
private __keys: K[] = [];
|
// buckets holding references to the respective first entry within
|
||||||
private __values: V[] = [];
|
private buckets: ArrayBuffer; // usize[bucketsMask + 1]
|
||||||
|
private bucketsMask: u32;
|
||||||
|
|
||||||
// FIXME: not a proper map implementation, just a filler
|
// entries in insertion order
|
||||||
|
private entries: ArrayBuffer; // MapEntry<K,V>[entriesCapacity]
|
||||||
|
private entriesCapacity: i32;
|
||||||
|
private entriesOffset: i32;
|
||||||
|
private entriesCount: i32;
|
||||||
|
|
||||||
get size(): i32 {
|
get size(): i32 { return this.entriesCount; }
|
||||||
return this.__keys.length;
|
|
||||||
|
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,V>();
|
||||||
|
this.entries = new ArrayBuffer(entriesSize, true);
|
||||||
|
this.entriesCapacity = INITIAL_CAPACITY;
|
||||||
|
this.entriesOffset = 0;
|
||||||
|
this.entriesCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
get(key: K): V | null {
|
private find(key: K, hashCode: u32): MapEntry<K,V> | null {
|
||||||
var keys = this.__keys;
|
var entry = load<MapEntry<K,V>>(
|
||||||
for (let i = 0, k = keys.length; i < k; ++i) {
|
changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE,
|
||||||
if (keys[i] == key) {
|
HEADER_SIZE_AB
|
||||||
return this.__values[i];
|
);
|
||||||
}
|
while (entry) {
|
||||||
|
if (!(entry.taggedNext & EMPTY) && entry.key == key) return entry;
|
||||||
|
entry = changetype<MapEntry<K,V>>(entry.taggedNext & ~EMPTY);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
has(key: K): bool {
|
has(key: K): bool {
|
||||||
var keys = this.__keys;
|
return this.find(key, hash<K>(key)) !== null;
|
||||||
for (let i = 0, k = keys.length; i < k; ++i) {
|
|
||||||
if (keys[i] == key) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
get(key: K): V {
|
||||||
|
var entry = this.find(key, hash<K>(key));
|
||||||
|
return entry ? entry.value : <V>unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
set(key: K, value: V): void {
|
set(key: K, value: V): void {
|
||||||
this.__keys.push(key);
|
var hashCode = hash<K>(key);
|
||||||
this.__values.push(value);
|
var entry = this.find(key, hashCode);
|
||||||
|
if (entry) {
|
||||||
|
entry.value = value;
|
||||||
|
} 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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// append new entry
|
||||||
|
let entries = this.entries;
|
||||||
|
entry = changetype<MapEntry<K,V>>(
|
||||||
|
changetype<usize>(entries) + HEADER_SIZE_AB + this.entriesOffset++ * ENTRY_SIZE<K,V>()
|
||||||
|
);
|
||||||
|
entry.key = key;
|
||||||
|
entry.value = value;
|
||||||
|
++this.entriesCount;
|
||||||
|
// link with previous entry in bucket
|
||||||
|
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
|
||||||
|
entry.taggedNext = load<usize>(bucketPtrBase, HEADER_SIZE_AB);
|
||||||
|
store<usize>(bucketPtrBase, changetype<usize>(entry), HEADER_SIZE_AB);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clear(): void {
|
delete(key: K): bool {
|
||||||
this.__keys.length = 0;
|
var entry = this.find(key, hash<K>(key));
|
||||||
this.__values.length = 0;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private rehash(newBucketsMask: u32): void {
|
||||||
|
var newBucketsCapacity = <i32>(newBucketsMask + 1);
|
||||||
|
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
|
||||||
|
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
|
||||||
|
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K,V>(), true);
|
||||||
|
|
||||||
|
// copy old entries to new entries
|
||||||
|
var oldPtr = changetype<usize>(this.entries) + HEADER_SIZE_AB;
|
||||||
|
var oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<K,V>();
|
||||||
|
var newPtr = changetype<usize>(newEntries) + HEADER_SIZE_AB;
|
||||||
|
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;
|
||||||
|
let newBucketIndex = hash<K>(oldEntry.key) & newBucketsMask;
|
||||||
|
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
|
||||||
|
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
|
||||||
|
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,87 +1,154 @@
|
|||||||
// const prime1: u32 = 73;
|
import {
|
||||||
// const prime2: u32 = 5009;
|
HEADER_SIZE as HEADER_SIZE_AB
|
||||||
|
} from "./internal/arraybuffer";
|
||||||
|
|
||||||
export class Set<T> {
|
import {
|
||||||
|
hash
|
||||||
|
} from "./internal/hash";
|
||||||
|
|
||||||
private __memory: usize;
|
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
|
||||||
private __capacity: u32;
|
|
||||||
private __size: u32;
|
|
||||||
|
|
||||||
constructor() {
|
const INITIAL_CAPACITY = 4;
|
||||||
this.__memory = 0;
|
const FILL_FACTOR: f64 = 8 / 3;
|
||||||
this.__capacity = this.__size = 0;
|
const FREE_FACTOR: f64 = 3 / 4;
|
||||||
|
|
||||||
|
/** Structure of a set entry. */
|
||||||
|
@unmanaged class SetEntry<K> {
|
||||||
|
key: K;
|
||||||
|
taggedNext: usize; // LSB=1 indicates EMPTY
|
||||||
}
|
}
|
||||||
|
|
||||||
get size(): i32 {
|
/** Empty bit. */
|
||||||
return <i32>this.__size;
|
const EMPTY: usize = 1 << 0;
|
||||||
|
|
||||||
|
/** Size of a bucket. */
|
||||||
|
const BUCKET_SIZE = sizeof<usize>();
|
||||||
|
|
||||||
|
/** Computes the alignment of an entry. */
|
||||||
|
@inline function ENTRY_ALIGN<K>(): usize {
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: not a proper set implementation, just a filler
|
/** Computes the aligned size of an entry. */
|
||||||
|
@inline function ENTRY_SIZE<K>(): usize {
|
||||||
has(value: T): bool {
|
const align = ENTRY_ALIGN<K>();
|
||||||
assert(this != null);
|
const size = (offsetof<SetEntry<K>>() + align) & ~align;
|
||||||
|
return size;
|
||||||
for (let index: usize = 0, limit: usize = this.__size; index < limit; ++index) {
|
|
||||||
if (load<T>(this.__memory + index * sizeof<T>()) == value) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add(value: T): Set<T> {
|
export class Set<K> {
|
||||||
assert(this != null);
|
|
||||||
|
|
||||||
if (this.__size >= this.__capacity) {
|
// buckets holding references to the respective first entry within
|
||||||
let newCapacity = max(this.__capacity << 1, 8);
|
private buckets: ArrayBuffer; // usize[bucketsMask + 1]
|
||||||
let newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
|
private bucketsMask: u32;
|
||||||
if (this.__memory) {
|
|
||||||
move_memory(newMemory, this.__memory, <usize>this.__capacity * sizeof<T>());
|
|
||||||
free_memory(this.__memory);
|
|
||||||
}
|
|
||||||
this.__capacity = newCapacity;
|
|
||||||
this.__memory = newMemory;
|
|
||||||
}
|
|
||||||
store<T>(this.__memory + <usize>this.__size * sizeof<T>(), value);
|
|
||||||
++this.__size;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(value: T): bool {
|
// entries in insertion order
|
||||||
assert(this != null);
|
private entries: ArrayBuffer; // SetEntry<K>[entriesCapacity]
|
||||||
|
private entriesCapacity: i32;
|
||||||
|
private entriesOffset: i32;
|
||||||
|
private entriesCount: i32;
|
||||||
|
|
||||||
for (let index: usize = 0, limit: usize = this.__size; index < limit; ++index) {
|
get size(): i32 { return this.entriesCount; }
|
||||||
if (load<T>(this.__memory + index * sizeof<T>()) == value) {
|
|
||||||
if (index + 1 < limit) {
|
constructor() { this.clear(); }
|
||||||
move_memory(
|
|
||||||
this.__memory + index * sizeof<T>(),
|
|
||||||
this.__memory + (index + 1) * sizeof<T>(),
|
|
||||||
limit - index - 1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
--this.__size;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
clear(): void {
|
clear(): void {
|
||||||
assert(this != null);
|
const bucketsSize = INITIAL_CAPACITY * <i32>BUCKET_SIZE;
|
||||||
|
this.buckets = new ArrayBuffer(bucketsSize);
|
||||||
this.__size = 0;
|
this.bucketsMask = INITIAL_CAPACITY - 1;
|
||||||
|
const entriesSize = INITIAL_CAPACITY * <i32>ENTRY_SIZE<K>();
|
||||||
|
this.entries = new ArrayBuffer(entriesSize, true);
|
||||||
|
this.entriesCapacity = INITIAL_CAPACITY;
|
||||||
|
this.entriesOffset = 0;
|
||||||
|
this.entriesCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: think about iterators
|
private find(key: K, hashCode: u32): SetEntry<K> | null {
|
||||||
|
var entry = load<SetEntry<K>>(
|
||||||
|
changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE,
|
||||||
|
HEADER_SIZE_AB
|
||||||
|
);
|
||||||
|
while (entry) {
|
||||||
|
if (!(entry.taggedNext & EMPTY) && entry.key == key) return entry;
|
||||||
|
entry = changetype<SetEntry<K>>(entry.taggedNext & ~EMPTY);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// class SetIterator<T> extends Iterator<T> {
|
has(key: K): bool {
|
||||||
|
return this.find(key, hash(key)) !== null;
|
||||||
|
}
|
||||||
|
|
||||||
// get done(): bool {
|
add(key: K): void {
|
||||||
// throw new Error("not implemented");
|
var hashCode = hash(key);
|
||||||
// }
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// append new entry
|
||||||
|
let entries = this.entries;
|
||||||
|
entry = changetype<SetEntry<K>>(
|
||||||
|
changetype<usize>(entries) + HEADER_SIZE_AB + this.entriesOffset++ * ENTRY_SIZE<K>()
|
||||||
|
);
|
||||||
|
entry.key = key;
|
||||||
|
++this.entriesCount;
|
||||||
|
// link with previous entry in bucket
|
||||||
|
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
|
||||||
|
entry.taggedNext = load<usize>(bucketPtrBase, HEADER_SIZE_AB);
|
||||||
|
store<usize>(bucketPtrBase, changetype<usize>(entry), HEADER_SIZE_AB);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// next(): T {
|
delete(key: K): bool {
|
||||||
// throw new Error("not implemented");
|
var entry = this.find(key, hash<K>(key));
|
||||||
// }
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private rehash(newBucketsMask: u32): void {
|
||||||
|
var newBucketsCapacity = <i32>(newBucketsMask + 1);
|
||||||
|
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
|
||||||
|
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
|
||||||
|
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K>(), true);
|
||||||
|
|
||||||
|
// copy old entries to new entries
|
||||||
|
var oldPtr = changetype<usize>(this.entries) + HEADER_SIZE_AB;
|
||||||
|
var oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<K>();
|
||||||
|
var newPtr = changetype<usize>(newEntries) + HEADER_SIZE_AB;
|
||||||
|
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;
|
||||||
|
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
|
||||||
|
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
|
||||||
|
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);
|
||||||
|
newPtr += ENTRY_SIZE<K>();
|
||||||
|
}
|
||||||
|
oldPtr += ENTRY_SIZE<K>();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.buckets = newBuckets;
|
||||||
|
this.bucketsMask = newBucketsMask;
|
||||||
|
this.entries = newEntries;
|
||||||
|
this.entriesCapacity = newEntriesCapacity;
|
||||||
|
this.entriesOffset = this.entriesCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,163 +1,3 @@
|
|||||||
import {
|
|
||||||
hash
|
|
||||||
} from "internal/hash";
|
|
||||||
|
|
||||||
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
|
|
||||||
|
|
||||||
const INITIAL_CAPACITY = 4;
|
|
||||||
const FILL_FACTOR: f64 = 8 / 3;
|
|
||||||
const FREE_FACTOR: f64 = 3 / 4;
|
|
||||||
|
|
||||||
/** Structure of a map entry. */
|
|
||||||
@unmanaged
|
|
||||||
class MapEntry<K,V> {
|
|
||||||
key: K;
|
|
||||||
value: V;
|
|
||||||
taggedNext: usize; // LSB=1 indicates EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Empty bit. */
|
|
||||||
const EMPTY: usize = 1 << 0;
|
|
||||||
|
|
||||||
/** Size of a bucket. */
|
|
||||||
const BUCKET_SIZE = sizeof<usize>();
|
|
||||||
|
|
||||||
/** Computes the alignment of an entry. */
|
|
||||||
@inline function ENTRY_ALIGN<K,V>(): usize {
|
|
||||||
// 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. */
|
|
||||||
@inline function ENTRY_SIZE<K,V>(): usize {
|
|
||||||
const align = ENTRY_ALIGN<K,V>();
|
|
||||||
const size = (offsetof<MapEntry<K,V>>() + align) & ~align;
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Map<K,V> {
|
|
||||||
|
|
||||||
// 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; }
|
|
||||||
|
|
||||||
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,V>();
|
|
||||||
this.entries = new ArrayBuffer(entriesSize, true);
|
|
||||||
this.entriesCapacity = INITIAL_CAPACITY;
|
|
||||||
this.entriesOffset = 0;
|
|
||||||
this.entriesCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private find(key: K, hashCode: u32): MapEntry<K,V> | null {
|
|
||||||
var entry = this.buckets.load<MapEntry<K,V>>(hashCode & this.bucketsMask);
|
|
||||||
while (entry) {
|
|
||||||
if (!(entry.taggedNext & EMPTY) && entry.key == key) return entry;
|
|
||||||
entry = changetype<MapEntry<K,V>>(entry.taggedNext & ~EMPTY);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
has(key: K): bool {
|
|
||||||
return this.find(key, hash<K>(key)) !== null;
|
|
||||||
}
|
|
||||||
|
|
||||||
get(key: K): V {
|
|
||||||
var entry = this.find(key, hash<K>(key));
|
|
||||||
return entry ? entry.value : <V>unreachable();
|
|
||||||
}
|
|
||||||
|
|
||||||
set(key: K, value: V): void {
|
|
||||||
var hashCode = hash<K>(key);
|
|
||||||
var entry = this.find(key, hashCode);
|
|
||||||
if (entry) {
|
|
||||||
entry.value = value;
|
|
||||||
} 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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// append new entry
|
|
||||||
let entries = this.entries;
|
|
||||||
entry = changetype<MapEntry<K,V>>(
|
|
||||||
changetype<usize>(entries) + ArrayBuffer.HEADER_SIZE + this.entriesOffset++ * ENTRY_SIZE<K,V>()
|
|
||||||
);
|
|
||||||
entry.key = key;
|
|
||||||
entry.value = value;
|
|
||||||
++this.entriesCount;
|
|
||||||
// link with previous entry in bucket
|
|
||||||
let bucketIndex = hashCode & this.bucketsMask;
|
|
||||||
entry.taggedNext = this.buckets.load<usize>(bucketIndex);
|
|
||||||
this.buckets.store<usize>(bucketIndex, changetype<usize>(entry));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(key: K): bool {
|
|
||||||
var entry = this.find(key, hash<K>(key));
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private rehash(newBucketsMask: u32): void {
|
|
||||||
var newBucketsCapacity = <i32>(newBucketsMask + 1);
|
|
||||||
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
|
|
||||||
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
|
|
||||||
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K,V>(), true);
|
|
||||||
|
|
||||||
// copy old entries to new entries
|
|
||||||
var oldPtr = changetype<usize>(this.entries) + ArrayBuffer.HEADER_SIZE;
|
|
||||||
var oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<K,V>();
|
|
||||||
var newPtr = changetype<usize>(newEntries) + ArrayBuffer.HEADER_SIZE;
|
|
||||||
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;
|
|
||||||
let newBucketIndex = hash<K>(oldEntry.key) & newBucketsMask;
|
|
||||||
let newBucketPtr = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
|
|
||||||
newEntry.taggedNext = load<usize>(newBucketPtr, ArrayBuffer.HEADER_SIZE);
|
|
||||||
store<usize>(newBucketPtr, newPtr, ArrayBuffer.HEADER_SIZE);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
import "allocator/arena";
|
import "allocator/arena";
|
||||||
|
|
||||||
function test<K,V>(): void {
|
function test<K,V>(): void {
|
||||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,153 +1,3 @@
|
|||||||
import {
|
|
||||||
hash
|
|
||||||
} from "internal/hash";
|
|
||||||
|
|
||||||
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
|
|
||||||
|
|
||||||
const INITIAL_CAPACITY = 4;
|
|
||||||
const FILL_FACTOR: f64 = 8 / 3;
|
|
||||||
const FREE_FACTOR: f64 = 3 / 4;
|
|
||||||
|
|
||||||
/** Structure of a set entry. */
|
|
||||||
@unmanaged
|
|
||||||
class SetEntry<K> {
|
|
||||||
key: K;
|
|
||||||
taggedNext: usize; // LSB=1 indicates EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Empty bit. */
|
|
||||||
const EMPTY: usize = 1 << 0;
|
|
||||||
|
|
||||||
/** Size of a bucket. */
|
|
||||||
const BUCKET_SIZE = sizeof<usize>();
|
|
||||||
|
|
||||||
/** Computes the alignment of an entry. */
|
|
||||||
@inline function ENTRY_ALIGN<K>(): usize {
|
|
||||||
// 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. */
|
|
||||||
@inline function ENTRY_SIZE<K>(): usize {
|
|
||||||
const align = ENTRY_ALIGN<K>();
|
|
||||||
const size = (offsetof<SetEntry<K>>() + align) & ~align;
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
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>();
|
|
||||||
this.entries = new ArrayBuffer(entriesSize, true);
|
|
||||||
this.entriesCapacity = INITIAL_CAPACITY;
|
|
||||||
this.entriesOffset = 0;
|
|
||||||
this.entriesCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private find(key: K, hashCode: u32): SetEntry<K> | null {
|
|
||||||
var entry = this.buckets.load<SetEntry<K>>(hashCode & this.bucketsMask);
|
|
||||||
var i = 0;
|
|
||||||
while (entry) {
|
|
||||||
if (!(entry.taggedNext & EMPTY) && entry.key == key) return entry;
|
|
||||||
entry = changetype<SetEntry<K>>(entry.taggedNext & ~EMPTY);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
has(key: K): bool {
|
|
||||||
return this.find(key, hash(key)) !== null;
|
|
||||||
}
|
|
||||||
|
|
||||||
add(key: K): void {
|
|
||||||
var hashCode = hash(key);
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// append new entry
|
|
||||||
let entries = this.entries;
|
|
||||||
entry = changetype<SetEntry<K>>(
|
|
||||||
changetype<usize>(entries) + ArrayBuffer.HEADER_SIZE + this.entriesOffset++ * ENTRY_SIZE<K>()
|
|
||||||
);
|
|
||||||
entry.key = key;
|
|
||||||
++this.entriesCount;
|
|
||||||
// link with previous entry in bucket
|
|
||||||
let bucketIndex = hashCode & this.bucketsMask;
|
|
||||||
entry.taggedNext = this.buckets.load<usize>(bucketIndex);
|
|
||||||
this.buckets.store<usize>(bucketIndex, changetype<usize>(entry));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(key: K): bool {
|
|
||||||
var entry = this.find(key, hash<K>(key));
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private rehash(newBucketsMask: u32): void {
|
|
||||||
var newBucketsCapacity = <i32>(newBucketsMask + 1);
|
|
||||||
var newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
|
|
||||||
var newEntriesCapacity = <i32>(newBucketsCapacity * FILL_FACTOR);
|
|
||||||
var newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K>(), true);
|
|
||||||
|
|
||||||
// copy old entries to new entries
|
|
||||||
var oldPtr = changetype<usize>(this.entries) + ArrayBuffer.HEADER_SIZE;
|
|
||||||
var oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<K>();
|
|
||||||
var newPtr = changetype<usize>(newEntries) + ArrayBuffer.HEADER_SIZE;
|
|
||||||
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;
|
|
||||||
let newBucketPtr = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
|
|
||||||
newEntry.taggedNext = load<usize>(newBucketPtr, ArrayBuffer.HEADER_SIZE);
|
|
||||||
store<usize>(newBucketPtr, newPtr, ArrayBuffer.HEADER_SIZE);
|
|
||||||
newPtr += ENTRY_SIZE<K>();
|
|
||||||
}
|
|
||||||
oldPtr += ENTRY_SIZE<K>();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.buckets = newBuckets;
|
|
||||||
this.bucketsMask = newBucketsMask;
|
|
||||||
this.entries = newEntries;
|
|
||||||
this.entriesCapacity = newEntriesCapacity;
|
|
||||||
this.entriesOffset = this.entriesCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
import "allocator/arena";
|
import "allocator/arena";
|
||||||
|
|
||||||
function test<K>(): void {
|
function test<K>(): void {
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user