@amxts/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +162 -0
- package/README.ru.md +162 -0
- package/amxmodx/base/include/amxconst.inc +594 -0
- package/amxmodx/base/include/amxmisc.inc +902 -0
- package/amxmodx/base/include/amxmodx.inc +3447 -0
- package/amxmodx/base/include/amxmodx_version.inc +20 -0
- package/amxmodx/base/include/cellarray.inc +525 -0
- package/amxmodx/base/include/cellstack.inc +166 -0
- package/amxmodx/base/include/celltrie.inc +412 -0
- package/amxmodx/base/include/core.inc +202 -0
- package/amxmodx/base/include/csstats.inc +298 -0
- package/amxmodx/base/include/csstats_const.inc +29 -0
- package/amxmodx/base/include/cstrike.inc +1287 -0
- package/amxmodx/base/include/cstrike_const.inc +521 -0
- package/amxmodx/base/include/csx.inc +259 -0
- package/amxmodx/base/include/cvars.inc +547 -0
- package/amxmodx/base/include/datapack.inc +161 -0
- package/amxmodx/base/include/dbi.inc +154 -0
- package/amxmodx/base/include/dodconst.inc +152 -0
- package/amxmodx/base/include/dodfun.inc +152 -0
- package/amxmodx/base/include/dodstats.inc +69 -0
- package/amxmodx/base/include/dodx.inc +161 -0
- package/amxmodx/base/include/engine.inc +1343 -0
- package/amxmodx/base/include/engine_const.inc +310 -0
- package/amxmodx/base/include/engine_stocks.inc +248 -0
- package/amxmodx/base/include/esf.inc +70 -0
- package/amxmodx/base/include/esf_const.inc +74 -0
- package/amxmodx/base/include/fakemeta.inc +1127 -0
- package/amxmodx/base/include/fakemeta_const.inc +793 -0
- package/amxmodx/base/include/fakemeta_stocks.inc +395 -0
- package/amxmodx/base/include/fakemeta_util.inc +882 -0
- package/amxmodx/base/include/file.inc +666 -0
- package/amxmodx/base/include/float.inc +422 -0
- package/amxmodx/base/include/fun.inc +333 -0
- package/amxmodx/base/include/gameconfig.inc +93 -0
- package/amxmodx/base/include/geoip.inc +264 -0
- package/amxmodx/base/include/ham_const.inc +4352 -0
- package/amxmodx/base/include/hamsandwich.inc +439 -0
- package/amxmodx/base/include/hlsdk_const.inc +808 -0
- package/amxmodx/base/include/json.inc +777 -0
- package/amxmodx/base/include/lang.inc +119 -0
- package/amxmodx/base/include/message_const.inc +1130 -0
- package/amxmodx/base/include/message_stocks.inc +161 -0
- package/amxmodx/base/include/messages.inc +604 -0
- package/amxmodx/base/include/newmenus.inc +387 -0
- package/amxmodx/base/include/ns.inc +754 -0
- package/amxmodx/base/include/ns_const.inc +196 -0
- package/amxmodx/base/include/nvault.inc +154 -0
- package/amxmodx/base/include/regex.inc +348 -0
- package/amxmodx/base/include/sockets.inc +183 -0
- package/amxmodx/base/include/sorting.inc +107 -0
- package/amxmodx/base/include/sqlx.inc +582 -0
- package/amxmodx/base/include/string.inc +786 -0
- package/amxmodx/base/include/string_const.inc +156 -0
- package/amxmodx/base/include/string_stocks.inc +325 -0
- package/amxmodx/base/include/textparse_ini.inc +214 -0
- package/amxmodx/base/include/textparse_smc.inc +261 -0
- package/amxmodx/base/include/tfcconst.inc +84 -0
- package/amxmodx/base/include/tfcstats.inc +62 -0
- package/amxmodx/base/include/tfcx.inc +148 -0
- package/amxmodx/base/include/time.inc +118 -0
- package/amxmodx/base/include/tsconst.inc +154 -0
- package/amxmodx/base/include/tsfun.inc +198 -0
- package/amxmodx/base/include/tsstats.inc +65 -0
- package/amxmodx/base/include/tsx.inc +88 -0
- package/amxmodx/base/include/vault.inc +60 -0
- package/amxmodx/base/include/vector.inc +131 -0
- package/amxmodx/base/include/xs.inc +1570 -0
- package/as/amxts.d.ts +153 -0
- package/as/constants.ts +4730 -0
- package/as/effects.ts +1449 -0
- package/as/entities.ts +5482 -0
- package/as/events.ts +3054 -0
- package/as/facade.ts +4639 -0
- package/as/flags.ts +299 -0
- package/as/fs.ts +287 -0
- package/as/hooks.ts +18536 -0
- package/as/kit.ts +25 -0
- package/as/lib/check.ts +86 -0
- package/as/modules/http.ts +184 -0
- package/as/natives.ts +11845 -0
- package/as/os.ts +30 -0
- package/as/promise.ts +1392 -0
- package/as/promise.types.d.ts +82 -0
- package/as/remote.ts +288 -0
- package/as/tsconfig.json +22 -0
- package/as/vector.ts +65 -0
- package/bin/amxts.mjs +10 -0
- package/includes/menu_core.inc +355 -0
- package/includes/order.txt +54 -0
- package/includes/sources.json +27 -0
- package/includes/universal_config.inc +355 -0
- package/lint/oxlint-plugin.mjs +77 -0
- package/node_modules/assemblyscript/LICENSE +201 -0
- package/node_modules/assemblyscript/NOTICE +93 -0
- package/node_modules/assemblyscript/package.json +63 -0
- package/node_modules/assemblyscript/std/README.md +6 -0
- package/node_modules/assemblyscript/std/assembly/array.ts +526 -0
- package/node_modules/assemblyscript/std/assembly/arraybuffer.ts +77 -0
- package/node_modules/assemblyscript/std/assembly/atomics.ts +127 -0
- package/node_modules/assemblyscript/std/assembly/bindings/asyncify.ts +16 -0
- package/node_modules/assemblyscript/std/assembly/bindings/dom.ts +291 -0
- package/node_modules/assemblyscript/std/assembly/bindings/node.ts +6 -0
- package/node_modules/assemblyscript/std/assembly/builtins.ts +2631 -0
- package/node_modules/assemblyscript/std/assembly/compat.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/console.ts +42 -0
- package/node_modules/assemblyscript/std/assembly/crypto.ts +9 -0
- package/node_modules/assemblyscript/std/assembly/dataview.ts +181 -0
- package/node_modules/assemblyscript/std/assembly/date.ts +373 -0
- package/node_modules/assemblyscript/std/assembly/diagnostics.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/error.ts +44 -0
- package/node_modules/assemblyscript/std/assembly/function.ts +38 -0
- package/node_modules/assemblyscript/std/assembly/index.d.ts +2822 -0
- package/node_modules/assemblyscript/std/assembly/iterator.ts +35 -0
- package/node_modules/assemblyscript/std/assembly/map.ts +260 -0
- package/node_modules/assemblyscript/std/assembly/math.ts +3289 -0
- package/node_modules/assemblyscript/std/assembly/memory.ts +123 -0
- package/node_modules/assemblyscript/std/assembly/number.ts +388 -0
- package/node_modules/assemblyscript/std/assembly/object.ts +36 -0
- package/node_modules/assemblyscript/std/assembly/performance.ts +9 -0
- package/node_modules/assemblyscript/std/assembly/polyfills.ts +27 -0
- package/node_modules/assemblyscript/std/assembly/process.ts +50 -0
- package/node_modules/assemblyscript/std/assembly/reference.ts +48 -0
- package/node_modules/assemblyscript/std/assembly/regexp.ts +12 -0
- package/node_modules/assemblyscript/std/assembly/rt/README.md +83 -0
- package/node_modules/assemblyscript/std/assembly/rt/common.ts +81 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-incremental.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-minimal.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-stub.ts +1 -0
- package/node_modules/assemblyscript/std/assembly/rt/index.d.ts +37 -0
- package/node_modules/assemblyscript/std/assembly/rt/itcms.ts +419 -0
- package/node_modules/assemblyscript/std/assembly/rt/rtrace.ts +15 -0
- package/node_modules/assemblyscript/std/assembly/rt/stub.ts +133 -0
- package/node_modules/assemblyscript/std/assembly/rt/tcms.ts +254 -0
- package/node_modules/assemblyscript/std/assembly/rt/tlsf.ts +592 -0
- package/node_modules/assemblyscript/std/assembly/rt.ts +87 -0
- package/node_modules/assemblyscript/std/assembly/set.ts +220 -0
- package/node_modules/assemblyscript/std/assembly/shared/feature.ts +65 -0
- package/node_modules/assemblyscript/std/assembly/shared/runtime.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/target.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/tsconfig.json +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/typeinfo.ts +72 -0
- package/node_modules/assemblyscript/std/assembly/staticarray.ts +401 -0
- package/node_modules/assemblyscript/std/assembly/string.ts +847 -0
- package/node_modules/assemblyscript/std/assembly/symbol.ts +114 -0
- package/node_modules/assemblyscript/std/assembly/table.ts +16 -0
- package/node_modules/assemblyscript/std/assembly/tsconfig.json +6 -0
- package/node_modules/assemblyscript/std/assembly/typedarray.ts +1945 -0
- package/node_modules/assemblyscript/std/assembly/uri.ts +17 -0
- package/node_modules/assemblyscript/std/assembly/util/bytes.ts +107 -0
- package/node_modules/assemblyscript/std/assembly/util/casemap.ts +497 -0
- package/node_modules/assemblyscript/std/assembly/util/error.ts +54 -0
- package/node_modules/assemblyscript/std/assembly/util/hash.ts +117 -0
- package/node_modules/assemblyscript/std/assembly/util/math.ts +1922 -0
- package/node_modules/assemblyscript/std/assembly/util/memory.ts +290 -0
- package/node_modules/assemblyscript/std/assembly/util/number.ts +873 -0
- package/node_modules/assemblyscript/std/assembly/util/sort.ts +313 -0
- package/node_modules/assemblyscript/std/assembly/util/string.ts +1202 -0
- package/node_modules/assemblyscript/std/assembly/util/uri.ts +275 -0
- package/node_modules/assemblyscript/std/assembly/vector.ts +4 -0
- package/node_modules/assemblyscript/std/assembly.json +16 -0
- package/node_modules/assemblyscript/std/portable/index.d.ts +462 -0
- package/node_modules/assemblyscript/std/portable/index.js +416 -0
- package/node_modules/assemblyscript/std/portable.json +11 -0
- package/node_modules/assemblyscript/std/types/assembly/index.d.ts +1 -0
- package/node_modules/assemblyscript/std/types/assembly/package.json +3 -0
- package/node_modules/assemblyscript/std/types/portable/index.d.ts +1 -0
- package/node_modules/assemblyscript/std/types/portable/package.json +3 -0
- package/node_modules/assemblyscript/tsconfig-base.json +13 -0
- package/package.json +112 -0
- package/runtime/deps/assemblyscript/LICENSE +201 -0
- package/runtime/deps/assemblyscript/NOTICE +93 -0
- package/runtime/deps/assemblyscript/bin/asc.js +35 -0
- package/runtime/deps/assemblyscript/dist/asc.js +25035 -0
- package/runtime/deps/assemblyscript/dist/assemblyscript.js +337 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/LICENSE +201 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/index.js +28905 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/package.json +50 -0
- package/runtime/deps/assemblyscript/node_modules/long/LICENSE +202 -0
- package/runtime/deps/assemblyscript/node_modules/long/index.js +1581 -0
- package/runtime/deps/assemblyscript/node_modules/long/package.json +58 -0
- package/runtime/deps/assemblyscript/package.json +114 -0
- package/runtime/host/amxts.inc +85 -0
- package/runtime/licenses/AMXX-LICENSE.txt +34 -0
- package/runtime/licenses/AssemblyScript-LICENSE +201 -0
- package/runtime/licenses/AssemblyScript-NOTICE +93 -0
- package/runtime/licenses/Binaryen-LICENSE +201 -0
- package/runtime/licenses/Bun-LICENSE.md +84 -0
- package/runtime/licenses/GPL-3.0.txt +674 -0
- package/runtime/licenses/LLVM-LICENSE.TXT +279 -0
- package/runtime/licenses/README.md +25 -0
- package/runtime/licenses/WAMR-LICENSE +219 -0
- package/runtime/natives.txt +1095 -0
- package/runtime/patches/README.md +59 -0
- package/runtime/patches/assemblyscript-0.28.20-amxts.patch +14174 -0
- package/runtime/patches/wamr-2.4.5-amxts.patch +364 -0
- package/scripts/apply-docs.ts +508 -0
- package/scripts/asc.ts +27 -0
- package/scripts/auto-imports.ts +258 -0
- package/scripts/build-wasm.ts +595 -0
- package/scripts/check.ts +93 -0
- package/scripts/compile-cache.ts +179 -0
- package/scripts/compile.ts +401 -0
- package/scripts/docker-server.ts +73 -0
- package/scripts/docs/as/amxts.ts +210 -0
- package/scripts/docs/as/effects.ts +1684 -0
- package/scripts/docs/as/facade.ts +3302 -0
- package/scripts/docs/as/fs.ts +173 -0
- package/scripts/docs/as/lib/check.ts +68 -0
- package/scripts/docs/as/modules/http.ts +66 -0
- package/scripts/docs/as/os.ts +16 -0
- package/scripts/docs/as/promise.ts +132 -0
- package/scripts/docs/as/promise.types.ts +63 -0
- package/scripts/docs/as/vector.ts +48 -0
- package/scripts/docs/entities.ts +2338 -0
- package/scripts/docs/events.ts +245 -0
- package/scripts/docs/game.ts +497 -0
- package/scripts/docs/messages.ts +148 -0
- package/scripts/file-contents.ts +74 -0
- package/scripts/includes.ts +134 -0
- package/scripts/player-fields.ts +866 -0
- package/scripts/plugin-cache.ts +92 -0
- package/scripts/plugin-natives.ts +1157 -0
- package/scripts/prebuilt.ts +268 -0
- package/scripts/prepare.ts +134 -0
- package/scripts/project.ts +973 -0
- package/scripts/run.ts +28 -0
- package/scripts/shared-modules.ts +906 -0
- package/scripts/system.ts +33 -0
- package/scripts/tracked-fs.ts +115 -0
- package/scripts/typed-configs.ts +788 -0
- package/scripts/ui.ts +77 -0
- package/src/cli-api.mjs +175 -0
- package/src/parser/include-parser.ts +506 -0
- package/src/system.mjs +155 -0
- package/src/testing/compile-cache.ts +31 -0
- package/src/testing/compile.ts +222 -0
- package/src/testing/coroutines.ts +191 -0
- package/src/testing/index.ts +39 -0
- package/src/testing/kits.ts +55 -0
- package/src/testing/memory.ts +125 -0
- package/src/testing/natives.ts +925 -0
- package/src/testing/server.ts +1874 -0
- package/src/testing/setup.ts +63 -0
- package/src/testing/tables.ts +139 -0
- package/src/types.ts +51 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export abstract class Iterable<T> {
|
|
2
|
+
// ?
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
@final
|
|
6
|
+
export abstract class Iterator<T> {
|
|
7
|
+
|
|
8
|
+
// private constructor(iterable: Iterable<T>) {
|
|
9
|
+
// }
|
|
10
|
+
|
|
11
|
+
// TODO: these need to evaluate the classId at the respective reference in order to obtain the
|
|
12
|
+
// next value, i.e. arrays work differently than maps. we'd then have:
|
|
13
|
+
//
|
|
14
|
+
// ╒═══════════════════ Iterator layout (32-bit) ══════════════════╕
|
|
15
|
+
// 3 2 1
|
|
16
|
+
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
|
|
17
|
+
// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤
|
|
18
|
+
// │ index │
|
|
19
|
+
// ├─────────────────────────────────────────────────────────┬───┬─┤
|
|
20
|
+
// │ reference │ 0 │D│
|
|
21
|
+
// └─────────────────────────────────────────────────────────┴───┴─┘
|
|
22
|
+
// D: Done flag
|
|
23
|
+
|
|
24
|
+
// get value(this: u64): T {
|
|
25
|
+
// ?
|
|
26
|
+
// }
|
|
27
|
+
|
|
28
|
+
// next(this: u64): Iterator<T> {
|
|
29
|
+
// ?
|
|
30
|
+
// }
|
|
31
|
+
|
|
32
|
+
done(this: u64): bool {
|
|
33
|
+
return <bool>(this & 1);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/// <reference path="./rt/index.d.ts" />
|
|
2
|
+
|
|
3
|
+
import { HASH } from "./util/hash";
|
|
4
|
+
import { E_KEYNOTFOUND } from "./util/error";
|
|
5
|
+
|
|
6
|
+
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
|
|
7
|
+
|
|
8
|
+
// @ts-ignore: decorator
|
|
9
|
+
@inline const INITIAL_CAPACITY = 4;
|
|
10
|
+
|
|
11
|
+
// @ts-ignore: decorator
|
|
12
|
+
@inline const FILL_FACTOR_N = 8;
|
|
13
|
+
|
|
14
|
+
// @ts-ignore: decorator
|
|
15
|
+
@inline const FILL_FACTOR_D = 3;
|
|
16
|
+
|
|
17
|
+
// @ts-ignore: decorator
|
|
18
|
+
@inline const FREE_FACTOR_N = 3;
|
|
19
|
+
|
|
20
|
+
// @ts-ignore: decorator
|
|
21
|
+
@inline const FREE_FACTOR_D = 4;
|
|
22
|
+
|
|
23
|
+
/** Structure of a map entry. */
|
|
24
|
+
@unmanaged class MapEntry<K,V> {
|
|
25
|
+
key: K;
|
|
26
|
+
value: V;
|
|
27
|
+
taggedNext: usize; // LSB=1 indicates EMPTY
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Empty bit. */
|
|
31
|
+
// @ts-ignore: decorator
|
|
32
|
+
@inline const EMPTY: usize = 1 << 0;
|
|
33
|
+
|
|
34
|
+
/** Size of a bucket. */
|
|
35
|
+
// @ts-ignore: decorator
|
|
36
|
+
@inline const BUCKET_SIZE = sizeof<usize>();
|
|
37
|
+
|
|
38
|
+
/** Computes the alignment of an entry. */
|
|
39
|
+
// @ts-ignore: decorator
|
|
40
|
+
@inline
|
|
41
|
+
function ENTRY_ALIGN<K,V>(): usize {
|
|
42
|
+
// can align to 4 instead of 8 if 32-bit and K/V is <= 32-bits
|
|
43
|
+
const maxkv = sizeof<K>() > sizeof<V>() ? sizeof<K>() : sizeof<V>();
|
|
44
|
+
const align = (maxkv > sizeof<usize>() ? maxkv : sizeof<usize>()) - 1;
|
|
45
|
+
return align;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Computes the aligned size of an entry. */
|
|
49
|
+
// @ts-ignore: decorator
|
|
50
|
+
@inline
|
|
51
|
+
function ENTRY_SIZE<K,V>(): usize {
|
|
52
|
+
const align = ENTRY_ALIGN<K,V>();
|
|
53
|
+
const size = (offsetof<MapEntry<K,V>>() + align) & ~align;
|
|
54
|
+
return size;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class Map<K,V> {
|
|
58
|
+
|
|
59
|
+
// buckets referencing their respective first entry, usize[bucketsMask + 1]
|
|
60
|
+
private buckets: ArrayBuffer = new ArrayBuffer(INITIAL_CAPACITY * <i32>BUCKET_SIZE);
|
|
61
|
+
private bucketsMask: u32 = INITIAL_CAPACITY - 1;
|
|
62
|
+
|
|
63
|
+
// entries in insertion order, MapEntry<K,V>[entriesCapacity]
|
|
64
|
+
private entries: ArrayBuffer = new ArrayBuffer(INITIAL_CAPACITY * <i32>ENTRY_SIZE<K,V>());
|
|
65
|
+
private entriesCapacity: i32 = INITIAL_CAPACITY;
|
|
66
|
+
private entriesOffset: i32 = 0;
|
|
67
|
+
private entriesCount: i32 = 0;
|
|
68
|
+
|
|
69
|
+
constructor() {
|
|
70
|
+
/* nop */
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get size(): i32 {
|
|
74
|
+
return this.entriesCount;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
clear(): void {
|
|
78
|
+
this.buckets = new ArrayBuffer(INITIAL_CAPACITY * <i32>BUCKET_SIZE);
|
|
79
|
+
this.bucketsMask = INITIAL_CAPACITY - 1;
|
|
80
|
+
this.entries = new ArrayBuffer(INITIAL_CAPACITY * <i32>ENTRY_SIZE<K,V>());
|
|
81
|
+
this.entriesCapacity = INITIAL_CAPACITY;
|
|
82
|
+
this.entriesOffset = 0;
|
|
83
|
+
this.entriesCount = 0;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private find(key: K, hashCode: u32): MapEntry<K,V> | null {
|
|
87
|
+
let entry = load<MapEntry<K,V>>( // unmanaged!
|
|
88
|
+
changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE
|
|
89
|
+
);
|
|
90
|
+
while (entry) {
|
|
91
|
+
let taggedNext = entry.taggedNext;
|
|
92
|
+
if (!(taggedNext & EMPTY) && entry.key == key) return entry;
|
|
93
|
+
entry = changetype<MapEntry<K,V>>(taggedNext & ~EMPTY);
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
has(key: K): bool {
|
|
99
|
+
return this.find(key, HASH<K>(key)) != null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@operator("[]")
|
|
103
|
+
get(key: K): V {
|
|
104
|
+
let entry = this.find(key, HASH<K>(key));
|
|
105
|
+
if (!entry) throw new Error(E_KEYNOTFOUND); // cannot represent `undefined`
|
|
106
|
+
return entry.value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@operator("[]=")
|
|
110
|
+
set(key: K, value: V): this {
|
|
111
|
+
let hashCode = HASH<K>(key);
|
|
112
|
+
let entry = this.find(key, hashCode); // unmanaged!
|
|
113
|
+
if (entry) {
|
|
114
|
+
entry.value = value;
|
|
115
|
+
if (isManaged<V>()) {
|
|
116
|
+
__link(changetype<usize>(this), changetype<usize>(value), true);
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
// check if rehashing is necessary
|
|
120
|
+
if (this.entriesOffset == this.entriesCapacity) {
|
|
121
|
+
this.rehash(
|
|
122
|
+
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
|
|
123
|
+
? this.bucketsMask // just rehash if 1/4+ entries are empty
|
|
124
|
+
: (this.bucketsMask << 1) | 1 // grow capacity to next 2^N
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
// append new entry
|
|
128
|
+
let entries = this.entries;
|
|
129
|
+
entry = changetype<MapEntry<K,V>>(changetype<usize>(entries) + <usize>(this.entriesOffset++) * ENTRY_SIZE<K,V>());
|
|
130
|
+
// link with the map
|
|
131
|
+
entry.key = key;
|
|
132
|
+
if (isManaged<K>()) {
|
|
133
|
+
__link(changetype<usize>(this), changetype<usize>(key), true);
|
|
134
|
+
}
|
|
135
|
+
entry.value = value;
|
|
136
|
+
if (isManaged<V>()) {
|
|
137
|
+
__link(changetype<usize>(this), changetype<usize>(value), true);
|
|
138
|
+
}
|
|
139
|
+
++this.entriesCount;
|
|
140
|
+
// link with previous entry in bucket
|
|
141
|
+
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
|
|
142
|
+
entry.taggedNext = load<usize>(bucketPtrBase);
|
|
143
|
+
store<usize>(bucketPtrBase, changetype<usize>(entry));
|
|
144
|
+
}
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
delete(key: K): bool {
|
|
149
|
+
let entry = this.find(key, HASH<K>(key));
|
|
150
|
+
if (!entry) return false;
|
|
151
|
+
entry.taggedNext |= EMPTY;
|
|
152
|
+
--this.entriesCount;
|
|
153
|
+
// check if rehashing is appropriate
|
|
154
|
+
let halfBucketsMask = this.bucketsMask >> 1;
|
|
155
|
+
if (
|
|
156
|
+
halfBucketsMask + 1 >= max<u32>(INITIAL_CAPACITY, this.entriesCount) &&
|
|
157
|
+
this.entriesCount < this.entriesCapacity * FREE_FACTOR_N / FREE_FACTOR_D
|
|
158
|
+
) this.rehash(halfBucketsMask);
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private rehash(newBucketsMask: u32): void {
|
|
163
|
+
let newBucketsCapacity = <i32>(newBucketsMask + 1);
|
|
164
|
+
let newBuckets = new ArrayBuffer(newBucketsCapacity * <i32>BUCKET_SIZE);
|
|
165
|
+
let newEntriesCapacity = newBucketsCapacity * FILL_FACTOR_N / FILL_FACTOR_D;
|
|
166
|
+
let newEntries = new ArrayBuffer(newEntriesCapacity * <i32>ENTRY_SIZE<K,V>());
|
|
167
|
+
|
|
168
|
+
// copy old entries to new entries
|
|
169
|
+
let oldPtr = changetype<usize>(this.entries);
|
|
170
|
+
let oldEnd = oldPtr + <usize>this.entriesOffset * ENTRY_SIZE<K,V>();
|
|
171
|
+
let newPtr = changetype<usize>(newEntries);
|
|
172
|
+
while (oldPtr != oldEnd) {
|
|
173
|
+
let oldEntry = changetype<MapEntry<K,V>>(oldPtr);
|
|
174
|
+
if (!(oldEntry.taggedNext & EMPTY)) {
|
|
175
|
+
let newEntry = changetype<MapEntry<K,V>>(newPtr);
|
|
176
|
+
let oldEntryKey = oldEntry.key;
|
|
177
|
+
newEntry.key = oldEntryKey;
|
|
178
|
+
newEntry.value = oldEntry.value;
|
|
179
|
+
let newBucketIndex = HASH<K>(oldEntryKey) & newBucketsMask;
|
|
180
|
+
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
|
|
181
|
+
newEntry.taggedNext = load<usize>(newBucketPtrBase);
|
|
182
|
+
store<usize>(newBucketPtrBase, newPtr);
|
|
183
|
+
newPtr += ENTRY_SIZE<K,V>();
|
|
184
|
+
}
|
|
185
|
+
oldPtr += ENTRY_SIZE<K,V>();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
this.buckets = newBuckets;
|
|
189
|
+
this.bucketsMask = newBucketsMask;
|
|
190
|
+
this.entries = newEntries;
|
|
191
|
+
this.entriesCapacity = newEntriesCapacity;
|
|
192
|
+
this.entriesOffset = this.entriesCount;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
keys(): K[] {
|
|
196
|
+
// FIXME: this is preliminary, needs iterators/closures
|
|
197
|
+
let start = changetype<usize>(this.entries);
|
|
198
|
+
let size = this.entriesOffset;
|
|
199
|
+
let keys = new Array<K>(size);
|
|
200
|
+
let length = 0;
|
|
201
|
+
for (let i = 0; i < size; ++i) {
|
|
202
|
+
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
|
|
203
|
+
if (!(entry.taggedNext & EMPTY)) {
|
|
204
|
+
unchecked(keys[length++] = entry.key);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
keys.length = length;
|
|
208
|
+
return keys;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
values(): V[] {
|
|
212
|
+
// FIXME: this is preliminary, needs iterators/closures
|
|
213
|
+
let start = changetype<usize>(this.entries);
|
|
214
|
+
let size = this.entriesOffset;
|
|
215
|
+
let values = new Array<V>(size);
|
|
216
|
+
let length = 0;
|
|
217
|
+
for (let i = 0; i < size; ++i) {
|
|
218
|
+
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
|
|
219
|
+
if (!(entry.taggedNext & EMPTY)) {
|
|
220
|
+
unchecked(values[length++] = entry.value);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
values.length = length;
|
|
224
|
+
return values;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
toString(): string {
|
|
228
|
+
return "[object Map]";
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// RT integration
|
|
232
|
+
|
|
233
|
+
@unsafe private __visit(cookie: u32): void {
|
|
234
|
+
__visit(changetype<usize>(this.buckets), cookie);
|
|
235
|
+
let entries = changetype<usize>(this.entries);
|
|
236
|
+
if (isManaged<K>() || isManaged<V>()) {
|
|
237
|
+
let cur = entries;
|
|
238
|
+
let end = cur + <usize>this.entriesOffset * ENTRY_SIZE<K,V>();
|
|
239
|
+
while (cur < end) {
|
|
240
|
+
let entry = changetype<MapEntry<K,V>>(cur);
|
|
241
|
+
if (!(entry.taggedNext & EMPTY)) {
|
|
242
|
+
if (isManaged<K>()) {
|
|
243
|
+
let val = changetype<usize>(entry.key);
|
|
244
|
+
if (isNullable<K>()) {
|
|
245
|
+
if (val) __visit(val, cookie);
|
|
246
|
+
} else __visit(val, cookie);
|
|
247
|
+
}
|
|
248
|
+
if (isManaged<V>()) {
|
|
249
|
+
let val = changetype<usize>(entry.value);
|
|
250
|
+
if (isNullable<V>()) {
|
|
251
|
+
if (val) __visit(val, cookie);
|
|
252
|
+
} else __visit(val, cookie);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
cur += ENTRY_SIZE<K,V>();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
__visit(entries, cookie);
|
|
259
|
+
}
|
|
260
|
+
}
|