list_c: initial capacity. ERROR_MSG(msg,...) supports optional arguments now.
--- 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)) ERROR;
/* add new element to end of list. Basically alocate required memory... */
/* will also increment n by 1 ! */
add_element(elem);
/* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */
- if (pos < (n-1)) for (int i = n-2; i >= 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, ...) \
--- a/absyntax/absyntax.hh Fri Aug 31 19:30:48 2012 +0100
+++ b/absyntax/absyntax.hh Sat Sep 08 10:44:04 2012 +0100
@@ -166,7 +166,7 @@
/* a list of symbols... */
class list_c: public symbol_c {
public:
- int n;
+ int c,n;
symbol_c **elements;
public:
--- a/main.hh Fri Aug 31 19:30:48 2012 +0100
+++ b/main.hh Sat Sep 08 10:44:04 2012 +0100
@@ -40,8 +40,7 @@
#include <stddef.h> /* required for NULL */
#define ERROR error_exit(__FILE__,__LINE__)
-#define ERROR_MSG(msg, ...) error_exit(__FILE__,__LINE__, msg)
-// #define ERROR_MSG(msg, ...) error_exit(__FILE__,__LINE__, msg, __VA_ARGS__)
+#define ERROR_MSG(msg, ...) error_exit(__FILE__,__LINE__, msg, ## __VA_ARGS__)
extern void error_exit(const char *file_name, int line_no, const char *errmsg = NULL, ...);
@@ -129,4 +128,4 @@
#include <inttypes.h>
-#endif // #ifndef _MAIN_HH
\ No newline at end of file
+#endif // #ifndef _MAIN_HH