src/kv_list.c

Mon, 01 Sep 2025 20:20:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 01 Sep 2025 20:20:12 +0200
changeset 1358
dda9c330e3e5
parent 1353
5a13b9c1c57b
permissions
-rw-r--r--

make test_kv_list_map_put() pass

relates to #461

1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2025 Mike Becker, Olaf Wintermann All rights reserved.
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/kv_list.h"
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
30 #include "cx/hash_map.h"
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
31 #include "cx/linked_list.h"
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
32
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
33 typedef struct cx_kv_list_s cx_kv_list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
34
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
35 struct cx_kv_list_map_s {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
36 struct cx_hash_map_s map_base;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
37 /** Back-reference to the list. */
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
38 cx_kv_list *list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
39 };
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
40
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
41 /** The list aspect (must have the same layout as the normal linked list). */
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
42 struct cx_kv_list_list_s {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
43 struct cx_list_s list_base;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
44 void *begin;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
45 void *end;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
46 };
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
47
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
48 struct cx_kv_list_s {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
49 struct cx_kv_list_list_s list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
50 /** The lookup map - stores pointers to the nodes. */
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
51 struct cx_kv_list_map_s *map;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
52 const cx_list_class *list_methods;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
53 const cx_map_class *map_methods;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
54 };
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
55
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
56 static void cx_kvl_deallocate(struct cx_list_s *list) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
57 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
58 // free the map first
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
59 kv_list->map_methods->deallocate(&kv_list->map->map_base.base);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
60 kv_list->list_methods->deallocate(list);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
61 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
62
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
63 static void *cx_kvl_insert_element(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
64 struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
65 size_t index,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
66 const void *data
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
67 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
68 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
69 // TODO: trick the base method by adding the required space for the key to the elem_size
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
70 return kv_list->list_methods->insert_element(list, index, data);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
71 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
72
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
73 static size_t cx_kvl_insert_array(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
74 struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
75 size_t index,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
76 const void *data,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
77 size_t n
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
78 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
79 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
80 // TODO: trick the base method by adding the required space for the key to the elem_size
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
81 return kv_list->list_methods->insert_array(list, index, data, n);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
82 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
83
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
84 static size_t cx_kvl_insert_sorted(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
85 struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
86 const void *sorted_data,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
87 size_t n
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
88 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
89 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
90 // TODO: trick the base method by adding the required space for the key to the elem_size
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
91 return kv_list->list_methods->insert_sorted(list, sorted_data, n);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
92 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
93
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
94 static int cx_kvl_insert_iter(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
95 struct cx_iterator_s *iter,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
96 const void *elem,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
97 int prepend
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
98 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
99 cx_kv_list *kv_list = (cx_kv_list*)iter->src_handle.m;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
100 // TODO: trick the base method by adding the required space for the key to the elem_size
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
101 return kv_list->list_methods->insert_iter(iter, elem, prepend);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
102 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
103
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
104 static size_t cx_kvl_remove(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
105 struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
106 size_t index,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
107 size_t num,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
108 void *targetbuf
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
109 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
110 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
111 // TODO: always use the target buffer to get the element first,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
112 // then obtain the key, remove it from the map,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
113 // and finally call any destructors manually
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
114 return kv_list->list_methods->remove(list, index, num, targetbuf);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
115 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
116
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
117 static void cx_kvl_clear(struct cx_list_s *list) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
118 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
119 kv_list->list_methods->clear(list);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
120 // also clear all lookup entries
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
121 cxMapClear(&kv_list->map->map_base.base);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
122 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
123
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
124 static int cx_kvl_swap(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
125 struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
126 size_t i,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
127 size_t j
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
128 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
129 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
130 return kv_list->list_methods->swap(list, i, j);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
131 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
132
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
133 static void *cx_kvl_at(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
134 const struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
135 size_t index
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
136 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
137 const cx_kv_list *kv_list = (const cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
138 return kv_list->list_methods->at(list, index);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
139 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
140
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
141 static size_t cx_kvl_find_remove(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
142 struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
143 const void *elem,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
144 bool remove
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
145 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
146 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
147 // TODO: implement removal of the key in the map
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
148 return kv_list->list_methods->find_remove(list, elem, remove);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
149 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
150
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
151 static void cx_kvl_sort(struct cx_list_s *list) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
152 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
153 kv_list->list_methods->sort(list);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
154 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
155
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
156 static void cx_kvl_reverse(struct cx_list_s *list) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
157 cx_kv_list *kv_list = (cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
158 kv_list->list_methods->reverse(list);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
159 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
160
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
161 static struct cx_iterator_s cx_kvl_iterator(
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
162 const struct cx_list_s *list,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
163 size_t index,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
164 bool backward
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
165 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
166 const cx_kv_list *kv_list = (const cx_kv_list*)list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
167 // TODO: cannot really forward, because mutating iterators must be able to remove the element
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
168 return kv_list->list_methods->iterator(list, index, backward);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
169 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
170
1358
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
171 static void cx_kvl_map_deallocate(struct cx_map_s *map) {
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
172 cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list;
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
173 kv_list->map_methods->deallocate(map);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
174 kv_list->list_methods->deallocate(&kv_list->list.list_base);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
175 }
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
176
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
177 static void cx_kvl_map_clear(struct cx_map_s *map) {
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
178 cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list;
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
179 // TODO: iterate through the map elements and remove the key from the referenced list items
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
180 kv_list->map_methods->clear(map);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
181 }
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
182
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
183 static void *cx_kvl_map_put(CxMap *map, CxHashKey key, void *value) {
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
184 cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list;
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
185 // insert the data into the list first (assume that insertion destroys the sorted property)
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
186 kv_list->list.list_base.collection.sorted = false;
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
187 // TODO: use the same trick as above to increase the element size temporarily to add the key to the data
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
188 void *node_data = kv_list->list_methods->insert_element(
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
189 &kv_list->list.list_base, kv_list->list.list_base.collection.size, value);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
190 if (node_data == NULL) return NULL; // LCOV_EXCL_LINE
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
191 // then insert the key into the map, referring to the node data
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
192 // TODO: check if we still get a correct pointer when the list is storing pointers
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
193 return kv_list->map_methods->put(map, key, node_data);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
194 }
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
195
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
196 void *cx_kvl_map_get(const CxMap *map, CxHashKey key) {
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
197 cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list;
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
198 return kv_list->map_methods->get(map, key);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
199 }
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
200
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
201 int cx_kvl_map_remove(CxMap *map, CxHashKey key, void *targetbuf) {
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
202 cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list;
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
203 // TODO: also remove the item from the list
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
204 return kv_list->map_methods->remove(map, key, targetbuf);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
205 }
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
206
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
207 CxMapIterator cx_kvl_map_iterator(const CxMap *map, enum cx_map_iterator_type type) {
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
208 cx_kv_list *kv_list = ((struct cx_kv_list_map_s*)map)->list;
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
209 return kv_list->map_methods->iterator(map, type);
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
210 }
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
211
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
212 static cx_list_class cx_kv_list_class = {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
213 cx_kvl_deallocate,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
214 cx_kvl_insert_element,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
215 cx_kvl_insert_array,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
216 cx_kvl_insert_sorted,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
217 cx_kvl_insert_iter,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
218 cx_kvl_remove,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
219 cx_kvl_clear,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
220 cx_kvl_swap,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
221 cx_kvl_at,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
222 cx_kvl_find_remove,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
223 cx_kvl_sort,
1352
8428516137dd make comparisons between kv_list and linked_list optimizable
Mike Becker <universe@uap-core.de>
parents: 1350
diff changeset
224 NULL,
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
225 cx_kvl_reverse,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
226 cx_kvl_iterator,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
227 };
1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228
1358
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
229 static cx_map_class cx_kv_map_class = {
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
230 cx_kvl_map_deallocate,
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
231 cx_kvl_map_clear,
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
232 cx_kvl_map_put,
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
233 cx_kvl_map_get,
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
234 cx_kvl_map_remove,
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
235 cx_kvl_map_iterator,
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
236 };
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
237
1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 CxList *cxKvListCreate(
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 const CxAllocator *allocator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 cx_compare_func comparator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 size_t elem_size
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 ) {
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
243 if (allocator == NULL) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
244 allocator = cxDefaultAllocator;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
245 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
246
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
247 // create a normal linked list and a normal hash map, first
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
248 CxList *list = cxLinkedListCreate(allocator, comparator, elem_size);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
249 if (list == NULL) return NULL; // LCOV_EXCL_LINE
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
250 CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
251 if (map == NULL) { // LCOV_EXCL_START
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
252 cxListFree(list);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
253 return NULL;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
254 } // LCOV_EXCL_STOP
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
255
1352
8428516137dd make comparisons between kv_list and linked_list optimizable
Mike Becker <universe@uap-core.de>
parents: 1350
diff changeset
256 // patch the kv-list class with the compare function of the linked list
8428516137dd make comparisons between kv_list and linked_list optimizable
Mike Becker <universe@uap-core.de>
parents: 1350
diff changeset
257 // this allows cxListCompare() to optimize comparisons between linked lists and kv-list
8428516137dd make comparisons between kv_list and linked_list optimizable
Mike Becker <universe@uap-core.de>
parents: 1350
diff changeset
258 cx_kv_list_class.compare = list->cl->compare;
8428516137dd make comparisons between kv_list and linked_list optimizable
Mike Becker <universe@uap-core.de>
parents: 1350
diff changeset
259
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
260 // reallocate the map to add memory for the list back-reference
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
261 struct cx_kv_list_map_s *kv_map = cxRealloc(allocator, map, sizeof(struct cx_kv_list_map_s));
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
262
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
263 // reallocate the list to add memory for storing the metadata
1358
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
264 cx_kv_list *kv_list = cxRealloc(allocator, list, sizeof(struct cx_kv_list_s));
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
265
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
266 // if any of the reallocations failed, we bail out
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
267 if (kv_map != NULL && kv_list != NULL) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
268 map = (CxMap*) kv_map;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
269 list = (CxList*) kv_list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
270 } else { // LCOV_EXCL_START
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
271 cxListFree(list);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
272 cxMapFree(map);
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
273 return NULL;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
274 } // LCOV_EXCL_STOP
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
275
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
276 // combine the list and the map aspect
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
277 kv_list->map = kv_map;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
278 kv_map->list = kv_list;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
279
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
280 // remember the base methods and override them
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
281 kv_list->map_methods = map->cl;
1358
dda9c330e3e5 make test_kv_list_map_put() pass
Mike Becker <universe@uap-core.de>
parents: 1353
diff changeset
282 map->cl = &cx_kv_map_class;
1353
5a13b9c1c57b fix that the wrong vtable is patched when CX_STORE_POINTERS is used
Mike Becker <universe@uap-core.de>
parents: 1352
diff changeset
283 if (list->climpl == NULL) {
5a13b9c1c57b fix that the wrong vtable is patched when CX_STORE_POINTERS is used
Mike Becker <universe@uap-core.de>
parents: 1352
diff changeset
284 kv_list->list_methods = list->cl;
5a13b9c1c57b fix that the wrong vtable is patched when CX_STORE_POINTERS is used
Mike Becker <universe@uap-core.de>
parents: 1352
diff changeset
285 list->cl = &cx_kv_list_class;
5a13b9c1c57b fix that the wrong vtable is patched when CX_STORE_POINTERS is used
Mike Becker <universe@uap-core.de>
parents: 1352
diff changeset
286 } else {
5a13b9c1c57b fix that the wrong vtable is patched when CX_STORE_POINTERS is used
Mike Becker <universe@uap-core.de>
parents: 1352
diff changeset
287 kv_list->list_methods = list->climpl;
5a13b9c1c57b fix that the wrong vtable is patched when CX_STORE_POINTERS is used
Mike Becker <universe@uap-core.de>
parents: 1352
diff changeset
288 list->climpl = &cx_kv_list_class;
5a13b9c1c57b fix that the wrong vtable is patched when CX_STORE_POINTERS is used
Mike Becker <universe@uap-core.de>
parents: 1352
diff changeset
289 }
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
290
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
291 return list;
1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 CxMap *cxKvListCreateAsMap(
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 const CxAllocator *allocator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 cx_compare_func comparator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 size_t elem_size
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 ) {
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299 CxList *list = cxKvListCreate(allocator, comparator, elem_size);
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 return list == NULL ? NULL : cxKvListAsMap(list);
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303 CxList *cxKvListAsList(CxMap *map) {
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
304 return &((struct cx_kv_list_map_s*)map)->list->list.list_base;
1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
305 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
306
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 CxMap *cxKvListAsMap(CxList *list) {
1350
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
308 return &((cx_kv_list*)list)->map->map_base.base;
1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
311 int cx_kv_list_set_key(CxList *list, size_t index, CxHashKey key) {
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312 return -1;
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
314
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
315 int cx_kv_list_remove_key(CxList *list, size_t index, CxHashKey key) {
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
316 return -1;
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
317 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
318
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
319 int cx_kv_list_insert(CxList *list, size_t index, CxHashKey key, void *value) {
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320 return -1;
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
321 }

mercurial