src/array_list.c

Tue, 17 Sep 2024 19:08:22 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 17 Sep 2024 19:08:22 +0200
changeset 882
f8ca6e6c0d48
parent 881
1dbbf8c1c42f
child 883
3012e9b4214e
permissions
-rw-r--r--

add missing documentation for cx_list_default_swap() return value

606
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/array_list.h"
763
741a2040fa33 make cx_cmp_ptr default comparator for pointer lists - relates to #340
Mike Becker <universe@uap-core.de>
parents: 735
diff changeset
30 #include "cx/compare.h"
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
31 #include <assert.h>
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
32 #include <string.h>
606
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
817
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
34 // Default array reallocator
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
35
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
36 static void *cx_array_default_realloc(
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
37 void *array,
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
38 size_t capacity,
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
39 size_t elem_size,
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
40 __attribute__((__unused__)) struct cx_array_reallocator_s *alloc
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
41 ) {
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
42 return realloc(array, capacity * elem_size);
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
43 }
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
44
818
2be8fe3d5a2d add cx_array_add() + fix type of cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 817
diff changeset
45 struct cx_array_reallocator_s cx_array_default_reallocator_impl = {
817
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
46 cx_array_default_realloc, NULL, NULL, 0, 0
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
47 };
949908c97474 add cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 807
diff changeset
48
818
2be8fe3d5a2d add cx_array_add() + fix type of cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 817
diff changeset
49 struct cx_array_reallocator_s *cx_array_default_reallocator = &cx_array_default_reallocator_impl;
2be8fe3d5a2d add cx_array_add() + fix type of cx_array_default_reallocator
Mike Becker <universe@uap-core.de>
parents: 817
diff changeset
50
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
51 // LOW LEVEL ARRAY LIST FUNCTIONS
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
52
819
5da2ead43077 rename cx_array_copy_result to just cx_array_result
Mike Becker <universe@uap-core.de>
parents: 818
diff changeset
53 enum cx_array_result cx_array_copy(
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
54 void **target,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
55 size_t *size,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
56 size_t *capacity,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
57 size_t index,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
58 void const *src,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
59 size_t elem_size,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
60 size_t elem_count,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
61 struct cx_array_reallocator_s *reallocator
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
62 ) {
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
63 // assert pointers
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
64 assert(target != NULL);
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
65 assert(size != NULL);
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
66 assert(src != NULL);
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
67
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
68 // determine capacity
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
69 size_t cap = capacity == NULL ? *size : *capacity;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
70
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
71 // check if resize is required
627
cc8cbabd27cd fix cx_array_copy() unintentionally shrinking the array
Mike Becker <universe@uap-core.de>
parents: 626
diff changeset
72 size_t minsize = index + elem_count;
cc8cbabd27cd fix cx_array_copy() unintentionally shrinking the array
Mike Becker <universe@uap-core.de>
parents: 626
diff changeset
73 size_t newsize = *size < minsize ? minsize : *size;
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
74 bool needrealloc = newsize > cap;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
75
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
76 // reallocate if possible
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
77 if (needrealloc) {
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
78 // a reallocator and a capacity variable must be available
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
79 if (reallocator == NULL || capacity == NULL) {
819
5da2ead43077 rename cx_array_copy_result to just cx_array_result
Mike Becker <universe@uap-core.de>
parents: 818
diff changeset
80 return CX_ARRAY_REALLOC_NOT_SUPPORTED;
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
81 }
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
82
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
83 // check, if we need to repair the src pointer
611
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
84 uintptr_t targetaddr = (uintptr_t) *target;
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
85 uintptr_t srcaddr = (uintptr_t) src;
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
86 bool repairsrc = targetaddr <= srcaddr
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
87 && srcaddr < targetaddr + cap * elem_size;
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
88
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
89 // calculate new capacity (next number divisible by 16)
625
a4c4a50c067a fix calculation of new capacity in cx_array_copy()
Mike Becker <universe@uap-core.de>
parents: 624
diff changeset
90 cap = newsize - (newsize % 16) + 16;
a4c4a50c067a fix calculation of new capacity in cx_array_copy()
Mike Becker <universe@uap-core.de>
parents: 624
diff changeset
91 assert(cap > newsize);
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
92
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
93 // perform reallocation
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
94 void *newmem = reallocator->realloc(
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
95 *target, cap, elem_size, reallocator
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
96 );
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
97 if (newmem == NULL) {
819
5da2ead43077 rename cx_array_copy_result to just cx_array_result
Mike Becker <universe@uap-core.de>
parents: 818
diff changeset
98 return CX_ARRAY_REALLOC_FAILED;
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
99 }
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
100
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
101 // repair src pointer, if necessary
611
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
102 if (repairsrc) {
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
103 src = ((char *) newmem) + (srcaddr - targetaddr);
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
104 }
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
105
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
106 // store new pointer and capacity
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
107 *target = newmem;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
108 *capacity = cap;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
109 }
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
110
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
111 // determine target pointer
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
112 char *start = *target;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
113 start += index * elem_size;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
114
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
115 // copy elements and set new size
611
77efa5163ae5 #219 array list: implement insert
Mike Becker <universe@uap-core.de>
parents: 610
diff changeset
116 memmove(start, src, elem_count * elem_size);
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
117 *size = newsize;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
118
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
119 // return successfully
819
5da2ead43077 rename cx_array_copy_result to just cx_array_result
Mike Becker <universe@uap-core.de>
parents: 818
diff changeset
120 return CX_ARRAY_SUCCESS;
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
121 }
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
122
643
5700ba9154ab #228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
123 #ifndef CX_ARRAY_SWAP_SBO_SIZE
735
b686d0c98c62 unify the list swap SBO sizes
Mike Becker <universe@uap-core.de>
parents: 708
diff changeset
124 #define CX_ARRAY_SWAP_SBO_SIZE 128
643
5700ba9154ab #228 make buffer sizes adjustable at compile time
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
125 #endif
807
c8d692131b1e remove flags to disable SBO in tests - fix #343 fix #358
Mike Becker <universe@uap-core.de>
parents: 804
diff changeset
126 unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE;
804
5136f2fc32ec add CX_DISABLE_ARRAY_LIST_SWAP_SBO flag
Mike Becker <universe@uap-core.de>
parents: 764
diff changeset
127
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
128 void cx_array_swap(
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
129 void *arr,
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
130 size_t elem_size,
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
131 size_t idx1,
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
132 size_t idx2
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
133 ) {
660
4738a9065907 add some asserts
Mike Becker <universe@uap-core.de>
parents: 655
diff changeset
134 assert(arr != NULL);
4738a9065907 add some asserts
Mike Becker <universe@uap-core.de>
parents: 655
diff changeset
135
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
136 // short circuit
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
137 if (idx1 == idx2) return;
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
138
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
139 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE];
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
140 void *tmp;
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
141
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
142 // decide if we can use the local buffer
807
c8d692131b1e remove flags to disable SBO in tests - fix #343 fix #358
Mike Becker <universe@uap-core.de>
parents: 804
diff changeset
143 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
144 tmp = malloc(elem_size);
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
145 // we don't want to enforce error handling
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
146 if (tmp == NULL) abort();
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
147 } else {
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
148 tmp = sbo_mem;
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
149 }
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
150
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
151 // calculate memory locations
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
152 char *left = arr, *right = arr;
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
153 left += idx1 * elem_size;
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
154 right += idx2 * elem_size;
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
155
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
156 // three-way swap
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
157 memcpy(tmp, left, elem_size);
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
158 memcpy(left, right, elem_size);
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
159 memcpy(right, tmp, elem_size);
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
160
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
161 // free dynamic memory, if it was needed
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
162 if (tmp != sbo_mem) {
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
163 free(tmp);
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
164 }
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
165 }
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
166
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
167 // HIGH LEVEL ARRAY LIST FUNCTIONS
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
168
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
169 typedef struct {
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
170 struct cx_list_s base;
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
171 void *data;
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
172 size_t capacity;
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
173 struct cx_array_reallocator_s reallocator;
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
174 } cx_array_list;
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
175
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
176 static void *cx_arl_realloc(
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
177 void *array,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
178 size_t capacity,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
179 size_t elem_size,
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
180 struct cx_array_reallocator_s *alloc
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
181 ) {
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
182 // retrieve the pointer to the list allocator
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
183 CxAllocator const *al = alloc->ptr1;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
184
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
185 // use the list allocator to reallocate the memory
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
186 return cxRealloc(al, array, capacity * elem_size);
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
187 }
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
188
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
189 static void cx_arl_destructor(struct cx_list_s *list) {
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
190 cx_array_list *arl = (cx_array_list *) list;
708
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
191
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
192 char *ptr = arl->data;
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
193
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
194 if (list->collection.simple_destructor) {
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
195 for (size_t i = 0; i < list->collection.size; i++) {
708
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
196 cx_invoke_simple_destructor(list, ptr);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
197 ptr += list->collection.elem_size;
708
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
198 }
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
199 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
200 if (list->collection.advanced_destructor) {
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
201 for (size_t i = 0; i < list->collection.size; i++) {
708
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
202 cx_invoke_advanced_destructor(list, ptr);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
203 ptr += list->collection.elem_size;
708
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
204 }
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
205 }
1caed6c9ba68 fix inconsistent destructor requirements for list and map classes
Mike Becker <universe@uap-core.de>
parents: 699
diff changeset
206
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
207 cxFree(list->collection.allocator, arl->data);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
208 cxFree(list->collection.allocator, list);
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
209 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
210
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
211 static size_t cx_arl_insert_array(
629
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
212 struct cx_list_s *list,
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
213 size_t index,
629
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
214 void const *array,
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
215 size_t n
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
216 ) {
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
217 // out of bounds and special case check
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
218 if (index > list->collection.size || n == 0) return 0;
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
219
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
220 // get a correctly typed pointer to the list
629
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
221 cx_array_list *arl = (cx_array_list *) list;
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
222
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
223 // do we need to move some elements?
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
224 if (index < list->collection.size) {
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
225 char const *first_to_move = (char const *) arl->data;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
226 first_to_move += index * list->collection.elem_size;
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
227 size_t elems_to_move = list->collection.size - index;
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
228 size_t start_of_moved = index + n;
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
229
819
5da2ead43077 rename cx_array_copy_result to just cx_array_result
Mike Becker <universe@uap-core.de>
parents: 818
diff changeset
230 if (CX_ARRAY_SUCCESS != cx_array_copy(
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
231 &arl->data,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
232 &list->collection.size,
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
233 &arl->capacity,
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
234 start_of_moved,
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
235 first_to_move,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
236 list->collection.elem_size,
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
237 elems_to_move,
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
238 &arl->reallocator
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
239 )) {
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
240 // if moving existing elems is unsuccessful, abort
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
241 return 0;
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
242 }
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
243 }
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
244
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
245 // note that if we had to move the elements, the following operation
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
246 // is guaranteed to succeed, because we have the memory already allocated
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
247 // therefore, it is impossible to leave this function with an invalid array
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
248
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
249 // place the new elements
819
5da2ead43077 rename cx_array_copy_result to just cx_array_result
Mike Becker <universe@uap-core.de>
parents: 818
diff changeset
250 if (CX_ARRAY_SUCCESS == cx_array_copy(
629
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
251 &arl->data,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
252 &list->collection.size,
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
253 &arl->capacity,
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
254 index,
629
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
255 array,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
256 list->collection.elem_size,
629
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
257 n,
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
258 &arl->reallocator
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
259 )) {
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
260 return n;
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
261 } else {
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
262 // array list implementation is "all or nothing"
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
263 return 0;
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
264 }
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
265 }
6c81ee4f11ad #224 add cxListAddArray()
Mike Becker <universe@uap-core.de>
parents: 628
diff changeset
266
881
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
267 static size_t cx_arl_insert_sorted(
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
268 struct cx_list_s *list,
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
269 void const *sorted_data,
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
270 size_t n
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
271 ) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
272 // corner case
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
273 if (n == 0) return 0;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
274
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
275 // define some handy pointers
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
276 cx_array_list *arl = (cx_array_list *) list;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
277 cx_compare_func cmp = list->collection.cmpfunc;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
278
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
279 // store some counts
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
280 size_t old_size = list->collection.size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
281 size_t capacity = arl->capacity;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
282 size_t needed_capacity = old_size + n;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
283 size_t elem_size = list->collection.elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
284
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
285 // if we need more than we have, try a reallocation
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
286 if (needed_capacity > capacity) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
287 capacity = needed_capacity - (needed_capacity % 16) + 16;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
288 if (0 != cxReallocate(list->collection.allocator,
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
289 &(arl->data),
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
290 capacity * elem_size)) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
291 // give it up right away, there is no contract
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
292 // that requires us to insert as much as we can
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
293 return 0;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
294 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
295 arl->capacity = capacity;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
296 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
297
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
298 // now we have guaranteed that we can insert everything
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
299 size_t new_size = list->collection.size + n;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
300 list->collection.size = new_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
301
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
302 // declare the source and destination indices/pointers
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
303 size_t si = 0, di = 0;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
304 char const *src = sorted_data;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
305 char *dest = arl->data;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
306
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
307 // find the first insertion point
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
308 while (di < old_size) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
309 if (cmp(src, dest) < 0) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
310 break;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
311 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
312 di++;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
313 dest += elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
314 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
315
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
316 // move the remaining elements in the array completely to the right
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
317 // we will call it the "buffer" for parked elements
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
318 size_t buf_size = old_size - di;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
319 size_t bi = new_size - buf_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
320 char *bptr = ((char *) arl->data) + bi * elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
321 memmove(bptr, dest, buf_size * elem_size);
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
322
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
323 // while there are both source and buffered elements left,
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
324 // copy them interleaving
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
325 while (si < n && bi < new_size) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
326 // determine how many source elements can be inserted
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
327 size_t copy_len = 1;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
328 si++;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
329 char const *next_src = src + elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
330 while (si < n) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
331 if (cmp(bptr, next_src) < 0) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
332 break;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
333 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
334 copy_len++;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
335 si++;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
336 next_src += elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
337 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
338
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
339 // copy the source elements
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
340 memcpy(dest, src, copy_len * elem_size);
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
341 dest += copy_len * elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
342 src = next_src;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
343
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
344 // determine how many buffered elements need to be restored
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
345 copy_len = 1;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
346 bi++;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
347 char *next_bptr = bptr + elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
348 while (bi < new_size) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
349 if (cmp(src, next_bptr) < 0) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
350 break;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
351 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
352 copy_len++;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
353 bi++;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
354 next_bptr += elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
355 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
356
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
357 // restore the buffered elements
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
358 memmove(dest, bptr, copy_len * elem_size);
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
359 dest += copy_len * elem_size;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
360 bptr = next_bptr;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
361 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
362
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
363 // still source elements left? simply append them
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
364 if (si < n) {
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
365 memcpy(dest, src, elem_size * (n - si));
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
366 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
367
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
368 // still buffer elements left?
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
369 // don't worry, we already moved them to the correct place
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
370
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
371 return n;
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
372 }
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
373
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
374 static int cx_arl_insert_element(
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
375 struct cx_list_s *list,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
376 size_t index,
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
377 void const *element
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
378 ) {
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
379 return 1 != cx_arl_insert_array(list, index, element, 1);
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
380 }
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
381
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
382 static int cx_arl_insert_iter(
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
383 struct cx_iterator_s *iter,
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
384 void const *elem,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
385 int prepend
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
386 ) {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
387 struct cx_list_s *list = iter->src_handle.m;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
388 if (iter->index < list->collection.size) {
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
389 int result = cx_arl_insert_element(
619
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
390 list,
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
391 iter->index + 1 - prepend,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
392 elem
619
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
393 );
874
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
394 if (result == 0) {
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
395 iter->elem_count++;
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
396 if (prepend != 0) {
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
397 iter->index++;
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
398 iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size;
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
399 }
619
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
400 }
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
401 return result;
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
402 } else {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
403 int result = cx_arl_insert_element(list, list->collection.size, elem);
874
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
404 if (result == 0) {
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
405 iter->elem_count++;
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
406 iter->index = list->collection.size;
cdce47f34d48 fix inserting via iterator correctly increases element count
Mike Becker <universe@uap-core.de>
parents: 856
diff changeset
407 }
619
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
408 return result;
5e58187ac707 #219 array list: implement insert via iterator
Mike Becker <universe@uap-core.de>
parents: 616
diff changeset
409 }
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
410 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
411
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
412 static int cx_arl_remove(
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
413 struct cx_list_s *list,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
414 size_t index
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
415 ) {
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
416 cx_array_list *arl = (cx_array_list *) list;
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
417
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
418 // out-of-bounds check
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
419 if (index >= list->collection.size) {
613
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
420 return 1;
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
421 }
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
422
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
423 // content destruction
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
424 cx_invoke_destructor(list, ((char *) arl->data) + index * list->collection.elem_size);
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
425
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
426 // short-circuit removal of last element
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
427 if (index == list->collection.size - 1) {
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
428 list->collection.size--;
624
b0bdff7d8203 #219: cx_arl_remove short-circuit for last element
Mike Becker <universe@uap-core.de>
parents: 623
diff changeset
429 return 0;
b0bdff7d8203 #219: cx_arl_remove short-circuit for last element
Mike Becker <universe@uap-core.de>
parents: 623
diff changeset
430 }
613
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
431
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
432 // just move the elements starting at index to the left
613
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
433 int result = cx_array_copy(
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
434 &arl->data,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
435 &list->collection.size,
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
436 &arl->capacity,
613
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
437 index,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
438 ((char *) arl->data) + (index + 1) * list->collection.elem_size,
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
439 list->collection.elem_size,
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
440 list->collection.size - index - 1,
613
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
441 &arl->reallocator
85c08391a090 #219 array list: implement remove
Mike Becker <universe@uap-core.de>
parents: 612
diff changeset
442 );
820
8b86ee2e09bb remove check that is always true in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents: 819
diff changeset
443
8b86ee2e09bb remove check that is always true in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents: 819
diff changeset
444 // cx_array_copy cannot fail, array cannot grow
8b86ee2e09bb remove check that is always true in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents: 819
diff changeset
445 assert(result == 0);
8b86ee2e09bb remove check that is always true in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents: 819
diff changeset
446
8b86ee2e09bb remove check that is always true in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents: 819
diff changeset
447 // decrease the size
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
448 list->collection.size--;
820
8b86ee2e09bb remove check that is always true in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents: 819
diff changeset
449
8b86ee2e09bb remove check that is always true in cx_arl_remove()
Mike Becker <universe@uap-core.de>
parents: 819
diff changeset
450 return 0;
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
451 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
452
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
453 static void cx_arl_clear(struct cx_list_s *list) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
454 if (list->collection.size == 0) return;
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
455
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
456 cx_array_list *arl = (cx_array_list *) list;
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
457 char *ptr = arl->data;
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
458
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
459 if (list->collection.simple_destructor) {
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
460 for (size_t i = 0; i < list->collection.size; i++) {
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
461 cx_invoke_simple_destructor(list, ptr);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
462 ptr += list->collection.elem_size;
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
463 }
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
464 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
465 if (list->collection.advanced_destructor) {
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
466 for (size_t i = 0; i < list->collection.size; i++) {
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
467 cx_invoke_advanced_destructor(list, ptr);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
468 ptr += list->collection.elem_size;
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
469 }
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
470 }
666
b5dd654deb3b add unit test for cxListClear + fix destructor functions not always invoked with the correct pointer
Mike Becker <universe@uap-core.de>
parents: 664
diff changeset
471
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
472 memset(arl->data, 0, list->collection.size * list->collection.elem_size);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
473 list->collection.size = 0;
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
474 }
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
475
647
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
476 static int cx_arl_swap(
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
477 struct cx_list_s *list,
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
478 size_t i,
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
479 size_t j
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
480 ) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
481 if (i >= list->collection.size || j >= list->collection.size) return 1;
647
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
482 cx_array_list *arl = (cx_array_list *) list;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
483 cx_array_swap(arl->data, list->collection.elem_size, i, j);
647
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
484 return 0;
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
485 }
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
486
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
487 static void *cx_arl_at(
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
488 struct cx_list_s const *list,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
489 size_t index
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
490 ) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
491 if (index < list->collection.size) {
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
492 cx_array_list const *arl = (cx_array_list const *) list;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
493 char *space = arl->data;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
494 return space + index * list->collection.elem_size;
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
495 } else {
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
496 return NULL;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
497 }
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
498 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
499
764
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
500 static ssize_t cx_arl_find_remove(
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
501 struct cx_list_s *list,
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
502 void const *elem,
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
503 bool remove
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
504 ) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
505 assert(list->collection.cmpfunc != NULL);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
506 assert(list->collection.size < SIZE_MAX / 2);
614
7aaec630cf15 #219 array list: implement find
Mike Becker <universe@uap-core.de>
parents: 613
diff changeset
507 char *cur = ((cx_array_list const *) list)->data;
7aaec630cf15 #219 array list: implement find
Mike Becker <universe@uap-core.de>
parents: 613
diff changeset
508
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
509 for (ssize_t i = 0; i < (ssize_t) list->collection.size; i++) {
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
510 if (0 == list->collection.cmpfunc(elem, cur)) {
764
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
511 if (remove) {
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
512 if (0 == cx_arl_remove(list, i)) {
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
513 return i;
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
514 } else {
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
515 return -1;
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
516 }
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
517 } else {
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
518 return i;
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
519 }
614
7aaec630cf15 #219 array list: implement find
Mike Becker <universe@uap-core.de>
parents: 613
diff changeset
520 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
521 cur += list->collection.elem_size;
614
7aaec630cf15 #219 array list: implement find
Mike Becker <universe@uap-core.de>
parents: 613
diff changeset
522 }
7aaec630cf15 #219 array list: implement find
Mike Becker <universe@uap-core.de>
parents: 613
diff changeset
523
699
35b2b99ee523 make list find return a negative value when elem not found
Mike Becker <universe@uap-core.de>
parents: 678
diff changeset
524 return -1;
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
525 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
526
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
527 static void cx_arl_sort(struct cx_list_s *list) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
528 assert(list->collection.cmpfunc != NULL);
615
b52b66dcd44b #219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents: 614
diff changeset
529 qsort(((cx_array_list *) list)->data,
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
530 list->collection.size,
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
531 list->collection.elem_size,
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
532 list->collection.cmpfunc
615
b52b66dcd44b #219 array list: implement sort
Mike Becker <universe@uap-core.de>
parents: 614
diff changeset
533 );
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
534 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
535
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
536 static int cx_arl_compare(
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
537 struct cx_list_s const *list,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
538 struct cx_list_s const *other
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
539 ) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
540 assert(list->collection.cmpfunc != NULL);
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
541 if (list->collection.size == other->collection.size) {
622
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
542 char const *left = ((cx_array_list const *) list)->data;
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
543 char const *right = ((cx_array_list const *) other)->data;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
544 for (size_t i = 0; i < list->collection.size; i++) {
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
545 int d = list->collection.cmpfunc(left, right);
622
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
546 if (d != 0) {
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
547 return d;
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
548 }
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
549 left += list->collection.elem_size;
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
550 right += other->collection.elem_size;
622
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
551 }
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
552 return 0;
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
553 } else {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
554 return list->collection.size < other->collection.size ? -1 : 1;
622
3d93cd78aa20 #219 array list: implement compare member func
Mike Becker <universe@uap-core.de>
parents: 620
diff changeset
555 }
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
556 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
557
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
558 static void cx_arl_reverse(struct cx_list_s *list) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
559 if (list->collection.size < 2) return;
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
560 void *data = ((cx_array_list const *) list)->data;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
561 size_t half = list->collection.size / 2;
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
562 for (size_t i = 0; i < half; i++) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
563 cx_array_swap(data, list->collection.elem_size, i, list->collection.size - 1 - i);
623
21082350a590 #219 array list: implement reverse
Mike Becker <universe@uap-core.de>
parents: 622
diff changeset
564 }
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
565 }
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
566
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
567 static bool cx_arl_iter_valid(void const *it) {
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
568 struct cx_iterator_s const *iter = it;
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
569 struct cx_list_s const *list = iter->src_handle.c;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
570 return iter->index < list->collection.size;
616
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
571 }
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
572
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
573 static void *cx_arl_iter_current(void const *it) {
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
574 struct cx_iterator_s const *iter = it;
616
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
575 return iter->elem_handle;
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
576 }
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
577
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
578 static void cx_arl_iter_next(void *it) {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
579 struct cx_iterator_s *iter = it;
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
580 if (iter->base.remove) {
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
581 iter->base.remove = false;
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
582 cx_arl_remove(iter->src_handle.m, iter->index);
616
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
583 } else {
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
584 iter->index++;
620
f220695aded6 #219 improve cx_arl_iter_next
Mike Becker <universe@uap-core.de>
parents: 619
diff changeset
585 iter->elem_handle =
f220695aded6 #219 improve cx_arl_iter_next
Mike Becker <universe@uap-core.de>
parents: 619
diff changeset
586 ((char *) iter->elem_handle)
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
587 + ((struct cx_list_s const *) iter->src_handle.c)->collection.elem_size;
616
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
588 }
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
589 }
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
590
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
591 static void cx_arl_iter_prev(void *it) {
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
592 struct cx_iterator_s *iter = it;
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
593 cx_array_list const* list = iter->src_handle.c;
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
594 if (iter->base.remove) {
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
595 iter->base.remove = false;
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
596 cx_arl_remove(iter->src_handle.m, iter->index);
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
597 }
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
598 iter->index--;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
599 if (iter->index < list->base.collection.size) {
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
600 iter->elem_handle = ((char *) list->data)
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
601 + iter->index * list->base.collection.elem_size;
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
602 }
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
603 }
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
604
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
605
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
606 static struct cx_iterator_s cx_arl_iterator(
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
607 struct cx_list_s const *list,
655
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
608 size_t index,
7340c4255f1f implement backwards iterator - fixes #238
Mike Becker <universe@uap-core.de>
parents: 654
diff changeset
609 bool backwards
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
610 ) {
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
611 struct cx_iterator_s iter;
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
612
616
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
613 iter.index = index;
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
614 iter.src_handle.c = list;
616
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
615 iter.elem_handle = cx_arl_at(list, index);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
616 iter.elem_size = list->collection.elem_size;
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
617 iter.elem_count = list->collection.size;
854
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
618 iter.base.valid = cx_arl_iter_valid;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
619 iter.base.current = cx_arl_iter_current;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
620 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
621 iter.base.remove = false;
fe0d69d72bcd fix members inherited by macro or include are not documented
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
622 iter.base.mutating = false;
630
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
623
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
624 return iter;
ac5e7f789048 separate iterators and mutating iterators
Mike Becker <universe@uap-core.de>
parents: 629
diff changeset
625 }
616
af7d8a29fbc5 #219 array list: add iterator
Mike Becker <universe@uap-core.de>
parents: 615
diff changeset
626
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
627 static cx_list_class cx_array_list_class = {
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
628 cx_arl_destructor,
641
d402fead3386 add new pointer list wrapper - resolves #234
Mike Becker <universe@uap-core.de>
parents: 640
diff changeset
629 cx_arl_insert_element,
638
eafb45eefc51 add cxListInsertArray() - fixes #224
Mike Becker <universe@uap-core.de>
parents: 630
diff changeset
630 cx_arl_insert_array,
881
1dbbf8c1c42f add optimized implementation of insert_sorted for array lists
Mike Becker <universe@uap-core.de>
parents: 876
diff changeset
631 cx_arl_insert_sorted,
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
632 cx_arl_insert_iter,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
633 cx_arl_remove,
664
af5bf4603a5d add cxListClear and fix missing destructor invocations - #241 #246
Mike Becker <universe@uap-core.de>
parents: 662
diff changeset
634 cx_arl_clear,
647
2e6e9d9f2159 implement swap function for list elements - fixes #218
Mike Becker <universe@uap-core.de>
parents: 643
diff changeset
635 cx_arl_swap,
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
636 cx_arl_at,
764
ccbdbd088455 add cxListFindRemove and cx_linked_list_find_node
Mike Becker <universe@uap-core.de>
parents: 763
diff changeset
637 cx_arl_find_remove,
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
638 cx_arl_sort,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
639 cx_arl_compare,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
640 cx_arl_reverse,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
641 cx_arl_iterator,
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
642 };
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
643
670
4ad8ea3aee49 allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents: 667
diff changeset
644 CxList *cxArrayListCreate(
606
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
645 CxAllocator const *allocator,
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
646 cx_compare_func comparator,
855
35bcb3216c0d fix inconsistent use of item_size and elem_size
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
647 size_t elem_size,
606
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
648 size_t initial_capacity
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
649 ) {
670
4ad8ea3aee49 allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents: 667
diff changeset
650 if (allocator == NULL) {
4ad8ea3aee49 allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents: 667
diff changeset
651 allocator = cxDefaultAllocator;
4ad8ea3aee49 allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents: 667
diff changeset
652 }
4ad8ea3aee49 allow NULL for allocator and comparator
Mike Becker <universe@uap-core.de>
parents: 667
diff changeset
653
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
654 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
655 if (list == NULL) return NULL;
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
656
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
657 list->base.cl = &cx_array_list_class;
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
658 list->base.collection.allocator = allocator;
677
b09aae58bba4 refactoring of collections to make use of destructors in map implementations
Mike Becker <universe@uap-core.de>
parents: 676
diff changeset
659 list->capacity = initial_capacity;
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
660
855
35bcb3216c0d fix inconsistent use of item_size and elem_size
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
661 if (elem_size > 0) {
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
662 list->base.collection.elem_size = elem_size;
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
663 list->base.collection.cmpfunc = comparator;
667
2f88a7c13a28 add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents: 666
diff changeset
664 } else {
855
35bcb3216c0d fix inconsistent use of item_size and elem_size
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
665 elem_size = sizeof(void *);
856
6bbbf219251d fix name of collection base member (to avoid base.base)
Mike Becker <universe@uap-core.de>
parents: 855
diff changeset
666 list->base.collection.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
667
2f88a7c13a28 add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents: 666
diff changeset
667 cxListStorePointers((CxList *) list);
2f88a7c13a28 add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents: 666
diff changeset
668 }
2f88a7c13a28 add CX_STORE_POINTERS special "item size" for lists
Mike Becker <universe@uap-core.de>
parents: 666
diff changeset
669
855
35bcb3216c0d fix inconsistent use of item_size and elem_size
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
670 // allocate the array after the real elem_size is known
35bcb3216c0d fix inconsistent use of item_size and elem_size
Mike Becker <universe@uap-core.de>
parents: 854
diff changeset
671 list->data = cxCalloc(allocator, initial_capacity, elem_size);
676
d0680a23d850 fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents: 670
diff changeset
672 if (list->data == NULL) {
d0680a23d850 fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents: 670
diff changeset
673 cxFree(allocator, list);
d0680a23d850 fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents: 670
diff changeset
674 return NULL;
d0680a23d850 fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents: 670
diff changeset
675 }
d0680a23d850 fix initial storage allocation for array lists created with CX_STORE_POINTERS
Mike Becker <universe@uap-core.de>
parents: 670
diff changeset
676
628
1e2be40f0cb5 use //-style single line comments everywhere
Mike Becker <universe@uap-core.de>
parents: 627
diff changeset
677 // configure the reallocator
610
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
678 list->reallocator.realloc = cx_arl_realloc;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
679 list->reallocator.ptr1 = (void *) allocator;
de5d3ee6435f #219 array list: implement add and at
Mike Becker <universe@uap-core.de>
parents: 607
diff changeset
680
607
2d99e978dc34 implement array list ctor and dtor
Mike Becker <universe@uap-core.de>
parents: 606
diff changeset
681 return (CxList *) list;
606
314e9288af2f add array list tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
682 }

mercurial