1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | *
|
---|
34 | * Implementation of dynamic array that grows or shrinks based upon no. of
|
---|
35 | * items it contains. Non-negligible part of implementation is in @ref
|
---|
36 | * array.h because of type genericity.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <assert.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <adt/array.h>
|
---|
42 | #include <macros.h>
|
---|
43 | #include <mem.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 |
|
---|
46 | static errno_t array_realloc(array_t *da, size_t capacity)
|
---|
47 | {
|
---|
48 | if (capacity == da->capacity) {
|
---|
49 | return EOK;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void *new_data = realloc(da->_data, da->_item_size * capacity);
|
---|
53 | if (new_data) {
|
---|
54 | da->_data = new_data;
|
---|
55 | da->capacity = capacity;
|
---|
56 | }
|
---|
57 | return (new_data == NULL) ? ENOMEM : EOK;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void array_destroy(array_t *da)
|
---|
61 | {
|
---|
62 | array_clear(da);
|
---|
63 | free(da->_data);
|
---|
64 | da->capacity = 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Remove item at given position, shift rest of array */
|
---|
68 | void array_remove(array_t *da, size_t index)
|
---|
69 | {
|
---|
70 | assert(index < da->size);
|
---|
71 | _array_unshift(da, index, 1);
|
---|
72 | errno_t rc = array_reserve(da, da->size);
|
---|
73 | assert(rc == EOK);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Clear dynamic array (empty) */
|
---|
77 | void array_clear(array_t *da)
|
---|
78 | {
|
---|
79 | da->size = 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Clear subsequence of array
|
---|
83 | *
|
---|
84 | * @param[in/out] da
|
---|
85 | * @param[in] begin index of first item to remove
|
---|
86 | * @param[in] end index behind last item to remove
|
---|
87 | */
|
---|
88 | void array_clear_range(array_t *da, size_t begin, size_t end)
|
---|
89 | {
|
---|
90 | assert(begin < da->size);
|
---|
91 | assert(end <= da->size);
|
---|
92 |
|
---|
93 | _array_unshift(da, begin, end - begin);
|
---|
94 | errno_t rc = array_reserve(da, da->size);
|
---|
95 | assert(rc == EOK);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Concatenate two arrays
|
---|
99 | *
|
---|
100 | * @param[in/out] da1 first array and concatenated output
|
---|
101 | * @param[in] da2 second array, it is untouched
|
---|
102 | *
|
---|
103 | * @return EOK on success
|
---|
104 | * @return ENOMEM when allocation fails
|
---|
105 | */
|
---|
106 | errno_t array_concat(array_t *da1, array_t *da2)
|
---|
107 | {
|
---|
108 | assert(da1->_item_size == da2->_item_size);
|
---|
109 |
|
---|
110 | errno_t rc = array_reserve(da1, da1->size + da2->size);
|
---|
111 | if (rc != EOK) {
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 | void *dst = da1->_data + da1->size * da1->_item_size;
|
---|
115 | void *src = da2->_data;
|
---|
116 | size_t size = da1->_item_size * da2->size;
|
---|
117 | memcpy(dst, src, size);
|
---|
118 | da1->size += da2->size;
|
---|
119 |
|
---|
120 | return EOK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** Grows/shrinks array so that it effeciently stores desired capacity
|
---|
124 | *
|
---|
125 | * @param da
|
---|
126 | * @param[in] desired capacity of array (items)
|
---|
127 | *
|
---|
128 | * @return EOK
|
---|
129 | * @return ENOMEM
|
---|
130 | */
|
---|
131 | errno_t array_reserve(array_t *da, size_t capacity)
|
---|
132 | {
|
---|
133 | const size_t factor = 2;
|
---|
134 | size_t new_capacity;
|
---|
135 | if (capacity > da->capacity) {
|
---|
136 | new_capacity = max(da->capacity * factor, capacity);
|
---|
137 | } else if (capacity < da->capacity / factor) {
|
---|
138 | new_capacity = min(da->capacity / factor, capacity);
|
---|
139 | } else {
|
---|
140 | new_capacity = capacity;
|
---|
141 | }
|
---|
142 |
|
---|
143 | return array_realloc(da, new_capacity);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void _array_initialize(array_t *da, size_t item_size)
|
---|
147 | {
|
---|
148 | da->_item_size = item_size;
|
---|
149 | da->_data = NULL;
|
---|
150 |
|
---|
151 | da->capacity = 0;
|
---|
152 | da->size = 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Shift block of array
|
---|
156 | *
|
---|
157 | * Extends size of dynamic array, assumes sufficient capacity.
|
---|
158 | *
|
---|
159 | * @param da
|
---|
160 | * @param[in] index first item shifted
|
---|
161 | * @param[in] offset shift in no. of items
|
---|
162 | */
|
---|
163 | void _array_shift(array_t *da, size_t index, size_t offset)
|
---|
164 | {
|
---|
165 | assert(da->capacity >= da->size + offset);
|
---|
166 |
|
---|
167 | void *src = da->_data + index * da->_item_size;
|
---|
168 | void *dst = da->_data + (index + offset) * da->_item_size;
|
---|
169 | size_t size = (da->size - index) * da->_item_size;
|
---|
170 | memmove(dst, src, size);
|
---|
171 | da->size += offset;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Unshift block of array
|
---|
175 | *
|
---|
176 | * Reduce size of dynamic array
|
---|
177 | *
|
---|
178 | * @param da
|
---|
179 | * @param[in] index first item unshifted (removed)
|
---|
180 | * @param[in] offset shift in no. of items
|
---|
181 | */
|
---|
182 | void _array_unshift(array_t *da, size_t index, size_t offset)
|
---|
183 | {
|
---|
184 | void *src = da->_data + (index + offset) * da->_item_size;
|
---|
185 | void *dst = da->_data + index * da->_item_size;
|
---|
186 | size_t size = (da->size - index - offset) * da->_item_size;
|
---|
187 | memmove(dst, src, size);
|
---|
188 | da->size -= offset;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** @}
|
---|
192 | */
|
---|