src/kv_list.c

changeset 1352
8428516137dd
parent 1350
189756516eaa
child 1353
5a13b9c1c57b
equal deleted inserted replaced
1351:4407229bd944 1352:8428516137dd
151 static void cx_kvl_sort(struct cx_list_s *list) { 151 static void cx_kvl_sort(struct cx_list_s *list) {
152 cx_kv_list *kv_list = (cx_kv_list*)list; 152 cx_kv_list *kv_list = (cx_kv_list*)list;
153 kv_list->list_methods->sort(list); 153 kv_list->list_methods->sort(list);
154 } 154 }
155 155
156 static int cx_kvl_compare(
157 const struct cx_list_s *list,
158 const struct cx_list_s *other
159 ) {
160 // TODO: make it so that comparison with normal linked lists is also optimized
161 const cx_kv_list *kv_list = (const cx_kv_list*)list;
162 return kv_list->list_methods->compare(list, other);
163 }
164
165 static void cx_kvl_reverse(struct cx_list_s *list) { 156 static void cx_kvl_reverse(struct cx_list_s *list) {
166 cx_kv_list *kv_list = (cx_kv_list*)list; 157 cx_kv_list *kv_list = (cx_kv_list*)list;
167 kv_list->list_methods->reverse(list); 158 kv_list->list_methods->reverse(list);
168 } 159 }
169 160
187 cx_kvl_clear, 178 cx_kvl_clear,
188 cx_kvl_swap, 179 cx_kvl_swap,
189 cx_kvl_at, 180 cx_kvl_at,
190 cx_kvl_find_remove, 181 cx_kvl_find_remove,
191 cx_kvl_sort, 182 cx_kvl_sort,
192 cx_kvl_compare, 183 NULL,
193 cx_kvl_reverse, 184 cx_kvl_reverse,
194 cx_kvl_iterator, 185 cx_kvl_iterator,
195 }; 186 };
196 187
197 CxList *cxKvListCreate( 188 CxList *cxKvListCreate(
209 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); 200 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
210 if (map == NULL) { // LCOV_EXCL_START 201 if (map == NULL) { // LCOV_EXCL_START
211 cxListFree(list); 202 cxListFree(list);
212 return NULL; 203 return NULL;
213 } // LCOV_EXCL_STOP 204 } // LCOV_EXCL_STOP
205
206 // patch the kv-list class with the compare function of the linked list
207 // this allows cxListCompare() to optimize comparisons between linked lists and kv-list
208 cx_kv_list_class.compare = list->cl->compare;
214 209
215 // reallocate the map to add memory for the list back-reference 210 // reallocate the map to add memory for the list back-reference
216 struct cx_kv_list_map_s *kv_map = cxRealloc(allocator, map, sizeof(struct cx_kv_list_map_s)); 211 struct cx_kv_list_map_s *kv_map = cxRealloc(allocator, map, sizeof(struct cx_kv_list_map_s));
217 212
218 // reallocate the list to add memory for storing the metadata 213 // reallocate the list to add memory for storing the metadata

mercurial