src/kv_list.c

Tue, 26 Aug 2025 21:55:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 26 Aug 2025 21:55:19 +0200
changeset 1350
189756516eaa
parent 1348
a1da355ed3b8
permissions
-rw-r--r--

implement kv-list to a point where it correctly behaves like a list

that means no lookup-map aspects are implemented just yet

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 int cx_kvl_compare(
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 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
158 const struct cx_list_s *other
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 // TODO: make it so that comparison with normal linked lists is also optimized
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 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
162 return kv_list->list_methods->compare(list, other);
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 }
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
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 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
166 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
167 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
168 }
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 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
171 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
172 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
173 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
174 ) {
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
175 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
176 // 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
177 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
178 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
179
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
180 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
181 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
182 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
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190 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
191 cx_kvl_sort,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
192 cx_kvl_compare,
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
193 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
194 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
195 };
1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 CxList *cxKvListCreate(
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 const CxAllocator *allocator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 cx_compare_func comparator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 size_t elem_size
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 ) {
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
202 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
203 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
204 }
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
205
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
206 // 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
207 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
208 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
209 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
210 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
211 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
212 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
213 } // 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
214
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 // 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
216 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
217
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 // reallocate the list to add memory for storing the metadata
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_kv_list *kv_list = cxRealloc(allocator, list, sizeof(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
220
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 // 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
222 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
223 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
224 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
225 } 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
226 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
227 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
228 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
229 } // 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
230
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
231 // 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
232 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
233 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
234
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
235 // 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
236 kv_list->list_methods = list->cl;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
237 kv_list->map_methods = map->cl;
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
238 list->cl = &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
239 // TODO: override all map members
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
240 // and remember to set the sorted flag of the list to false in the put/emplace 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
241
189756516eaa implement kv-list to a point where it correctly behaves like a list
Mike Becker <universe@uap-core.de>
parents: 1348
diff changeset
242 return list;
1348
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 CxMap *cxKvListCreateAsMap(
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 const CxAllocator *allocator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 cx_compare_func comparator,
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 size_t elem_size
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 ) {
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 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
251 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
252 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 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
255 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
256 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 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
259 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
260 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 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
263 return -1;
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 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
267 return -1;
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 }
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 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
271 return -1;
a1da355ed3b8 roll out the function stubs for the kv-list
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 }

mercurial