316 }
317
318 // The order is preserved.
319 void remove_at(int index) {
320 assert(0 <= index && index < _len, "illegal index");
321 for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
322 _len--;
323 }
324
325 // The order is changed.
326 void delete_at(int index) {
327 assert(0 <= index && index < _len, "illegal index");
328 if (index < --_len) {
329 // Replace removed element with last one.
330 _data[index] = _data[_len];
331 }
332 }
333
334 // inserts the given element before the element at index i
335 void insert_before(const int idx, const E& elem) {
336 check_nesting();
337 if (_len == _max) grow(_len);
338 for (int j = _len - 1; j >= idx; j--) {
339 _data[j + 1] = _data[j];
340 }
341 _len++;
342 _data[idx] = elem;
343 }
344
345 void appendAll(const GrowableArray<E>* l) {
346 for (int i = 0; i < l->_len; i++) {
347 raw_at_put_grow(_len, l->_data[i], 0);
348 }
349 }
350
351 void sort(int f(E*,E*)) {
352 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
353 }
354 // sort by fixed-stride sub arrays:
355 void sort(int f(E*,E*), int stride) {
356 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
357 }
358 };
359
360 // Global GrowableArray methods (one instance in the library per each 'E' type).
361
362 template<class E> void GrowableArray<E>::grow(int j) {
363 // grow the array by doubling its size (amortized growth)
364 int old_max = _max;
365 if (_max == 0) _max = 1; // prevent endless loop
366 while (j >= _max) _max = _max*2;
367 // j < _max
|
316 }
317
318 // The order is preserved.
319 void remove_at(int index) {
320 assert(0 <= index && index < _len, "illegal index");
321 for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
322 _len--;
323 }
324
325 // The order is changed.
326 void delete_at(int index) {
327 assert(0 <= index && index < _len, "illegal index");
328 if (index < --_len) {
329 // Replace removed element with last one.
330 _data[index] = _data[_len];
331 }
332 }
333
334 // inserts the given element before the element at index i
335 void insert_before(const int idx, const E& elem) {
336 assert(0 <= idx && idx <= _len, "illegal index");
337 check_nesting();
338 if (_len == _max) grow(_len);
339 for (int j = _len - 1; j >= idx; j--) {
340 _data[j + 1] = _data[j];
341 }
342 _len++;
343 _data[idx] = elem;
344 }
345
346 void appendAll(const GrowableArray<E>* l) {
347 for (int i = 0; i < l->_len; i++) {
348 raw_at_put_grow(_len, l->_data[i], E());
349 }
350 }
351
352 void sort(int f(E*,E*)) {
353 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
354 }
355 // sort by fixed-stride sub arrays:
356 void sort(int f(E*,E*), int stride) {
357 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
358 }
359 };
360
361 // Global GrowableArray methods (one instance in the library per each 'E' type).
362
363 template<class E> void GrowableArray<E>::grow(int j) {
364 // grow the array by doubling its size (amortized growth)
365 int old_max = _max;
366 if (_max == 0) _max = 1; // prevent endless loop
367 while (j >= _max) _max = _max*2;
368 // j < _max
|