@@ -46,13 +46,18 @@ class HashMap {
4646 * @param {any } value
4747 */
4848 set ( key , value ) {
49- const index = this . getIndex ( key ) ;
50- if ( this . array [ index ] ) {
51- this . array [ index ] . push ( { key, value} ) ;
49+ const { hashIndex, arrayIndex} = this . _getIndexes ( key ) ;
50+
51+ if ( arrayIndex === undefined ) {
52+ // initialize array and save key/value
53+ this . array [ hashIndex ] = this . array [ hashIndex ] || [ ] ;
54+ this . array [ hashIndex ] . push ( { key, value} ) ;
55+ this . size ++ ;
5256 } else {
53- this . array [ index ] = [ { key, value} ] ;
57+ // override existing value
58+ this . array [ hashIndex ] [ arrayIndex ] . value = value ;
5459 }
55- this . size ++ ;
60+
5661 return this ;
5762 }
5863
@@ -144,6 +149,7 @@ assert.equal(hashMap.has('rat'), false);
144149assert . equal ( hashMap . size , 3 ) ;
145150
146151// set override
147- // assert.equal(hashMap.get('art'), 0);
148- // hashMap.set('art', 2);
149- // assert.equal(hashMap.get('art'), 2);
152+ assert . equal ( hashMap . get ( 'art' ) , 0 ) ;
153+ hashMap . set ( 'art' , 2 ) ;
154+ assert . equal ( hashMap . get ( 'art' ) , 2 ) ;
155+ assert . equal ( hashMap . size , 3 ) ;
0 commit comments