233 E const& at(int i) const {
234 assert(0 <= i && i < _len, "illegal index");
235 return _data[i];
236 }
237
238 E* adr_at(int i) const {
239 assert(0 <= i && i < _len, "illegal index");
240 return &_data[i];
241 }
242
243 E first() const {
244 assert(_len > 0, "empty list");
245 return _data[0];
246 }
247
248 E top() const {
249 assert(_len > 0, "empty list");
250 return _data[_len-1];
251 }
252
253 GrowableArrayIterator<E> begin() const {
254 return GrowableArrayIterator<E>(this, 0);
255 }
256
257 GrowableArrayIterator<E> end() const {
258 return GrowableArrayIterator<E>(this, length());
259 }
260
261 void push(const E& elem) { append(elem); }
262
263 E pop() {
264 assert(_len > 0, "empty list");
265 return _data[--_len];
266 }
267
268 void at_put(int i, const E& elem) {
269 assert(0 <= i && i < _len, "illegal index");
270 _data[i] = elem;
271 }
272
344
345 // The order is changed.
346 void delete_at(int index) {
347 assert(0 <= index && index < _len, "illegal index");
348 if (index < --_len) {
349 // Replace removed element with last one.
350 _data[index] = _data[_len];
351 }
352 }
353
354 // inserts the given element before the element at index i
355 void insert_before(const int idx, const E& elem) {
356 assert(0 <= idx && idx <= _len, "illegal index");
357 check_nesting();
358 if (_len == _max) grow(_len);
359 for (int j = _len - 1; j >= idx; j--) {
360 _data[j + 1] = _data[j];
361 }
362 _len++;
363 _data[idx] = elem;
364 }
365
366 void appendAll(const GrowableArray<E>* l) {
367 for (int i = 0; i < l->_len; i++) {
368 raw_at_put_grow(_len, l->_data[i], E());
369 }
370 }
371
372 void sort(int f(E*,E*)) {
373 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
374 }
375 // sort by fixed-stride sub arrays:
376 void sort(int f(E*,E*), int stride) {
377 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
378 }
379
380 // Binary search and insertion utility. Search array for element
381 // matching key according to the static compare function. Insert
382 // that element is not already in the list. Assumes the list is
383 // already sorted according to compare function.
|
233 E const& at(int i) const {
234 assert(0 <= i && i < _len, "illegal index");
235 return _data[i];
236 }
237
238 E* adr_at(int i) const {
239 assert(0 <= i && i < _len, "illegal index");
240 return &_data[i];
241 }
242
243 E first() const {
244 assert(_len > 0, "empty list");
245 return _data[0];
246 }
247
248 E top() const {
249 assert(_len > 0, "empty list");
250 return _data[_len-1];
251 }
252
253 E last() const {
254 return top();
255 }
256
257 GrowableArrayIterator<E> begin() const {
258 return GrowableArrayIterator<E>(this, 0);
259 }
260
261 GrowableArrayIterator<E> end() const {
262 return GrowableArrayIterator<E>(this, length());
263 }
264
265 void push(const E& elem) { append(elem); }
266
267 E pop() {
268 assert(_len > 0, "empty list");
269 return _data[--_len];
270 }
271
272 void at_put(int i, const E& elem) {
273 assert(0 <= i && i < _len, "illegal index");
274 _data[i] = elem;
275 }
276
348
349 // The order is changed.
350 void delete_at(int index) {
351 assert(0 <= index && index < _len, "illegal index");
352 if (index < --_len) {
353 // Replace removed element with last one.
354 _data[index] = _data[_len];
355 }
356 }
357
358 // inserts the given element before the element at index i
359 void insert_before(const int idx, const E& elem) {
360 assert(0 <= idx && idx <= _len, "illegal index");
361 check_nesting();
362 if (_len == _max) grow(_len);
363 for (int j = _len - 1; j >= idx; j--) {
364 _data[j + 1] = _data[j];
365 }
366 _len++;
367 _data[idx] = elem;
368 }
369
370 void insert_before(const int idx, const GrowableArray<E>* array) {
371 assert(0 <= idx && idx <= _len, "illegal index");
372 check_nesting();
373 int array_len = array->length();
374 int new_len = _len + array_len;
375 if (new_len >= _max) grow(new_len);
376
377 for (int j = _len - 1; j >= idx; j--) {
378 _data[j + array_len] = _data[j];
379 }
380
381 for (int j = 0; j < array_len; j++) {
382 _data[idx + j] = array->_data[j];
383 }
384
385 _len += array_len;
386 }
387
388 void appendAll(const GrowableArray<E>* l) {
389 for (int i = 0; i < l->_len; i++) {
390 raw_at_put_grow(_len, l->_data[i], E());
391 }
392 }
393
394 void sort(int f(E*,E*)) {
395 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
396 }
397 // sort by fixed-stride sub arrays:
398 void sort(int f(E*,E*), int stride) {
399 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
400 }
401
402 // Binary search and insertion utility. Search array for element
403 // matching key according to the static compare function. Insert
404 // that element is not already in the list. Assumes the list is
405 // already sorted according to compare function.
|