absyntax/absyntax.cc
changeset 438 744b125d911e
parent 417 d48f53715f77
child 564 dabffc3086dc
equal deleted inserted replaced
437:0e09a8840c92 438:744b125d911e
   129 
   129 
   130 /* insert a new element before position pos. */
   130 /* insert a new element before position pos. */
   131 /* To insert into the begining of list, call with pos=0  */
   131 /* To insert into the begining of list, call with pos=0  */
   132 /* To insert into the end of list, call with pos=list->n */
   132 /* To insert into the end of list, call with pos=list->n */
   133 void list_c::insert_element(symbol_c *elem, int pos) {
   133 void list_c::insert_element(symbol_c *elem, int pos) {
   134   int i;
       
   135   if (pos > n) ERROR;
   134   if (pos > n) ERROR;
   136   
   135   
   137   /* add new element to end of list. Basically alocate required memory... */
   136   /* add new element to end of list. Basically alocate required memory... */
   138   /* will also increment n by 1 ! */
   137   /* will also increment n by 1 ! */
   139   add_element(elem);
   138   add_element(elem);
   140   /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */
   139   /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */
   141   if (pos < (n-1)) for (i = n-2; i >= pos; i--) elements[i+1] = elements[i];
   140   if (pos < (n-1)) for (int i = n-2; i >= pos; i--) elements[i+1] = elements[i];
   142   elements[pos] = elem;
   141   elements[pos] = elem;
   143 }
   142 }
   144 
   143 
   145 
   144 
       
   145 /* remove element at position pos. */
       
   146 void list_c::remove_element(int pos) {
       
   147   if (pos > n) ERROR;
       
   148   
       
   149   /* Shift all elements down one position, starting at the entry to delete. */
       
   150   for (int i = pos; i < n-1; i++) elements[i] = elements[i+1];
       
   151   /* corrent the new size, and free unused memory */
       
   152   n--;
       
   153   elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *));
       
   154 }
   146 
   155 
   147 #define SYM_LIST(class_name_c, ...)								\
   156 #define SYM_LIST(class_name_c, ...)								\
   148 class_name_c::class_name_c(									\
   157 class_name_c::class_name_c(									\
   149                            int fl, int fc, const char *ffile, long int forder,			\
   158                            int fl, int fc, const char *ffile, long int forder,			\
   150                            int ll, int lc, const char *lfile, long int lorder)			\
   159                            int ll, int lc, const char *lfile, long int lorder)			\