stdlib unlink wiring

This commit is contained in:
dcode
2019-03-20 17:12:33 +01:00
parent 9b664dc175
commit 952ac8627d
6 changed files with 65 additions and 59 deletions

View File

@ -1,4 +1,4 @@
import { LINK } from "./runtime";
import { LINK, UNLINK } from "./runtime";
import { HASH } from "./util/hash";
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
@ -93,7 +93,7 @@ export class Set<K> {
add(key: K): void {
var hashCode = HASH<K>(key);
var entry = this.find(key, hashCode);
var entry = this.find(key, hashCode); // unmanaged!
if (!entry) {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
@ -108,10 +108,8 @@ export class Set<K> {
entry = changetype<SetEntry<K>>(
changetype<usize>(entries) + this.entriesOffset++ * ENTRY_SIZE<K>()
);
// link with the set itself (entry is unmanaged)
entry.key = isManaged<K>()
? LINK<K,this>(key, this)
: key;
// link with the set
entry.key = isManaged<K>() ? LINK<K,this>(key, this) : key;
++this.entriesCount;
// link with previous entry in bucket
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
@ -123,6 +121,7 @@ export class Set<K> {
delete(key: K): bool {
var entry = this.find(key, HASH<K>(key));
if (!entry) return false;
if (isManaged<K>()) UNLINK<K,this>(entry.key, this);
entry.taggedNext |= EMPTY;
--this.entriesCount;
// check if rehashing is appropriate