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 | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 103 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 104 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 105 | newset = malloc(sizeof *newset); |
| 106 | LY_CHECK_ERR_RET(!newset, LOGMEM(NULL), LY_EMEM); |
| 107 | newset->count = set->count; |
| 108 | newset->size = set->count; /* optimize the size */ |
| 109 | newset->objs = malloc(newset->size * sizeof *(newset->objs)); |
| 110 | LY_CHECK_ERR_RET(!newset->objs, LOGMEM(NULL); free(newset), LY_EMEM); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 111 | if (duplicator) { |
| 112 | for (u = 0; u < set->count; ++u) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 113 | newset->objs[u] = duplicator(set->objs[u]); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 114 | } |
| 115 | } else { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 116 | memcpy(newset->objs, set->objs, newset->size * sizeof *(newset->objs)); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 117 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 118 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 119 | *newset_p = newset; |
| 120 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 123 | API LY_ERR |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 124 | 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] | 125 | { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 126 | void **new; |
| 127 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 128 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 129 | |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 130 | if (!list) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 131 | /* search for duplication */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 132 | for (uint32_t i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 133 | if (set->objs[i] == object) { |
| 134 | /* already in set */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 135 | if (index_p) { |
| 136 | *index_p = i; |
| 137 | } |
| 138 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 143 | if (set->size == set->count) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 144 | new = realloc(set->objs, (set->size + 8) * sizeof *(set->objs)); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 145 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 146 | set->size += 8; |
| 147 | set->objs = new; |
| 148 | } |
| 149 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 150 | if (index_p) { |
| 151 | *index_p = set->count; |
| 152 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 153 | set->objs[set->count++] = object; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 154 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 155 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 158 | API LY_ERR |
Radek Krejci | 52ca92d | 2020-10-12 16:51:22 +0200 | [diff] [blame^] | 159 | ly_set_merge(struct ly_set *trg, struct ly_set *src, ly_bool list, void *(*duplicator)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 160 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 161 | uint32_t u; |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 162 | void *obj; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 163 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 164 | LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL); |
| 165 | |
| 166 | if (!src) { |
| 167 | /* nothing to do */ |
| 168 | return LY_SUCCESS; |
| 169 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 170 | |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 171 | for (u = 0; u < src->count; ++u) { |
| 172 | if (duplicator) { |
| 173 | obj = duplicator(src->objs[u]); |
| 174 | } else { |
| 175 | obj = src->objs[u]; |
| 176 | } |
Radek Krejci | 52ca92d | 2020-10-12 16:51:22 +0200 | [diff] [blame^] | 177 | LY_CHECK_RET(ly_set_add(trg, obj, list, NULL)); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 180 | return LY_SUCCESS; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | API LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 184 | 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] | 185 | { |
Radek Krejci | 1324639 | 2018-09-07 14:57:41 +0200 | [diff] [blame] | 186 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 187 | LY_CHECK_ERR_RET(index >= set->count, LOGARG(NULL, index), LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 188 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 189 | if (destructor) { |
| 190 | destructor(set->objs[index]); |
| 191 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 192 | if (index == set->count - 1) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 193 | /* removing last item in set */ |
| 194 | set->objs[index] = NULL; |
| 195 | } else { |
| 196 | /* removing item somewhere in a middle, so put there the last item */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 197 | set->objs[index] = set->objs[set->count - 1]; |
| 198 | set->objs[set->count - 1] = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 199 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 200 | set->count--; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 201 | |
| 202 | return LY_SUCCESS; |
| 203 | } |
| 204 | |
| 205 | API LY_ERR |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 206 | 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] | 207 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 208 | uint32_t i; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 209 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 210 | LY_CHECK_ARG_RET(NULL, set, object, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 211 | |
| 212 | /* get index */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 213 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 214 | if (set->objs[i] == object) { |
| 215 | break; |
| 216 | } |
| 217 | } |
Radek Krejci | 5661616 | 2018-09-18 14:11:00 +0200 | [diff] [blame] | 218 | 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] | 219 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 220 | return ly_set_rm_index(set, i, destructor); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 221 | } |