source: list.c @ 8298425

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 8298425 was 53f421b, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Reformatted context and list to new code style
  • Property mode set to 100644
File size: 1.7 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3
4static const char fileIdent[] = "$Id$";
5
6#define INITSIZE 30
7#define GROWAT 2
8#define GROWBY 1.5
9
10int owl_list_create(owl_list *l)
11{
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
19int owl_list_get_size(owl_list *l)
20{
21  return(l->size);
22}
23
24void *owl_list_get_element(owl_list *l, int n)
25{
26  if (n>l->size-1) return(NULL);
27  return(l->list[n]);
28}
29
30int owl_list_append_element(owl_list *l, void *element)
31{
32  void *ptr;
33 
34  if ((l->size+1) > (l->avail/GROWAT)) {
35    ptr=owl_realloc(l->list, l->avail*GROWBY*sizeof(void *));
36    if (ptr==NULL) return(-1);
37    l->list=ptr;
38    l->avail=l->avail*GROWBY;
39  }
40
41  l->list[l->size]=element;
42  l->size++;
43  return(0);
44}
45
46int owl_list_prepend_element(owl_list *l, void *element)
47{
48  void *ptr;
49  int i;
50 
51  if ((l->size+1) > (l->avail/GROWAT)) {
52    ptr=owl_realloc(l->list, l->avail*GROWBY*sizeof(void *));
53    if (ptr==NULL) return(-1);
54    l->list=ptr;
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
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
78/* todo: might leak memory */
79int owl_list_replace_element(owl_list *l, int n, void *element)
80{
81  if (n>l->size-1) return(-1);
82
83  l->list[n]=element;
84  return(0);
85}
86
87void owl_list_free_all(owl_list *l, void (*elefree)(void *))
88{
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
97void owl_list_free_simple(owl_list *l)
98{
99  if (l->list) owl_free(l->list);
100}
Note: See TracBrowser for help on using the repository browser.