ucx/array_list.c

Wed, 10 Dec 2025 18:36:02 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 10 Dec 2025 18:36:02 +0100
changeset 28
e46f9f254fcd
parent 23
b26390e77237
permissions
-rw-r--r--

implement json child object serialization

0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "cx/array_list.h"
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include "cx/compare.h"
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <assert.h>
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <string.h>
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
33 #include <errno.h>
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 // Default array reallocator
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 static void *cx_array_default_realloc(
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 void *array,
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
39 cx_attr_unused size_t old_capacity,
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
40 size_t new_capacity,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 size_t elem_size,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
42 cx_attr_unused CxArrayReallocator *alloc
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 ) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
44 size_t n;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
45 // LCOV_EXCL_START
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
46 if (cx_szmul(new_capacity, elem_size, &n)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
47 errno = EOVERFLOW;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
48 return NULL;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
49 } // LCOV_EXCL_STOP
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
50 return cxReallocDefault(array, n);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
53 CxArrayReallocator cx_array_default_reallocator_impl = {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
54 cx_array_default_realloc, NULL, NULL
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 };
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
57 CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
58
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
59 // Stack-aware array reallocator
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
60
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
61 static void *cx_array_advanced_realloc(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
62 void *array,
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
63 size_t old_capacity,
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
64 size_t new_capacity,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
65 size_t elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
66 cx_attr_unused CxArrayReallocator *alloc
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
67 ) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
68 // check for overflow
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
69 size_t n;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
70 // LCOV_EXCL_START
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
71 if (cx_szmul(new_capacity, elem_size, &n)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
72 errno = EOVERFLOW;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
73 return NULL;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
74 } // LCOV_EXCL_STOP
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
75
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
76 // retrieve the pointer to the actual allocator
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
77 const CxAllocator *al = alloc->allocator;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
78
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
79 // check if the array is still located on the stack
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
80 void *newmem;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
81 if (array == alloc->stack_ptr) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
82 newmem = cxMalloc(al, n);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
83 if (newmem != NULL && array != NULL) {
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
84 memcpy(newmem, array, old_capacity*elem_size);
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
85 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
86 } else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
87 newmem = cxRealloc(al, array, n);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
88 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
89 return newmem;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
90 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
91
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
92 struct cx_array_reallocator_s cx_array_reallocator(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
93 const struct cx_allocator_s *allocator,
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
94 const void *stack_ptr
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
95 ) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
96 if (allocator == NULL) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
97 allocator = cxDefaultAllocator;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
98 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
99 return (struct cx_array_reallocator_s) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
100 cx_array_advanced_realloc,
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
101 allocator, stack_ptr,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
102 };
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
103 }
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 // LOW LEVEL ARRAY LIST FUNCTIONS
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
107 /**
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
108 * Intelligently calculates a new capacity, reserving some more
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
109 * elements than required to prevent too many allocations.
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
110 *
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
111 * @param current_capacity the current capacity of the array
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
112 * @param needed_capacity the required capacity of the array
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
113 * @return the new capacity
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
114 */
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
115 static size_t cx_array_grow_capacity(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
116 size_t current_capacity,
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
117 size_t needed_capacity
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
118 ) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
119 if (current_capacity >= needed_capacity) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
120 return current_capacity;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
121 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
122 size_t cap = needed_capacity;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
123 size_t alignment;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
124 if (cap < 128) alignment = 16;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
125 else if (cap < 1024) alignment = 64;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
126 else if (cap < 8192) alignment = 512;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
127 else alignment = 1024;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
128 return cap - (cap % alignment) + alignment;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
129 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
130
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
131 int cx_array_reserve(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
132 void **array,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
133 void *size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
134 void *capacity,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
135 unsigned width,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 size_t elem_size,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 size_t elem_count,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
138 CxArrayReallocator *reallocator
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
139 ) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
140 // assert pointers
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
141 assert(array != NULL);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
142 assert(size != NULL);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
143 assert(capacity != NULL);
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
144
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
145 // default reallocator
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
146 if (reallocator == NULL) {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
147 reallocator = cx_array_default_reallocator;
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
148 }
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
149
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
150 // determine size and capacity
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
151 size_t oldcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
152 size_t oldsize;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
153 size_t max_size;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
154 if (width == 0 || width == sizeof(size_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
155 oldcap = *(size_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
156 oldsize = *(size_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
157 max_size = SIZE_MAX;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
158 } else if (width == sizeof(uint16_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
159 oldcap = *(uint16_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
160 oldsize = *(uint16_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
161 max_size = UINT16_MAX;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
162 } else if (width == sizeof(uint8_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
163 oldcap = *(uint8_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
164 oldsize = *(uint8_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
165 max_size = UINT8_MAX;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
166 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
167 #if CX_WORDSIZE == 64
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
168 else if (width == sizeof(uint32_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
169 oldcap = *(uint32_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
170 oldsize = *(uint32_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
171 max_size = UINT32_MAX;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
172 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
173 #endif
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
174 else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
175 errno = EINVAL;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
176 return 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
177 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
178
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
179 // assert that the array is allocated when it has capacity
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
180 assert(*array != NULL || oldcap == 0);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
181
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
182 // check for overflow
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
183 if (elem_count > max_size - oldsize) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
184 errno = EOVERFLOW;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
185 return 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
186 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
187
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
188 // determine new capacity
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
189 size_t newcap = oldsize + elem_count;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
190
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
191 // reallocate if possible
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
192 if (newcap > oldcap) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
193 void *newmem = reallocator->realloc(
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
194 *array, oldcap, newcap, elem_size, reallocator
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
195 );
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
196 if (newmem == NULL) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
197 return 1; // LCOV_EXCL_LINE
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
198 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
199
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
200 // store new pointer
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
201 *array = newmem;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
202
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
203 // store new capacity
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
204 if (width == 0 || width == sizeof(size_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
205 *(size_t*) capacity = newcap;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
206 } else if (width == sizeof(uint16_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
207 *(uint16_t*) capacity = (uint16_t) newcap;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
208 } else if (width == sizeof(uint8_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
209 *(uint8_t*) capacity = (uint8_t) newcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
210 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
211 #if CX_WORDSIZE == 64
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
212 else if (width == sizeof(uint32_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
213 *(uint32_t*) capacity = (uint32_t) newcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
214 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
215 #endif
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
216 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
217
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
218 return 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
219 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
220
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
221 int cx_array_copy(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
222 void **target,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
223 void *size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
224 void *capacity,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
225 unsigned width,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
226 size_t index,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
227 const void *src,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
228 size_t elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
229 size_t elem_count,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
230 CxArrayReallocator *reallocator
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 // assert pointers
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 assert(target != NULL);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 assert(size != NULL);
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
235 assert(capacity != NULL);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 assert(src != NULL);
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
237
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
238 // default reallocator
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
239 if (reallocator == NULL) {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
240 reallocator = cx_array_default_reallocator;
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
241 }
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
243 // determine size and capacity
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
244 size_t oldcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
245 size_t oldsize;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
246 size_t max_size;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
247 if (width == 0 || width == sizeof(size_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
248 oldcap = *(size_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
249 oldsize = *(size_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
250 max_size = SIZE_MAX;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
251 } else if (width == sizeof(uint16_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
252 oldcap = *(uint16_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
253 oldsize = *(uint16_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
254 max_size = UINT16_MAX;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
255 } else if (width == sizeof(uint8_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
256 oldcap = *(uint8_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
257 oldsize = *(uint8_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
258 max_size = UINT8_MAX;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
259 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
260 #if CX_WORDSIZE == 64
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
261 else if (width == sizeof(uint32_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
262 oldcap = *(uint32_t*) capacity;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
263 oldsize = *(uint32_t*) size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
264 max_size = UINT32_MAX;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
265 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
266 #endif
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
267 else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
268 errno = EINVAL;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
269 return 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
270 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
271
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
272 // assert that the array is allocated when it has capacity
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
273 assert(*target != NULL || oldcap == 0);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
274
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
275 // check for overflow
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
276 if (index > max_size || elem_count > max_size - index) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
277 errno = EOVERFLOW;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
278 return 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
279 }
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 // check if resize is required
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
282 const size_t minsize = index + elem_count;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
283 const size_t newsize = oldsize < minsize ? minsize : oldsize;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
285 // reallocate if necessary
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
286 const size_t newcap = cx_array_grow_capacity(oldcap, newsize);
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
287 if (newcap > oldcap) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
288 // check if we need to repair the src pointer
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 uintptr_t targetaddr = (uintptr_t) *target;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 uintptr_t srcaddr = (uintptr_t) src;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 bool repairsrc = targetaddr <= srcaddr
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
292 && srcaddr < targetaddr + oldcap * elem_size;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 // perform reallocation
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 void *newmem = reallocator->realloc(
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
296 *target, oldcap, newcap, elem_size, reallocator
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 );
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 if (newmem == NULL) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
299 return 1; // LCOV_EXCL_LINE
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 // repair src pointer, if necessary
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 if (repairsrc) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 src = ((char *) newmem) + (srcaddr - targetaddr);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
307 // store new pointer
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 *target = newmem;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 // determine target pointer
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 char *start = *target;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 start += index * elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 // copy elements and set new size
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
316 // note: no overflow check here, b/c we cannot get here w/o allocation
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 memmove(start, src, elem_count * elem_size);
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
318
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
319 // if any of size or capacity changed, store them back
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
320 if (newsize != oldsize || newcap != oldcap) {
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
321 if (width == 0 || width == sizeof(size_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
322 *(size_t*) capacity = newcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
323 *(size_t*) size = newsize;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
324 } else if (width == sizeof(uint16_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
325 *(uint16_t*) capacity = (uint16_t) newcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
326 *(uint16_t*) size = (uint16_t) newsize;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
327 } else if (width == sizeof(uint8_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
328 *(uint8_t*) capacity = (uint8_t) newcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
329 *(uint8_t*) size = (uint8_t) newsize;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
330 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
331 #if CX_WORDSIZE == 64
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
332 else if (width == sizeof(uint32_t)) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
333 *(uint32_t*) capacity = (uint32_t) newcap;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
334 *(uint32_t*) size = (uint32_t) newsize;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
335 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
336 #endif
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
337 }
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 // return successfully
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
340 return 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
341 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
342
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
343 static int cx_array_insert_sorted_impl(
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
344 void **target,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
345 size_t *size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
346 size_t *capacity,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
347 cx_compare_func cmp_func,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
348 const void *sorted_data,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
349 size_t elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
350 size_t elem_count,
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
351 CxArrayReallocator *reallocator,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
352 bool allow_duplicates
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
353 ) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
354 // assert pointers
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
355 assert(target != NULL);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
356 assert(size != NULL);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
357 assert(capacity != NULL);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
358 assert(cmp_func != NULL);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
359 assert(sorted_data != NULL);
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
360
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
361 // default reallocator
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
362 if (reallocator == NULL) {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
363 reallocator = cx_array_default_reallocator;
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
364 }
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
365
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
366 // corner case
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
367 if (elem_count == 0) return 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
368
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
369 // overflow check
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
370 // LCOV_EXCL_START
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
371 if (elem_count > SIZE_MAX - *size) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
372 errno = EOVERFLOW;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
373 return 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
374 }
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
375 // LCOV_EXCL_STOP
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
376
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
377 // store some counts
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
378 const size_t old_size = *size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
379 const size_t old_capacity = *capacity;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
380 // the necessary capacity is the worst case assumption, including duplicates
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
381 const size_t needed_capacity = cx_array_grow_capacity(old_capacity, old_size + elem_count);
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
382
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
383 // if we need more than we have, try a reallocation
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
384 if (needed_capacity > old_capacity) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
385 void *new_mem = reallocator->realloc(
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
386 *target, old_capacity, needed_capacity, elem_size, reallocator
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
387 );
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
388 if (new_mem == NULL) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
389 // give it up right away, there is no contract
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
390 // that requires us to insert as much as we can
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
391 return 1; // LCOV_EXCL_LINE
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
392 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
393 *target = new_mem;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
394 *capacity = needed_capacity;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
395 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
396
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
397 // now we have guaranteed that we can insert everything
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
398 size_t new_size = old_size + elem_count;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
399 *size = new_size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
400
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
401 // declare the source and destination indices/pointers
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
402 size_t si = 0, di = 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
403 const char *src = sorted_data;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
404 char *dest = *target;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
405
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
406 // find the first insertion point
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
407 di = cx_array_binary_search_sup(dest, old_size, elem_size, src, cmp_func);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
408 dest += di * elem_size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
409
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
410 // move the remaining elements in the array completely to the right
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
411 // we will call it the "buffer" for parked elements
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
412 size_t buf_size = old_size - di;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
413 size_t bi = new_size - buf_size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
414 char *bptr = ((char *) *target) + bi * elem_size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
415 memmove(bptr, dest, buf_size * elem_size);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
416
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
417 // while there are both source and buffered elements left,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
418 // copy them interleaving
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
419 while (si < elem_count && bi < new_size) {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
420 // determine how many source elements can be inserted.
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
421 // the first element that shall not be inserted is the smallest element
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
422 // that is strictly larger than the first buffered element
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
423 // (located at the index of the infimum plus one).
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
424 // the infimum is guaranteed to exist:
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
425 // - if all src elements are larger,
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
426 // there is no buffer, and this loop is skipped
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
427 // - if any src element is smaller or equal, the infimum exists
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
428 // - when all src elements that are smaller are copied, the second part
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
429 // of this loop body will copy the remaining buffer (emptying it)
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
430 // Therefore, the buffer can never contain an element that is smaller
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
431 // than any element in the source and the infimum exists.
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
432 size_t copy_len, bytes_copied;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
433 copy_len = cx_array_binary_search_inf(
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
434 src, elem_count - si, elem_size, bptr, cmp_func
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
435 );
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
436 copy_len++;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
437
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
438 // copy the source elements
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
439 if (copy_len > 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
440 if (allow_duplicates) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
441 // we can copy the entire chunk
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
442 bytes_copied = copy_len * elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
443 memcpy(dest, src, bytes_copied);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
444 dest += bytes_copied;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
445 src += bytes_copied;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
446 si += copy_len;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
447 di += copy_len;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
448 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
449 // first, check the end of the source chunk
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
450 // for being a duplicate of the bptr
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
451 const char *end_of_src = src + (copy_len - 1) * elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
452 size_t skip_len = 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
453 while (copy_len > 0 && cmp_func(bptr, end_of_src) == 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
454 end_of_src -= elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
455 skip_len++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
456 copy_len--;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
457 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
458 char *last = dest == *target ? NULL : dest - elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
459 // then iterate through the source chunk
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
460 // and skip all duplicates with the last element in the array
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
461 size_t more_skipped = 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
462 for (unsigned j = 0; j < copy_len; j++) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
463 if (last != NULL && cmp_func(last, src) == 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
464 // duplicate - skip
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
465 src += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
466 si++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
467 more_skipped++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
468 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
469 memcpy(dest, src, elem_size);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
470 src += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
471 last = dest;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
472 dest += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
473 si++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
474 di++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
475 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
476 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
477 // skip the previously identified elements as well
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
478 src += skip_len * elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
479 si += skip_len;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
480 skip_len += more_skipped;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
481 // reduce the actual size by the number of skipped elements
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
482 *size -= skip_len;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
483 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
484 }
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
485
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
486 // when all source elements are in place, we are done
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
487 if (si >= elem_count) break;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
488
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
489 // determine how many buffered elements need to be restored
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
490 copy_len = cx_array_binary_search_sup(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
491 bptr,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
492 new_size - bi,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
493 elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
494 src,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
495 cmp_func
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
496 );
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
497
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
498 // restore the buffered elements
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
499 bytes_copied = copy_len * elem_size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
500 memmove(dest, bptr, bytes_copied);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
501 dest += bytes_copied;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
502 bptr += bytes_copied;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
503 di += copy_len;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
504 bi += copy_len;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
505 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
506
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
507 // still source elements left?
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
508 if (si < elem_count) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
509 if (allow_duplicates) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
510 // duplicates allowed or nothing inserted yet: simply copy everything
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
511 memcpy(dest, src, elem_size * (elem_count - si));
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
512 } else {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
513 // we must check the remaining source elements one by one
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
514 // to skip the duplicates.
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
515 // Note that no source element can equal the last element in the
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
516 // destination, because that would have created an insertion point
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
517 // and a buffer, s.t. the above loop already handled the duplicates
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
518 while (si < elem_count) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
519 // find a chain of elements that can be copied
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
520 size_t copy_len = 1, skip_len = 0;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
521 {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
522 const char *left_src = src;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
523 while (si + copy_len + skip_len < elem_count) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
524 const char *right_src = left_src + elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
525 int d = cmp_func(left_src, right_src);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
526 if (d < 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
527 if (skip_len > 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
528 // new larger element found;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
529 // handle it in the next cycle
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
530 break;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
531 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
532 left_src += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
533 copy_len++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
534 } else if (d == 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
535 left_src += elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
536 skip_len++;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
537 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
538 break;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
539 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
540 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
541 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
542 size_t bytes_copied = copy_len * elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
543 memcpy(dest, src, bytes_copied);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
544 dest += bytes_copied;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
545 src += bytes_copied + skip_len * elem_size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
546 si += copy_len + skip_len;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
547 di += copy_len;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
548 *size -= skip_len;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
549 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
550 }
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
551 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
552
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
553 // buffered elements need to be moved when we skipped duplicates
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
554 size_t total_skipped = new_size - *size;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
555 if (bi < new_size && total_skipped > 0) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
556 // move the remaining buffer to the end of the array
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
557 memmove(dest, bptr, elem_size * (new_size - bi));
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
558 }
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
559
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
560 return 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
561 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
562
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
563 int cx_array_insert_sorted(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
564 void **target,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
565 size_t *size,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
566 size_t *capacity,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
567 cx_compare_func cmp_func,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
568 const void *sorted_data,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
569 size_t elem_size,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
570 size_t elem_count,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
571 CxArrayReallocator *reallocator
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
572 ) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
573 return cx_array_insert_sorted_impl(target, size, capacity,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
574 cmp_func, sorted_data, elem_size, elem_count, reallocator, true);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
575 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
576
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
577 int cx_array_insert_unique(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
578 void **target,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
579 size_t *size,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
580 size_t *capacity,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
581 cx_compare_func cmp_func,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
582 const void *sorted_data,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
583 size_t elem_size,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
584 size_t elem_count,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
585 CxArrayReallocator *reallocator
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
586 ) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
587 return cx_array_insert_sorted_impl(target, size, capacity,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
588 cmp_func, sorted_data, elem_size, elem_count, reallocator, false);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
589 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
590
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
591 // implementation that finds ANY index
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
592 static size_t cx_array_binary_search_inf_impl(
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
593 const void *arr,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
594 size_t size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
595 size_t elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
596 const void *elem,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
597 cx_compare_func cmp_func
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
598 ) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
599 // special case: empty array
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
600 if (size == 0) return 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
601
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
602 // declare a variable that will contain the compare results
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
603 int result;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
604
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
605 // cast the array pointer to something we can use offsets with
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
606 const char *array = arr;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
607
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
608 // check the first array element
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
609 result = cmp_func(elem, array);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
610 if (result < 0) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
611 return size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
612 } else if (result == 0) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
613 return 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
614 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
615
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
616 // special case: there is only one element and that is smaller
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
617 if (size == 1) return 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
618
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
619 // check the last array element
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
620 result = cmp_func(elem, array + elem_size * (size - 1));
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
621 if (result >= 0) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
622 return size - 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
623 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
624
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
625 // the element is now guaranteed to be somewhere in the list
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
626 // so start the binary search
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
627 size_t left_index = 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
628 size_t right_index = size - 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
629 size_t pivot_index;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
630
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
631 while (left_index <= right_index) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
632 pivot_index = left_index + (right_index - left_index) / 2;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
633 const char *arr_elem = array + pivot_index * elem_size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
634 result = cmp_func(elem, arr_elem);
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
635 if (result == 0) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
636 // found it!
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
637 return pivot_index;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
638 } else if (result < 0) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
639 // element is smaller than pivot, continue search left
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
640 right_index = pivot_index - 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
641 } else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
642 // element is larger than pivot, continue search right
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
643 left_index = pivot_index + 1;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
644 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
645 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
646
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
647 // report the largest upper bound
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
648 return result < 0 ? (pivot_index - 1) : pivot_index;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
649 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
650
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
651 size_t cx_array_binary_search_inf(
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
652 const void *arr,
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
653 size_t size,
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
654 size_t elem_size,
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
655 const void *elem,
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
656 cx_compare_func cmp_func
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
657 ) {
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
658 size_t index = cx_array_binary_search_inf_impl(
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
659 arr, size, elem_size, elem, cmp_func);
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
660 // in case of equality, report the largest index
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
661 const char *e = ((const char *) arr) + (index + 1) * elem_size;
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
662 while (index + 1 < size && cmp_func(e, elem) == 0) {
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
663 e += elem_size;
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
664 index++;
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
665 }
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
666 return index;
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
667 }
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
668
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
669 size_t cx_array_binary_search(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
670 const void *arr,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
671 size_t size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
672 size_t elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
673 const void *elem,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
674 cx_compare_func cmp_func
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
675 ) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
676 size_t index = cx_array_binary_search_inf(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
677 arr, size, elem_size, elem, cmp_func
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
678 );
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
679 if (index < size &&
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
680 cmp_func(((const char *) arr) + index * elem_size, elem) == 0) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
681 return index;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
682 } else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
683 return size;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
684 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
685 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
686
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
687 size_t cx_array_binary_search_sup(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
688 const void *arr,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
689 size_t size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
690 size_t elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
691 const void *elem,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
692 cx_compare_func cmp_func
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
693 ) {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
694 size_t index = cx_array_binary_search_inf_impl(
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
695 arr, size, elem_size, elem, cmp_func
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
696 );
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
697 const char *e = ((const char *) arr) + index * elem_size;
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
698 if (index == size) {
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
699 // no infimum means the first element is supremum
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
700 return 0;
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
701 } else if (cmp_func(e, elem) == 0) {
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
702 // found an equal element, search the smallest index
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
703 e -= elem_size; // e now contains the element at index-1
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
704 while (index > 0 && cmp_func(e, elem) == 0) {
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
705 e -= elem_size;
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
706 index--;
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
707 }
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
708 return index;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
709 } else {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
710 // we already have the largest index of the infimum (by design)
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
711 // the next element is the supremum (or there is no supremum)
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
712 return index + 1;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
713 }
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
714 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
715
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
716 #ifndef CX_ARRAY_SWAP_SBO_SIZE
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
717 #define CX_ARRAY_SWAP_SBO_SIZE 128
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
718 #endif
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
719 const unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
720
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
721 void cx_array_swap(
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
722 void *arr,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
723 size_t elem_size,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
724 size_t idx1,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
725 size_t idx2
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
726 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
727 assert(arr != NULL);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
728
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
729 // short circuit
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
730 if (idx1 == idx2) return;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
731
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
732 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
733 void *tmp;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
734
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
735 // decide if we can use the local buffer
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
736 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
737 tmp = cxMallocDefault(elem_size);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
738 // we don't want to enforce error handling
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
739 if (tmp == NULL) abort();
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
740 } else {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
741 tmp = sbo_mem;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
742 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
743
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
744 // calculate memory locations
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
745 char *left = arr, *right = arr;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
746 left += idx1 * elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
747 right += idx2 * elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
748
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
749 // three-way swap
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
750 memcpy(tmp, left, elem_size);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
751 memcpy(left, right, elem_size);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
752 memcpy(right, tmp, elem_size);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
753
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
754 // free dynamic memory, if it was needed
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
755 if (tmp != sbo_mem) {
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
756 cxFreeDefault(tmp);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
757 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
758 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
759
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
760 // HIGH LEVEL ARRAY LIST FUNCTIONS
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
761
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
762 typedef struct {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
763 struct cx_list_s base;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
764 void *data;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
765 size_t capacity;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
766 CxArrayReallocator reallocator;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
767 } cx_array_list;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
768
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
769 static void cx_arl_destructor(struct cx_list_s *list) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
770 cx_array_list *arl = (cx_array_list *) list;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
771
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
772 char *ptr = arl->data;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
773
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
774 if (list->collection.simple_destructor) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
775 for (size_t i = 0; i < list->collection.size; i++) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
776 cx_invoke_simple_destructor(list, ptr);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
777 ptr += list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
778 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
779 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
780 if (list->collection.advanced_destructor) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
781 for (size_t i = 0; i < list->collection.size; i++) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
782 cx_invoke_advanced_destructor(list, ptr);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
783 ptr += list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
784 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
785 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
786
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
787 cxFree(list->collection.allocator, arl->data);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
788 cxFree(list->collection.allocator, list);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
789 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
790
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
791 static size_t cx_arl_insert_array(
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
792 struct cx_list_s *list,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
793 size_t index,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
794 const void *array,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
795 size_t n
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
796 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
797 // out of bounds and special case check
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
798 if (index > list->collection.size || n == 0) return 0;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
799
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
800 // get a correctly typed pointer to the list
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
801 cx_array_list *arl = (cx_array_list *) list;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
802
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
803 // guarantee enough capacity
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
804 if (arl->capacity < list->collection.size + n) {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
805 const size_t new_capacity = cx_array_grow_capacity(arl->capacity,list->collection.size + n);
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
806 if (cxReallocateArray(
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
807 list->collection.allocator,
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
808 &arl->data, new_capacity,
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
809 list->collection.elem_size)
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
810 ) {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
811 return 0; // LCOV_EXCL_LINE
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
812 }
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
813 arl->capacity = new_capacity;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
814 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
815
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
816 // determine insert position
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
817 char *arl_data = arl->data;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
818 char *insert_pos = arl_data + index * list->collection.elem_size;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
819
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
820 // do we need to move some elements?
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
821 if (index < list->collection.size) {
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
822 size_t elems_to_move = list->collection.size - index;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
823 char *target = insert_pos + n * list->collection.elem_size;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
824 memmove(target, insert_pos, elems_to_move * list->collection.elem_size);
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
825 }
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
826
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
827 // place the new elements, if any
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
828 if (array != NULL) {
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
829 memcpy(insert_pos, array, n * list->collection.elem_size);
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
830 }
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
831 list->collection.size += n;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
832
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
833 return n;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
834 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
835
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
836 static size_t cx_arl_insert_sorted(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
837 struct cx_list_s *list,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
838 const void *sorted_data,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
839 size_t n
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
840 ) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
841 // get a correctly typed pointer to the list
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
842 cx_array_list *arl = (cx_array_list *) list;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
843
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
844 if (cx_array_insert_sorted(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
845 &arl->data,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
846 &list->collection.size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
847 &arl->capacity,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
848 list->collection.cmpfunc,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
849 sorted_data,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
850 list->collection.elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
851 n,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
852 &arl->reallocator
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
853 )) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
854 // array list implementation is "all or nothing"
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
855 return 0; // LCOV_EXCL_LINE
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
856 } else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
857 return n;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
858 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
859 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
860
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
861 static size_t cx_arl_insert_unique(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
862 struct cx_list_s *list,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
863 const void *sorted_data,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
864 size_t n
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
865 ) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
866 // get a correctly typed pointer to the list
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
867 cx_array_list *arl = (cx_array_list *) list;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
868
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
869 if (cx_array_insert_unique(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
870 &arl->data,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
871 &list->collection.size,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
872 &arl->capacity,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
873 list->collection.cmpfunc,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
874 sorted_data,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
875 list->collection.elem_size,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
876 n,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
877 &arl->reallocator
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
878 )) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
879 // array list implementation is "all or nothing"
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
880 return 0; // LCOV_EXCL_LINE
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
881 } else {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
882 return n;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
883 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
884 }
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
885
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
886 static void *cx_arl_insert_element(
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
887 struct cx_list_s *list,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
888 size_t index,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
889 const void *element
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
890 ) {
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
891 if (cx_arl_insert_array(list, index, element, 1) == 1) {
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
892 return ((char*)((cx_array_list *) list)->data) + index * list->collection.elem_size;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
893 } else {
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
894 return NULL;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
895 }
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
896 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
897
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
898 static int cx_arl_insert_iter(
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
899 struct cx_iterator_s *iter,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
900 const void *elem,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
901 int prepend
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
902 ) {
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
903 struct cx_list_s *list = iter->src_handle;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
904 if (iter->index < list->collection.size) {
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
905 if (cx_arl_insert_element(list,
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
906 iter->index + 1 - prepend, elem) == NULL) {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
907 return 1; // LCOV_EXCL_LINE
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
908 }
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
909 iter->elem_count++;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
910 if (prepend != 0) {
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
911 iter->index++;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
912 iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
913 }
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
914 return 0;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
915 } else {
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
916 if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) {
23
b26390e77237 add first tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
917 return 1; // LCOV_EXCL_LINE
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
918 }
21
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
919 iter->elem_count++;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
920 iter->index = list->collection.size;
5ea41679e15d update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
921 return 0;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
922 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
923 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
924
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
925 static size_t cx_arl_remove(
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
926 struct cx_list_s *list,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
927 size_t index,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
928 size_t num,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
929 void *targetbuf
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
930 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
931 cx_array_list *arl = (cx_array_list *) list;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
932
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
933 // out-of-bounds check
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
934 size_t remove;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
935 if (index >= list->collection.size) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
936 remove = 0;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
937 } else if (index + num > list->collection.size) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
938 remove = list->collection.size - index;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
939 } else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
940 remove = num;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
941 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
942
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
943 // easy exit
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
944 if (remove == 0) return 0;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
945
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
946 // destroy or copy contents
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
947 if (targetbuf == NULL) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
948 for (size_t idx = index; idx < index + remove; idx++) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
949 cx_invoke_destructor(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
950 list,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
951 ((char *) arl->data) + idx * list->collection.elem_size
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
952 );
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
953 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
954 } else {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
955 memcpy(
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
956 targetbuf,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
957 ((char *) arl->data) + index * list->collection.elem_size,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
958 remove * list->collection.elem_size
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
959 );
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
960 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
961
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
962 // short-circuit removal of last elements
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
963 if (index + remove == list->collection.size) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
964 list->collection.size -= remove;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
965 return remove;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
966 }
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
967
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
968 // just move the elements to the left
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
969 cx_array_copy(
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
970 &arl->data,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
971 &list->collection.size,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
972 &arl->capacity,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
973 0,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
974 index,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
975 ((char *) arl->data) + (index + remove) * list->collection.elem_size,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
976 list->collection.elem_size,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
977 list->collection.size - index - remove,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
978 &arl->reallocator
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
979 );
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
980
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
981 // decrease the size
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
982 list->collection.size -= remove;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
983
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
984 return remove;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
985 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
986
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
987 static void cx_arl_clear(struct cx_list_s *list) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
988 if (list->collection.size == 0) return;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
989
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
990 cx_array_list *arl = (cx_array_list *) list;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
991 char *ptr = arl->data;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
992
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
993 if (list->collection.simple_destructor) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
994 for (size_t i = 0; i < list->collection.size; i++) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
995 cx_invoke_simple_destructor(list, ptr);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
996 ptr += list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
997 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
998 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
999 if (list->collection.advanced_destructor) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1000 for (size_t i = 0; i < list->collection.size; i++) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1001 cx_invoke_advanced_destructor(list, ptr);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1002 ptr += list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1003 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1004 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1005
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1006 memset(arl->data, 0, list->collection.size * list->collection.elem_size);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1007 list->collection.size = 0;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1008 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1009
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1010 static int cx_arl_swap(
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1011 struct cx_list_s *list,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1012 size_t i,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1013 size_t j
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1014 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1015 if (i >= list->collection.size || j >= list->collection.size) return 1;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1016 cx_array_list *arl = (cx_array_list *) list;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1017 cx_array_swap(arl->data, list->collection.elem_size, i, j);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1018 return 0;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1019 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1020
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1021 static void *cx_arl_at(
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1022 const struct cx_list_s *list,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1023 size_t index
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1024 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1025 if (index < list->collection.size) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1026 const cx_array_list *arl = (const cx_array_list *) list;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1027 char *space = arl->data;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1028 return space + index * list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1029 } else {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1030 return NULL;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1031 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1032 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1033
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1034 static size_t cx_arl_find_remove(
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1035 struct cx_list_s *list,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1036 const void *elem,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1037 bool remove
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1038 ) {
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1039 assert(list != NULL);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1040 assert(list->collection.cmpfunc != NULL);
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1041 if (list->collection.size == 0) return 0;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1042 char *cur = ((const cx_array_list *) list)->data;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1043
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1044 // optimize with binary search, when sorted
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1045 if (list->collection.sorted) {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1046 size_t i = cx_array_binary_search(
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1047 cur,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1048 list->collection.size,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1049 list->collection.elem_size,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1050 elem,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1051 list->collection.cmpfunc
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1052 );
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1053 if (remove && i < list->collection.size) {
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1054 cx_arl_remove(list, i, 1, NULL);
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1055 }
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1056 return i;
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1057 }
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1058
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1059 // fallback: linear search
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1060 for (size_t i = 0; i < list->collection.size; i++) {
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1061 if (0 == list->collection.cmpfunc(elem, cur)) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1062 if (remove) {
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1063 cx_arl_remove(list, i, 1, NULL);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1064 }
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1065 return i;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1066 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1067 cur += list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1068 }
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1069 return list->collection.size;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1070 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1071
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1072 static void cx_arl_sort(struct cx_list_s *list) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1073 assert(list->collection.cmpfunc != NULL);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1074 qsort(((cx_array_list *) list)->data,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1075 list->collection.size,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1076 list->collection.elem_size,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1077 list->collection.cmpfunc
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1078 );
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1079 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1080
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1081 static int cx_arl_compare(
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1082 const struct cx_list_s *list,
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1083 const struct cx_list_s *other
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1084 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1085 assert(list->collection.cmpfunc != NULL);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1086 if (list->collection.size == other->collection.size) {
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1087 const char *left = ((const cx_array_list *) list)->data;
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1088 const char *right = ((const cx_array_list *) other)->data;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1089 for (size_t i = 0; i < list->collection.size; i++) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1090 int d = list->collection.cmpfunc(left, right);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1091 if (d != 0) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1092 return d;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1093 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1094 left += list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1095 right += other->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1096 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1097 return 0;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1098 } else {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1099 return list->collection.size < other->collection.size ? -1 : 1;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1100 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1101 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1102
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1103 static void cx_arl_reverse(struct cx_list_s *list) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1104 if (list->collection.size < 2) return;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1105 void *data = ((const cx_array_list *) list)->data;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1106 size_t half = list->collection.size / 2;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1107 for (size_t i = 0; i < half; i++) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1108 cx_array_swap(data, list->collection.elem_size, i, list->collection.size - 1 - i);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1109 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1110 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1111
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1112 static bool cx_arl_iter_valid(const void *it) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1113 const struct cx_iterator_s *iter = it;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1114 const struct cx_list_s *list = iter->src_handle;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1115 return iter->index < list->collection.size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1116 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1117
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1118 static void *cx_arl_iter_current(const void *it) {
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1119 const struct cx_iterator_s *iter = it;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1120 return iter->elem_handle;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1121 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1122
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1123 static void cx_arl_iter_next(void *it) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1124 struct cx_iterator_s *iter = it;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1125 if (iter->base.remove) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1126 iter->base.remove = false;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1127 cx_arl_remove(iter->src_handle, iter->index, 1, NULL);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1128 iter->elem_count--;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1129 } else {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1130 iter->index++;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1131 iter->elem_handle =
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1132 ((char *) iter->elem_handle)
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1133 + ((const struct cx_list_s *) iter->src_handle)->collection.elem_size;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1134 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1135 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1136
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1137 static void cx_arl_iter_prev(void *it) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1138 struct cx_iterator_s *iter = it;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1139 if (iter->base.remove) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1140 iter->base.remove = false;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1141 cx_arl_remove(iter->src_handle, iter->index, 1, NULL);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1142 iter->elem_count--;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1143 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1144 iter->index--;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1145 cx_array_list *list = iter->src_handle;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1146 if (iter->index < list->base.collection.size) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1147 iter->elem_handle = ((char *) list->data)
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1148 + iter->index * list->base.collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1149 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1150 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1151
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1152 static int cx_arl_change_capacity(
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1153 struct cx_list_s *list,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1154 size_t new_capacity
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1155 ) {
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1156 cx_array_list *arl = (cx_array_list *)list;
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1157 return cxReallocateArray(list->collection.allocator,
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1158 &arl->data, new_capacity, list->collection.elem_size);
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1159 }
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1160
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1161 static struct cx_iterator_s cx_arl_iterator(
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1162 const struct cx_list_s *list,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1163 size_t index,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1164 bool backwards
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1165 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1166 struct cx_iterator_s iter;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1167
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1168 iter.index = index;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1169 iter.src_handle = (void*)list;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1170 iter.elem_handle = cx_arl_at(list, index);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1171 iter.elem_size = list->collection.elem_size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1172 iter.elem_count = list->collection.size;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1173 iter.base.valid = cx_arl_iter_valid;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1174 iter.base.current = cx_arl_iter_current;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1175 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1176 iter.base.remove = false;
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1177 iter.base.allow_remove = true;
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1178
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1179 return iter;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1180 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1181
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1182 static cx_list_class cx_array_list_class = {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1183 cx_arl_destructor,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1184 cx_arl_insert_element,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1185 cx_arl_insert_array,
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1186 cx_arl_insert_sorted,
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1187 cx_arl_insert_unique,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1188 cx_arl_insert_iter,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1189 cx_arl_remove,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1190 cx_arl_clear,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1191 cx_arl_swap,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1192 cx_arl_at,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1193 cx_arl_find_remove,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1194 cx_arl_sort,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1195 cx_arl_compare,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1196 cx_arl_reverse,
22
112b85020dc9 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
1197 cx_arl_change_capacity,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1198 cx_arl_iterator,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1199 };
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1200
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1201 CxList *cxArrayListCreate(
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1202 const CxAllocator *allocator,
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1203 cx_compare_func comparator,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1204 size_t elem_size,
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1205 size_t initial_capacity
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1206 ) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1207 if (allocator == NULL) {
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1208 allocator = cxDefaultAllocator;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1209 }
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1210
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1211 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1212 if (list == NULL) return NULL;
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1213 cx_list_init((CxList*)list, &cx_array_list_class,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1214 allocator, comparator, elem_size);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1215 list->capacity = initial_capacity;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1216
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1217 // allocate the array after the real elem_size is known
16
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1218 list->data = cxCalloc(allocator, initial_capacity,
04c9f8d8f03b update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 11
diff changeset
1219 list->base.collection.elem_size);
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1220 if (list->data == NULL) { // LCOV_EXCL_START
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1221 cxFree(allocator, list);
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1222 return NULL;
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1223 } // LCOV_EXCL_STOP
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1224
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1225 // configure the reallocator
11
0aa8cbd7912e refactor dbuObjectExecuteQuery: replace multiple lists with a single ll list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
1226 list->reallocator = cx_array_reallocator(allocator, NULL);
0
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1227
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1228 return (CxList *) list;
1a157da63d7c add API for registering types and simple SQLite proof of concept
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1229 }

mercurial