1 |
1e2b2c27
|
Tomáš Šimandl
|
/**
|
2 |
|
|
* The class represents hash table.
|
3 |
|
|
*/
|
4 |
|
|
function Hash() {
|
5 |
|
|
var size = 0;
|
6 |
|
|
var list = {};
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Low-level access to the hash table.
|
10 |
|
|
*/
|
11 |
|
|
this.getAll = function() {
|
12 |
|
|
return list;
|
13 |
|
|
};
|
14 |
|
|
|
15 |
|
|
/**
|
16 |
|
|
* Add item
|
17 |
|
|
*
|
18 |
|
|
* @param key key of added item
|
19 |
|
|
* @param val value of added item
|
20 |
|
|
*/
|
21 |
|
|
this.add = function(key, val) {
|
22 |
|
|
if (!this.contains(key)) {
|
23 |
|
|
size++;
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
list[key] = val;
|
27 |
|
|
};
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* Gets item with given key
|
31 |
|
|
*
|
32 |
|
|
* @param key key of searched item
|
33 |
|
|
* @returns {*} item
|
34 |
|
|
*/
|
35 |
|
|
this.get = function(key) {
|
36 |
|
|
return list[key];
|
37 |
|
|
};
|
38 |
|
|
|
39 |
|
|
/**
|
40 |
|
|
* Remove item with given key
|
41 |
|
|
*
|
42 |
|
|
* @param key key of item
|
43 |
|
|
* @returns {*} removed item
|
44 |
|
|
*/
|
45 |
|
|
this.remove = function(key) {
|
46 |
|
|
var val = list[key];
|
47 |
|
|
delete list[key];
|
48 |
|
|
size--;
|
49 |
|
|
|
50 |
|
|
return val;
|
51 |
|
|
};
|
52 |
|
|
|
53 |
|
|
/**
|
54 |
|
|
* Gets count of item in "hash list".
|
55 |
|
|
*/
|
56 |
|
|
this.size = function() {
|
57 |
|
|
return size;
|
58 |
|
|
};
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* Tests if the item with given key is in "hash table".
|
62 |
|
|
*
|
63 |
|
|
* @param key key of item
|
64 |
|
|
* @returns {boolean} true if item with key is in "hash table"
|
65 |
|
|
*/
|
66 |
|
|
this.contains = function(key) {
|
67 |
|
|
if (list[key] == "undefined" || list[key] == null) {
|
68 |
|
|
return false;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
return true;
|
72 |
|
|
};
|
73 |
|
|
}
|