1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
275
276 int find_at_end(void* token, bool f(void*, E)) const {
277 // start at the end of the array
278 for (int i = _len-1; i >= 0; i--) {
279 if (f(token, _data[i])) return i;
280 }
281 return -1;
282 }
283
284 void remove(const E& elem) {
285 for (int i = 0; i < _len; i++) {
286 if (_data[i] == elem) {
287 for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
288 _len--;
289 return;
290 }
291 }
292 ShouldNotReachHere();
293 }
294
295 void remove_at(int index) {
296 assert(0 <= index && index < _len, "illegal index");
297 for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
298 _len--;
299 }
300
301 // inserts the given element before the element at index i
302 void insert_before(const int idx, const E& elem) {
303 check_nesting();
304 if (_len == _max) grow(_len);
305 for (int j = _len - 1; j >= idx; j--) {
306 _data[j + 1] = _data[j];
307 }
308 _len++;
309 _data[idx] = elem;
310 }
311
312 void appendAll(const GrowableArray<E>* l) {
313 for (int i = 0; i < l->_len; i++) {
314 raw_at_put_grow(_len, l->_data[i], 0);
315 }
316 }
317
318 void sort(int f(E*,E*)) {
319 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
320 }
|
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
275
276 int find_at_end(void* token, bool f(void*, E)) const {
277 // start at the end of the array
278 for (int i = _len-1; i >= 0; i--) {
279 if (f(token, _data[i])) return i;
280 }
281 return -1;
282 }
283
284 void remove(const E& elem) {
285 for (int i = 0; i < _len; i++) {
286 if (_data[i] == elem) {
287 for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
288 _len--;
289 return;
290 }
291 }
292 ShouldNotReachHere();
293 }
294
295 // The order is preserved.
296 void remove_at(int index) {
297 assert(0 <= index && index < _len, "illegal index");
298 for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
299 _len--;
300 }
301
302 // The order is changed.
303 void delete_at(int index) {
304 assert(0 <= index && index < _len, "illegal index");
305 if (index < --_len) {
306 // Replace removed element with last one.
307 _data[index] = _data[_len];
308 }
309 }
310
311 // inserts the given element before the element at index i
312 void insert_before(const int idx, const E& elem) {
313 check_nesting();
314 if (_len == _max) grow(_len);
315 for (int j = _len - 1; j >= idx; j--) {
316 _data[j + 1] = _data[j];
317 }
318 _len++;
319 _data[idx] = elem;
320 }
321
322 void appendAll(const GrowableArray<E>* l) {
323 for (int i = 0; i < l->_len; i++) {
324 raw_at_put_grow(_len, l->_data[i], 0);
325 }
326 }
327
328 void sort(int f(E*,E*)) {
329 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
330 }
|