1+ /**
2+ * Created by Stefano on 05/04/2014.
3+ */
4+
5+ /**
6+ * Class for managing an hash table.
7+ * @param size {number} The size of the table.
8+ * @constructor
9+ */
10+ function HashTable ( size ) {
11+ /**
12+ * The size of the table
13+ * @type {number }
14+ */
15+ this . size = size ;
16+
17+ this . p = 1000 ;
18+
19+ this . a = Math . floor ( Math . random ( ) * this . p ) ;
20+
21+ this . b = Math . floor ( Math . random ( ) * this . p ) ;
22+
23+ /**
24+ * Calculate the hash of the param key.
25+ * @param key {number} The key to hash.
26+ * @return {number } The hash of the key.
27+ */
28+ this . hash = function ( key ) {
29+ return ( ( this . a * key + this . b ) % this . p ) % this . size ;
30+ } ;
31+
32+ /**
33+ * The items stored in the hash table.
34+ * @type {Array<DoubleLinkedList> }
35+ */
36+ this . items = [ ] ;
37+
38+ for ( var i = 0 ; i < size ; i ++ )
39+ this . items [ i ] = new DoubleLinkedList ( ) ;
40+ }
41+
42+ /**
43+ * Store the item with its key.
44+ * @param key {number} The key relatives to the item.
45+ * @param item {*} The item to store.
46+ */
47+ HashTable . prototype . insert = function ( key , item ) {
48+ this . items [ this . hash ( key ) ] . pushBack ( { key : key , item : item } ) ;
49+ } ;
50+
51+ /**
52+ * Delete the first item relatives to the key value.
53+ * @param key {number} The key to delete.
54+ * @return {void }
55+ */
56+ HashTable . prototype . deleteKey = function ( key ) {
57+ var list = this . items [ this . hash ( key ) ] ;
58+ var it = list . getIterator ( ) ;
59+ for ( it . first ( ) ; ! it . isDone ( ) && it . getItem ( ) . key !== key ; )
60+ it . next ( ) ;
61+ if ( ! it . isDone ( ) )
62+ list . deleteNode ( it . getNode ( ) ) ;
63+ } ;
64+
65+ /**
66+ * Delete all the items relative to the key value.
67+ * @param key {number} The key to delete.
68+ * @return {void }
69+ */
70+ HashTable . prototype . deleteAllKey = function ( key ) {
71+ var list = this . items [ this . hash ( key ) ] ;
72+ var it = list . getIterator ( ) ;
73+ for ( it . first ( ) ; ! it . isDone ( ) ; it . next ( ) )
74+ if ( it . getItem ( ) . key === key )
75+ list . deleteNode ( it . getNode ( ) ) ;
76+ } ;
77+
78+ /**
79+ * Search the item relative to the key value.
80+ * @param key {number} The key of the item to search.
81+ * @return {*|undefined } The item found or undefined if the key does not exist.
82+ */
83+ HashTable . prototype . search = function ( key ) {
84+ var list = this . items [ this . hash ( key ) ] ;
85+ var it = list . getIterator ( ) ;
86+ for ( it . first ( ) ; ! it . isDone ( ) ; it . next ( ) )
87+ if ( it . getItem ( ) . key === key )
88+ return it . getItem ( ) . item ;
89+ return undefined ;
90+ } ;
91+
92+ /**
93+ * Search all the items relative to the key value.
94+ * @param key {number} The key of the items to search.
95+ * @return {Array.<*> } An array with the items found.
96+ */
97+ HashTable . prototype . searchAll = function ( key ) {
98+ var list = this . items [ this . hash ( key ) ] ;
99+ var it = list . getIterator ( ) ;
100+ var array = [ ] ;
101+ for ( it . first ( ) ; ! it . isDone ( ) ; it . next ( ) )
102+ if ( it . getItem ( ) . key === key )
103+ array . push ( it . getItem ( ) . item ) ;
104+ return array ;
105+ } ;
0 commit comments