-
-
Notifications
You must be signed in to change notification settings - Fork 677
feat: Map support constructor initialization with MapInitialEntries #2941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
970fdb2
6ac8e6a
d746161
71fefe7
4b5981f
bcd86f2
5d0ecdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ under the licensing terms detailed in LICENSE: | |
* Fabián Heredia Montiel <[email protected]> | ||
* Jonas Minnberg <[email protected]> | ||
* Kam Chehresa <[email protected]> | ||
* Pebrian <[email protected]> | ||
|
||
Portions of this software are derived from third-party works licensed under | ||
the following terms: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,13 @@ function ENTRY_SIZE<K,V>(): usize { | |
return size; | ||
} | ||
|
||
class KeyValue<K,V> { | ||
key: K; | ||
value: V; | ||
} | ||
|
||
export type MapInitialEntries<K,V> = KeyValue<K,V>[]; | ||
|
||
export class Map<K,V> { | ||
|
||
// buckets referencing their respective first entry, usize[bucketsMask + 1] | ||
|
@@ -66,8 +73,44 @@ export class Map<K,V> { | |
private entriesOffset: i32 = 0; | ||
private entriesCount: i32 = 0; | ||
|
||
constructor() { | ||
/* nop */ | ||
constructor(initialEntries: MapInitialEntries<K,V> = []) { | ||
|
||
let entriesLength = initialEntries.length; | ||
|
||
if (entriesLength > 0) { | ||
if (entriesLength >= this.entriesCapacity) this.bucketsMask = entriesLength; | ||
this.rehash((this.bucketsMask << 1) | 1); | ||
|
||
for (let i = 0; i < entriesLength; i++) { | ||
let key = initialEntries[i].key; | ||
let value = initialEntries[i].value; | ||
let hashCode = HASH<K>(key); | ||
let entry = this.find(key, hashCode); // unmanaged! | ||
if (entry) { | ||
entry.value = value; | ||
if (isManaged<V>()) { | ||
__link(changetype<usize>(this), changetype<usize>(value), true); | ||
} | ||
} else { | ||
// append new entry | ||
let entries = this.entries; | ||
let entry = changetype<MapEntry<K,V>>(changetype<usize>(entries) + <usize>(this.entriesOffset++) * ENTRY_SIZE<K,V>()); | ||
// link with the map | ||
entry.key = key; | ||
if (isManaged<K>()) { | ||
__link(changetype<usize>(this), changetype<usize>(key), true); | ||
} | ||
entry.value = value; | ||
if (isManaged<V>()) { | ||
__link(changetype<usize>(this), changetype<usize>(value), 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)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
get size(): i32 { | ||
|
Uh oh!
There was an error while loading. Please reload this page.