blob: 92bd64547624835ad9535c7b42f97dcbc355e1d5 [file] [log] [blame]
Nikias Bassenc45ae1f2013-09-17 11:00:31 +02001/*
Martin Szuleckicc4f7aa2014-10-02 23:35:00 +02002 * 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 Bassenc45ae1f2013-09-17 11:00:31 +020021
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 Bassen2501f512019-06-12 21:15:08 +020031#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 Bassenc45ae1f2013-09-17 11:00:31 +020038void collection_init(struct collection *col)
39{
Nikias Bassen2501f512019-06-12 21:15:08 +020040 col->list = malloc(sizeof(void *) * CAPACITY_STEP);
41 assert(col->list);
42 INIT_NULL(col->list, CAPACITY_STEP);
43 col->capacity = CAPACITY_STEP;
Nikias Bassenc45ae1f2013-09-17 11:00:31 +020044}
45
46void collection_free(struct collection *col)
47{
48 free(col->list);
49 col->list = NULL;
50 col->capacity = 0;
51}
52
53void 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 Bassen2501f512019-06-12 21:15:08 +020062 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 Bassenc45ae1f2013-09-17 11:00:31 +020066 col->list[col->capacity] = element;
Nikias Bassen2501f512019-06-12 21:15:08 +020067 col->capacity += CAPACITY_STEP;
Nikias Bassenc45ae1f2013-09-17 11:00:31 +020068}
69
Nikias Bassena6b542b2019-05-22 11:32:11 +020070int collection_remove(struct collection *col, void *element)
Nikias Bassenc45ae1f2013-09-17 11:00:31 +020071{
72 int i;
73 for(i=0; i<col->capacity; i++) {
74 if(col->list[i] == element) {
75 col->list[i] = NULL;
Nikias Bassena6b542b2019-05-22 11:32:11 +020076 return 0;
Nikias Bassenc45ae1f2013-09-17 11:00:31 +020077 }
78 }
79 fprintf(stderr, "%s: WARNING: element %p not present in collection %p (cap %d)", __func__, element, col, col->capacity);
Nikias Bassena6b542b2019-05-22 11:32:11 +020080 return -1;
Nikias Bassenc45ae1f2013-09-17 11:00:31 +020081}
82
83int 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}