blob: be93e2639b32b4d6fd3ff9802c4a1445be25b128 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clark4504df72007-03-13 08:26:20 +00002 * $Id: arraylist.c,v 1.3 2005/06/14 22:41:51 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
4 * Copyright Metaparadigm Pte. Ltd. 2004.
5 * Michael Clark <michael@metaparadigm.com>
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 (LGPL)
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: http://www.gnu.org/
16 *
17 */
18
Michael Clark4504df72007-03-13 08:26:20 +000019#include "config.h"
20
21#if STDC_HEADERS
22# include <stdlib.h>
23# include <string.h>
24#endif /* STDC_HEADERS */
25
26#if HAVE_STRINGS_H
27# include <strings.h>
28#endif /* HAVE_STRINGS_H */
Michael Clarkf0d08882007-03-13 08:26:18 +000029
30#include "bits.h"
31#include "arraylist.h"
32
Michael Clarkf0d08882007-03-13 08:26:18 +000033struct array_list*
34array_list_new(array_list_free_fn *free_fn)
35{
36 struct array_list *this;
37
38 if(!(this = calloc(1, sizeof(struct array_list)))) return NULL;
39 this->size = ARRAY_LIST_DEFAULT_SIZE;
40 this->length = 0;
41 this->free_fn = free_fn;
42 if(!(this->array = calloc(sizeof(void*), this->size))) {
43 free(this);
44 return NULL;
45 }
46 return this;
47}
48
49extern void
50array_list_free(struct array_list *this)
51{
52 int i;
53 for(i = 0; i < this->length; i++)
54 if(this->array[i]) this->free_fn(this->array[i]);
55 free(this->array);
56 free(this);
57}
58
59void*
60array_list_get_idx(struct array_list *this, int i)
61{
62 if(i >= this->length) return NULL;
63 return this->array[i];
64}
65
66static int array_list_expand_internal(struct array_list *this, int max)
67{
68 void *t;
69 int new_size;
70
71 if(max < this->size) return 0;
72 new_size = max(this->size << 1, max);
73 if(!(t = realloc(this->array, new_size*sizeof(void*)))) return -1;
74 this->array = t;
Michael Clark4504df72007-03-13 08:26:20 +000075 (void)memset(this->array + this->size, 0, (new_size-this->size)*sizeof(void*));
Michael Clarkf0d08882007-03-13 08:26:18 +000076 this->size = new_size;
77 return 0;
78}
79
80int
81array_list_put_idx(struct array_list *this, int idx, void *data)
82{
83 if(array_list_expand_internal(this, idx)) return -1;
84 if(this->array[idx]) this->free_fn(this->array[idx]);
85 this->array[idx] = data;
86 if(this->length <= idx) this->length = idx + 1;
87 return 0;
88}
89
90int
91array_list_add(struct array_list *this, void *data)
92{
93 return array_list_put_idx(this, this->length, data);
94}
95
96int
97array_list_length(struct array_list *this)
98{
99 return this->length;
100}