@@ -9,8 +9,9 @@ class HashMap {
99 * @param {number } size
1010 */
1111 constructor ( size = 1000 ) {
12- this . array = new Array ( size ) ;
12+ this . buckets = new Array ( size ) ;
1313 this . size = 0 ;
14+ this . collisions = 0 ;
1415 }
1516
1617 /**
@@ -33,10 +34,10 @@ class HashMap {
3334 * Get the array index after applying the hash funtion to the given key
3435 * @param {any } key
3536 */
36- getIndex ( key ) {
37- const indexHash = this . hash ( key ) ;
38- const index = indexHash % this . array . length ;
39- return index ;
37+ _getBucketIndex ( key ) {
38+ const hashValue = this . hash ( key ) ;
39+ const bucketIndex = hashValue % this . buckets . length ;
40+ return bucketIndex ;
4041 }
4142
4243 /**
@@ -46,16 +47,18 @@ class HashMap {
4647 * @param {any } value
4748 */
4849 set ( key , value ) {
49- const { hashIndex , arrayIndex } = this . _getIndexes ( key ) ;
50+ const { bucketIndex , entryIndex } = this . _getIndexes ( key ) ;
5051
51- if ( arrayIndex === undefined ) {
52+ if ( entryIndex === undefined ) {
5253 // initialize array and save key/value
53- this . array [ hashIndex ] = this . array [ hashIndex ] || [ ] ;
54- this . array [ hashIndex ] . push ( { key, value} ) ;
54+ this . buckets [ bucketIndex ] = this . buckets [ bucketIndex ] || [ ] ;
55+ this . buckets [ bucketIndex ] . push ( { key, value} ) ;
5556 this . size ++ ;
57+ // Optional: keep count of collisions
58+ if ( this . buckets [ bucketIndex ] . length > 1 ) { this . collisions ++ ; }
5659 } else {
5760 // override existing value
58- this . array [ hashIndex ] [ arrayIndex ] . value = value ;
61+ this . buckets [ bucketIndex ] [ entryIndex ] . value = value ;
5962 }
6063
6164 return this ;
@@ -67,13 +70,13 @@ class HashMap {
6770 * @param {any } key
6871 */
6972 get ( key ) {
70- const { hashIndex , arrayIndex } = this . _getIndexes ( key ) ;
73+ const { bucketIndex , entryIndex } = this . _getIndexes ( key ) ;
7174
72- if ( arrayIndex === undefined ) {
75+ if ( entryIndex === undefined ) {
7376 return ;
7477 }
7578
76- return this . array [ hashIndex ] [ arrayIndex ] . value ;
79+ return this . buckets [ bucketIndex ] [ entryIndex ] . value ;
7780 }
7881
7982 /**
@@ -86,35 +89,35 @@ class HashMap {
8689
8790 /**
8891 * Search for a key in the map. It returns it's internal array indexes.
89- * Returns hashIndex and the internal array index
92+ * Returns bucketIndex and the internal array index
9093 * @param {any } key
9194 */
9295 _getIndexes ( key ) {
93- const hashIndex = this . getIndex ( key ) ;
94- const values = this . array [ hashIndex ] || [ ] ;
96+ const bucketIndex = this . _getBucketIndex ( key ) ;
97+ const values = this . buckets [ bucketIndex ] || [ ] ;
9598
96- for ( let arrayIndex = 0 ; arrayIndex < values . length ; arrayIndex ++ ) {
97- const entry = values [ arrayIndex ] ;
99+ for ( let entryIndex = 0 ; entryIndex < values . length ; entryIndex ++ ) {
100+ const entry = values [ entryIndex ] ;
98101 if ( entry . key === key ) {
99- return { hashIndex , arrayIndex } ;
102+ return { bucketIndex , entryIndex } ;
100103 }
101104 }
102105
103- return { hashIndex } ;
106+ return { bucketIndex } ;
104107 }
105108
106109 /**
107110 * Returns true if an element in the Map object existed and has been removed, or false if the element does not exist.
108111 * @param {any } key
109112 */
110113 delete ( key ) {
111- const { hashIndex , arrayIndex } = this . _getIndexes ( key ) ;
114+ const { bucketIndex , entryIndex } = this . _getIndexes ( key ) ;
112115
113- if ( arrayIndex === undefined ) {
116+ if ( entryIndex === undefined ) {
114117 return false ;
115118 }
116119
117- this . array [ hashIndex ] . splice ( arrayIndex , 1 ) ;
120+ this . buckets [ bucketIndex ] . splice ( entryIndex , 1 ) ;
118121 this . size -- ;
119122
120123 return true ;
@@ -123,6 +126,7 @@ class HashMap {
123126
124127// Usage:
125128const hashMap = new HashMap ( ) ;
129+ // const hashMap = new HashMap(1);
126130// const hashMap = new Map();
127131
128132const assert = require ( 'assert' ) ;
@@ -134,8 +138,6 @@ hashMap.set('dog', 1);
134138hashMap . set ( 'art' , 0 ) ;
135139assert . equal ( hashMap . size , 4 ) ;
136140
137- // console.log(hashMap.array);
138-
139141assert . equal ( hashMap . get ( 'cat' ) , 2 ) ;
140142assert . equal ( hashMap . get ( 'rat' ) , 7 ) ;
141143assert . equal ( hashMap . get ( 'dog' ) , 1 ) ;
@@ -152,4 +154,8 @@ assert.equal(hashMap.size, 3);
152154assert . equal ( hashMap . get ( 'art' ) , 0 ) ;
153155hashMap . set ( 'art' , 2 ) ;
154156assert . equal ( hashMap . get ( 'art' ) , 2 ) ;
155- assert . equal ( hashMap . size , 3 ) ;
157+ assert . equal ( hashMap . size , 3 ) ;
158+
159+ // internal structure
160+ console . log ( hashMap . collisions ) ;
161+ console . log ( hashMap . buckets ) ;
0 commit comments