Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file set.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Generic set routines implementations |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 15 | #include "common.h" |
| 16 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 17 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "log.h" |
| 22 | #include "set.h" |
| 23 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 24 | API LY_ERR |
| 25 | ly_set_new(struct ly_set **set_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 26 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 27 | LY_CHECK_ARG_RET(NULL, set_p, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 28 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 29 | *set_p = calloc(1, sizeof **set_p); |
| 30 | LY_CHECK_ERR_RET(!(*set_p), LOGMEM(NULL), LY_EMEM); |
| 31 | |
| 32 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | API void |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 36 | ly_set_clean(struct ly_set *set, void (*destructor)(void *obj)) |
| 37 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 38 | uint32_t u; |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 39 | |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 40 | if (!set) { |
| 41 | return; |
| 42 | } |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 43 | |
| 44 | if (destructor) { |
| 45 | for (u = 0; u < set->count; ++u) { |
| 46 | destructor(set->objs[u]); |
| 47 | } |
| 48 | } |
| 49 | set->count = 0; |
| 50 | } |
| 51 | |
| 52 | API void |
| 53 | ly_set_erase(struct ly_set *set, void (*destructor)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 54 | { |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 55 | if (!set) { |
| 56 | return; |
| 57 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 58 | |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 59 | ly_set_clean(set, destructor); |
| 60 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 61 | free(set->objs); |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 62 | set->size = 0; |
| 63 | set->objs = NULL; |
| 64 | } |
| 65 | |
| 66 | API void |
| 67 | ly_set_free(struct ly_set *set, void (*destructor)(void *obj)) |
| 68 | { |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 69 | if (!set) { |
| 70 | return; |
| 71 | } |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 72 | |
| 73 | ly_set_erase(set, destructor); |
| 74 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 75 | free(set); |
| 76 | } |
| 77 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 78 | API ly_bool |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 79 | ly_set_contains(const struct ly_set *set, void *object, uint32_t *index_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 80 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 81 | LY_CHECK_ARG_RET(NULL, set, 0); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 82 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 83 | for (uint32_t i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 84 | if (set->objs[i] == object) { |
| 85 | /* object found */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 86 | if (index_p) { |
| 87 | *index_p = i; |
| 88 | } |
| 89 | return 1; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | /* object not found */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 94 | return 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 97 | API LY_ERR |
| 98 | ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj), struct ly_set **newset_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 99 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 100 | struct ly_set *newset; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 101 | uint32_t u; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 102 | |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 103 | LY_CHECK_ARG_RET(NULL, set, newset_p, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 104 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 105 | newset = calloc(1, sizeof *newset); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 106 | LY_CHECK_ERR_RET(!newset, LOGMEM(NULL), LY_EMEM); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 107 | if (!set->count) { |
| 108 | *newset_p = newset; |
| 109 | return LY_SUCCESS; |
| 110 | } |
| 111 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 112 | newset->count = set->count; |
| 113 | newset->size = set->count; /* optimize the size */ |
| 114 | newset->objs = malloc(newset->size * sizeof *(newset->objs)); |
| 115 | LY_CHECK_ERR_RET(!newset->objs, LOGMEM(NULL); free(newset), LY_EMEM); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 116 | if (duplicator) { |
| 117 | for (u = 0; u < set->count; ++u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 118 | newset->objs[u] = duplicator(set->objs[u]); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 119 | } |
| 120 | } else { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 121 | memcpy(newset->objs, set->objs, newset->size * sizeof *(newset->objs)); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 122 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 123 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 124 | *newset_p = newset; |
| 125 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 128 | API LY_ERR |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 129 | ly_set_add(struct ly_set *set, void *object, ly_bool list, uint32_t *index_p) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 130 | { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 131 | void **new; |
| 132 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 133 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 134 | |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 135 | if (!list) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 136 | /* search for duplication */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 137 | for (uint32_t i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 138 | if (set->objs[i] == object) { |
| 139 | /* already in set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 140 | if (index_p) { |
| 141 | *index_p = i; |
| 142 | } |
| 143 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 148 | if (set->size == set->count) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 149 | #define SET_SIZE_STEP 8 |
| 150 | new = realloc(set->objs, (set->size + SET_SIZE_STEP) * sizeof *(set->objs)); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 151 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 152 | set->size += SET_SIZE_STEP; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 153 | set->objs = new; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 154 | #undef SET_SIZE_STEP |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 157 | if (index_p) { |
| 158 | *index_p = set->count; |
| 159 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 160 | set->objs[set->count++] = object; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 161 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 162 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 165 | API LY_ERR |
Michal Vasko | 50694be | 2021-02-04 12:09:09 +0100 | [diff] [blame] | 166 | ly_set_merge(struct ly_set *trg, const struct ly_set *src, ly_bool list, void *(*duplicator)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 167 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 168 | uint32_t u; |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 169 | void *obj; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 170 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 171 | LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL); |
| 172 | |
| 173 | if (!src) { |
| 174 | /* nothing to do */ |
| 175 | return LY_SUCCESS; |
| 176 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 177 | |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 178 | for (u = 0; u < src->count; ++u) { |
| 179 | if (duplicator) { |
| 180 | obj = duplicator(src->objs[u]); |
| 181 | } else { |
| 182 | obj = src->objs[u]; |
| 183 | } |
Radek Krejci | 52ca92d | 2020-10-12 16:51:22 +0200 | [diff] [blame] | 184 | LY_CHECK_RET(ly_set_add(trg, obj, list, NULL)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 187 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | API LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 191 | ly_set_rm_index(struct ly_set *set, uint32_t index, void (*destructor)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 192 | { |
Radek Krejci | 1324639 | 2018-09-07 14:57:41 +0200 | [diff] [blame] | 193 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 194 | LY_CHECK_ERR_RET(index >= set->count, LOGARG(NULL, index), LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 195 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 196 | if (destructor) { |
| 197 | destructor(set->objs[index]); |
| 198 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 199 | if (index == set->count - 1) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 200 | /* removing last item in set */ |
| 201 | set->objs[index] = NULL; |
| 202 | } else { |
| 203 | /* removing item somewhere in a middle, so put there the last item */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 204 | set->objs[index] = set->objs[set->count - 1]; |
| 205 | set->objs[set->count - 1] = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 206 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 207 | set->count--; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 208 | |
| 209 | return LY_SUCCESS; |
| 210 | } |
| 211 | |
| 212 | API LY_ERR |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 213 | ly_set_rm(struct ly_set *set, void *object, void (*destructor)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 214 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 215 | uint32_t i; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 216 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 217 | LY_CHECK_ARG_RET(NULL, set, object, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 218 | |
| 219 | /* get index */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 220 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 221 | if (set->objs[i] == object) { |
| 222 | break; |
| 223 | } |
| 224 | } |
Radek Krejci | 5661616 | 2018-09-18 14:11:00 +0200 | [diff] [blame] | 225 | LY_CHECK_ERR_RET((i == set->count), LOGARG(NULL, object), LY_EINVAL); /* object is not in set */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 226 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 227 | return ly_set_rm_index(set, i, destructor); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 228 | } |