source: list.c @ b73bcbb

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