[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 |
|
---|
| 35 | #ifndef LIBC_DYN_ARRAY_H_
|
---|
| 36 | #define LIBC_DYN_ARRAY_H_
|
---|
| 37 |
|
---|
| 38 | #include <stdbool.h>
|
---|
| 39 | #include <stddef.h>
|
---|
| 40 |
|
---|
| 41 | typedef struct {
|
---|
| 42 | /** Data buffer of array */
|
---|
| 43 | uint8_t *_data;
|
---|
| 44 | /** Size on bytes of a single item */
|
---|
| 45 | size_t _item_size;
|
---|
| 46 |
|
---|
| 47 | /** Allocated space (in items) of the array */
|
---|
| 48 | size_t capacity;
|
---|
| 49 | /** No. of items in the array */
|
---|
| 50 | size_t size;
|
---|
| 51 | } dyn_array_t;
|
---|
| 52 |
|
---|
| 53 | /** Initialize dynamic array
|
---|
| 54 | *
|
---|
| 55 | * @param[in] capacity initial capacity of array
|
---|
| 56 | *
|
---|
[dd5c623] | 57 | * @return void
|
---|
[62d3d87] | 58 | */
|
---|
[dd5c623] | 59 | #define dyn_array_initialize(dyn_array, type) \
|
---|
| 60 | _dyn_array_initialize((dyn_array), sizeof(type))
|
---|
[62d3d87] | 61 |
|
---|
| 62 |
|
---|
| 63 | /** Dynamic array accessor
|
---|
| 64 | *
|
---|
| 65 | * @return lvalue for the given item
|
---|
| 66 | */
|
---|
| 67 | #define dyn_array_at(dyn_array, type, index) \
|
---|
[c8891c8] | 68 | (*((type *) (dyn_array)->_data + index))
|
---|
[62d3d87] | 69 |
|
---|
| 70 | /** Insert item at given position, shift rest of array
|
---|
| 71 | *
|
---|
| 72 | * @return EOK on success
|
---|
| 73 | * @return ENOMEM on failure
|
---|
| 74 | */
|
---|
| 75 | #define dyn_array_insert(dyn_array, type, index, value) \
|
---|
| 76 | ({ \
|
---|
| 77 | size_t _index = (index); \
|
---|
| 78 | dyn_array_t *_da = (dyn_array); \
|
---|
[dd5c623] | 79 | int rc = dyn_array_reserve(_da, _da->size + 1); \
|
---|
[62d3d87] | 80 | if (!rc) { \
|
---|
| 81 | _dyn_array_shift(_da, _index, 1); \
|
---|
| 82 | dyn_array_at(_da, type, _index) = (value); \
|
---|
| 83 | } \
|
---|
| 84 | rc; \
|
---|
| 85 | })
|
---|
| 86 |
|
---|
| 87 | /** Insert item at the end of array
|
---|
| 88 | *
|
---|
| 89 | * @return EOK on success
|
---|
| 90 | * @return ENOMEM on failure
|
---|
| 91 | */
|
---|
| 92 | #define dyn_array_append(dyn_array, type, value) \
|
---|
| 93 | dyn_array_insert(dyn_array, type, (dyn_array)->size, (value))
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | /** Dynamic array iteration
|
---|
| 97 | *
|
---|
[4b1c6a4b] | 98 | * @param[in] dyn_array dyn_array_t (not pointer)
|
---|
| 99 | * @param[in] it name of variable used as iterator, it's pointer
|
---|
| 100 | * to @p type
|
---|
[62d3d87] | 101 | */
|
---|
| 102 | #define dyn_array_foreach(dyn_array, type, it) \
|
---|
[c1b2084] | 103 | for (type *it = NULL; it == NULL; it = (type *)1) \
|
---|
| 104 | for (type *_it = (type *)(dyn_array)._data; \
|
---|
| 105 | it = _it, _it != ((type *)(dyn_array)._data + (dyn_array).size); \
|
---|
| 106 | ++_it)
|
---|
[62d3d87] | 107 |
|
---|
[4b1c6a4b] | 108 | /** Find first occurence of value
|
---|
| 109 | *
|
---|
| 110 | * @param[in] dyn_array dyn_array_t *
|
---|
| 111 | * @param[in] value value to search for
|
---|
| 112 | *
|
---|
| 113 | * @return index of found value or size of array when no found
|
---|
| 114 | */
|
---|
| 115 | #define dyn_array_find(dyn_array, type, value) \
|
---|
| 116 | ({ \
|
---|
| 117 | size_t _result = (dyn_array)->size; \
|
---|
| 118 | dyn_array_foreach(*(dyn_array), type, _it) { \
|
---|
| 119 | if (*_it == value) { \
|
---|
| 120 | _result = _it - (type *)(dyn_array)->_data; \
|
---|
| 121 | break; \
|
---|
| 122 | } \
|
---|
| 123 | } \
|
---|
| 124 | _result; \
|
---|
| 125 | })
|
---|
[62d3d87] | 126 |
|
---|
| 127 | extern void dyn_array_destroy(dyn_array_t *);
|
---|
| 128 | extern void dyn_array_remove(dyn_array_t *, size_t);
|
---|
[4b1c6a4b] | 129 | void dyn_array_clear(dyn_array_t *);
|
---|
[dd5c623] | 130 | extern int dyn_array_reserve(dyn_array_t *, size_t);
|
---|
[62d3d87] | 131 |
|
---|
[dd5c623] | 132 | extern void _dyn_array_initialize(dyn_array_t *, size_t);
|
---|
[62d3d87] | 133 | extern void _dyn_array_shift(dyn_array_t *, size_t, size_t);
|
---|
| 134 | extern void _dyn_array_unshift(dyn_array_t *, size_t, size_t);
|
---|
| 135 |
|
---|
| 136 | #endif
|
---|
| 137 |
|
---|
| 138 | /** @}
|
---|
| 139 | */
|
---|