Sat, 05 Oct 2019 16:58:16 +0200
merges the UcxArray implementation
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> |
355
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
38 | #include <errno.h> |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
39 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
40 | #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
|
41 | #ifdef __GLIBC__ |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
42 | #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
|
43 | #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
|
44 | #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
|
45 | #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
|
46 | #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
|
47 | #define USE_UCX_QSORT_R |
348
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
48 | #elif /* not (__APPLE || __FreeBSD__) */ defined(__sun) |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
49 | #if __STDC_VERSION__ >= 201112L |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
50 | #define ucx_array_sort_impl qsort_s |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
51 | #endif |
3b9b4f6e9fd6
use qsort_s on solaris
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
347
diff
changeset
|
52 | #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
|
53 | #endif /* UCX_ARRAY_DISABLE_QSORT */ |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
54 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
55 | #ifndef ucx_array_sort_impl |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
56 | #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
|
57 | #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
|
58 | |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
59 | static int ucx_array_ensurecap(UcxArray *array, size_t reqcap) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
60 | size_t required_capacity = array->capacity; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
61 | while (reqcap > required_capacity) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
62 | if (required_capacity * 2 < required_capacity) |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
63 | return 1; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
64 | required_capacity <<= 1; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
65 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
66 | if (ucx_array_reserve(array, required_capacity)) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
67 | return 1; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
68 | } |
357
0f5732f0dc00
fixes two bugs: clone to uninitialized array and missing return in ucx_array_ensurecap()
Mike Becker <universe@uap-core.de>
parents:
356
diff
changeset
|
69 | return 0; |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
70 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
71 | |
355
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
72 | int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
73 | size_t elmsize, size_t index, ...) { |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
74 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
75 | if(!alloc || !capacity || !array) { |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
76 | errno = EINVAL; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
77 | return 1; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
78 | } |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
79 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
80 | size_t newcapacity = *capacity; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
81 | while(index >= newcapacity) { |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
82 | if(ucx_szmul(newcapacity, 2, &newcapacity)) { |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
83 | errno = EOVERFLOW; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
84 | return 1; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
85 | } |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
86 | } |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
87 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
88 | size_t memlen, offset; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
89 | if(ucx_szmul(newcapacity, elmsize, &memlen)) { |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
90 | errno = EOVERFLOW; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
91 | return 1; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
92 | } |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
93 | /* we don't need to check index*elmsize - it is smaller than memlen */ |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
94 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
95 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
96 | void* newptr = alrealloc(alloc, *array, memlen); |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
97 | if(newptr == NULL) { |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
98 | errno = ENOMEM; /* we cannot assume that every allocator sets this */ |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
99 | return 1; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
100 | } |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
101 | *array = newptr; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
102 | *capacity = newcapacity; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
103 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
104 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
105 | char* dest = *array; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
106 | dest += elmsize*index; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
107 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
108 | va_list ap; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
109 | va_start(ap, index); |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
110 | int elem = va_arg(ap, int); |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
111 | memcpy(dest, &elem, elmsize); |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
112 | va_end(ap); |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
113 | |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
114 | return 0; |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
115 | } |
d315a068235a
adds array utility functions for user defined arrays
Mike Becker <universe@uap-core.de>
parents:
354
diff
changeset
|
116 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
117 | UcxArray* ucx_array_new(size_t capacity, size_t elemsize) { |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
118 | 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
|
119 | } |
69636f81db31
added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
120 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
121 | UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize, |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
122 | UcxAllocator* allocator) { |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
123 | UcxArray* array = almalloc(allocator, sizeof(UcxArray)); |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
124 | if(array) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
125 | ucx_array_init_a(array, capacity, elemsize, allocator); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
126 | } |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
127 | 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
|
128 | } |
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
129 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
130 | void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
131 | ucx_array_init_a(array, capacity, elemsize, ucx_default_allocator()); |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
132 | } |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
133 | |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
134 | void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize, |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
135 | UcxAllocator* allocator) { |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
136 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
137 | array->allocator = allocator; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
138 | array->elemsize = elemsize; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
139 | array->size = 0; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
140 | array->data = alcalloc(allocator, capacity, elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
141 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
142 | if (array->data) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
143 | array->capacity = capacity; |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
144 | } else { |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
145 | array->capacity = 0; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
146 | } |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
147 | } |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
148 | |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
149 | int ucx_array_clone(UcxArray* dest, UcxArray const* src) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
150 | if (ucx_array_ensurecap(dest, src->capacity)) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
151 | return 1; |
336
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 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
154 | dest->elemsize = src->elemsize; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
155 | dest->size = src->size; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
156 | |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
157 | if (dest->data) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
158 | memcpy(dest->data, src->data, src->size*src->elemsize); |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
159 | } |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
160 | |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
161 | return 0; |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
162 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
163 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
164 | int ucx_array_equals(UcxArray const *array1, UcxArray const *array2, |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
165 | 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
|
166 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
167 | if (array1->size != array2->size || array1->elemsize != array2->elemsize) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
168 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
169 | } else { |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
170 | if (array1->size == 0) |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
171 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
172 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
173 | size_t elemsize; |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
174 | if (cmpfnc == NULL) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
175 | cmpfnc = ucx_cmp_mem; |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
176 | elemsize = array1->elemsize; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
177 | data = &elemsize; |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
178 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
179 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
180 | for (size_t i = 0 ; i < array1->size ; i++) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
181 | int r = cmpfnc( |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
182 | ucx_array_at(array1, i), |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
183 | ucx_array_at(array2, i), |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
184 | data); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
185 | if (r != 0) |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
186 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
187 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
188 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
189 | } |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
190 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
191 | |
353
135ce35d5108
renames ucx_array_free() to ucx_array_destroy()
Mike Becker <universe@uap-core.de>
parents:
348
diff
changeset
|
192 | void ucx_array_destroy(UcxArray *array) { |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
193 | if(array->data) |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
194 | alfree(array->allocator, array->data); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
195 | array->data = NULL; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
196 | 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
|
197 | } |
7fb0f74517c5
changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents:
122
diff
changeset
|
198 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
199 | void ucx_array_free(UcxArray *array) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
200 | ucx_array_destroy(array); |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
201 | alfree(array->allocator, array); |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
202 | } |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
203 | |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
204 | int ucx_array_append_from(UcxArray *array, void *data, size_t count) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
205 | if (ucx_array_ensurecap(array, array->size + count)) |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
206 | return 1; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
207 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
208 | void* dest = ucx_array_at(array, array->size); |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
209 | if (data) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
210 | memcpy(dest, data, array->elemsize*count); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
211 | } else { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
212 | memset(dest, 0, array->elemsize*count); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
213 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
214 | array->size += count; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
215 | |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
216 | return 0; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
217 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
218 | |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
219 | int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
220 | if (ucx_array_ensurecap(array, array->size + count)) |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
221 | return 1; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
222 | |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
223 | if (array->size > 0) { |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
224 | void *dest = ucx_array_at(array, count); |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
225 | memmove(dest, array->data, array->elemsize*array->size); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
226 | } |
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 | if (data) { |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
229 | memcpy(array->data, data, array->elemsize*count); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
230 | } else { |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
231 | memset(array->data, 0, array->elemsize*count); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
232 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
233 | array->size += count; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
234 | |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
235 | return 0; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
236 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
237 | |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
238 | int ucx_array_set_from(UcxArray *array, size_t index, |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
239 | void *data, size_t count) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
240 | if (ucx_array_ensurecap(array, index + count)) |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
241 | return 1; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
242 | |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
243 | if (index+count > array->size) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
244 | array->size = index+count; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
245 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
246 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
247 | void *dest = ucx_array_at(array, index); |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
248 | if (data) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
249 | memcpy(dest, data, array->elemsize*count); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
250 | } else { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
251 | memset(dest, 0, array->elemsize*count); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
252 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
253 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
254 | return 0; |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
255 | } |
8 | 256 | |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
257 | int ucx_array_appendv(UcxArray *array, ...) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
258 | va_list ap; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
259 | va_start(ap, array); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
260 | int elem = va_arg(ap, int); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
261 | int ret = ucx_array_append_from(array, &elem, 1); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
262 | va_end(ap); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
263 | return ret; |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
264 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
265 | |
354
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
266 | int ucx_array_prependv(UcxArray *array, ...) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
267 | va_list ap; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
268 | va_start(ap, array); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
269 | int elem = va_arg(ap, int); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
270 | int ret = ucx_array_prepend_from(array, &elem, 1); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
271 | va_end(ap); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
272 | return ret; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
273 | } |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
274 | |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
275 | int ucx_array_setv(UcxArray *array, size_t index, ...) { |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
276 | va_list ap; |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
277 | va_start(ap, index); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
278 | int elem = va_arg(ap, int); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
279 | int ret = ucx_array_set_from(array, index, &elem, 1); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
280 | va_end(ap); |
7fd13b9f8f60
improves array append/prepend/set interface
Mike Becker <universe@uap-core.de>
parents:
353
diff
changeset
|
281 | return ret; |
337 | 282 | } |
283 | ||
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
284 | 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
|
285 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
286 | if (array1->elemsize != array2->elemsize) |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
287 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
288 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
289 | size_t capacity = array1->capacity+array2->capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
290 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
291 | if (array1->capacity < capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
292 | if (ucx_array_reserve(array1, capacity)) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
293 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
294 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
295 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
296 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
297 | void* dest = ucx_array_at(array1, array1->size); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
298 | memcpy(dest, array2->data, array2->size*array2->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
299 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
300 | array1->size += array2->size; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
301 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
302 | 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
|
303 | } |
7fb0f74517c5
changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents:
122
diff
changeset
|
304 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
305 | void *ucx_array_at(UcxArray const *array, size_t index) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
306 | char* memory = array->data; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
307 | char* loc = memory + index*array->elemsize; |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
308 | return loc; |
7
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
309 | } |
68091406d1cf
added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents:
4
diff
changeset
|
310 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
311 | size_t ucx_array_find(UcxArray const *array, void *elem, |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
312 | cmp_func cmpfnc, void *data) { |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
313 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
314 | size_t elemsize; |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
315 | if (cmpfnc == NULL) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
316 | cmpfnc = ucx_cmp_mem; |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
317 | elemsize = array->elemsize; |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
318 | data = &elemsize; |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
319 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
320 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
321 | if (array->size > 0) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
322 | for (size_t i = 0 ; i < array->size ; i++) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
323 | void* ptr = ucx_array_at(array, i); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
324 | if (cmpfnc(ptr, elem, data) == 0) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
325 | return i; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
326 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
327 | } |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
328 | return array->size; |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
329 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
330 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
331 | } |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
332 | } |
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
|
333 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
334 | int ucx_array_contains(UcxArray const *array, void *elem, |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
335 | cmp_func cmpfnc, void *data) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
336 | return ucx_array_find(array, elem, cmpfnc, data) != array->size; |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
337 | } |
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
|
338 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
339 | 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
|
340 | cmp_func cmpfnc, void *data, |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
341 | size_t start, size_t mid, size_t end) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
342 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
343 | char* array = arrdata; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
344 | |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
345 | size_t rightstart = mid + 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
346 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
347 | if (cmpfnc(array + mid*elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
348 | array + rightstart*elemsize, data) <= 0) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
349 | /* already sorted */ |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
350 | return; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
351 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
352 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
353 | /* 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
|
354 | void *value = malloc(elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
355 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
356 | 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
|
357 | if (cmpfnc(array + start*elemsize, |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
358 | array + rightstart*elemsize, data) <= 0) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
359 | start++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
360 | } else { |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
361 | /* 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
|
362 | memcpy(value, array + rightstart*elemsize, elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
363 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
364 | /* 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
|
365 | 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
|
366 | void *startptr = array + start*elemsize; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
367 | 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
|
368 | memmove(dest, startptr, shiftcount*elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
369 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
370 | /* 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
|
371 | memcpy(startptr, value, elemsize); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
372 | |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
373 | start++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
374 | mid++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
375 | rightstart++; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
376 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
377 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
378 | |
342
8f0a3c00d1d2
removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents:
337
diff
changeset
|
379 | /* free the temporary memory */ |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
380 | free(value); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
381 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
382 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
383 | 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
|
384 | 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
|
385 | if (l < r) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
386 | size_t m = l + (r - l) / 2; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
387 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
388 | 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
|
389 | 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
|
390 | 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
|
391 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
392 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
393 | |
345
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
394 | 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
|
395 | cmp_func cmpfnc, void *data) { |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
396 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
397 | 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
|
398 | } |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
399 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
400 | #ifdef USE_UCX_QSORT_R |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
401 | struct cmpfnc_swapargs_info { |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
402 | cmp_func func; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
403 | void *data; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
404 | }; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
405 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
406 | 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
|
407 | 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
|
408 | 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
|
409 | } |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
410 | |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
411 | 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
|
412 | cmp_func cmpfnc, void *data) { |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
413 | struct cmpfnc_swapargs_info info; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
414 | info.func = cmpfnc; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
415 | info.data = data; |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
416 | 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
|
417 | } |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
418 | #endif /* USE_UCX_QSORT_R */ |
6089eb30a51a
ucx_array_sort() uses qsort_r(), if available
Mike Becker <universe@uap-core.de>
parents:
342
diff
changeset
|
419 | |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
420 | void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data) { |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
421 | ucx_array_sort_impl(array->data, array->size, array->elemsize, |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
422 | 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
|
423 | } |
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
424 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
425 | 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
|
426 | array->size--; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
427 | if (index < array->size) { |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
428 | void* dest = ucx_array_at(array, index); |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
429 | void* src = ucx_array_at(array, index+1); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
430 | memmove(dest, src, (array->size - index)*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
431 | } |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
432 | } |
37
ec296899d12f
replaced qsort with natural merge sort
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
433 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
434 | 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
|
435 | array->size--; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
436 | if (index < array->size) { |
356
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
437 | void* dest = ucx_array_at(array, index); |
77efe51c6c9a
changes UcxArray from value to pointer semantics
Mike Becker <universe@uap-core.de>
parents:
355
diff
changeset
|
438 | void* src = ucx_array_at(array, array->size); |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
439 | memcpy(dest, src, array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
440 | } |
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
|
441 | } |
fdabd1240b69
added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
442 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
443 | int ucx_array_shrink(UcxArray* array) { |
336
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
444 | void* newptr = alrealloc(array->allocator, array->data, |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
445 | array->size*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
446 | if (newptr) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
447 | array->data = newptr; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
448 | array->capacity = array->size; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
449 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
450 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
451 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
452 | } |
13 | 453 | } |
22
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
454 | |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
455 | 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
|
456 | if (array->capacity >= capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
457 | void* newptr = alrealloc(array->allocator, array->data, |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
458 | capacity*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
459 | if (newptr) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
460 | array->data = newptr; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
461 | array->capacity = capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
462 | if (array->size > array->capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
463 | array->size = array->capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
464 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
465 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
466 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
467 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
468 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
469 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
470 | return ucx_array_reserve(array, capacity); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
471 | } |
125
fca8efb122de
changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents:
123
diff
changeset
|
472 | } |
334
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
473 | |
bc81faa9afda
adds array interface and tests
Mike Becker <universe@uap-core.de>
parents:
323
diff
changeset
|
474 | 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
|
475 | if (array->capacity > capacity) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
476 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
477 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
478 | void* newptr = alrealloc(array->allocator, array->data, |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
479 | capacity*array->elemsize); |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
480 | if (newptr) { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
481 | array->data = newptr; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
482 | array->capacity = capacity; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
483 | return 0; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
484 | } else { |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
485 | return 1; |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
486 | } |
6d7aa8a1a3b3
implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents:
334
diff
changeset
|
487 | } |
22
76cdd8209f1f
added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
488 | } |