Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 1 | /* |
Martin Szulecki | cc4f7aa | 2014-10-02 23:35:00 +0200 | [diff] [blame] | 2 | * collection.c |
| 3 | * |
| 4 | * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> |
| 5 | * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | #include "collection.h" |
| 30 | |
Nikias Bassen | 2501f51 | 2019-06-12 21:15:08 +0200 | [diff] [blame] | 31 | #undef NDEBUG // we need to make sure we still get assertions because we can't handle memory allocation errors |
| 32 | #include <assert.h> |
| 33 | |
| 34 | #define INIT_NULL(addr, count) { unsigned int i_ = 0; for (i_ = 0; i_ < count; i_++) ((void**)addr)[i_] = NULL; } |
| 35 | |
| 36 | #define CAPACITY_STEP 8 |
| 37 | |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 38 | void collection_init(struct collection *col) |
| 39 | { |
Nikias Bassen | 2501f51 | 2019-06-12 21:15:08 +0200 | [diff] [blame] | 40 | col->list = malloc(sizeof(void *) * CAPACITY_STEP); |
| 41 | assert(col->list); |
| 42 | INIT_NULL(col->list, CAPACITY_STEP); |
| 43 | col->capacity = CAPACITY_STEP; |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void collection_free(struct collection *col) |
| 47 | { |
| 48 | free(col->list); |
| 49 | col->list = NULL; |
| 50 | col->capacity = 0; |
| 51 | } |
| 52 | |
| 53 | void collection_add(struct collection *col, void *element) |
| 54 | { |
| 55 | int i; |
| 56 | for(i=0; i<col->capacity; i++) { |
| 57 | if(!col->list[i]) { |
| 58 | col->list[i] = element; |
| 59 | return; |
| 60 | } |
| 61 | } |
Nikias Bassen | 2501f51 | 2019-06-12 21:15:08 +0200 | [diff] [blame] | 62 | void **newlist = realloc(col->list, sizeof(void*) * (col->capacity + CAPACITY_STEP)); |
| 63 | assert(newlist); |
| 64 | col->list = newlist; |
| 65 | INIT_NULL(&col->list[col->capacity], CAPACITY_STEP); |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 66 | col->list[col->capacity] = element; |
Nikias Bassen | 2501f51 | 2019-06-12 21:15:08 +0200 | [diff] [blame] | 67 | col->capacity += CAPACITY_STEP; |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Nikias Bassen | a6b542b | 2019-05-22 11:32:11 +0200 | [diff] [blame] | 70 | int collection_remove(struct collection *col, void *element) |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 71 | { |
| 72 | int i; |
| 73 | for(i=0; i<col->capacity; i++) { |
| 74 | if(col->list[i] == element) { |
| 75 | col->list[i] = NULL; |
Nikias Bassen | a6b542b | 2019-05-22 11:32:11 +0200 | [diff] [blame] | 76 | return 0; |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | fprintf(stderr, "%s: WARNING: element %p not present in collection %p (cap %d)", __func__, element, col, col->capacity); |
Nikias Bassen | a6b542b | 2019-05-22 11:32:11 +0200 | [diff] [blame] | 80 | return -1; |
Nikias Bassen | c45ae1f | 2013-09-17 11:00:31 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int collection_count(struct collection *col) |
| 84 | { |
| 85 | int i, cnt = 0; |
| 86 | for(i=0; i<col->capacity; i++) { |
| 87 | if(col->list[i]) |
| 88 | cnt++; |
| 89 | } |
| 90 | return cnt; |
| 91 | } |