1
1
var HashTableMain = /** @class */ ( function ( ) {
2
- function HashTableMain ( size ) {
3
- this . data = [ ] ;
4
- this . data = new Array ( size ) ;
5
- }
6
- HashTableMain . prototype . hash = function ( key ) {
7
- var hash = 0 ;
8
- for ( var i = 0 ; i < key . length ; i ++ ) {
9
- hash = ( hash + key . charCodeAt ( i ) * i ) % this . data . length ;
2
+ function HashTableMain ( size ) {
3
+ this . data = [ ] ;
4
+ this . data = new Array ( size ) ;
10
5
}
11
- return hash ;
12
- } ;
13
- /**
14
- * set
15
- */
16
- HashTableMain . prototype . set = function ( key , value ) {
17
- var address = this . hash ( key ) ;
18
- if ( ! this . data [ address ] ) {
19
- this . data [ address ] = [ ] ;
20
- }
21
- this . data [ address ] . push ( [ key , value ] ) ;
22
- return this . data ;
23
- } ;
24
- /**
25
- * Get
26
- */
27
- HashTableMain . prototype . get = function ( key ) {
28
- var getAddress = this . hash ( key ) ;
29
- var currentBucket = this . data [ getAddress ] ;
30
- if ( currentBucket ) {
31
- for ( var i = 0 ; i < currentBucket . length ; i ++ ) {
32
- if ( currentBucket [ i ] [ 0 ] === key ) {
33
- return currentBucket [ i ] [ 1 ] ;
6
+ HashTableMain . prototype . hash = function ( key ) {
7
+ var hash = 0 ;
8
+ for ( var i = 0 ; i < key . length ; i ++ ) {
9
+ hash = ( hash + key . charCodeAt ( i ) * i ) % this . data . length ;
34
10
}
35
- }
36
- }
37
- return undefined ;
38
- } ;
39
- return HashTableMain ;
40
- } ) ( ) ;
11
+ return hash ;
12
+ } ;
13
+ /**
14
+ * set
15
+ */
16
+ HashTableMain . prototype . set = function ( key , value ) {
17
+ var address = this . hash ( key ) ;
18
+ if ( ! this . data [ address ] ) {
19
+ this . data [ address ] = [ ] ;
20
+ }
21
+ this . data [ address ] . push ( [ key , value ] ) ;
22
+ return this . data ;
23
+ } ;
24
+ /**
25
+ * Get
26
+ */
27
+ HashTableMain . prototype . get = function ( key ) {
28
+ var getAddress = this . hash ( key ) ;
29
+ var currentBucket = this . data [ getAddress ] ;
30
+ if ( currentBucket ) {
31
+ for ( var i = 0 ; i < currentBucket . length ; i ++ ) {
32
+ if ( currentBucket [ i ] [ 0 ] === key ) {
33
+ return currentBucket [ i ] [ 1 ] ;
34
+ }
35
+ }
36
+ }
37
+ return undefined ;
38
+ } ;
39
+ /**
40
+ * keys
41
+ */
42
+ HashTableMain . prototype . keys = function ( ) {
43
+ var keysArray = [ ] ;
44
+ console . log ( this . data . length ) ;
45
+ for ( var i = 0 ; i < this . data . length ; i ++ ) {
46
+ console . log ( this . data [ i ] ) ;
47
+ if ( this . data [ i ] ) {
48
+ console . log ( this . data [ i ] [ 0 ] ) ;
49
+ console . log ( this . data [ i ] [ 0 ] [ 0 ] ) ;
50
+ keysArray . push ( this . data [ i ] [ 0 ] [ 0 ] ) ;
51
+ }
52
+ }
53
+ return keysArray ;
54
+ } ;
55
+ return HashTableMain ;
56
+ } ( ) ) ;
41
57
// Example
42
58
var myHashTable01 = new HashTableMain ( 50 ) ;
43
59
myHashTable01 . set ( 'grapes' , 10000 ) ;
@@ -46,5 +62,5 @@ myHashTable01.set('apples', 9);
46
62
myHashTable01 . get ( 'apples' ) ;
47
63
myHashTable01 . set ( 'banana' , 'Nice foots' ) ;
48
64
myHashTable01 . get ( 'banana' ) ;
49
-
65
+ myHashTable01 . keys ( ) ;
50
66
console . log ( myHashTable01 . data ) ;
0 commit comments