[62d3d87] | 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
|
---|
[03daabd] | 36 | * array.h because of type genericity.
|
---|
[62d3d87] | 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <assert.h>
|
---|
| 40 | #include <errno.h>
|
---|
[03daabd] | 41 | #include <adt/array.h>
|
---|
[62d3d87] | 42 | #include <macros.h>
|
---|
| 43 | #include <mem.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 |
|
---|
[03daabd] | 46 | static errno_t array_realloc(array_t *da, size_t capacity)
|
---|
[62d3d87] | 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 |
|
---|
[03daabd] | 60 | void array_destroy(array_t *da)
|
---|
[62d3d87] | 61 | {
|
---|
[03daabd] | 62 | array_clear(da);
|
---|
[62d3d87] | 63 | free(da->_data);
|
---|
| 64 | da->capacity = 0;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[c2d50c8] | 67 | /** Remove item at given position, shift rest of array */
|
---|
[03daabd] | 68 | void array_remove(array_t *da, size_t index)
|
---|
[62d3d87] | 69 | {
|
---|
| 70 | assert(index < da->size);
|
---|
[03daabd] | 71 | _array_unshift(da, index, 1);
|
---|
| 72 | errno_t rc = array_reserve(da, da->size);
|
---|
[102f641] | 73 | assert(rc == EOK);
|
---|
[62d3d87] | 74 | }
|
---|
| 75 |
|
---|
[4b1c6a4b] | 76 | /** Clear dynamic array (empty) */
|
---|
[03daabd] | 77 | void array_clear(array_t *da)
|
---|
[4b1c6a4b] | 78 | {
|
---|
| 79 | da->size = 0;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[c2d50c8] | 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 | */
|
---|
[03daabd] | 88 | void array_clear_range(array_t *da, size_t begin, size_t end)
|
---|
[c2d50c8] | 89 | {
|
---|
| 90 | assert(begin < da->size);
|
---|
| 91 | assert(end <= da->size);
|
---|
| 92 |
|
---|
[03daabd] | 93 | _array_unshift(da, begin, end - begin);
|
---|
| 94 | errno_t rc = array_reserve(da, da->size);
|
---|
[102f641] | 95 | assert(rc == EOK);
|
---|
[c2d50c8] | 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 | */
|
---|
[03daabd] | 106 | errno_t array_concat(array_t *da1, array_t *da2)
|
---|
[c2d50c8] | 107 | {
|
---|
| 108 | assert(da1->_item_size == da2->_item_size);
|
---|
| 109 |
|
---|
[03daabd] | 110 | errno_t rc = array_reserve(da1, da1->size + da2->size);
|
---|
[c2d50c8] | 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 |
|
---|
[62d3d87] | 123 | /** Grows/shrinks array so that it effeciently stores desired capacity
|
---|
| 124 | *
|
---|
| 125 | * @param da
|
---|
[dd5c623] | 126 | * @param[in] desired capacity of array (items)
|
---|
[62d3d87] | 127 | *
|
---|
| 128 | * @return EOK
|
---|
| 129 | * @return ENOMEM
|
---|
| 130 | */
|
---|
[03daabd] | 131 | errno_t array_reserve(array_t *da, size_t capacity)
|
---|
[62d3d87] | 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 |
|
---|
[03daabd] | 143 | return array_realloc(da, new_capacity);
|
---|
[dd5c623] | 144 | }
|
---|
| 145 |
|
---|
[03daabd] | 146 | void _array_initialize(array_t *da, size_t item_size)
|
---|
[dd5c623] | 147 | {
|
---|
| 148 | da->_item_size = item_size;
|
---|
| 149 | da->_data = NULL;
|
---|
| 150 |
|
---|
| 151 | da->capacity = 0;
|
---|
| 152 | da->size = 0;
|
---|
[62d3d87] | 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 | */
|
---|
[03daabd] | 163 | void _array_shift(array_t *da, size_t index, size_t offset)
|
---|
[62d3d87] | 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 | */
|
---|
[03daabd] | 182 | void _array_unshift(array_t *da, size_t index, size_t offset)
|
---|
[62d3d87] | 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 | */
|
---|