[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 | |
---|
[53f421b] | 24 | void *owl_list_get_element(owl_list *l, int n) |
---|
| 25 | { |
---|
[7d4fbcd] | 26 | if (n>l->size-1) return(NULL); |
---|
| 27 | return(l->list[n]); |
---|
| 28 | } |
---|
| 29 | |
---|
[53f421b] | 30 | int owl_list_append_element(owl_list *l, void *element) |
---|
| 31 | { |
---|
[d09e5a1] | 32 | void *ptr; |
---|
| 33 | |
---|
[7d4fbcd] | 34 | if ((l->size+1) > (l->avail/GROWAT)) { |
---|
[d09e5a1] | 35 | ptr=owl_realloc(l->list, l->avail*GROWBY*sizeof(void *)); |
---|
| 36 | if (ptr==NULL) return(-1); |
---|
| 37 | l->list=ptr; |
---|
[7d4fbcd] | 38 | l->avail=l->avail*GROWBY; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | l->list[l->size]=element; |
---|
| 42 | l->size++; |
---|
| 43 | return(0); |
---|
| 44 | } |
---|
| 45 | |
---|
[53f421b] | 46 | int owl_list_prepend_element(owl_list *l, void *element) |
---|
| 47 | { |
---|
[d09e5a1] | 48 | void *ptr; |
---|
[7d4fbcd] | 49 | int i; |
---|
| 50 | |
---|
| 51 | if ((l->size+1) > (l->avail/GROWAT)) { |
---|
[d09e5a1] | 52 | ptr=owl_realloc(l->list, l->avail*GROWBY*sizeof(void *)); |
---|
| 53 | if (ptr==NULL) return(-1); |
---|
| 54 | l->list=ptr; |
---|
[7d4fbcd] | 55 | l->avail=l->avail*GROWBY; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | for (i=l->size; i>0; i--) { |
---|
| 59 | l->list[i]=l->list[i-1]; |
---|
| 60 | } |
---|
| 61 | l->list[0]=element; |
---|
| 62 | l->size++; |
---|
| 63 | return(0); |
---|
| 64 | } |
---|
| 65 | |
---|
[53f421b] | 66 | int owl_list_remove_element(owl_list *l, int n) |
---|
| 67 | { |
---|
[7d4fbcd] | 68 | int i; |
---|
| 69 | |
---|
| 70 | if (n>l->size-1) return(-1); |
---|
| 71 | for (i=n; i<l->size-1; i++) { |
---|
| 72 | l->list[i]=l->list[i+1]; |
---|
| 73 | } |
---|
| 74 | l->size--; |
---|
| 75 | return(0); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /* todo: might leak memory */ |
---|
[53f421b] | 79 | int owl_list_replace_element(owl_list *l, int n, void *element) |
---|
| 80 | { |
---|
[7d4fbcd] | 81 | if (n>l->size-1) return(-1); |
---|
| 82 | |
---|
| 83 | l->list[n]=element; |
---|
| 84 | return(0); |
---|
| 85 | } |
---|
| 86 | |
---|
[53f421b] | 87 | void owl_list_free_all(owl_list *l, void (*elefree)(void *)) |
---|
| 88 | { |
---|
[7d4fbcd] | 89 | int i; |
---|
| 90 | |
---|
| 91 | for (i=0; i<l->size; i++) { |
---|
| 92 | (elefree)(l->list[i]); |
---|
| 93 | } |
---|
| 94 | owl_free(l->list); |
---|
| 95 | } |
---|
| 96 | |
---|
[53f421b] | 97 | void owl_list_free_simple(owl_list *l) |
---|
| 98 | { |
---|
[7d4fbcd] | 99 | if (l->list) owl_free(l->list); |
---|
| 100 | } |
---|