diff -r ea78924a1f60 -r 7421cb63defa absyntax/absyntax.cc --- a/absyntax/absyntax.cc Fri Aug 31 19:30:48 2012 +0100 +++ b/absyntax/absyntax.cc Sat Sep 08 10:44:04 2012 +0100 @@ -76,36 +76,27 @@ - +# define LIST_CAP_INIT 8 +# define LIST_CAP_INCR 8 list_c::list_c( int fl, int fc, const char *ffile, long int forder, int ll, int lc, const char *lfile, long int lorder) - :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder) { - n = 0; - elements = NULL; -} + :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder),c(LIST_CAP_INIT),n(0),elements((symbol_c**)malloc(LIST_CAP_INIT*sizeof(symbol_c*))) {} list_c::list_c(symbol_c *elem, int fl, int fc, const char *ffile, long int forder, int ll, int lc, const char *lfile, long int lorder) - :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder) { - n = 0; - elements = NULL; - add_element(elem); -} + :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder),c(LIST_CAP_INIT),n(0),elements((symbol_c**)malloc(LIST_CAP_INIT*sizeof(symbol_c*))) + { add_element(elem); } /* append a new element to the end of the list */ void list_c::add_element(symbol_c *elem) { -//printf("list_c::add_element()\n"); - n++; - elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); - if (elements == NULL) - ERROR_MSG("Out of memory"); - elements[n - 1] = elem; + // printf("list_c::add_element()\n"); + if((c <= n) && !(elements=(symbol_c**)realloc(elements,(c+=LIST_CAP_INCR)*sizeof(symbol_c *)))) ERROR_MSG("out of memory"); + elements[n++] = elem; - if (elem == NULL) - return; + if(elem == NULL) return; /* adjust the location parameters, taking into account the new element. */ if ((first_line == elem->first_line) && @@ -130,26 +121,28 @@ /* To insert into the begining of list, call with pos=0 */ /* To insert into the end of list, call with pos=list->n */ void list_c::insert_element(symbol_c *elem, int pos) { - if (pos > n) ERROR; + if((pos<0) || (n= pos; i--) elements[i+1] = elements[i]; - elements[pos] = elem; + if(pos < (n-1)){ + for(int i=n-2 ; i>=pos ; --i) elements[i+1] = elements[i]; + elements[pos] = elem; + } } /* remove element at position pos. */ void list_c::remove_element(int pos) { - if (pos > n) ERROR; + if((pos<0) || (n<=pos)) ERROR; /* Shift all elements down one position, starting at the entry to delete. */ for (int i = pos; i < n-1; i++) elements[i] = elements[i+1]; - /* corrent the new size, and free unused memory */ + /* corrent the new size */ n--; - elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); + /* elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); */ } #define SYM_LIST(class_name_c, ...) \