-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.h
More file actions
47 lines (43 loc) · 2.64 KB
/
Copy pathMap.h
File metadata and controls
47 lines (43 loc) · 2.64 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
#ifndef MAP_H_
#define MAP_H_
// Extend List with Map operations
#define MAP_DECL(K, ALIAS) \
typedef K _Map_##ALIAS##_K; \
List_##ALIAS* Map_##ALIAS##_find(List_##ALIAS *current_ptr,\
_Map_##ALIAS##_K key); \
_List_##ALIAS##_T* \
Map_##ALIAS##_get(List_##ALIAS *const current_ptr, \
_Map_##ALIAS##_K key); \
int Map_##ALIAS##_remove(List_##ALIAS *current_ptr, \
_Map_##ALIAS##_K key, \
_List_##ALIAS##_T *out);
#define MAP_IMPL(ALIAS, CMP) \
List_##ALIAS * \
Map_##ALIAS##_find(List_##ALIAS *current_ptr, _Map_##ALIAS##_K key){ \
struct List_##ALIAS *current = *current_ptr; \
while(current){ \
if(CMP(key, ¤t->val)) \
return current_ptr; \
current_ptr = ¤t->next; \
current = current->next; \
} \
return NULL; \
} \
_List_##ALIAS##_T* \
Map_##ALIAS##_get(List_##ALIAS *const map, _Map_##ALIAS##_K key) { \
List_##ALIAS *node = Map_##ALIAS##_find(map, key); \
if(!node) return NULL; \
return &(*node)->val; \
} \
int Map_##ALIAS##_remove(List_##ALIAS *map, \
_Map_##ALIAS##_K key, \
_List_##ALIAS##_T* out){ \
List_##ALIAS *node = Map_##ALIAS##_find(map, key); \
if(!node) return -1; \
if(out) \
*out = List_##ALIAS##_pop(node); \
else \
List_##ALIAS##_pop(node); \
return 0; \
}
#endif // MAP_H_