src/ucx/ucx/array.h

Fri, 28 Jan 2022 16:01:05 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 28 Jan 2022 16:01:05 +0100
branch
webdav
changeset 277
7608af69739f
parent 260
4779a6fb4fbe
permissions
-rw-r--r--

add support for vfsclass parameter in nametrans safs

254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 * Dynamically allocated array implementation.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 * @file array.h
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 * @author Mike Becker
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 * @author Olaf Wintermann
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #ifndef UCX_ARRAY_H
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #define UCX_ARRAY_H
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include "ucx.h"
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "allocator.h"
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #ifdef __cplusplus
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 extern "C" {
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #endif
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 * UCX array type.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 typedef struct {
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 * The current capacity of the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 size_t capacity;
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 * The actual number of elements in the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 size_t size;
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 * The size of an individual element in bytes.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 size_t elemsize;
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 * A pointer to the data.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 void* data;
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 * The allocator used for the data.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 UcxAllocator* allocator;
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 } UcxArray;
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 * Sets an element in an arbitrary user defined array.
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
74 * The data is copied from the specified data location.
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 * If the capacity is insufficient, the array is automatically reallocated and
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 * the possibly new pointer is stored in the <code>array</code> argument.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 * On reallocation the capacity of the array is doubled until it is sufficient.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 * The new capacity is stored back to <code>capacity</code>.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 * @param array a pointer to location of the array pointer
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 * @param capacity a pointer to the capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 * @param elmsize the size of each element
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 * @param idx the index of the element to set
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
86 * @param data a pointer to the element data
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 * @return zero on success or non-zero on error (errno will be set)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 elmsize, idx, data)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 * Sets an element in an arbitrary user defined array.
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
95 * The data is copied from the specified data location.
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 * If the capacity is insufficient, the array is automatically reallocated
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 * using the specified allocator and the possibly new pointer is stored in
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 * the <code>array</code> argument.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 * On reallocation the capacity of the array is doubled until it is sufficient.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 * The new capacity is stored back to <code>capacity</code>.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 * @param alloc the allocator that shall be used to reallocate the array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 * @param array a pointer to location of the array pointer
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 * @param capacity a pointer to the capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 * @param elmsize the size of each element
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 * @param idx the index of the element to set
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
109 * @param data a pointer to the element data
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 * @return zero on success or non-zero on error (errno will be set)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
113 size_t elmsize, size_t idx, void* data);
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 /**
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
116 * Stores a pointer in an arbitrary user defined array.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
117 * The element size of the array must be sizeof(void*).
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
118 *
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
119 * If the capacity is insufficient, the array is automatically reallocated and
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
120 * the possibly new pointer is stored in the <code>array</code> argument.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
121 *
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
122 * On reallocation the capacity of the array is doubled until it is sufficient.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
123 * The new capacity is stored back to <code>capacity</code>.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
124 *
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
125 * @param array a pointer to location of the array pointer
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
126 * @param capacity a pointer to the capacity
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
127 * @param idx the index of the element to set
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
128 * @param ptr the pointer to store
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
129 * @return zero on success or non-zero on error (errno will be set)
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
130 */
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
131 #define ucx_array_util_setptr(array, capacity, idx, ptr) \
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
132 ucx_array_util_setptr_a(ucx_default_allocator(), (void**)(array), \
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
133 capacity, idx, ptr)
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
134
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
135 /**
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
136 * Stores a pointer in an arbitrary user defined array.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
137 * The element size of the array must be sizeof(void*).
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
138 *
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
139 * If the capacity is insufficient, the array is automatically reallocated
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
140 * using the specified allocator and the possibly new pointer is stored in
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
141 * the <code>array</code> argument.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
142 *
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
143 * On reallocation the capacity of the array is doubled until it is sufficient.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
144 * The new capacity is stored back to <code>capacity</code>.
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 * @param alloc the allocator that shall be used to reallocate the array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 * @param array a pointer to location of the array pointer
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 * @param capacity a pointer to the capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 * @param idx the index of the element to set
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
150 * @param ptr the pointer to store
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 * @return zero on success or non-zero on error (errno will be set)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 */
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
153 int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity,
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
154 size_t idx, void* ptr);
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
155
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 * Creates a new UCX array with the given capacity and element size.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 * @param capacity the initial capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 * @param elemsize the element size
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 * @return a pointer to a new UCX array structure
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 UcxArray* ucx_array_new(size_t capacity, size_t elemsize);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 * Creates a new UCX array using the specified allocator.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 * @param capacity the initial capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 * @param elemsize the element size
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 * @param allocator the allocator to use
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 * @return a pointer to new UCX array structure
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 UcxAllocator* allocator);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 * Initializes a UCX array structure with the given capacity and element size.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 * The structure must be uninitialized as the data pointer will be overwritten.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 * @param array the structure to initialize
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 * @param capacity the initial capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 * @param elemsize the element size
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 * Initializes a UCX array structure using the specified allocator.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 * The structure must be uninitialized as the data pointer will be overwritten.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 * @param array the structure to initialize
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 * @param capacity the initial capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 * @param elemsize the element size
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 * @param allocator the allocator to use
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 UcxAllocator* allocator);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 * Creates an shallow copy of an array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 * This function clones the specified array by using memcpy().
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 * If the destination capacity is insufficient, an automatic reallocation is
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 * attempted.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 * Note: if the destination array is uninitialized, the behavior is undefined.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 * @param dest the array to copy to
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 * @param src the array to copy from
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 * @return zero on success, non-zero on reallocation failure.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 int ucx_array_clone(UcxArray* dest, UcxArray const* src);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 * Compares two UCX arrays element-wise by using a compare function.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 * Elements of the two specified arrays are compared by using the specified
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 * compare function and the additional data. The type and content of this
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 * additional data depends on the cmp_func() used.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 * This function always returns zero, if the element sizes of the arrays do
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 * not match and performs no comparisons in this case.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 * @param array1 the first array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 * @param array2 the second array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 * @param cmpfnc the compare function
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 * @param data additional data for the compare function
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 cmp_func cmpfnc, void* data);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 * Destroys the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 * The data is freed and both capacity and count are reset to zero.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 * If the array structure itself has been dynamically allocated, it has to be
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 * freed separately.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 * @param array the array to destroy
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 void ucx_array_destroy(UcxArray *array);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 * Destroys and frees the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 * @param array the array to free
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 void ucx_array_free(UcxArray *array);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 * Inserts elements at the end of the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 * This is an O(1) operation.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 * The array will automatically grow, if the capacity is exceeded.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 * If a pointer to data is provided, the data is copied into the array with
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 * memcpy(). Otherwise the new elements are completely zeroed.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 * @param array a pointer the array where to append the data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 * @param data a pointer to the data to insert (may be <code>NULL</code>)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 * @param count number of elements to copy from data (if data is
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 * <code>NULL</code>, zeroed elements are appended)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 * @return zero on success, non-zero if a reallocation was necessary but failed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 * @see ucx_array_set_from()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 * @see ucx_array_append()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 * Inserts elements at the beginning of the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 * This is an expensive operation, because the contents must be moved.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 * If there is no particular reason to prepend data, you should use
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 * ucx_array_append_from() instead.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 * @param array a pointer the array where to prepend the data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 * @param data a pointer to the data to insert (may be <code>NULL</code>)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 * @param count number of elements to copy from data (if data is
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 * <code>NULL</code>, zeroed elements are inserted)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 * @return zero on success, non-zero if a reallocation was necessary but failed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 * @see ucx_array_append_from()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 * @see ucx_array_set_from()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 * @see ucx_array_prepend()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 * Sets elements starting at the specified index.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 * If the any index is out of bounds, the array automatically grows.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 * The pointer to the data may be NULL, in which case the elements are zeroed.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 * @param array a pointer the array where to set the data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 * @param index the index of the element to set
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 * @param data a pointer to the data to insert (may be <code>NULL</code>)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 * @param count number of elements to copy from data (if data is
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 * <code>NULL</code>, the memory in the array is zeroed)
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 * @return zero on success, non-zero if a reallocation was necessary but failed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 * @see ucx_array_append_from()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 * @see ucx_array_set()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 * Concatenates two arrays.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 * The contents of the second array are appended to the first array in one
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 * single operation. The second array is otherwise left untouched.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 * The first array may grow automatically. If this fails, both arrays remain
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 * unmodified.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 * @param array1 first array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 * @param array2 second array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 * @return zero on success, non-zero if reallocation was necessary but failed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 * or the element size does not match
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 * Returns a pointer to the array element at the specified index.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 * @param array the array to retrieve the element from
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 * @param index index of the element to return
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 * @return a pointer to the element at the specified index or <code>NULL</code>,
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 * if the index is greater than the array size
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 void *ucx_array_at(UcxArray const* array, size_t index);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 * Returns the index of an element containing the specified data.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 * This function uses a cmp_func() to compare the data of each list element
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 * with the specified data. If no cmp_func is provided, memcmp() is used.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 * If the array contains the data more than once, the index of the first
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 * occurrence is returned.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 * If the array does not contain the data, the size of array is returned.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 * @param array the array where to search for the data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 * @param elem the element data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 * @param cmpfnc the compare function
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 * @param data additional data for the compare function
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 * @return the index of the element containing the specified data or the size of
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 * the array, if the data is not found in this array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 size_t ucx_array_find(UcxArray const *array, void *elem,
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 cmp_func cmpfnc, void *data);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 * Checks, if an array contains a specific element.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 * An element is found, if ucx_array_find() returns a value less than the size.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 * @param array the array where to search for the data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 * @param elem the element data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 * @param cmpfnc the compare function
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 * @param data additional data for the compare function
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 * @return 1, if and only if the array contains the specified element data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 * @see ucx_array_find()
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 int ucx_array_contains(UcxArray const *array, void *elem,
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 cmp_func cmpfnc, void *data);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 * Sorts a UcxArray with the best available sort algorithm.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 * The order of arguments is automatically adjusted for the FreeBSD and MacOS
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 * version of qsort_r().
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 * If qsort_r() is not available, a merge sort algorithm is used, which is
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 * guaranteed to use no more additional memory than for exactly one element.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 * @param array the array to sort
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 * @param cmpfnc the function that shall be used to compare the element data
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 * @param data additional data for the cmp_func() or <code>NULL</code>
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 * Removes an element from the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 * This is in general an expensive operation, because several elements may
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 * be moved. If the order of the elements is not relevant, use
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 * ucx_array_remove_fast() instead.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 * @param array pointer to the array from which the element shall be removed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 * @param index the index of the element to remove
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 void ucx_array_remove(UcxArray *array, size_t index);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 * Removes an element from the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 * This is an O(1) operation, but does not maintain the order of the elements.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 * The last element in the array is moved to the location of the removed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 * element.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 * @param array pointer to the array from which the element shall be removed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 * @param index the index of the element to remove
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 void ucx_array_remove_fast(UcxArray *array, size_t index);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 * Shrinks the memory to exactly fit the contents.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 * After this operation, the capacity equals the size.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 * @param array a pointer to the array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 * @return zero on success, non-zero if reallocation failed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 int ucx_array_shrink(UcxArray* array);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 * Sets the capacity of the array.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 * If the new capacity is smaller than the size of the array, the elements
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 * are removed and the size is adjusted accordingly.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 * @param array a pointer to the array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 * @param capacity the new capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 * @return zero on success, non-zero if reallocation failed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 int ucx_array_resize(UcxArray* array, size_t capacity);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 /**
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 * Resizes the array only, if the capacity is insufficient.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 * If the requested capacity is smaller than the current capacity, this
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 * function does nothing.
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 *
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 * @param array a pointer to the array
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 * @param capacity the guaranteed capacity
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 * @return zero on success, non-zero if reallocation failed
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 int ucx_array_reserve(UcxArray* array, size_t capacity);
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440
260
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
441 /**
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
442 * Resizes the capacity, if the specified number of elements would not fit.
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
443 *
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
444 * A call to ucx_array_grow(array, count) is effectively the same as
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
445 * ucx_array_reserve(array, array->size+count).
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
446 *
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
447 * @param array a pointer to the array
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
448 * @param count the number of elements that should additionally fit
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
449 * into the array
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
450 * @return zero on success, non-zero if reallocation failed
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
451 */
4779a6fb4fbe fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
452 int ucx_array_grow(UcxArray* array, size_t count);
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 #ifdef __cplusplus
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 }
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 #endif
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 #endif /* UCX_ARRAY_H */
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460

mercurial