release-1.10release-1.8release-1.9
Last change
on this file since fda61d3 was
fda61d3,
checked in by David Benjamin <davidben@mit.edu>, 13 years ago
|
g_strdup and g_new0 never fail
Remove pointless failure checks cluttering the code.
|
-
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 |
---|
[0f15f12] | 5 | #define GROWBY 3 / 2 |
---|
[7d4fbcd] | 6 | |
---|
[fda61d3] | 7 | void owl_list_create(owl_list *l) |
---|
[53f421b] | 8 | { |
---|
[7d4fbcd] | 9 | l->size=0; |
---|
[96828e4] | 10 | l->list=g_new(void *, INITSIZE); |
---|
[7d4fbcd] | 11 | l->avail=INITSIZE; |
---|
| 12 | } |
---|
| 13 | |
---|
[77bced3] | 14 | int owl_list_get_size(const owl_list *l) |
---|
[53f421b] | 15 | { |
---|
[7d4fbcd] | 16 | return(l->size); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | |
---|
[e9c6fc8] | 20 | static void owl_list_grow(owl_list *l, int n) |
---|
[53f421b] | 21 | { |
---|
[d09e5a1] | 22 | void *ptr; |
---|
[ef6e2d1] | 23 | |
---|
[50622a5] | 24 | if ((l->size+n) > l->avail) { |
---|
[0f15f12] | 25 | int avail = MAX(l->avail * GROWBY, l->size + n); |
---|
[35b6eb9] | 26 | ptr = g_renew(void *, l->list, avail); |
---|
[ef6e2d1] | 27 | if (ptr==NULL) abort(); |
---|
[d09e5a1] | 28 | l->list=ptr; |
---|
[0f15f12] | 29 | l->avail = avail; |
---|
[7d4fbcd] | 30 | } |
---|
| 31 | |
---|
| 32 | } |
---|
| 33 | |
---|
[77bced3] | 34 | void *owl_list_get_element(const owl_list *l, int n) |
---|
[ef6e2d1] | 35 | { |
---|
| 36 | if (n>l->size-1) return(NULL); |
---|
| 37 | return(l->list[n]); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | int owl_list_insert_element(owl_list *l, int at, void *element) |
---|
[53f421b] | 41 | { |
---|
[7d4fbcd] | 42 | int i; |
---|
[ef6e2d1] | 43 | if(at < 0 || at > l->size) return -1; |
---|
| 44 | owl_list_grow(l, 1); |
---|
[7d4fbcd] | 45 | |
---|
[ef6e2d1] | 46 | for (i=l->size; i>at; i--) { |
---|
[7d4fbcd] | 47 | l->list[i]=l->list[i-1]; |
---|
| 48 | } |
---|
[ef6e2d1] | 49 | |
---|
| 50 | l->list[at] = element; |
---|
[7d4fbcd] | 51 | l->size++; |
---|
| 52 | return(0); |
---|
| 53 | } |
---|
| 54 | |
---|
[ef6e2d1] | 55 | int owl_list_append_element(owl_list *l, void *element) |
---|
| 56 | { |
---|
| 57 | return owl_list_insert_element(l, l->size, element); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | int owl_list_prepend_element(owl_list *l, void *element) |
---|
| 61 | { |
---|
| 62 | return owl_list_insert_element(l, 0, element); |
---|
| 63 | } |
---|
| 64 | |
---|
[53f421b] | 65 | int owl_list_remove_element(owl_list *l, int n) |
---|
| 66 | { |
---|
[7d4fbcd] | 67 | int i; |
---|
| 68 | |
---|
| 69 | if (n>l->size-1) return(-1); |
---|
| 70 | for (i=n; i<l->size-1; i++) { |
---|
| 71 | l->list[i]=l->list[i+1]; |
---|
| 72 | } |
---|
| 73 | l->size--; |
---|
| 74 | return(0); |
---|
| 75 | } |
---|
| 76 | |
---|
[8c59178] | 77 | void owl_list_cleanup(owl_list *l, void (*elefree)(void *)) |
---|
[53f421b] | 78 | { |
---|
[7d4fbcd] | 79 | int i; |
---|
| 80 | |
---|
[5e5f08f] | 81 | if (elefree) { |
---|
| 82 | for (i = 0; i < l->size; i++) { |
---|
| 83 | (elefree)(l->list[i]); |
---|
| 84 | } |
---|
[7d4fbcd] | 85 | } |
---|
[ddbbcffa] | 86 | g_free(l->list); |
---|
[7d4fbcd] | 87 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.