1 module HashMapDemo;
2 
3 import common;
4 
5 import hunt.collection.HashMap;
6 import hunt.collection.Map;
7 import hunt.collection.Iterator;
8 
9 import std.stdio;
10 import std.conv;
11 import std.range;
12 
13 
14 // http://www.java2novice.com/java-collections-and-util/hashmap/
15 class HashMapDemo
16 {
17     void testBasicOperations()
18     {
19         HashMap!(string, string) hm = new HashMap!(string, string)();
20         //add key-value pair to hashmap
21         hm.put("first", "FIRST INSERTED");
22         hm.put("second", "SECOND INSERTED");
23         hm.put("third","THIRD INSERTED");
24         writeln(hm);
25         assert(hm.size() == 3);
26         //getting value for the given key from hashmap
27         writeln("Value of second: " ~ hm.get("second"));
28         writeln("Is HashMap empty? " ~ hm.isEmpty());
29 
30         hm.remove("third");
31         writeln();
32         writeln(hm);
33         writeln("Size of the HashMap: " ~ hm.size().to!string());
34         assert(hm.size() == 2);
35 
36         writeln(hm);
37 		foreach(string key ; hm.byKey){
38 			writeln("Value of " ~ key ~ " is: " ~ hm.get(key));
39 		}
40 
41         if(hm.containsKey("first")){
42 			writeln("The hashmap contains key first");
43 		} else {
44 			writeln("The hashmap does not contains key first");
45 		}
46 		if(hm.containsKey("fifth")){
47 			writeln("The hashmap contains key fifth");
48 		} else {
49 			writeln("The hashmap does not contains key fifth");
50 		}
51 
52         writeln(hm);
53 		if(hm.containsValue("SECOND INSERTED")){
54 			writeln("The hashmap contains value SECOND INSERTED");
55 		} else {
56 			writeln("The hashmap does not contains value SECOND INSERTED");
57 		}
58 		if(hm.containsValue("first")){
59 			writeln("The hashmap contains value first");
60 		} else {
61 			writeln("The hashmap does not contains value first");
62 		}
63     }
64 
65     void testObjectKey()
66     {
67         HashMap!(Price, string) hm = new HashMap!(Price, string)();
68         hm.put(new Price("Banana", 20), "Yellow Banana");
69         hm.put(new Price("Apple", 40), "Red Apple");
70         hm.put(new Price("Orange", 30), "Juicy Orange");
71         printMap(hm);
72 
73 
74         assert(hm.size() == 3);
75         Price key = new Price("Banana", 20);
76         writeln("\nAdding duplicate key...");
77         hm.put(key, "Grape");
78         
79         assert(hm.size() == 3);
80         writeln("After adding dulicate key:");
81         printMap(hm);
82 
83         writeln("Does key available? " ~ hm.containsKey(key).to!string());
84 
85 		writeln("\nDeleting key...");
86 		hm.remove(key);
87 		writeln("After deleting key:");
88         writeln("Does key available? " ~ hm.containsKey(key).to!string());
89 		printMap(hm);
90     }
91 
92     
93     void printMap(HashMap!(Price, string) map)
94     {
95         foreach (Price p; map.byKey)
96         {
97             writeln(p.toString() ~ " ==> " ~ map.get(p));
98         }
99     }
100 
101     
102     void testHashMapForeach() {
103         // https://stackoverflow.com/questions/4234985/how-to-for-each-the-hashmap
104         // HashMap Declaration
105         writeln("Testing HashMap...");
106         HashMap!(int, string) hmap = new HashMap!(int, string)();
107 
108         //Adding elements to LinkedHashMap
109         hmap.put(22, "Abey");
110         hmap.put(33, "Dawn");
111         hmap.put(1, "Sherry");
112         hmap.put(2, "Karon");
113         hmap.put(100, "Jim");
114 
115         writeln(hmap.toString());
116 
117         writeln("\nTesting HashMap foreach1...");
118         foreach (int key, string v; hmap) {
119             writeln("Key is: " ~ key.to!string ~ " & Value is: " ~ v);
120         }
121 
122         writeln("\nTesting HashMap foreach2...");
123         foreach (MapEntry!(int, string) entry; hmap) {
124             writeln("Key is: " ~ entry.getKey().to!string ~ " & Value is: " ~ entry.getValue());
125         }
126 
127         writeln("\nTesting HashMap byKey1...");
128         // Iterator!int keyIterator  = hmap.byKey();
129         // while(keyIterator.hasNext)
130         // {
131         //         writeln("Key is: " ~ keyIterator.next().to!string());
132         // }
133 
134         InputRange!int keyIterator = hmap.byKey();
135         while (!keyIterator.empty) {
136             writeln("Key is: " ~ keyIterator.front.to!string());
137             keyIterator.popFront();
138         }
139 
140         writeln("\nTesting HashMap byKey2...");
141         foreach (int key; hmap.byKey) {
142             writeln("Key is: " ~ key.to!string());
143         }
144 
145         writeln("\nTesting HashMap byKey3...");
146         foreach (size_t index, int key; hmap.byKey) {
147             writefln("Key[%d] is: %d ", index, key);
148         }
149 
150         writeln("\nTesting HashMap byValue1...");
151         InputRange!string valueIterator = hmap.byValue();
152         while (!valueIterator.empty) {
153             writeln("value is: " ~ valueIterator.front.to!string());
154             valueIterator.popFront();
155         }
156 
157         writeln("\nTesting HashMap byValue2...");
158         foreach (string value; hmap.byValue) {
159             writeln("value is: " ~ value);
160         }
161 
162         writeln("\nTesting HashMap byValue3...");
163         foreach (size_t index, string value; hmap.byValue) {
164             writefln("value[%d] is: %s ", index, value);
165         }
166 
167     }
168 
169     void testHashMapRemove() {
170         HashMap!(int, string) hmap = new HashMap!(int, string)();
171 
172         //Adding elements to LinkedHashMap
173         hmap.put(22, "Abey");
174         hmap.put(33, "Dawn");
175         hmap.put(1, "Sherry");
176         hmap.put(2, "Karon");
177         hmap.put(100, "Jim");
178 
179         writefln("item[%d]=%s", 33, hmap.get(33));
180 
181         writeln(hmap.toString());
182 
183         assert(hmap.size() == 5);
184         hmap.remove(1);
185 
186         writeln(hmap.toString());
187         assert(hmap.size() == 4);
188 
189     }
190 
191     void testEquals1() {
192 
193         HashMap!(int, string) hmap1 = new HashMap!(int, string)();
194         hmap1.put(1, "Sherry");
195         hmap1.put(22, "Abey");
196 
197         HashMap!(int, string) hmap2 = new HashMap!(int, string)();
198         hmap2.put(22, "Abey");
199         hmap2.put(1, "Sherry");
200 
201         assert(hmap1 == hmap2);
202     }
203 
204 
205     void testEquals2() {
206 
207         HashMap!(Price, string) hm1 = new HashMap!(Price, string)();
208         hm1.put(new Price("Banana", 20), "Yellow Banana");
209         hm1.put(new Price("Apple", 40), "Red Apple");
210 
211         HashMap!(Price, string) hm2 = new HashMap!(Price, string)();
212         hm2.put(new Price("Apple", 40), "Red Apple");
213         hm2.put(new Price("Banana", 20), "Yellow Banana");
214 
215         assert(hm1 == hm2);
216     }
217 
218 }