File tree Expand file tree Collapse file tree 4 files changed +80
-3
lines changed Expand file tree Collapse file tree 4 files changed +80
-3
lines changed Original file line number Diff line number Diff line change @@ -108,11 +108,11 @@ class HashMap {
108108 for ( let entryIndex = 0 ; entryIndex < values . length ; entryIndex ++ ) {
109109 const entry = values [ entryIndex ] ;
110110 if ( entry . key === key ) {
111- return { bucketIndex, entryIndex} ;
111+ return { bucketIndex, entryIndex, keyIndex : entry . keyIndex } ;
112112 }
113113 }
114114
115- return { bucketIndex, entryIndex : undefined } ;
115+ return { bucketIndex} ;
116116 }
117117
118118 /**
Original file line number Diff line number Diff line change @@ -35,9 +35,21 @@ describe('without collisions', () => {
3535 hashMap . set ( 'test' , 'uno' ) ;
3636 expect ( hashMap . has ( 'test' ) ) . toBe ( true ) ;
3737 } ) ;
38+
39+ it ( 'should update keys on deletes' , ( ) => {
40+ hashMap . set ( 'Despacito' , 'Luis Fonsi' ) ;
41+ hashMap . set ( 'Bailando' , 'Enrique Iglesias' ) ;
42+ hashMap . set ( 'Dura' , 'Daddy Yankee' ) ;
43+
44+ expect ( hashMap . delete ( 'Bailando' ) ) . toBe ( true ) ;
45+ expect ( hashMap . delete ( 'Bailando' ) ) . toBe ( false ) ;
46+ expect ( hashMap . get ( 'Bailando' ) ) . toBe ( undefined ) ;
47+
48+ expect ( hashMap . keys . map ( k => k . content ) ) . toEqual ( [ "Despacito" , , "Dura" ] ) ;
49+ } ) ;
3850} ) ;
3951
40- describe ( 'with many collisions' , ( ) => {
52+ describe ( 'with many values (and collisions) ' , ( ) => {
4153 let hashMap ;
4254
4355 beforeEach ( ( ) => {
Original file line number Diff line number Diff line change @@ -22,6 +22,15 @@ class MySet {
2222 delete ( value ) {
2323 return this . map . delete ( value ) ;
2424 }
25+
26+ entries ( ) {
27+ return this . map . keys . reduce ( ( acc , key ) => {
28+ if ( key !== undefined ) {
29+ acc . push ( key . content ) ;
30+ }
31+ return acc
32+ } , [ ] ) ;
33+ }
2534}
2635
2736module . exports = MySet ;
Original file line number Diff line number Diff line change 1+ const MySet = require ( './set' ) ;
2+
3+ describe ( 'Set' , function ( ) {
4+ let set ;
5+
6+ beforeEach ( ( ) => {
7+ set = new MySet ( ) ;
8+ } ) ;
9+
10+ it ( 'should set size and has' , ( ) => {
11+ expect ( set . size ) . toBe ( 0 ) ;
12+ expect ( set . has ( 'uno' ) ) . toBe ( false ) ;
13+
14+ set . add ( 'uno' ) ;
15+
16+ expect ( set . has ( 'uno' ) ) . toBe ( true ) ;
17+ expect ( set . size ) . toBe ( 1 ) ;
18+ } ) ;
19+
20+ it ( 'should not allow duplicates' , ( ) => {
21+ set . add ( 'uno' ) ;
22+ set . add ( 'one' ) ;
23+ set . add ( 'uno' ) ;
24+
25+ expect ( set . size ) . toBe ( 2 ) ;
26+ expect ( set . has ( 'uno' ) ) . toBe ( true ) ;
27+ expect ( set . has ( 'one' ) ) . toBe ( true ) ;
28+ } ) ;
29+
30+ it ( 'should delete items' , ( ) => {
31+ expect ( set . delete ( 'uno' ) ) . toBe ( false ) ;
32+
33+ set . add ( 'uno' ) ;
34+
35+ expect ( set . delete ( 'uno' ) ) . toBe ( true ) ;
36+ expect ( set . size ) . toBe ( 0 ) ;
37+ } ) ;
38+
39+ it ( 'should return entries' , ( ) => {
40+ set . add ( 1 ) ;
41+ set . add ( 2 ) ;
42+ set . add ( 3 ) ;
43+ expect ( set . entries ( ) ) . toEqual ( [ 1 , 2 , 3 ] ) ;
44+ } ) ;
45+
46+ it ( 'should return entries' , ( ) => {
47+ set . add ( 0 ) ;
48+ set . add ( 1 ) ;
49+ set . add ( 2 ) ;
50+ set . add ( 3 ) ;
51+
52+ expect ( set . delete ( 2 ) ) . toBe ( true ) ;
53+
54+ expect ( set . entries ( ) ) . toEqual ( [ 0 , 1 , 3 ] ) ;
55+ } ) ;
56+ } ) ;
You can’t perform that action at this time.
0 commit comments