1
+ let hashTable = {
2
+ setup : function ( length ) {
3
+ // helper function for ensuring length of hash table is prime
4
+ function nextPrime ( num ) {
5
+ function isPrime ( n ) {
6
+ if ( n <= 1 ) throw new Error ( "Length must be greater then 1!" ) ;
7
+ for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) {
8
+ if ( n % i == 0 ) return false ;
9
+ }
10
+ return true ;
11
+ }
12
+ while ( ! isPrime ( num ) ) num ++ ;
13
+ return num ;
14
+ }
15
+
16
+ this . length = nextPrime ( length ) ;
17
+ this . numberOfElements = 0 ;
18
+ this . table = [ ] ;
19
+ for ( let i = 0 ; i < this . length ; i ++ ) this . table [ i ] = [ ] ;
20
+ } ,
21
+ hashFunction : function ( input ) {
22
+ let sum = 0 ;
23
+ input = input . toLowerCase ( ) ;
24
+ for ( let i = 1 ; i < input . length ; i ++ ) {
25
+ sum += input . charCodeAt ( i ) - 96 ;
26
+ }
27
+ return sum % this . length ;
28
+ } ,
29
+ put : function ( key , value = key ) {
30
+ // helper function for resizing hash table when # of elements equals the length of table
31
+ function resize ( table ) {
32
+ let oldTable = JSON . parse ( JSON . stringify ( table ) ) ;
33
+ Object . setPrototypeOf ( oldTable , table ) ;
34
+ table . setup ( 2 * table . length ) ;
35
+ for ( let element of oldTable ) {
36
+ table . put ( element . key , element . value ) ;
37
+ }
38
+ }
39
+
40
+ if ( this . numberOfElements >= this . length ) resize ( this ) ;
41
+ let index = this . hashFunction ( key ) ;
42
+ this . table [ index ] . push ( {
43
+ key : key ,
44
+ value : value
45
+ } ) ;
46
+ this . numberOfElements ++ ;
47
+ } ,
48
+ includes : function ( key ) {
49
+ let index = this . hashFunction ( key ) ;
50
+ for ( let element of this . table [ index ] ) {
51
+ if ( element . key == key ) return true ;
52
+ }
53
+ return false ;
54
+ } ,
55
+ find : function ( key ) {
56
+ let index = this . hashFunction ( key ) ;
57
+ for ( let element of this . table [ index ] ) {
58
+ if ( element . key == key ) return element . value ;
59
+ }
60
+ } ,
61
+ remove : function ( key ) {
62
+ let index = this . hashFunction ( key ) ;
63
+ for ( let i = 0 ; i < this . table [ index ] . length ; i ++ ) {
64
+ if ( this . table [ index ] [ i ] . key == key ) {
65
+ this . table [ index ] . splice ( i , 1 ) ;
66
+ this . numberOfElements -- ;
67
+ return true ;
68
+ }
69
+ }
70
+ return false ;
71
+ } ,
72
+ clear : function ( ) {
73
+ this . setup ( this . length ) ;
74
+ } ,
75
+ [ Symbol . iterator ] : function ( ) {
76
+ let i = 0 , j = 0 ;
77
+ let self = this ;
78
+ return {
79
+ [ Symbol . iterator ] ( ) { return this ; } ,
80
+ next : function next ( ) {
81
+ if ( j < self . table [ i ] . length ) {
82
+ j ++ ;
83
+ return {
84
+ value : self . table [ i ] [ j - 1 ] ,
85
+ done : false
86
+ }
87
+ }
88
+ else if ( i < self . table . length - 1 ) {
89
+ i ++ ;
90
+ j = 0 ;
91
+ return next ( ) ;
92
+ }
93
+ else {
94
+ return {
95
+ done : true
96
+ }
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ export default hashTable ;
0 commit comments