File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed
src/data-structures/hash-table Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -105,4 +105,16 @@ export default class HashTable {
105
105
getKeys ( ) {
106
106
return Object . keys ( this . keys ) ;
107
107
}
108
+
109
+ /**
110
+ * Gets the list of all the stored values in the hash table in the order of
111
+ * the keys map.
112
+ *
113
+ * @return {*[] }
114
+ */
115
+ getValues ( ) {
116
+ const keys = this . getKeys ( ) ;
117
+
118
+ return keys . map ( key => this . buckets [ this . hash ( key ) ] . head . value . value ) ;
119
+ }
108
120
}
Original file line number Diff line number Diff line change @@ -86,4 +86,14 @@ describe('HashTable', () => {
86
86
expect ( hashTable . has ( 'b' ) ) . toBe ( true ) ;
87
87
expect ( hashTable . has ( 'x' ) ) . toBe ( false ) ;
88
88
} ) ;
89
+
90
+ it ( 'should get all the values' , ( ) => {
91
+ const hashTable = new HashTable ( 3 ) ;
92
+
93
+ hashTable . set ( 'a' , 'alpha' ) ;
94
+ hashTable . set ( 'b' , 'beta' ) ;
95
+ hashTable . set ( 'c' , 'gamma' ) ;
96
+
97
+ expect ( hashTable . getValues ( ) ) . toEqual ( [ 'alpha' , 'beta' , 'gamma' ] ) ;
98
+ } ) ;
89
99
} ) ;
You can’t perform that action at this time.
0 commit comments