-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
223 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* A WeakMap with iterable entries | ||
*/ | ||
export class IterableWeakMap<K extends WeakKey, V> extends Map<any, V> { | ||
|
||
set(key: K, value: V): this { | ||
return super.set(new WeakRef(key), value); | ||
} | ||
|
||
delete(key: K): boolean { | ||
const deleting = new Set<WeakRef<K>>() | ||
try { | ||
for (const keyRef of super.keys() as Iterable<WeakRef<K>>) { | ||
const unwrappedKey = keyRef.deref() | ||
if (unwrappedKey == undefined) { | ||
deleting.add(keyRef); | ||
continue; | ||
} | ||
if (unwrappedKey === key) { | ||
deleting.add(keyRef) | ||
for (const keyRef of deleting) super.delete(keyRef); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
finally { | ||
for (const keyRef of deleting) super.delete(keyRef); | ||
} | ||
} | ||
|
||
has(key: K): boolean { | ||
for (const unwrappedKey of this.keys()) { | ||
if (unwrappedKey === key) return true; | ||
} | ||
return false; | ||
} | ||
|
||
*keys(): IterableIterator<K> { | ||
const deleting = new Set<WeakRef<K>>() | ||
try { | ||
for (const keyRef of super.keys() as Iterable<WeakRef<K>>) { | ||
const unwrappedKey = keyRef.deref() | ||
if (unwrappedKey == undefined) { | ||
deleting.add(keyRef) | ||
continue; | ||
} | ||
yield unwrappedKey; | ||
} | ||
} | ||
finally { | ||
for (const keyRef of deleting) super.delete(keyRef); | ||
} | ||
} | ||
|
||
get(key: K): V|undefined { | ||
for (const [unwrappedKey, val] of this.entries()) { | ||
if (unwrappedKey === key) return val; | ||
} | ||
} | ||
|
||
*values(): IterableIterator<V> { | ||
for (const [key, val] of this.entries()) { | ||
yield val | ||
} | ||
} | ||
|
||
*entries(): IterableIterator<[K, V]> { | ||
const deleting = new Set<WeakRef<K>>() | ||
try { | ||
for (const [keyRef, val] of super.entries() as Iterable<[WeakRef<K>, V]>) { | ||
const unwrappedKey = keyRef.deref() | ||
if (unwrappedKey == undefined) { | ||
deleting.add(keyRef) | ||
continue; | ||
} | ||
yield [unwrappedKey, val]; | ||
} | ||
} | ||
finally { | ||
for (const keyRef of deleting) super.delete(keyRef); | ||
} | ||
} | ||
|
||
[Symbol.iterator]() { | ||
return this.entries() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* A WeakSet with iterable entries | ||
*/ | ||
export class IterableWeakSet<T extends WeakKey> extends Set { | ||
|
||
add(value: T): this { | ||
return super.add(new WeakRef(value)); | ||
} | ||
|
||
delete(value: T): boolean { | ||
const deleting = new Set<WeakRef<T>>() | ||
try { | ||
for (const valRef of super.values() as Iterable<WeakRef<T>>) { | ||
const val = valRef.deref() | ||
if (val == undefined) { | ||
deleting.add(valRef) | ||
continue; | ||
} | ||
if (val === value) { | ||
deleting.add(valRef) | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
finally { | ||
for (const valRef of deleting) super.delete(valRef); | ||
} | ||
} | ||
|
||
has(value: T): boolean { | ||
for (const val of this.values()) { | ||
if (val === value) return true; | ||
} | ||
return false; | ||
} | ||
|
||
*values(): IterableIterator<T> { | ||
const deleting = new Set<WeakRef<T>>() | ||
try { | ||
for (const valRef of super.values() as Iterable<WeakRef<T>>) { | ||
const val = valRef.deref() | ||
if (val == undefined) { | ||
deleting.add(valRef); | ||
continue; | ||
} | ||
yield val; | ||
} | ||
} | ||
finally { | ||
for (const valRef of deleting) super.delete(valRef); | ||
} | ||
} | ||
|
||
keys(): IterableIterator<T> { | ||
return this.values() | ||
} | ||
|
||
*entries(): IterableIterator<[T, T]> { | ||
for (const val of this.values()) { | ||
yield [val, val] | ||
} | ||
} | ||
|
||
[Symbol.iterator]() { | ||
return this.values() | ||
} | ||
} |