source: mainline/uspace/lib/c/generic/adt/dyn_array.c@ 62d3d87

Last change on this file since 62d3d87 was 62d3d87, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Create ADT for dynamic array and its tests
Conflicts:

uspace/Makefile
uspace/lib/c/Makefile
uspace/lib/c/test/main.c

  • Property mode set to 100644
File size: 4.4 KB
Line 
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 * dyn_array.h because of type genericity.
37 */
38
39#include <assert.h>
40#include <errno.h>
41#include <adt/dyn_array.h>
42#include <macros.h>
43#include <mem.h>
44#include <stdlib.h>
45
46
47static void dyn_array_clear(dyn_array_t *da)
48{
49 da->size = 0;
50}
51
52static int dyn_array_realloc(dyn_array_t *da, size_t capacity)
53{
54 if (capacity == da->capacity) {
55 return EOK;
56 }
57
58 void *new_data = realloc(da->_data, da->_item_size * capacity);
59 if (new_data) {
60 da->_data = new_data;
61 da->capacity = capacity;
62 }
63 return (new_data == NULL) ? ENOMEM : EOK;
64}
65
66void dyn_array_destroy(dyn_array_t *da)
67{
68 dyn_array_clear(da);
69 free(da->_data);
70 da->capacity = 0;
71}
72
73/** Remove item at give position, shift rest of array */
74void dyn_array_remove(dyn_array_t *da, size_t index)
75{
76 assert(index < da->size);
77 _dyn_array_unshift(da, index, 1);
78 int rc = _dyn_array_reserve(da, da->size);
79 assert(rc == EOK);
80}
81
82int _dyn_array_initialize(dyn_array_t *da, size_t item_size, size_t capacity)
83{
84 da->_item_size = item_size;
85 da->_data = NULL;
86
87 da->capacity = 0;
88 da->size = 0;
89
90 return _dyn_array_reserve(da, capacity);
91}
92
93void *_dyn_array_get(dyn_array_t *da, size_t index)
94{
95 assert(index < da->size);
96 return da->_data + (index * da->_item_size);
97}
98
99/** Grows/shrinks array so that it effeciently stores desired capacity
100 *
101 * @param da
102 * @param[in] desired capacity of array
103 *
104 * @return EOK
105 * @return ENOMEM
106 */
107int _dyn_array_reserve(dyn_array_t *da, size_t capacity)
108{
109 const size_t factor = 2;
110 size_t new_capacity;
111 if (capacity > da->capacity) {
112 new_capacity = max(da->capacity * factor, capacity);
113 } else if (capacity < da->capacity / factor) {
114 new_capacity = min(da->capacity / factor, capacity);
115 } else {
116 new_capacity = capacity;
117 }
118
119 return dyn_array_realloc(da, new_capacity);
120}
121
122/** Shift block of array
123 *
124 * Extends size of dynamic array, assumes sufficient capacity.
125 *
126 * @param da
127 * @param[in] index first item shifted
128 * @param[in] offset shift in no. of items
129 */
130void _dyn_array_shift(dyn_array_t *da, size_t index, size_t offset)
131{
132 assert(da->capacity >= da->size + offset);
133
134 void *src = da->_data + index * da->_item_size;
135 void *dst = da->_data + (index + offset) * da->_item_size;
136 size_t size = (da->size - index) * da->_item_size;
137 memmove(dst, src, size);
138 da->size += offset;
139}
140
141/** Unshift block of array
142 *
143 * Reduce size of dynamic array
144 *
145 * @param da
146 * @param[in] index first item unshifted (removed)
147 * @param[in] offset shift in no. of items
148 */
149void _dyn_array_unshift(dyn_array_t *da, size_t index, size_t offset)
150{
151 void *src = da->_data + (index + offset) * da->_item_size;
152 void *dst = da->_data + index * da->_item_size;
153 size_t size = (da->size - index - offset) * da->_item_size;
154 memmove(dst, src, size);
155 da->size -= offset;
156}
157
158
159/** @}
160 */
Note: See TracBrowser for help on using the repository browser.