NVMLib  very early alpha
A library to optimally use a Hybrid RAM setup.
hashmap_debug_example.c
Go to the documentation of this file.
1 
94 #include "../hashmap.h"
95 
96 #include <stdio.h>
97 #include <string.h>
98 
99 typedef struct a_st {
100  int b;
101  char a[10];
102 } att;
103 
104 
105 int compare(att *a, att *b) {
106  if (a->b == b->b){
107  return 0;
108  }
109  return 1;
110 }
111 
113  printf("%s", entry->a);
114 }
115 
117  return entry->b;
118 }
119 
121 DEFINE_HASHMAP(att, compare, get_hash, free, realloc)
122 
124 
125 int main() {
126 
127  HASH_MAP(att) *map = HASH_MAP_CREATE(att)();
128 
129  att *x = (att *)malloc(sizeof(att));
130  x->b = 1;
131  strcpy(x->a, "ONE");
132 
133  att *y = (att *)malloc(sizeof(att));
134  y->b = 3;
135  strcpy(y->a, "TWO");
136 
137  att *z = (att *)malloc(sizeof(att));
138  z->b = 8;
139  strcpy(z->a, "THREE");
140 
141 
142  att *a = (att *)malloc(sizeof(att));
143  a->b = 5;
144  strcpy(a->a, "FOUR");
145 
146 
147  att *b = (att *)malloc(sizeof(att));
148  b->b = 10;
149  strcpy(b->a, "FIVE");
150 
151 
152  att *c = (att *)malloc(sizeof(att));
153  c->b = 7;
154  strcpy(c->a, "SIX");
155 
156 
157  att *d = (att *)malloc(sizeof(att));
158  d->b = 9;
159  strcpy(d->a, "SEVEN");
160 
161  att *e = (att *)malloc(sizeof(att));
162  e->b = 17;
163  strcpy(e->a, "EIGTH");
164 
165  HASH_MAP_INSERT(att)(map, &x, HMDR_FIND);
166  printf("Inserting : ");
167  print_obj(x);
168  printf("\n");
169  HASH_MAP_PRINT(att)(map);
170 
171  HASH_MAP_INSERT(att)(map, &y, HMDR_FIND);
172  printf("Inserting : ");
173  print_obj(y);
174  printf("\n");
175  HASH_MAP_PRINT(att)(map);
176 
177  HASH_MAP_INSERT(att)(map, &z, HMDR_FIND);
178  printf("Inserting : ");
179  print_obj(z);
180  printf("\n");
181  HASH_MAP_PRINT(att)(map);
182 
183  HASH_MAP_INSERT(att)(map, &a, HMDR_FIND);
184  printf("Inserting : ");
185  print_obj(a);
186  printf("\n");
187  HASH_MAP_PRINT(att)(map);
188 
189  HASH_MAP_INSERT(att)(map, &b, HMDR_FIND);
190  printf("Inserting : ");
191  print_obj(b);
192  printf("\n");
193  HASH_MAP_PRINT(att)(map);
194 
195  HASH_MAP_INSERT(att)(map, &c, HMDR_FIND);
196  printf("Inserting : ");
197  print_obj(c);
198  printf("\n");
199  HASH_MAP_PRINT(att)(map);
200 
201  HASH_MAP_INSERT(att)(map, &d, HMDR_FIND);
202  printf("Inserting : ");
203  print_obj(d);
204  printf("\n");
205  HASH_MAP_PRINT(att)(map);
206 
207  HASH_MAP_INSERT(att)(map, &e, HMDR_FIND);
208  printf("Inserting : ");
209  print_obj(e);
210  printf("\n");
211  HASH_MAP_PRINT(att)(map);
212 
213  att *x1 = (att *)malloc(sizeof(att));
214  x1->b = 3;
215  strcpy(x1->a, "TWO");
216 
217  if(HASH_MAP_FIND(att)(map, &x1)){
218  printf("Finding: FOUND %s\n", x1->a);
219  }
220 
221  printf("\nMAP SIZE : Before deletion -- %ld\n", map->size);
222 
223  att *x2 = (att *)malloc(sizeof(att));
224  x2->b = 3;
225  strcpy(x2->a, "TWO");
226 
227  if(HASH_MAP_ERASE(att)(map, x2)){
228  if(HASH_MAP_FIND(att)(map, &x2)){
229  printf("Finding %s\n", x2->a);
230  } else {
231  printf("Erased 1\n");
232  }
233  }
234 
235  x = (att *)malloc(sizeof(att));
236  x->b = 3;
237  strcpy(x->a, "TWO");
238 
239  if(HASH_MAP_ERASE(att)(map, x)){
240  if(HASH_MAP_FIND(att)(map, &x)){
241  printf("Finding %s\n", x->a);
242  } else {
243  printf("Erased 1\n");
244  }
245  } else {
246  printf("Erased 2\n");
247 
248  }
249 
250  printf("MAP SIZE : After deletion -- %ld\n", map->size);
251 
252  HASH_MAP_PRINT(att)(map);
253 
254  y = (att *)malloc(sizeof(att));
255  y->b = 3;
256  strcpy(y->a, "TWO");
257  HASH_MAP_INSERT(att)(map, &y, HMDR_FIND);
258 
259  printf("MAP SIZE : After reinsertion -- %ld\n", map->size);
260 
261  HASH_MAP_PRINT(att)(map);
262 
263 
264  HASH_MAP_DESTROY(att)(map);
265 
266  return 0;
267 }
HASH_MAP_DESTROY
#define HASH_MAP_DESTROY(VALUE_TYPE)
Definition: hashmap.h:36
a_st::a
char a[10]
Definition: hashmap_debug_example.c:101
DECLARE_HASHMAP
DECLARE_HASHMAP(pool_free_slot_val)
HMDR_FIND
@ HMDR_FIND
Definition: hashmap.h:40
print_obj
void print_obj(att *entry)
Definition: hashmap_debug_example.c:112
compare
int compare(att *a, att *b)
Definition: hashmap_debug_example.c:105
att
struct a_st att
File contains a example test program for the generic Hash map implemented.
HASH_MAP_ERASE
#define HASH_MAP_ERASE(VALUE_TYPE)
Definition: hashmap.h:35
main
int main()
Definition: hashmap_debug_example.c:125
a_st::b
int b
Definition: hashmap_debug_example.c:100
get_hash
int get_hash(att *entry)
Definition: hashmap_debug_example.c:116
HASH_MAP_INSERT
#define HASH_MAP_INSERT(VALUE_TYPE)
Definition: hashmap.h:32
HASH_MAP
#define HASH_MAP(VALUE_TYPE)
Definition: hashmap.h:29
HASH_MAP_PRINT_FUNC
#define HASH_MAP_PRINT_FUNC(VALUE_TYPE, PRINT_OBJ_FUNC)
Definition: hashmap.h:295
entry
Definition: hashmap_tx.h:33
a_st
File contains a example test program for the generic Hash map implemented.
Definition: hashmap_debug_example.c:99
HASH_MAP_PRINT
#define HASH_MAP_PRINT(VALUE_TYPE)
For Debug purposes.
Definition: hashmap.h:293
HASH_MAP_CREATE
#define HASH_MAP_CREATE(VALUE_TYPE)
Definition: hashmap.h:34
DEFINE_HASHMAP
#define DEFINE_HASHMAP(VALUE_TYPE, CMP, GET_HASH, FREE, REALLOC)
Definition of the declared HashMap.
Definition: hashmap.h:126
HASH_MAP_FIND
#define HASH_MAP_FIND(VALUE_TYPE)
Definition: hashmap.h:33