Skip to content

Commit

Permalink
Fix PairMap
Browse files Browse the repository at this point in the history
  • Loading branch information
huynhtrankhanh committed Mar 9, 2024
1 parent ceebdfa commit 1b4ed75
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions compiler/PairMap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ describe('PairMap', () => {
test('set method works for different types of keys', () => {
pairMap.set([1, 'a'], 'value')
expect(pairMap.get([1, 'a'])).toBe('value')
pairMap.set(['x', { key: 'b' }], 'anotherValue')
expect(pairMap.get(['x', { key: 'b' }])).toBe('anotherValue')
const key = { key: 'b' }
pairMap.set(['x', key], 'anotherValue')
expect(pairMap.get(['x', key])).toBe('anotherValue')
})

test('get method returns undefined for non-existing keys', () => {
Expand Down
8 changes: 4 additions & 4 deletions compiler/PairMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ function createCounter() {
}

export class PairMap<KeyLeft, KeyRight, Value> {
private leftKeyMap = new WeakMap<KeyLeft, number>()
private rightKeyMap = new WeakMap<KeyRight, number>()
private valueMap = new WeakMap<string, Value>()
private leftKeyMap = new Map<KeyLeft, number>()
private rightKeyMap = new Map<KeyRight, number>()
private valueMap = new Map<string, Value>()
private getNextCounter: () => number

constructor() {
this.getNextCounter = createCounter()
}

private getOrCreateCounterForKey<T>(
map: WeakMap<object, number>,
map: Map<T, number>,
key: T
): number {
if (!map.has(key)) {
Expand Down

0 comments on commit 1b4ed75

Please sign in to comment.