release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change
on this file since 3a6277a was
77bced3,
checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
|
Add const qualifiers for owl_list *.
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
|
-
Property mode set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | |
---|
[50622a5] | 4 | #define INITSIZE 10 |
---|
[7d4fbcd] | 5 | #define GROWBY 1.5 |
---|
| 6 | |
---|
[53f421b] | 7 | int owl_list_create(owl_list *l) |
---|
| 8 | { |
---|
[7d4fbcd] | 9 | l->size=0; |
---|
[4d86e06] | 10 | l->list=owl_malloc(INITSIZE*sizeof(void *)); |
---|
[7d4fbcd] | 11 | l->avail=INITSIZE; |
---|
| 12 | if (l->list==NULL) return(-1); |
---|
| 13 | return(0); |
---|
| 14 | } |
---|
| 15 | |
---|
[77bced3] | 16 | int owl_list_get_size(const owl_list *l) |
---|
[53f421b] | 17 | { |
---|
[7d4fbcd] | 18 | return(l->size); |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | |
---|
[ef6e2d1] | 22 | void owl_list_grow(owl_list *l, int n) /*noproto*/ |
---|
[53f421b] | 23 | { |
---|
[d09e5a1] | 24 | void *ptr; |
---|
[ef6e2d1] | 25 | |
---|
[50622a5] | 26 | if ((l->size+n) > l->avail) { |
---|
[d09e5a1] | 27 | ptr=owl_realloc(l->list, l->avail*GROWBY*sizeof(void *)); |
---|
[ef6e2d1] | 28 | if (ptr==NULL) abort(); |
---|
[d09e5a1] | 29 | l->list=ptr; |
---|
[7d4fbcd] | 30 | l->avail=l->avail*GROWBY; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | } |
---|
| 34 | |
---|
[77bced3] | 35 | void *owl_list_get_element(const owl_list *l, int n) |
---|
[ef6e2d1] | 36 | { |
---|
| 37 | if (n>l->size-1) return(NULL); |
---|
| 38 | return(l->list[n]); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | int owl_list_insert_element(owl_list *l, int at, void *element) |
---|
[53f421b] | 42 | { |
---|
[7d4fbcd] | 43 | int i; |
---|
[ef6e2d1] | 44 | if(at < 0 || at > l->size) return -1; |
---|
| 45 | owl_list_grow(l, 1); |
---|
[7d4fbcd] | 46 | |
---|
[ef6e2d1] | 47 | for (i=l->size; i>at; i--) { |
---|
[7d4fbcd] | 48 | l->list[i]=l->list[i-1]; |
---|
| 49 | } |
---|
[ef6e2d1] | 50 | |
---|
| 51 | l->list[at] = element; |
---|
[7d4fbcd] | 52 | l->size++; |
---|
| 53 | return(0); |
---|
| 54 | } |
---|
| 55 | |
---|
[ef6e2d1] | 56 | int owl_list_append_element(owl_list *l, void *element) |
---|
| 57 | { |
---|
| 58 | return owl_list_insert_element(l, l->size, element); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | int owl_list_prepend_element(owl_list *l, void *element) |
---|
| 62 | { |
---|
| 63 | return owl_list_insert_element(l, 0, element); |
---|
| 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 | |
---|
[53f421b] | 78 | void owl_list_free_all(owl_list *l, void (*elefree)(void *)) |
---|
| 79 | { |
---|
[7d4fbcd] | 80 | int i; |
---|
| 81 | |
---|
| 82 | for (i=0; i<l->size; i++) { |
---|
| 83 | (elefree)(l->list[i]); |
---|
| 84 | } |
---|
| 85 | owl_free(l->list); |
---|
| 86 | } |
---|
| 87 | |
---|
[53f421b] | 88 | void owl_list_free_simple(owl_list *l) |
---|
| 89 | { |
---|
[7d4fbcd] | 90 | if (l->list) owl_free(l->list); |
---|
| 91 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.