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