-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: opt computeBandState & use internMap instead of Map (close: #193)…
… (#194)
- Loading branch information
1 parent
384ab2d
commit 8908324
Showing
6 changed files
with
227 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { InternMap } from '../../../src/utils'; | ||
|
||
describe('create InternMap ', () => { | ||
test('create InternMap with key of string', () => { | ||
const internMap = new InternMap([ | ||
[1, 'dog'], | ||
[2, 'cat'], | ||
]); | ||
internMap.set(3, 'cow'); | ||
expect(internMap.get(1)).toBe('dog'); | ||
expect(internMap.get(3)).toBe('cow'); | ||
|
||
internMap.set(1, 'mouse'); | ||
expect(internMap.get(1)).toBe('mouse'); | ||
|
||
internMap.delete(2); | ||
expect(internMap.has(2)).toBeFalsy(); | ||
}); | ||
|
||
test('create InternMap with key of object', () => { | ||
const time1 = new Date(Date.UTC(2022, 9, 5)); | ||
const time2 = new Date(Date.UTC(2022, 9, 5)); | ||
expect(time1 === time2).toBeFalsy(); | ||
|
||
const internMap = new InternMap([ | ||
[time1, 'time1'], | ||
[time2, 'time2'], | ||
]); | ||
|
||
expect(internMap.get(time1)).toBe('time2'); | ||
}); | ||
}); |
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,58 @@ | ||
function internGet({ map, initKey }, value) { | ||
const key = initKey(value); | ||
return map.has(key) ? map.get(key) : value; | ||
} | ||
|
||
function internSet({ map, initKey }, value) { | ||
const key = initKey(value); | ||
if (map.has(key)) return map.get(key); | ||
map.set(key, value); | ||
return value; | ||
} | ||
|
||
function internDelete({ map, initKey }, value) { | ||
const key = initKey(value); | ||
if (map.has(key)) { | ||
value = map.get(key); | ||
map.delete(key); | ||
} | ||
return value; | ||
} | ||
|
||
function keyof(value) { | ||
return typeof value === 'object' ? value.valueOf() : value; | ||
} | ||
|
||
/** | ||
* @see 参考 https://github.com/mbostock/internmap/blob/main/src/index.js | ||
*/ | ||
export class InternMap<K, V> extends Map { | ||
private map = new Map<K, V>(); | ||
|
||
private initKey = keyof; | ||
|
||
constructor(entries) { | ||
super(); | ||
if (entries !== null) { | ||
for (const [key, value] of entries) { | ||
this.set(key, value); | ||
} | ||
} | ||
} | ||
|
||
get(key: K) { | ||
return super.get(internGet({ map: this.map, initKey: this.initKey }, key)); | ||
} | ||
|
||
has(key: K) { | ||
return super.has(internGet({ map: this.map, initKey: this.initKey }, key)); | ||
} | ||
|
||
set(key: K, value: V) { | ||
return super.set(internSet({ map: this.map, initKey: this.initKey }, key), value); | ||
} | ||
|
||
delete(key: K) { | ||
return super.delete(internDelete({ map: this.map, initKey: this.initKey }, key)); | ||
} | ||
} |