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