-
-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathset.ts
More file actions
220 lines (190 loc) · 6.87 KB
/
set.ts
File metadata and controls
220 lines (190 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/// <reference path="./rt/index.d.ts" />
import { HASH } from "./util/hash";
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
// @ts-ignore: decorator
@inline const INITIAL_CAPACITY = 4;
// @ts-ignore: decorator
@inline const FILL_FACTOR_N = 8;
// @ts-ignore: decorator
@inline const FILL_FACTOR_D = 3;
// @ts-ignore: decorator
@inline const FREE_FACTOR_N = 3;
// @ts-ignore: decorator
@inline const FREE_FACTOR_D = 4;
/** Structure of a set entry. */
@unmanaged class SetEntry<K> {
key: K;
taggedNext: usize; // LSB=1 indicates EMPTY
}
/** Empty bit. */
// @ts-ignore: decorator
@inline const EMPTY: usize = 1 << 0;
/** Size of a bucket. */
// @ts-ignore: decorator
@inline const BUCKET_SIZE = sizeof<usize>();
/** Computes the alignment of an entry. */
// @ts-ignore: decorator
@inline
function ENTRY_ALIGN<T>(): usize {
// can align to 4 instead of 8 if 32-bit and K is <= 32-bits
const align = (sizeof<T>() > sizeof<usize>() ? sizeof<T>() : sizeof<usize>()) - 1;
return align;
}
/** Computes the aligned size of an entry. */
// @ts-ignore: decorator
@inline
function ENTRY_SIZE<T>(): usize {
const align = ENTRY_ALIGN<T>();
const size = (offsetof<SetEntry<T>>() + align) & ~align;
return size;
}
export class Set<T> {
// buckets referencing their respective first entry, usize[bucketsMask + 1]
private buckets: ArrayBuffer = new ArrayBuffer(INITIAL_CAPACITY * <i32>BUCKET_SIZE);
private bucketsMask: u32 = INITIAL_CAPACITY - 1;
// entries in insertion order, SetEntry<K>[entriesCapacity]
private entries: ArrayBuffer = new ArrayBuffer(INITIAL_CAPACITY * <i32>ENTRY_SIZE<T>());
private entriesCapacity: i32 = INITIAL_CAPACITY;
private entriesOffset: i32 = 0;
private entriesCount: i32 = 0;
constructor() {
/* nop */
}
get size(): i32 {
return this.entriesCount;
}
clear(): void {
this.buckets = new ArrayBuffer(INITIAL_CAPACITY * <i32>BUCKET_SIZE);
this.bucketsMask = INITIAL_CAPACITY - 1;
this.entries = new ArrayBuffer(INITIAL_CAPACITY * <i32>ENTRY_SIZE<T>());
this.entriesCapacity = INITIAL_CAPACITY;
this.entriesOffset = 0;
this.entriesCount = 0;
}
private find(key: T, hashCode: u32): SetEntry<T> | null {
let entry = load<SetEntry<T>>( // unmanaged!
changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE
);
while (entry) {
let taggedNext = entry.taggedNext;
if (!(taggedNext & EMPTY) && entry.key == key) return entry;
entry = changetype<SetEntry<T>>(taggedNext & ~EMPTY);
}
return null;
}
@operator("[]")
has(key: T): bool {
return this.find(key, HASH<T>(key)) != null;
}
add(key: T): this {
let hashCode = HASH<T>(key);
let entry = this.find(key, hashCode); // unmanaged!
if (!entry) {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
this.rehash(
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
? this.bucketsMask // just rehash if 1/4+ entries are empty
: (this.bucketsMask << 1) | 1 // grow capacity to next 2^N
);
}
// append new entry
entry = changetype<SetEntry<T>>(changetype<usize>(this.entries) + <usize>(this.entriesOffset++) * ENTRY_SIZE<T>());
entry.key = key;
if (isManaged<T>()) {
__link(changetype<usize>(this), changetype<usize>(key), true);
}
++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);
store<usize>(bucketPtrBase, changetype<usize>(entry));
}
return this;
}
@operator("[]=")
private __set(key: T, value: bool): void {
if (value) this.add(key);
else this.delete(key);
}
delete(key: T): bool {
let entry = this.find(key, HASH<T>(key)); // unmanaged!
if (!entry) return false;
entry.taggedNext |= EMPTY;
--this.entriesCount;
// check if rehashing is appropriate
let halfBucketsMask = this.bucketsMask >> 1;
if (
halfBucketsMask + 1 >= max<u32>(INITIAL_CAPACITY, this.entriesCount) &&
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
) this.rehash(halfBucketsMask);
return true;
}
private rehash(newBucketsMask: u32): void {
let newBucketsCapacity = <i32>(newBucketsMask + 1);
let newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
let newEntriesCapacity = newBucketsCapacity * FILL_FACTOR_N / FILL_FACTOR_D;
let newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<T>());
// copy old entries to new entries
let oldPtr = changetype<usize>(this.entries);
let oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<T>();
let newPtr = changetype<usize>(newEntries);
while (oldPtr != oldEnd) {
let oldEntry = changetype<SetEntry<T>>(oldPtr); // unmanaged!
if (!(oldEntry.taggedNext & EMPTY)) {
let newEntry = changetype<SetEntry<T>>(newPtr); // unmanaged!
let oldEntryKey = oldEntry.key;
newEntry.key = oldEntryKey;
let newBucketIndex = HASH<T>(oldEntryKey) & newBucketsMask;
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
newEntry.taggedNext = load<usize>(newBucketPtrBase);
store<usize>(newBucketPtrBase, newPtr);
newPtr += ENTRY_SIZE<T>();
}
oldPtr += ENTRY_SIZE<T>();
}
this.buckets = newBuckets;
this.bucketsMask = newBucketsMask;
this.entries = newEntries;
this.entriesCapacity = newEntriesCapacity;
this.entriesOffset = this.entriesCount;
}
values(): T[] {
// FIXME: this is preliminary, needs iterators/closures
let start = changetype<usize>(this.entries);
let size = this.entriesOffset;
let values = new Array<T>(size);
let length = 0;
for (let i = 0; i < size; ++i) {
let entry = changetype<SetEntry<T>>(start + <usize>i * ENTRY_SIZE<T>());
if (!(entry.taggedNext & EMPTY)) {
unchecked(values[length++] = entry.key);
}
}
values.length = length;
return values;
}
toString(): string {
return "[object Set]";
}
// RT integration
@unsafe private __visit(cookie: u32): void {
__visit(changetype<usize>(this.buckets), cookie);
let entries = changetype<usize>(this.entries);
if (isManaged<T>()) {
let cur = entries;
let end = cur + <usize>this.entriesOffset * ENTRY_SIZE<T>();
while (cur < end) {
let entry = changetype<SetEntry<T>>(cur);
if (!(entry.taggedNext & EMPTY)) {
let val = changetype<usize>(entry.key);
if (isNullable<T>()) {
if (val) __visit(val, cookie);
} else __visit(val, cookie);
}
cur += ENTRY_SIZE<T>();
}
}
__visit(entries, cookie);
}
}