src/ucx/array_list.c

Sun, 20 Nov 2022 12:43:44 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 20 Nov 2022 12:43:44 +0100
changeset 437
545010bc5e71
parent 436
1260fad21be7
child 438
22eca559aded
permissions
-rw-r--r--

replace linkedlist with arraylist in the expression parser

436
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1260fad21be7 update ucx
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
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "cx/array_list.h"
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <assert.h>
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdint.h>
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 /* LOW LEVEL ARRAY LIST FUNCTIONS */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 enum cx_array_copy_result cx_array_copy(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 void **target,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 size_t *size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 size_t *capacity,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 size_t index,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 void const *src,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 size_t elem_size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 size_t elem_count,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 struct cx_array_reallocator_s *reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 /* assert pointers */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 assert(target != NULL);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 assert(size != NULL);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 assert(src != NULL);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 /* determine capacity */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 size_t cap = capacity == NULL ? *size : *capacity;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 /* check if resize is required */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 size_t newsize = index + elem_count;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 bool needrealloc = newsize > cap;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 /* reallocate if possible */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 if (needrealloc) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 /* a reallocator and a capacity variable must be available */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 if (reallocator == NULL || capacity == NULL) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 /* check, if we need to repair the src pointer */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 uintptr_t targetaddr = (uintptr_t) *target;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 uintptr_t srcaddr = (uintptr_t) src;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 bool repairsrc = targetaddr <= srcaddr
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 && srcaddr < targetaddr + cap * elem_size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 /* increase capacity linearly */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 cap += 16;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 /* perform reallocation */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 void *newmem = reallocator->realloc(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 *target, cap, elem_size, reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 );
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 if (newmem == NULL) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 return CX_ARRAY_COPY_REALLOC_FAILED;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 /* repair src pointer, if necessary */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 if (repairsrc) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 src = ((char *) newmem) + (srcaddr - targetaddr);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 /* store new pointer and capacity */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 *target = newmem;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 *capacity = cap;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 /* determine target pointer */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 char *start = *target;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 start += index * elem_size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 /* copy elements and set new size */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 memmove(start, src, elem_count * elem_size);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 *size = newsize;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 /* return successfully */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 return CX_ARRAY_COPY_SUCCESS;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 /* HIGH LEVEL ARRAY LIST FUNCTIONS */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 typedef struct {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 struct cx_list_s base;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 void *data;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 struct cx_array_reallocator_s reallocator;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 } cx_array_list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 static void *cx_arl_realloc(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 void *array,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 size_t capacity,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 size_t elem_size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 struct cx_array_reallocator_s *alloc
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 /* retrieve the pointer to the list allocator */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 CxAllocator const *al = alloc->ptr1;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 /* use the list allocator to reallocate the memory */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 return cxRealloc(al, array, capacity * elem_size);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 static void cx_arl_destructor(struct cx_list_s *list) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 cx_array_list *arl = (cx_array_list *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 cxFree(list->allocator, arl->data);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 static int cx_arl_add(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 struct cx_list_s *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 void const *elem
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 cx_array_list *arl = (cx_array_list *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 return cx_array_copy(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 &arl->data,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 &list->size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 &list->capacity,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 list->size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 elem,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 list->itemsize,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 1,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 &arl->reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 );
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 static int cx_arl_insert(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 struct cx_list_s *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 size_t index,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 void const *elem
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 if (index > list->size) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 return 1;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 } else if (index == list->size) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 return cx_arl_add(list, elem);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 } else {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 cx_array_list *arl = (cx_array_list *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 /* move elements starting at index to the right */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 if (cx_array_copy(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 &arl->data,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 &list->size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 &list->capacity,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 index + 1,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 ((char *) arl->data) + index * list->itemsize,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 list->itemsize,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 list->size - index,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 &arl->reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 )) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 return 1;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 /* place the element */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 memcpy(((char *) arl->data) + index * list->itemsize,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 elem, list->itemsize);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 return 0;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 static int cx_arl_insert_iter(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 struct cx_iterator_s *iter,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 void const *elem,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 int prepend
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 return 1;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 static int cx_arl_remove(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 struct cx_list_s *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 size_t index
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 /* out-of-bounds check */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 if (index >= list->size) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 return 1;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 cx_array_list *arl = (cx_array_list *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 /* just move the elements starting at index to the left */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 int result = cx_array_copy(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 &arl->data,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 &list->size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 &list->capacity,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 index,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 ((char *) arl->data) + (index + 1) * list->itemsize,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 list->itemsize,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 list->size - index,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 &arl->reallocator
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 );
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 if (result == 0) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 /* decrease the size */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 list->size--;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 return result;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 static void *cx_arl_at(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 struct cx_list_s const *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 size_t index
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 if (index < list->size) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 cx_array_list const *arl = (cx_array_list const *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 char *space = arl->data;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 return space + index * list->itemsize;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 } else {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 return NULL;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 static size_t cx_arl_find(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 struct cx_list_s const *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 void const *elem
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 char *cur = ((cx_array_list const *) list)->data;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 for (size_t i = 0; i < list->size; i++) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 if (0 == list->cmpfunc(elem, cur)) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 return i;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 cur += list->itemsize;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 return list->size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 static void cx_arl_sort(struct cx_list_s *list) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 qsort(((cx_array_list *) list)->data,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 list->size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 list->itemsize,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 list->cmpfunc
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 );
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 static int cx_arl_compare(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 struct cx_list_s const *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 struct cx_list_s const *other
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 static void cx_arl_reverse(struct cx_list_s *list) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 struct cx_list_s const *list = iter->src_handle;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 return iter->index < list->size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 static void *cx_arl_iter_current(struct cx_iterator_s const *iter) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 return iter->elem_handle;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 static void cx_arl_iter_next(struct cx_iterator_s *iter) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 if (iter->remove) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 iter->remove = false;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 cx_arl_remove(iter->src_handle, iter->index);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 } else {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 iter->index++;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 iter->elem_handle = cx_arl_at(iter->src_handle, iter->index);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 static struct cx_iterator_s cx_arl_iterator(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 struct cx_list_s *list,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 size_t index
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 struct cx_iterator_s iter;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 iter.index = index;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 iter.src_handle = list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 iter.elem_handle = cx_arl_at(list, index);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 iter.valid = cx_arl_iter_valid;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 iter.current = cx_arl_iter_current;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 iter.next = cx_arl_iter_next;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 iter.remove = false;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 return iter;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 static cx_list_class cx_array_list_class = {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 cx_arl_destructor,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 cx_arl_add,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 cx_arl_insert,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 cx_arl_insert_iter,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 cx_arl_remove,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 cx_arl_at,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 cx_arl_find,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 cx_arl_sort,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 cx_arl_compare,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 cx_arl_reverse,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 cx_arl_iterator,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 };
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 CxList *cxArrayListCreate(
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 CxAllocator const *allocator,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 CxListComparator comparator,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 size_t item_size,
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 size_t initial_capacity
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 ) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 if (list == NULL) return NULL;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 list->data = cxCalloc(allocator, initial_capacity, item_size);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 if (list->data == NULL) {
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 cxFree(allocator, list);
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 return NULL;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 }
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 list->base.cl = &cx_array_list_class;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 list->base.allocator = allocator;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 list->base.cmpfunc = comparator;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 list->base.itemsize = item_size;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 list->base.capacity = initial_capacity;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 /* configure the reallocator */
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 list->reallocator.realloc = cx_arl_realloc;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 list->reallocator.ptr1 = (void *) allocator;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 return (CxList *) list;
1260fad21be7 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 }

mercurial