Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 2 | * $Id: arraylist.c,v 1.3 2005/06/14 22:41:51 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
| 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 Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 19 | #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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 29 | |
| 30 | #include "bits.h" |
| 31 | #include "arraylist.h" |
| 32 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 33 | struct array_list* |
| 34 | array_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 | |
| 49 | extern void |
| 50 | array_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 | |
| 59 | void* |
| 60 | array_list_get_idx(struct array_list *this, int i) |
| 61 | { |
| 62 | if(i >= this->length) return NULL; |
| 63 | return this->array[i]; |
| 64 | } |
| 65 | |
| 66 | static 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 Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 75 | (void)memset(this->array + this->size, 0, (new_size-this->size)*sizeof(void*)); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 76 | this->size = new_size; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int |
| 81 | array_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 | |
| 90 | int |
| 91 | array_list_add(struct array_list *this, void *data) |
| 92 | { |
| 93 | return array_list_put_idx(this, this->length, data); |
| 94 | } |
| 95 | |
| 96 | int |
| 97 | array_list_length(struct array_list *this) |
| 98 | { |
| 99 | return this->length; |
| 100 | } |