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 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 19 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | |
| 21 | #include "log.h" |
| 22 | #include "set.h" |
| 23 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 24 | API struct ly_set * |
| 25 | ly_set_new(void) |
| 26 | { |
| 27 | struct ly_set *new; |
| 28 | |
| 29 | new = calloc(1, sizeof(struct ly_set)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 30 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 31 | return new; |
| 32 | } |
| 33 | |
| 34 | API void |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 35 | ly_set_clean(struct ly_set *set, void (*destructor)(void *obj)) |
| 36 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 37 | uint32_t u; |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 38 | |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 39 | if (!set) { |
| 40 | return; |
| 41 | } |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 42 | |
| 43 | if (destructor) { |
| 44 | for (u = 0; u < set->count; ++u) { |
| 45 | destructor(set->objs[u]); |
| 46 | } |
| 47 | } |
| 48 | set->count = 0; |
| 49 | } |
| 50 | |
| 51 | API void |
| 52 | ly_set_erase(struct ly_set *set, void (*destructor)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 53 | { |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 54 | if (!set) { |
| 55 | return; |
| 56 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 57 | |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 58 | ly_set_clean(set, destructor); |
| 59 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 60 | free(set->objs); |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 61 | set->size = 0; |
| 62 | set->objs = NULL; |
| 63 | } |
| 64 | |
| 65 | API void |
| 66 | ly_set_free(struct ly_set *set, void (*destructor)(void *obj)) |
| 67 | { |
Radek Krejci | 0bfec16 | 2019-05-02 09:54:25 +0200 | [diff] [blame] | 68 | if (!set) { |
| 69 | return; |
| 70 | } |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 71 | |
| 72 | ly_set_erase(set, destructor); |
| 73 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 74 | free(set); |
| 75 | } |
| 76 | |
| 77 | API int |
| 78 | ly_set_contains(const struct ly_set *set, void *object) |
| 79 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 80 | uint32_t i; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 81 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 82 | LY_CHECK_ARG_RET(NULL, set, -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 83 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 84 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 85 | if (set->objs[i] == object) { |
| 86 | /* object found */ |
| 87 | return i; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* object not found */ |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | API struct ly_set * |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 96 | ly_set_dup(const struct ly_set *set, void *(*duplicator)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 97 | { |
| 98 | struct ly_set *new; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 99 | uint32_t u; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 100 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 101 | LY_CHECK_ARG_RET(NULL, set, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 102 | |
| 103 | new = malloc(sizeof *new); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 104 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 105 | new->count = set->count; |
Radek Krejci | 9495507 | 2019-05-22 15:28:29 +0200 | [diff] [blame] | 106 | new->size = set->count; /* optimize the size */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 107 | new->objs = malloc(new->size * sizeof *(new->objs)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 108 | LY_CHECK_ERR_RET(!new->objs, LOGMEM(NULL); free(new), NULL); |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 109 | if (duplicator) { |
| 110 | for (u = 0; u < set->count; ++u) { |
| 111 | new->objs[u] = duplicator(set->objs[u]); |
| 112 | } |
| 113 | } else { |
| 114 | memcpy(new->objs, set->objs, new->size * sizeof *(new->objs)); |
| 115 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 116 | |
| 117 | return new; |
| 118 | } |
| 119 | |
| 120 | API int |
| 121 | ly_set_add(struct ly_set *set, void *object, int options) |
| 122 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 123 | uint32_t i; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 124 | void **new; |
| 125 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 126 | LY_CHECK_ARG_RET(NULL, set, -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 127 | |
| 128 | if (!(options & LY_SET_OPT_USEASLIST)) { |
| 129 | /* search for duplication */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 130 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 131 | if (set->objs[i] == object) { |
| 132 | /* already in set */ |
| 133 | return i; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 138 | if (set->size == set->count) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 139 | new = realloc(set->objs, (set->size + 8) * sizeof *(set->objs)); |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 140 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), -1); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 141 | set->size += 8; |
| 142 | set->objs = new; |
| 143 | } |
| 144 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 145 | set->objs[set->count++] = object; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 146 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 147 | return set->count - 1; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | API int |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 151 | ly_set_merge(struct ly_set *trg, struct ly_set *src, int options, void *(*duplicator)(void *obj)) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 152 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 153 | uint32_t u, c, ret = 0; |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 154 | int i; |
| 155 | void *obj; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 156 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 157 | LY_CHECK_ARG_RET(NULL, trg, -1); |
| 158 | LY_CHECK_ARG_RET(NULL, src, 0); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 159 | |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 160 | for (u = 0; u < src->count; ++u) { |
| 161 | if (duplicator) { |
| 162 | obj = duplicator(src->objs[u]); |
| 163 | } else { |
| 164 | obj = src->objs[u]; |
| 165 | } |
| 166 | c = trg->count; |
| 167 | i = ly_set_add(trg, obj, options); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 168 | if (i > 0 && (unsigned)i == c) { |
Radek Krejci | 2f2bd90 | 2018-09-18 17:04:24 +0200 | [diff] [blame] | 169 | ++ret; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | API LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 177 | 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] | 178 | { |
Radek Krejci | 1324639 | 2018-09-07 14:57:41 +0200 | [diff] [blame] | 179 | LY_CHECK_ARG_RET(NULL, set, LY_EINVAL); |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 180 | LY_CHECK_ERR_RET(index >= set->count, LOGARG(NULL, index), LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 181 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 182 | if (destructor) { |
| 183 | destructor(set->objs[index]); |
| 184 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 185 | if (index == set->count - 1) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 186 | /* removing last item in set */ |
| 187 | set->objs[index] = NULL; |
| 188 | } else { |
| 189 | /* removing item somewhere in a middle, so put there the last item */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 190 | set->objs[index] = set->objs[set->count - 1]; |
| 191 | set->objs[set->count - 1] = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 192 | } |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 193 | set->count--; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 194 | |
| 195 | return LY_SUCCESS; |
| 196 | } |
| 197 | |
| 198 | API LY_ERR |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 199 | 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] | 200 | { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 201 | uint32_t i; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 202 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 203 | LY_CHECK_ARG_RET(NULL, set, object, LY_EINVAL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 204 | |
| 205 | /* get index */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 206 | for (i = 0; i < set->count; i++) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 207 | if (set->objs[i] == object) { |
| 208 | break; |
| 209 | } |
| 210 | } |
Radek Krejci | 5661616 | 2018-09-18 14:11:00 +0200 | [diff] [blame] | 211 | 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] | 212 | |
Radek Krejci | 820d226 | 2018-09-20 12:15:31 +0200 | [diff] [blame] | 213 | return ly_set_rm_index(set, i, destructor); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 214 | } |
| 215 | |