forked from barnowl/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
89 lines (72 loc) · 1.5 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "owl.h"
#include <stdlib.h>
#define INITSIZE 10
#define GROWBY 3 / 2
int owl_list_create(owl_list *l)
{
l->size=0;
l->list=g_new(void *, INITSIZE);
l->avail=INITSIZE;
if (l->list==NULL) return(-1);
return(0);
}
int owl_list_get_size(const owl_list *l)
{
return(l->size);
}
static void owl_list_grow(owl_list *l, int n)
{
void *ptr;
if ((l->size+n) > l->avail) {
int avail = MAX(l->avail * GROWBY, l->size + n);
ptr = g_renew(void *, l->list, avail);
if (ptr==NULL) abort();
l->list=ptr;
l->avail = avail;
}
}
void *owl_list_get_element(const owl_list *l, int n)
{
if (n>l->size-1) return(NULL);
return(l->list[n]);
}
int owl_list_insert_element(owl_list *l, int at, void *element)
{
int i;
if(at < 0 || at > l->size) return -1;
owl_list_grow(l, 1);
for (i=l->size; i>at; i--) {
l->list[i]=l->list[i-1];
}
l->list[at] = element;
l->size++;
return(0);
}
int owl_list_append_element(owl_list *l, void *element)
{
return owl_list_insert_element(l, l->size, element);
}
int owl_list_prepend_element(owl_list *l, void *element)
{
return owl_list_insert_element(l, 0, element);
}
int owl_list_remove_element(owl_list *l, int n)
{
int i;
if (n>l->size-1) return(-1);
for (i=n; i<l->size-1; i++) {
l->list[i]=l->list[i+1];
}
l->size--;
return(0);
}
void owl_list_cleanup(owl_list *l, void (*elefree)(void *))
{
int i;
if (elefree) {
for (i = 0; i < l->size; i++) {
(elefree)(l->list[i]);
}
}
g_free(l->list);
}