1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #define SG_BUILD_LIBRARY |
16 | #include <siege/util/map.h> |
17 | |
18 | #include <stdlib.h> |
19 | #include <string.h> |
20 | |
21 | static SGint _sgMapSetCmp(const SGMapNode* a, const SGMapNode* b, void* data) |
22 | { |
23 | SGMap* map = data; |
24 | return map->cmp(a->key, b->key, map->data); |
25 | } |
26 | |
27 | SGMap* SG_CALL__cdecl sgMapInit(SGMap* map, SGMapCmp* cmp, void* data) |
28 | { |
29 | if(!map) return NULL((void*)0); |
30 | |
31 | if(!sgSetInit(&map->set, (SGSetCmp*)_sgMapSetCmp, map)) |
32 | return NULL((void*)0); |
33 | map->cmp = cmp; |
34 | map->data = data; |
35 | return map; |
36 | } |
37 | void SG_CALL__cdecl sgMapDeinit(SGMap* map) |
38 | { |
39 | if(!map) return; |
40 | |
41 | sgSetDeinit(&map->set); |
42 | } |
43 | |
44 | SGMap* SG_CALL__cdecl sgMapCreate(SGMapCmp* cmp, void* data) |
45 | { |
46 | return sgMapInit(malloc(sizeof(SGMap)), cmp, data); |
47 | } |
48 | void SG_CALL__cdecl sgMapDestroy(SGMap* map) |
49 | { |
50 | sgMapDeinit(map); |
51 | free(map); |
52 | } |
53 | |
54 | void* SG_CALL__cdecl sgMapReplace(SGMap* map, void* key, void* val) |
55 | { |
56 | SGMapNode search; |
57 | search.key = key; |
58 | |
59 | void* old; |
60 | |
61 | SGMapNode* mnode; |
62 | SGSetNode* snode = sgSetSearch(&map->set, &search); |
63 | if(snode) |
64 | { |
65 | mnode = snode->item; |
66 | old = mnode->val; |
67 | |
68 | mnode->key = key; |
69 | mnode->val = val; |
70 | |
71 | return old; |
72 | } |
73 | |
74 | mnode = malloc(sizeof(mnode)); |
75 | if(!mnode) return NULL((void*)0); |
76 | |
77 | mnode->key = key; |
78 | mnode->val = val; |
79 | |
80 | sgSetInsert(&map->set, mnode); |
81 | return NULL((void*)0); |
82 | } |
83 | void* SG_CALL__cdecl sgMapAssign(SGMap* map, void* key, void* val) |
84 | { |
85 | sgMapReplace(map, key, val); |
86 | return val; |
87 | } |
88 | void* SG_CALL__cdecl sgMapFind(SGMap* map, const void* key) |
89 | { |
90 | SGMapNode* node = sgMapFindNode(map, key); |
91 | if(!node) |
92 | return NULL((void*)0); |
93 | return node->val; |
94 | } |
95 | void* SG_CALL__cdecl sgMapRemove(SGMap* map, const void* key) |
96 | { |
97 | SGMapNode search; |
98 | search.key = (void*)key; |
99 | |
100 | SGMapNode* mnode; |
101 | SGSetNode* snode = sgSetSearch(&map->set, &search); |
102 | if(!snode) |
103 | return NULL((void*)0); |
104 | |
105 | mnode = snode->item; |
106 | sgSetRemoveNode(&map->set, snode); |
107 | |
108 | void* val = mnode->val; |
109 | free(mnode); |
110 | return val; |
111 | } |
112 | |
113 | SGMapNode* SG_CALL__cdecl sgMapAssignNode(SGMap* map, void* key, void* val) |
114 | { |
115 | SGMapNode search; |
116 | search.key = key; |
117 | |
118 | SGMapNode* mnode; |
119 | SGSetNode* snode = sgSetSearch(&map->set, &search); |
120 | if(snode) |
121 | { |
122 | mnode = snode->item; |
123 | |
124 | mnode->key = key; |
125 | mnode->val = val; |
126 | |
127 | return mnode; |
128 | } |
129 | |
130 | mnode = malloc(sizeof(mnode)); |
| Result of 'malloc' is converted to a pointer of type 'SGMapNode', which is incompatible with sizeof operand type 'SGMapNode *' |
131 | if(!mnode) return NULL((void*)0); |
132 | |
133 | mnode->key = key; |
134 | mnode->val = val; |
135 | |
136 | sgSetInsert(&map->set, mnode); |
137 | return mnode; |
138 | } |
139 | SGMapNode* SG_CALL__cdecl sgMapFindNode(SGMap* map, const void* key) |
140 | { |
141 | SGMapNode search; |
142 | search.key = (void*)key; |
143 | |
144 | SGSetNode* snode = sgSetSearch(&map->set, &search); |
145 | if(snode) |
146 | return snode->item; |
147 | return NULL((void*)0); |
148 | } |
149 | |
150 | SGMapNode* SG_CALL__cdecl sgMapGetRoot(SGMap* map) |
151 | { |
152 | SGSetNode* snode = sgSetGetRoot(&map->set); |
153 | if(!snode) |
154 | return NULL((void*)0); |
155 | return snode->item; |
156 | } |
157 | SGMapNode* SG_CALL__cdecl sgMapGetFirst(SGMap* map) |
158 | { |
159 | SGSetNode* snode = sgSetGetFirst(&map->set); |
160 | if(!snode) |
161 | return NULL((void*)0); |
162 | return snode->item; |
163 | } |
164 | SGMapNode* SG_CALL__cdecl sgMapGetLast(SGMap* map) |
165 | { |
166 | SGSetNode* snode = sgSetGetLast(&map->set); |
167 | if(!snode) |
168 | return NULL((void*)0); |
169 | return snode->item; |
170 | } |
171 | |
172 | void* SG_CALL__cdecl sgMapPopRoot(SGMap* map) |
173 | { |
174 | SGMapNode* mnode = sgSetPopRoot(&map->set); |
175 | if(!mnode) |
176 | return NULL((void*)0); |
177 | return mnode->val; |
178 | } |
179 | void* SG_CALL__cdecl sgMapPopFirst(SGMap* map) |
180 | { |
181 | SGMapNode* mnode = sgSetPopFirst(&map->set); |
182 | if(!mnode) |
183 | return NULL((void*)0); |
184 | return mnode->val; |
185 | } |
186 | void* SG_CALL__cdecl sgMapPopLast(SGMap* map) |
187 | { |
188 | SGMapNode* mnode = sgSetPopLast(&map->set); |
189 | if(!mnode) |
190 | return NULL((void*)0); |
191 | return mnode->val; |
192 | } |