File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ export class HashTable {
2+ protected size : number ;
3+ private data ;
4+
5+ constructor ( size : number ) {
6+ this . size = size ;
7+ this . data = new Array ( size ) ;
8+ }
9+
10+ private hash ( key : string ) {
11+ let hash = 0 ;
12+ for ( let i = 0 ; i < key . length ; i ++ ) {
13+ hash = ( hash + key . charCodeAt ( i ) * i ) % this . data . length ;
14+ }
15+ return hash ;
16+ }
17+
18+ public set ( key : string , value : any ) {
19+ let address = this . hash ( key ) ;
20+ if ( ! this . data [ address ] ) {
21+ this . data [ address ] = [ ] ;
22+ }
23+ this . data [ address ] . push ( [ key , value ] ) ;
24+ return this . data ;
25+ }
26+
27+ public get ( key : string ) {
28+ let address = this . hash ( key ) ;
29+ const currentBucket = this . data [ address ] ;
30+ if ( currentBucket ) {
31+ for ( let 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+ public keys ( ) {
41+ const keysArray = [ ] ;
42+ for ( let i = 0 ; i < this . data . length ; i ++ ) {
43+ if ( this . data [ i ] ) {
44+ keysArray . push ( this . data [ i ] [ 0 ] [ 0 ] ) ;
45+ }
46+ }
47+ return keysArray ;
48+ }
49+ }
Original file line number Diff line number Diff line change 11console . log ( 'Typescript Data Structures & Algorithms' ) ;
22import './arrays/arrayClass' ;
3+ import './hash-table/HashTable' ;
You can’t perform that action at this time.
0 commit comments