1+ class HashMap {
2+ constructor ( initialCapacity = 16 , loadFactor = 0.75 ) {
3+ this . buckets = new Array ( initialCapacity ) ;
4+ this . size = 0 ;
5+ this . capacity = initialCapacity ;
6+ this . loadFactor = loadFactor ;
7+ }
8+
9+ hash ( key ) {
10+ let hashCode = 0 ;
11+ const primeNumber = 31 ;
12+ for ( let i = 0 ; i < key . length ; i ++ ) {
13+ hashCode = ( primeNumber * hashCode + key . charCodeAt ( i ) ) % this . capacity ;
14+ }
15+ return hashCode ;
16+ }
17+
18+ set ( key , value ) {
19+ if ( this . size / this . capacity >= this . loadFactor ) {
20+ this . resize ( ) ;
21+ }
22+ const index = this . hash ( key ) ;
23+ if ( ! this . buckets [ index ] ) {
24+ this . buckets [ index ] = [ ] ;
25+ }
26+ for ( let pair of this . buckets [ index ] ) {
27+ if ( pair [ 0 ] === key ) {
28+ pair [ 1 ] = value ;
29+ return ;
30+ }
31+ }
32+ this . buckets [ index ] . push ( [ key , value ] ) ;
33+ this . size ++ ;
34+ }
35+
36+ get ( key ) {
37+ const index = this . hash ( key ) ;
38+ const bucket = this . buckets [ index ] ;
39+ if ( ! bucket ) return null ;
40+ for ( let pair of bucket ) {
41+ if ( pair [ 0 ] === key ) return pair [ 1 ] ;
42+ }
43+ return null ;
44+ }
45+
46+ has ( key ) {
47+ return this . get ( key ) !== null ;
48+ }
49+
50+ remove ( key ) {
51+ const index = this . hash ( key ) ;
52+ const bucket = this . buckets [ index ] ;
53+ if ( ! bucket ) return false ;
54+ for ( let i = 0 ; i < bucket . length ; i ++ ) {
55+ if ( bucket [ i ] [ 0 ] === key ) {
56+ bucket . splice ( i , 1 ) ;
57+ this . size -- ;
58+ return true ;
59+ }
60+ }
61+ return false ;
62+ }
63+
64+ length ( ) {
65+ return this . size ;
66+ }
67+
68+ clear ( ) {
69+ this . buckets = new Array ( this . capacity ) ;
70+ this . size = 0 ;
71+ }
72+
73+ keys ( ) {
74+ return this . buckets . flatMap ( bucket => ( bucket ? bucket . map ( pair => pair [ 0 ] ) : [ ] ) ) ;
75+ }
76+
77+ values ( ) {
78+ return this . buckets . flatMap ( bucket => ( bucket ? bucket . map ( pair => pair [ 1 ] ) : [ ] ) ) ;
79+ }
80+
81+ entries ( ) {
82+ return this . buckets . flatMap ( bucket => ( bucket ? bucket . map ( pair => pair ) : [ ] ) ) ;
83+ }
84+
85+ resize ( ) {
86+ this . capacity *= 2 ;
87+ const oldBuckets = this . buckets ;
88+ this . buckets = new Array ( this . capacity ) ;
89+ this . size = 0 ;
90+ for ( const bucket of oldBuckets ) {
91+ if ( bucket ) {
92+ for ( const [ key , value ] of bucket ) {
93+ this . set ( key , value ) ;
94+ }
95+ }
96+ }
97+ }
98+ }
99+
100+
101+ const test = new HashMap ( ) ;
102+ test . set ( 'apple' , 'red' ) ;
103+ test . set ( 'banana' , 'yellow' ) ;
104+ test . set ( 'carrot' , 'orange' ) ;
105+ test . set ( 'dog' , 'brown' ) ;
106+ test . set ( 'elephant' , 'gray' ) ;
107+ test . set ( 'frog' , 'green' ) ;
108+ test . set ( 'grape' , 'purple' ) ;
109+ test . set ( 'hat' , 'black' ) ;
110+ test . set ( 'ice cream' , 'white' ) ;
111+ test . set ( 'jacket' , 'blue' ) ;
112+ test . set ( 'kite' , 'pink' ) ;
113+ test . set ( 'lion' , 'golden' ) ;
114+ test . set ( 'moon' , 'silver' ) ;
115+
116+ console . log ( test . entries ( ) ) ;
0 commit comments