add concurrent hashmap
[model-checker-benchmarks.git] / concurrent-hashmap / testcase3.cc
1 #include <threads.h>
2
3 #ifdef WILDCARD
4 #include "hashmap_wildcard.h"
5 #else
6 #include "hashmap.h"
7 #endif
8
9 HashMap *table;
10
11 void printKey(Key *key) {
12         if (key)
13                 printf("pos = (%d, %d, %d)\n", key->x, key->y, key->z);
14         else
15                 printf("pos = NULL\n");
16 }
17
18 void printValue(Value *value) {
19         if (value)
20                 printf("velocity = (%d, %d, %d)\n", value->vX, value->vY, value->vZ);
21         else
22                 printf("velocity = NULL\n");
23 }
24
25 // Key(3, 2, 6) & Key(1, 3, 3) are hashed to the same slot -> 4
26 // Key(1, 1, 1) & Key(3, 2, 2) are hashed to the same slot -> 0
27 // Key(2, 4, 1) & Key(3, 4, 2) are hashed to the same slot -> 3
28 // Key(3, 4, 5) & Key(1, 4, 3) & Key(1, 1, 6) are hashed to the same slot -> 5
29 // Key(2, 4, 8) & Key(1, 3, 8) -> 9
30 // Key(1, 4, 8) -> 10
31 // Key(1, 3, 7) -> 8
32 // Key(1, 2, 7) -> 7
33 // Key(1, 2, 6) -> 6
34
35 void threadA(void *arg) {
36         Key *k1 = new Key(3, 4, 5);
37         Key *k2 = new Key(1, 4, 3);
38         Key *k3 = new Key(1, 1, 6);
39         Value *v2 = new Value(10, 10, 10);
40         Value *r1 = table->put(k2, v2);
41         //printValue(r1);
42         Value *r2 = table->get(k3);
43         printf("k1 -> %d:\n", table->hashKey(k1) % table->capacity);
44         printf("k2 -> %d:\n", table->hashKey(k2) % table->capacity);
45         printf("k3 -> %d:\n", table->hashKey(k3) % table->capacity);
46         //printValue(r2);
47 }
48
49 void threadB(void *arg) {
50         Key *k1 = new Key(3, 4, 5);
51         Key *k2 = new Key(1, 4, 3);
52         Key *k3 = new Key(1, 1, 6);
53         Value *v3 = new Value(30, 40, 50);
54         Value *r3 = table->put(k3, v3);
55         //printValue(r3);
56         //Value *r4 = table->get(k1);
57         //printf("Thrd B:\n");
58         //printValue(r4);
59 }
60
61 int user_main(int argc, char *argv[]) {
62         Key *k1 = new Key(3, 4, 5);
63         Key *k2 = new Key(1, 4, 3);
64         Key *k3 = new Key(1, 1, 6);
65         //Key *k2 = new Key(1, 3, 3);
66         Value *v1 = new Value(111, 111, 111);
67         //Value *v2 = new Value(222, 222, 222);
68         thrd_t t1, t2;
69         table = new HashMap;
70         table->put(k1, v1);
71         //table->put(k2, v2);
72
73         thrd_create(&t1, threadA, NULL);
74         thrd_create(&t2, threadB, NULL);
75         thrd_join(t1);
76         thrd_join(t2);
77         
78         return 0;
79 }
80
81