-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.html
More file actions
151 lines (127 loc) · 3.9 KB
/
demo.html
File metadata and controls
151 lines (127 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!doctype>
<html>
<head>
<script type = "text/javascript" src = "http://difractal.com/js/jquery.js"></script>
<script type = "text/javascript">
var BiHashMap = function () {
var hashmap_keys = [];
var hashmap_values = [];
return {
Add: function (key, value) {
if (hashmap_keys[key]) {
console.warn("Add failed. Key already exists in map. Delete the key first");
return false;
}
hashmap_keys[key] = value;
if (!hashmap_values[value]) {
hashmap_values[value] = [];
}
hashmap_values[value][key] = true;
return true;
},
ContainsValue: function (value) {
if (hashmap_values[value]) {
return true;
}
return false;
},
ContainsKey: function (key) {
if (hashmap_keys[key]) {
return true;
}
return false;
},
DeleteByKey: function (key) {
var value = hashmap_keys[key];
var deleteValue = true;
delete hashmap_values[value][key];
for(var x in hashmap_values[value]){
if(x){
deleteValue = false;
break;
}
}
if(deleteValue){
delete hashmap_values[value];
}
delete hashmap_keys[key];
},
DeleteByValue: function (value) {
for (var key in hashmap_values[value]) {
delete hashmap_keys[key];
}
delete hashmap_values[value];
},
GetValueFromKey: function (key) {
return hashmap_keys[key];
},
GetKeysFromValue: function (value) {
return hashmap_values[value];
},
GetKeyToValueMap: function () {
return hashmap_keys;
},
GetValueToKeyMap: function () {
return hashmap_values;
}
//.....add the rest of your object definition
}
}
$(document).ready(function(){
var bhm = new BiHashMap();
$("#add").click(function(){
var key = $("#key").val();
var value = $("#value").val();
if(bhm.Add(key,value)) {
console.log("Added: KEY: " + key + " VALUE: " + value);
}
});
$("#delete-key").click(function(){
var key = $("#key").val();
if(bhm.ContainsKey(key)){
bhm.DeleteByKey(key);
console.log("DELETED KEY: " + key);
}
});
$("#delete-value").click(function(){
var value = $("#value").val();
if(bhm.ContainsValue(value)){
bhm.DeleteByValue(value);
console.log("DELETED VALUE: " + value);
}
});
$("#has-key").click(function(){
var key = $("#key").val();
console.log(bhm.ContainsKey(key));
});
$("#has-value").click(function(){
var value = $("#value").val();
console.log(bhm.ContainsValue(value));
});
$("#get-map").click(function(){
console.log({"key-to-value-map" : bhm.GetKeyToValueMap() , "value-to-key-map" : bhm.GetValueToKeyMap()});
});
});
</script>
</head>
<body>
<div id = 'funzone-container'>
<div id = 'input-fields' style = 'margin-top:50px;'>
<div>
<label>Key</label>
<input type = "text" id = "key" value = "200"></input>
</div>
<div>
<label>Value</label>
<input type = "text" id = "value" value = "200"></input>
</div>
<button id = "add">Add Key-Value Pair</button>
<button id = "delete-key">Delete Key</button>
<button id = "delete-value">Delete Value</button>
<button id = "has-key">Has Key?</button>
<button id = "has-value">Has Value?</button>
<button id = "get-map">Get Map</button>
</div>
</div>
</body>
</html>