[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | |
---|
[1aee7d9] | 4 | static const char fileIdent[] = "$Id$"; |
---|
| 5 | |
---|
[7d4fbcd] | 6 | #define INITSIZE 30 |
---|
| 7 | #define GROWAT 2 |
---|
| 8 | #define GROWBY 1.5 |
---|
| 9 | |
---|
[53f421b] | 10 | int owl_list_create(owl_list *l) |
---|
| 11 | { |
---|
[7d4fbcd] | 12 | l->size=0; |
---|
| 13 | l->list=(void **)owl_malloc(INITSIZE*sizeof(void *)); |
---|
| 14 | l->avail=INITSIZE; |
---|
| 15 | if (l->list==NULL) return(-1); |
---|
| 16 | return(0); |
---|
| 17 | } |
---|
| 18 | |
---|
[53f421b] | 19 | int owl_list_get_size(owl_list *l) |
---|
| 20 | { |
---|
[7d4fbcd] | 21 | return(l->size); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | |
---|
[ef6e2d1] | 25 | void owl_list_grow(owl_list *l, int n) /*noproto*/ |
---|
[53f421b] | 26 | { |
---|
[d09e5a1] | 27 | void *ptr; |
---|
[ef6e2d1] | 28 | |
---|
| 29 | if ((l->size+n) > (l->avail/GROWAT)) { |
---|
[d09e5a1] | 30 | ptr=owl_realloc(l->list, l->avail*GROWBY*sizeof(void *)); |
---|
[ef6e2d1] | 31 | if (ptr==NULL) abort(); |
---|
[d09e5a1] | 32 | l->list=ptr; |
---|
[7d4fbcd] | 33 | l->avail=l->avail*GROWBY; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | } |
---|
| 37 | |
---|
[ef6e2d1] | 38 | void *owl_list_get_element(owl_list *l, int n) |
---|
| 39 | { |
---|
| 40 | if (n>l->size-1) return(NULL); |
---|
| 41 | return(l->list[n]); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | int owl_list_insert_element(owl_list *l, int at, void *element) |
---|
[53f421b] | 45 | { |
---|
[7d4fbcd] | 46 | int i; |
---|
[ef6e2d1] | 47 | if(at < 0 || at > l->size) return -1; |
---|
| 48 | owl_list_grow(l, 1); |
---|
[7d4fbcd] | 49 | |
---|
[ef6e2d1] | 50 | for (i=l->size; i>at; i--) { |
---|
[7d4fbcd] | 51 | l->list[i]=l->list[i-1]; |
---|
| 52 | } |
---|
[ef6e2d1] | 53 | |
---|
| 54 | l->list[at] = element; |
---|
[7d4fbcd] | 55 | l->size++; |
---|
| 56 | return(0); |
---|
| 57 | } |
---|
| 58 | |
---|
[ef6e2d1] | 59 | int owl_list_append_element(owl_list *l, void *element) |
---|
| 60 | { |
---|
| 61 | return owl_list_insert_element(l, l->size, element); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | int owl_list_prepend_element(owl_list *l, void *element) |
---|
| 65 | { |
---|
| 66 | return owl_list_insert_element(l, 0, element); |
---|
| 67 | } |
---|
| 68 | |
---|
[53f421b] | 69 | int owl_list_remove_element(owl_list *l, int n) |
---|
| 70 | { |
---|
[7d4fbcd] | 71 | int i; |
---|
| 72 | |
---|
| 73 | if (n>l->size-1) return(-1); |
---|
| 74 | for (i=n; i<l->size-1; i++) { |
---|
| 75 | l->list[i]=l->list[i+1]; |
---|
| 76 | } |
---|
| 77 | l->size--; |
---|
| 78 | return(0); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /* todo: might leak memory */ |
---|
[53f421b] | 82 | int owl_list_replace_element(owl_list *l, int n, void *element) |
---|
| 83 | { |
---|
[7d4fbcd] | 84 | if (n>l->size-1) return(-1); |
---|
| 85 | |
---|
| 86 | l->list[n]=element; |
---|
| 87 | return(0); |
---|
| 88 | } |
---|
| 89 | |
---|
[53f421b] | 90 | void owl_list_free_all(owl_list *l, void (*elefree)(void *)) |
---|
| 91 | { |
---|
[7d4fbcd] | 92 | int i; |
---|
| 93 | |
---|
| 94 | for (i=0; i<l->size; i++) { |
---|
| 95 | (elefree)(l->list[i]); |
---|
| 96 | } |
---|
| 97 | owl_free(l->list); |
---|
| 98 | } |
---|
| 99 | |
---|
[53f421b] | 100 | void owl_list_free_simple(owl_list *l) |
---|
| 101 | { |
---|
[7d4fbcd] | 102 | if (l->list) owl_free(l->list); |
---|
| 103 | } |
---|