| 792 | } |
| 793 | |
| 794 | class HashMapIterator<K, V, A> implements IterableIterator<A> { |
| 795 | readonly stack: Array<Node<K, V>> |
| 796 | readonly project: (key: K, value: V) => A |
| 797 | entries: Array<[K, V]> = [] |
| 798 | index = 0 |
| 799 | |
| 800 | constructor(root: Node<K, V>, project: (key: K, value: V) => A) { |
| 801 | this.stack = [root] |
| 802 | this.project = project |
| 803 | } |
| 804 | |
| 805 | next(): IteratorResult<A> { |
| 806 | while (true) { |
| 807 | if (this.index < this.entries.length) { |
| 808 | const entry = this.entries[this.index++] |
| 809 | return { done: false, value: this.project(entry[0], entry[1]) } |
| 810 | } |
| 811 | if (this.stack.length === 0) { |
| 812 | return { done: true, value: undefined } |
| 813 | } |
| 814 | const node = this.stack.pop()! |
| 815 | switch (node._tag) { |
| 816 | case "LeafNode": { |
| 817 | const leaf = node as LeafNode<K, V> |
| 818 | return { done: false, value: this.project(leaf.key, leaf.value) } |
| 819 | } |
| 820 | case "CollisionNode": { |
| 821 | this.entries = (node as CollisionNode<K, V>).entries |
| 822 | this.index = 0 |
| 823 | break |
| 824 | } |
| 825 | case "IndexedNode": |
| 826 | case "ArrayNode": { |
| 827 | const children = (node as IndexedNode<K, V> | ArrayNode<K, V>).children |
| 828 | for (let i = children.length - 1; i >= 0; i--) { |
| 829 | const child = children[i] |
| 830 | if (child) this.stack.push(child) |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | [Symbol.iterator](): IterableIterator<A> { |
| 838 | return this |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | const toEntry = <K, V>(key: K, value: V): [K, V] => [key, value] |
| 843 | const toValue = <V>(_: unknown, value: V): V => value |
nothing calls this directly
no outgoing calls
no test coverage detected