74 } |
74 } |
75 |
75 |
76 |
76 |
77 |
77 |
78 |
78 |
79 |
79 # define LIST_CAP_INIT 8 |
|
80 # define LIST_CAP_INCR 8 |
80 |
81 |
81 list_c::list_c( |
82 list_c::list_c( |
82 int fl, int fc, const char *ffile, long int forder, |
83 int fl, int fc, const char *ffile, long int forder, |
83 int ll, int lc, const char *lfile, long int lorder) |
84 int ll, int lc, const char *lfile, long int lorder) |
84 :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder) { |
85 :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*))) {} |
85 n = 0; |
|
86 elements = NULL; |
|
87 } |
|
88 |
86 |
89 list_c::list_c(symbol_c *elem, |
87 list_c::list_c(symbol_c *elem, |
90 int fl, int fc, const char *ffile, long int forder, |
88 int fl, int fc, const char *ffile, long int forder, |
91 int ll, int lc, const char *lfile, long int lorder) |
89 int ll, int lc, const char *lfile, long int lorder) |
92 :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder) { |
90 :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*))) |
93 n = 0; |
91 { add_element(elem); } |
94 elements = NULL; |
|
95 add_element(elem); |
|
96 } |
|
97 |
92 |
98 /* append a new element to the end of the list */ |
93 /* append a new element to the end of the list */ |
99 void list_c::add_element(symbol_c *elem) { |
94 void list_c::add_element(symbol_c *elem) { |
100 //printf("list_c::add_element()\n"); |
95 // printf("list_c::add_element()\n"); |
101 n++; |
96 if((c <= n) && !(elements=(symbol_c**)realloc(elements,(c+=LIST_CAP_INCR)*sizeof(symbol_c *)))) ERROR_MSG("out of memory"); |
102 elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); |
97 elements[n++] = elem; |
103 if (elements == NULL) |
|
104 ERROR_MSG("Out of memory"); |
|
105 elements[n - 1] = elem; |
|
106 |
98 |
107 if (elem == NULL) |
99 if(elem == NULL) return; |
108 return; |
|
109 |
100 |
110 /* adjust the location parameters, taking into account the new element. */ |
101 /* adjust the location parameters, taking into account the new element. */ |
111 if ((first_line == elem->first_line) && |
102 if ((first_line == elem->first_line) && |
112 (first_column > elem->first_column)) { |
103 (first_column > elem->first_column)) { |
113 first_column = elem->first_column; |
104 first_column = elem->first_column; |
128 |
119 |
129 /* insert a new element before position pos. */ |
120 /* insert a new element before position pos. */ |
130 /* To insert into the begining of list, call with pos=0 */ |
121 /* To insert into the begining of list, call with pos=0 */ |
131 /* To insert into the end of list, call with pos=list->n */ |
122 /* To insert into the end of list, call with pos=list->n */ |
132 void list_c::insert_element(symbol_c *elem, int pos) { |
123 void list_c::insert_element(symbol_c *elem, int pos) { |
133 if (pos > n) ERROR; |
124 if((pos<0) || (n<pos)) ERROR; |
134 |
125 |
135 /* add new element to end of list. Basically alocate required memory... */ |
126 /* add new element to end of list. Basically alocate required memory... */ |
136 /* will also increment n by 1 ! */ |
127 /* will also increment n by 1 ! */ |
137 add_element(elem); |
128 add_element(elem); |
138 /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */ |
129 /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */ |
139 if (pos < (n-1)) for (int i = n-2; i >= pos; i--) elements[i+1] = elements[i]; |
130 if(pos < (n-1)){ |
140 elements[pos] = elem; |
131 for(int i=n-2 ; i>=pos ; --i) elements[i+1] = elements[i]; |
|
132 elements[pos] = elem; |
|
133 } |
141 } |
134 } |
142 |
135 |
143 |
136 |
144 /* remove element at position pos. */ |
137 /* remove element at position pos. */ |
145 void list_c::remove_element(int pos) { |
138 void list_c::remove_element(int pos) { |
146 if (pos > n) ERROR; |
139 if((pos<0) || (n<=pos)) ERROR; |
147 |
140 |
148 /* Shift all elements down one position, starting at the entry to delete. */ |
141 /* Shift all elements down one position, starting at the entry to delete. */ |
149 for (int i = pos; i < n-1; i++) elements[i] = elements[i+1]; |
142 for (int i = pos; i < n-1; i++) elements[i] = elements[i+1]; |
150 /* corrent the new size, and free unused memory */ |
143 /* corrent the new size */ |
151 n--; |
144 n--; |
152 elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); |
145 /* elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); */ |
153 } |
146 } |
154 |
147 |
155 #define SYM_LIST(class_name_c, ...) \ |
148 #define SYM_LIST(class_name_c, ...) \ |
156 class_name_c::class_name_c( \ |
149 class_name_c::class_name_c( \ |
157 int fl, int fc, const char *ffile, long int forder, \ |
150 int fl, int fc, const char *ffile, long int forder, \ |