Changeset ef6e2d1 for list.c


Ignore:
Timestamp:
Feb 28, 2007, 9:07:15 PM (14 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
124aebc
Parents:
cc5b906
git-author:
Nelson Elhage <nelhage@mit.edu> (02/27/07 23:37:40)
git-committer:
Nelson Elhage <nelhage@mit.edu> (02/28/07 21:07:15)
Message:
Refactoring list, to include a generic insert method
File:
1 edited

Legend:

Unmodified
Added
Removed
  • list.c

    r53f421b ref6e2d1  
    2222}
    2323
     24
     25void owl_list_grow(owl_list *l, int n) /*noproto*/
     26{
     27  void *ptr;
     28
     29  if ((l->size+n) > (l->avail/GROWAT)) {
     30    ptr=owl_realloc(l->list, l->avail*GROWBY*sizeof(void *));
     31    if (ptr==NULL) abort();
     32    l->list=ptr;
     33    l->avail=l->avail*GROWBY;
     34  }
     35
     36}
     37
    2438void *owl_list_get_element(owl_list *l, int n)
    2539{
     
    2842}
    2943
    30 int owl_list_append_element(owl_list *l, void *element)
     44int owl_list_insert_element(owl_list *l, int at, void *element)
    3145{
    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;
     46  int i;
     47  if(at < 0 || at > l->size) return -1;
     48  owl_list_grow(l, 1);
     49
     50  for (i=l->size; i>at; i--) {
     51    l->list[i]=l->list[i-1];
    3952  }
    4053
    41   l->list[l->size]=element;
     54  l->list[at] = element;
    4255  l->size++;
    4356  return(0);
    4457}
    4558
     59int owl_list_append_element(owl_list *l, void *element)
     60{
     61  return owl_list_insert_element(l, l->size, element);
     62}
     63
    4664int owl_list_prepend_element(owl_list *l, void *element)
    4765{
    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);
     66  return owl_list_insert_element(l, 0, element);
    6467}
    6568
Note: See TracChangeset for help on using the changeset viewer.