Sat, 10 Aug 2019 08:44:36 +0200
adds a broader set of compare and distance functions
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
1 | /* |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
3 | * |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
4 | * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
27 | */ |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
93
diff
changeset
|
28 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
29 | #define _GNU_SOURCE /* we want to use qsort_r(), if available */ |
348
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
30 | #define __STDC_WANT_LIB_EXT1__ 1 /* use qsort_s, if available */ |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
31 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
32 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
33 | #include "ucx/array.h" |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
34 | #include "ucx/utils.h" |
4 | 35 | |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
36 | #include <string.h> |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
37 | #include <stdlib.h> |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
38 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
39 | #ifndef UCX_ARRAY_DISABLE_QSORT |
346
1a9c112f4116
fixes #ifdefs to be sure no redefine can ever happen
Mike Becker <universe@uap-core.de>
parents:
345
diff
changeset
|
40 | #ifdef __GLIBC__ |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
41 | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8) |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
42 | #define ucx_array_sort_impl qsort_r |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
43 | #endif /* glibc version >= 2.8 */ |
346
1a9c112f4116
fixes #ifdefs to be sure no redefine can ever happen
Mike Becker <universe@uap-core.de>
parents:
345
diff
changeset
|
44 | #elif /* not __GLIBC__ */ defined(__APPLE__) || defined(__FreeBSD__) |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
45 | #define ucx_array_sort_impl ucx_qsort_r |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
46 | #define USE_UCX_QSORT_R |
348
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
47 | #elif /* not (__APPLE || __FreeBSD__) */ defined(__sun) |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
48 | #if __STDC_VERSION__ >= 201112L |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
49 | #define ucx_array_sort_impl qsort_s |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
50 | #endif |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
51 | #endif /* __GLIBC__, __APLE__, __FreeBSD__, __sun */ |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
52 | #endif /* UCX_ARRAY_DISABLE_QSORT */ |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
53 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
54 | #ifndef ucx_array_sort_impl |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
55 | #define ucx_array_sort_impl ucx_mergesort |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
56 | #endif |
125
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
57 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
58 | UcxArray ucx_array_new(size_t capacity, size_t elemsize) { |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
59 | return ucx_array_new_a(capacity, elemsize, ucx_default_allocator()); |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
60 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
61 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
62 | UcxArray ucx_array_new_a(size_t capacity, size_t elemsize, |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
63 | UcxAllocator* allocator) { |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
64 | UcxArray array; |
18
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
65 | |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
66 | array.allocator = allocator; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
67 | array.elemsize = elemsize; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
68 | array.size = 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
69 | array.data = alcalloc(allocator, capacity, elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
70 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
71 | if (array.data) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
72 | array.capacity = capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
73 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
74 | array.capacity = 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
75 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
76 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
77 | return array; |
125
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
78 | } |
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
79 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
80 | UcxArray ucx_array_clone(UcxArray array) { |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
81 | UcxArray clone; |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
82 | |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
83 | clone.allocator = array.allocator; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
84 | clone.elemsize = array.elemsize; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
85 | clone.size = array.size; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
86 | clone.data = alcalloc(array.allocator, array.capacity, array.elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
87 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
88 | if (clone.data) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
89 | clone.capacity = array.capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
90 | memcpy(clone.data, array.data, array.size*array.elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
91 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
92 | clone.capacity = clone.size = 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
93 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
94 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
95 | return clone; |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
96 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
97 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
98 | int ucx_array_equals(UcxArray array1, UcxArray array2, |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
99 | cmp_func cmpfnc, void* data) { |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
100 | |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
101 | if (array1.size != array2.size || array1.elemsize != array2.elemsize) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
102 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
103 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
104 | if (array1.size == 0) |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
105 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
106 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
107 | if (cmpfnc == NULL) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
108 | cmpfnc = ucx_cmp_mem; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
109 | data = &array1.elemsize; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
110 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
111 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
112 | for (size_t i = 0 ; i < array1.size ; i++) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
113 | int r = cmpfnc( |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
114 | ucx_array_at(array1, i), |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
115 | ucx_array_at(array2, i), |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
116 | data); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
117 | if (r != 0) |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
118 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
119 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
120 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
121 | } |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
122 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
123 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
124 | void ucx_array_free(UcxArray *array) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
125 | alfree(array->allocator, array->data); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
126 | array->data = NULL; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
127 | array->capacity = array->size = 0; |
123
7fb0f74517c5
changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents:
122
diff
changeset
|
128 | } |
7fb0f74517c5
changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents:
122
diff
changeset
|
129 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
130 | int ucx_array_append(UcxArray *array, void *data) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
131 | if (array->size == array->capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
132 | if (ucx_array_reserve(array, array->capacity*2)) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
133 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
134 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
135 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
136 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
137 | void* dest = ucx_array_at(*array, array->size++); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
138 | if (data) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
139 | memcpy(dest, data, array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
140 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
141 | memset(dest, 0, array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
142 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
143 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
144 | return 0; |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
145 | } |
8 | 146 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
147 | int ucx_array_prepend(UcxArray *array, void *data) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
148 | if (array->size == array->capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
149 | if (ucx_array_reserve(array, array->capacity*2)) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
150 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
151 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
152 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
153 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
154 | array->size++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
155 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
156 | if (array->size > 1) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
157 | void *dest = ucx_array_at(*array,1); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
158 | memmove(dest, array->data, array->elemsize*array->size); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
159 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
160 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
161 | if (data) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
162 | memcpy(array->data, data, array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
163 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
164 | memset(array->data, 0, array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
165 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
166 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
167 | return 0; |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
168 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
169 | |
337 | 170 | int ucx_array_set(UcxArray *array, size_t index, void *data) { |
171 | if (index >= array->size) { | |
172 | if (ucx_array_reserve(array, index+1)) { | |
173 | return 1; | |
174 | } | |
175 | array->size = index+1; | |
176 | } | |
177 | ||
178 | void *dest = ucx_array_at(*array, index); | |
179 | if (data) { | |
180 | memcpy(dest, data, array->elemsize); | |
181 | } else { | |
182 | memset(dest, 0, array->elemsize); | |
183 | } | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
188 | int ucx_array_concat(UcxArray *array1, const UcxArray *array2) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
189 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
190 | if (array1->elemsize != array2->elemsize) |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
191 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
192 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
193 | size_t capacity = array1->capacity+array2->capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
194 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
195 | if (array1->capacity < capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
196 | if (ucx_array_reserve(array1, capacity)) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
197 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
198 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
199 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
200 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
201 | void* dest = ucx_array_at(*array1, array1->size); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
202 | memcpy(dest, array2->data, array2->size*array2->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
203 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
204 | array1->size += array2->size; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
205 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
206 | return 0; |
123
7fb0f74517c5
changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents:
122
diff
changeset
|
207 | } |
7fb0f74517c5
changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents:
122
diff
changeset
|
208 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
209 | void *ucx_array_at(UcxArray array, size_t index) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
210 | char* memory = array.data; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
211 | char* loc = memory + index*array.elemsize; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
212 | return loc; |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
213 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
214 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
215 | size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data) { |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
216 | |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
217 | if (cmpfnc == NULL) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
218 | cmpfnc = ucx_cmp_mem; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
219 | data = &array.elemsize; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
220 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
221 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
222 | if (array.size > 0) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
223 | for (size_t i = 0 ; i < array.size ; i++) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
224 | void* ptr = ucx_array_at(array, i); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
225 | if (cmpfnc(ptr, elem, data) == 0) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
226 | return i; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
227 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
228 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
229 | return array.size; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
230 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
231 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
232 | } |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
233 | } |
35
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
234 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
235 | int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data) { |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
236 | return ucx_array_find(array, elem, cmpfnc, data) != array.size; |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
237 | } |
35
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
238 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
239 | static void ucx_mergesort_merge(void *arrdata,size_t elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
240 | cmp_func cmpfnc, void *data, |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
241 | size_t start, size_t mid, size_t end) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
242 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
243 | char* array = arrdata; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
244 | |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
245 | size_t rightstart = mid + 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
246 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
247 | if (cmpfnc(array + mid*elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
248 | array + rightstart*elemsize, data) <= 0) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
249 | /* already sorted */ |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
250 | return; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
251 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
252 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
253 | /* we need memory for one element */ |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
254 | void *value = malloc(elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
255 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
256 | while (start <= mid && rightstart <= end) { |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
257 | if (cmpfnc(array + start*elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
258 | array + rightstart*elemsize, data) <= 0) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
259 | start++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
260 | } else { |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
261 | /* save the value from the right */ |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
262 | memcpy(value, array + rightstart*elemsize, elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
263 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
264 | /* shift all left elements one element to the right */ |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
265 | size_t shiftcount = rightstart-start; |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
266 | void *startptr = array + start*elemsize; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
267 | void *dest = array + (start+1)*elemsize; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
268 | memmove(dest, startptr, shiftcount*elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
269 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
270 | /* bring the first value from the right to the left */ |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
271 | memcpy(startptr, value, elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
272 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
273 | start++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
274 | mid++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
275 | rightstart++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
276 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
277 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
278 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
279 | /* free the temporary memory */ |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
280 | free(value); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
281 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
282 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
283 | static void ucx_mergesort_impl(void *arrdata, size_t elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
284 | cmp_func cmpfnc, void *data, size_t l, size_t r) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
285 | if (l < r) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
286 | size_t m = l + (r - l) / 2; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
287 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
288 | ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, l, m); |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
289 | ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, m + 1, r); |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
290 | ucx_mergesort_merge(arrdata, elemsize, cmpfnc, data, l, m, r); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
291 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
292 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
293 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
294 | static void ucx_mergesort(void *arrdata, size_t count, size_t elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
295 | cmp_func cmpfnc, void *data) { |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
296 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
297 | ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, 0, count-1); |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
298 | } |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
299 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
300 | #ifdef USE_UCX_QSORT_R |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
301 | struct cmpfnc_swapargs_info { |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
302 | cmp_func func; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
303 | void *data; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
304 | }; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
305 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
306 | static int cmp_func_swap_args(void *data, const void *x, const void *y) { |
348
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
307 | cmpfnc_swapargs_info* info = data; |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
308 | return info->func(x, y, info->data); |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
309 | } |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
310 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
311 | static void ucx_qsort_r(void *array, size_t count, size_t elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
312 | cmp_func cmpfnc, void *data) { |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
313 | struct cmpfnc_swapargs_info info; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
314 | info.func = cmpfnc; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
315 | info.data = data; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
316 | qsort_r(array, count, elemsize, &info, cmp_func_swap_args); |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
317 | } |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
318 | #endif /* USE_UCX_QSORT_R */ |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
319 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
320 | void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data) { |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
321 | ucx_array_sort_impl(array.data, array.size, array.elemsize, cmpfnc, data); |
35
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
322 | } |
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
323 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
324 | void ucx_array_remove(UcxArray *array, size_t index) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
325 | array->size--; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
326 | if (index < array->size) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
327 | void* dest = ucx_array_at(*array, index); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
328 | void* src = ucx_array_at(*array, index+1); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
329 | memmove(dest, src, (array->size - index)*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
330 | } |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
331 | } |
37
ec296899d12f
replaced qsort with natural merge sort
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
332 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
333 | void ucx_array_remove_fast(UcxArray *array, size_t index) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
334 | array->size--; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
335 | if (index < array->size) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
336 | void* dest = ucx_array_at(*array, index); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
337 | void* src = ucx_array_at(*array, array->size); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
338 | memcpy(dest, src, array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
339 | } |
35
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
340 | } |
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
341 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
342 | int ucx_array_shrink(UcxArray* array) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
343 | void* newptr = alrealloc(array->allocator, array->data, |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
344 | array->size*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
345 | if (newptr) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
346 | array->data = newptr; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
347 | array->capacity = array->size; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
348 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
349 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
350 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
351 | } |
13 | 352 | } |
22
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
353 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
354 | int ucx_array_resize(UcxArray* array, size_t capacity) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
355 | if (array->capacity >= capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
356 | void* newptr = alrealloc(array->allocator, array->data, |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
357 | capacity*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
358 | if (newptr) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
359 | array->data = newptr; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
360 | array->capacity = capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
361 | if (array->size > array->capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
362 | array->size = array->capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
363 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
364 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
365 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
366 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
367 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
368 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
369 | return ucx_array_reserve(array, capacity); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
370 | } |
125
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
371 | } |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
372 | |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
373 | int ucx_array_reserve(UcxArray* array, size_t capacity) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
374 | if (array->capacity > capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
375 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
376 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
377 | void* newptr = alrealloc(array->allocator, array->data, |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
378 | capacity*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
379 | if (newptr) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
380 | array->data = newptr; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
381 | array->capacity = capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
382 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
383 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
384 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
385 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
386 | } |
22
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
387 | } |