src/share/vm/utilities/growableArray.hpp

Tue, 24 Jul 2018 13:22:11 +0800

author
aoqi
date
Tue, 24 Jul 2018 13:22:11 +0800
changeset 9137
dc1769738300
parent 7535
7ae4e26cb1e0
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

#7048 added Loongson release info to hs_err crash files

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "memory/allocation.inline.hpp"
aoqi@0 30 #include "utilities/debug.hpp"
aoqi@0 31 #include "utilities/globalDefinitions.hpp"
aoqi@0 32 #include "utilities/top.hpp"
aoqi@0 33
aoqi@0 34 // A growable array.
aoqi@0 35
aoqi@0 36 /*************************************************************************/
aoqi@0 37 /* */
aoqi@0 38 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
aoqi@0 39 /* */
aoqi@0 40 /* Should you use GrowableArrays to contain handles you must be certain */
aoqi@0 41 /* the the GrowableArray does not outlive the HandleMark that contains */
aoqi@0 42 /* the handles. Since GrowableArrays are typically resource allocated */
aoqi@0 43 /* the following is an example of INCORRECT CODE, */
aoqi@0 44 /* */
aoqi@0 45 /* ResourceMark rm; */
aoqi@0 46 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */
aoqi@0 47 /* if (blah) { */
aoqi@0 48 /* while (...) { */
aoqi@0 49 /* HandleMark hm; */
aoqi@0 50 /* ... */
aoqi@0 51 /* Handle h(THREAD, some_oop); */
aoqi@0 52 /* arr->append(h); */
aoqi@0 53 /* } */
aoqi@0 54 /* } */
aoqi@0 55 /* if (arr->length() != 0 ) { */
aoqi@0 56 /* oop bad_oop = arr->at(0)(); // Handle is BAD HERE. */
aoqi@0 57 /* ... */
aoqi@0 58 /* } */
aoqi@0 59 /* */
aoqi@0 60 /* If the GrowableArrays you are creating is C_Heap allocated then it */
aoqi@0 61 /* hould not old handles since the handles could trivially try and */
aoqi@0 62 /* outlive their HandleMark. In some situations you might need to do */
aoqi@0 63 /* this and it would be legal but be very careful and see if you can do */
aoqi@0 64 /* the code in some other manner. */
aoqi@0 65 /* */
aoqi@0 66 /*************************************************************************/
aoqi@0 67
aoqi@0 68 // To call default constructor the placement operator new() is used.
aoqi@0 69 // It should be empty (it only returns the passed void* pointer).
aoqi@0 70 // The definition of placement operator new(size_t, void*) in the <new>.
aoqi@0 71
aoqi@0 72 #include <new>
aoqi@0 73
aoqi@0 74 // Need the correct linkage to call qsort without warnings
aoqi@0 75 extern "C" {
aoqi@0 76 typedef int (*_sort_Fn)(const void *, const void *);
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 class GenericGrowableArray : public ResourceObj {
aoqi@0 80 friend class VMStructs;
aoqi@0 81
aoqi@0 82 protected:
aoqi@0 83 int _len; // current length
aoqi@0 84 int _max; // maximum length
aoqi@0 85 Arena* _arena; // Indicates where allocation occurs:
aoqi@0 86 // 0 means default ResourceArea
aoqi@0 87 // 1 means on C heap
aoqi@0 88 // otherwise, allocate in _arena
aoqi@0 89
aoqi@0 90 MEMFLAGS _memflags; // memory type if allocation in C heap
aoqi@0 91
aoqi@0 92 #ifdef ASSERT
aoqi@0 93 int _nesting; // resource area nesting at creation
aoqi@0 94 void set_nesting();
aoqi@0 95 void check_nesting();
aoqi@0 96 #else
aoqi@0 97 #define set_nesting();
aoqi@0 98 #define check_nesting();
aoqi@0 99 #endif
aoqi@0 100
aoqi@0 101 // Where are we going to allocate memory?
aoqi@0 102 bool on_C_heap() { return _arena == (Arena*)1; }
aoqi@0 103 bool on_stack () { return _arena == NULL; }
aoqi@0 104 bool on_arena () { return _arena > (Arena*)1; }
aoqi@0 105
aoqi@0 106 // This GA will use the resource stack for storage if c_heap==false,
aoqi@0 107 // Else it will use the C heap. Use clear_and_deallocate to avoid leaks.
aoqi@0 108 GenericGrowableArray(int initial_size, int initial_len, bool c_heap, MEMFLAGS flags = mtNone) {
aoqi@0 109 _len = initial_len;
aoqi@0 110 _max = initial_size;
aoqi@0 111 _memflags = flags;
aoqi@0 112
aoqi@0 113 // memory type has to be specified for C heap allocation
aoqi@0 114 assert(!(c_heap && flags == mtNone), "memory type not specified for C heap object");
aoqi@0 115
aoqi@0 116 assert(_len >= 0 && _len <= _max, "initial_len too big");
aoqi@0 117 _arena = (c_heap ? (Arena*)1 : NULL);
aoqi@0 118 set_nesting();
aoqi@0 119 assert(!on_C_heap() || allocated_on_C_heap(), "growable array must be on C heap if elements are");
aoqi@0 120 assert(!on_stack() ||
aoqi@0 121 (allocated_on_res_area() || allocated_on_stack()),
aoqi@0 122 "growable array must be on stack if elements are not on arena and not on C heap");
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 // This GA will use the given arena for storage.
aoqi@0 126 // Consider using new(arena) GrowableArray<T> to allocate the header.
aoqi@0 127 GenericGrowableArray(Arena* arena, int initial_size, int initial_len) {
aoqi@0 128 _len = initial_len;
aoqi@0 129 _max = initial_size;
aoqi@0 130 assert(_len >= 0 && _len <= _max, "initial_len too big");
aoqi@0 131 _arena = arena;
aoqi@0 132 _memflags = mtNone;
aoqi@0 133
aoqi@0 134 assert(on_arena(), "arena has taken on reserved value 0 or 1");
aoqi@0 135 // Relax next assert to allow object allocation on resource area,
aoqi@0 136 // on stack or embedded into an other object.
aoqi@0 137 assert(allocated_on_arena() || allocated_on_stack(),
aoqi@0 138 "growable array must be on arena or on stack if elements are on arena");
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 void* raw_allocate(int elementSize);
aoqi@0 142
aoqi@0 143 // some uses pass the Thread explicitly for speed (4990299 tuning)
aoqi@0 144 void* raw_allocate(Thread* thread, int elementSize) {
aoqi@0 145 assert(on_stack(), "fast ResourceObj path only");
aoqi@0 146 return (void*)resource_allocate_bytes(thread, elementSize * _max);
aoqi@0 147 }
aoqi@0 148 };
aoqi@0 149
anoll@7028 150 template<class E> class GrowableArrayIterator;
anoll@7028 151 template<class E, class UnaryPredicate> class GrowableArrayFilterIterator;
anoll@7028 152
aoqi@0 153 template<class E> class GrowableArray : public GenericGrowableArray {
aoqi@0 154 friend class VMStructs;
aoqi@0 155
aoqi@0 156 private:
aoqi@0 157 E* _data; // data array
aoqi@0 158
aoqi@0 159 void grow(int j);
aoqi@0 160 void raw_at_put_grow(int i, const E& p, const E& fill);
aoqi@0 161 void clear_and_deallocate();
aoqi@0 162 public:
aoqi@0 163 GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
aoqi@0 164 _data = (E*)raw_allocate(thread, sizeof(E));
aoqi@0 165 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal)
aoqi@0 169 : GenericGrowableArray(initial_size, 0, C_heap, F) {
aoqi@0 170 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 171 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false, MEMFLAGS memflags = mtInternal)
aoqi@0 175 : GenericGrowableArray(initial_size, initial_len, C_heap, memflags) {
aoqi@0 176 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 177 int i = 0;
aoqi@0 178 for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
aoqi@0 179 for (; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 GrowableArray(Arena* arena, int initial_size, int initial_len, const E& filler) : GenericGrowableArray(arena, initial_size, initial_len) {
aoqi@0 183 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 184 int i = 0;
aoqi@0 185 for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
aoqi@0 186 for (; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 GrowableArray() : GenericGrowableArray(2, 0, false) {
aoqi@0 190 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 191 ::new ((void*)&_data[0]) E();
aoqi@0 192 ::new ((void*)&_data[1]) E();
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 // Does nothing for resource and arena objects
aoqi@0 196 ~GrowableArray() { if (on_C_heap()) clear_and_deallocate(); }
aoqi@0 197
aoqi@0 198 void clear() { _len = 0; }
aoqi@0 199 int length() const { return _len; }
aoqi@0 200 int max_length() const { return _max; }
aoqi@0 201 void trunc_to(int l) { assert(l <= _len,"cannot increase length"); _len = l; }
aoqi@0 202 bool is_empty() const { return _len == 0; }
aoqi@0 203 bool is_nonempty() const { return _len != 0; }
aoqi@0 204 bool is_full() const { return _len == _max; }
aoqi@0 205 DEBUG_ONLY(E* data_addr() const { return _data; })
aoqi@0 206
aoqi@0 207 void print();
aoqi@0 208
aoqi@0 209 int append(const E& elem) {
aoqi@0 210 check_nesting();
aoqi@0 211 if (_len == _max) grow(_len);
aoqi@0 212 int idx = _len++;
aoqi@0 213 _data[idx] = elem;
aoqi@0 214 return idx;
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 bool append_if_missing(const E& elem) {
aoqi@0 218 // Returns TRUE if elem is added.
aoqi@0 219 bool missed = !contains(elem);
aoqi@0 220 if (missed) append(elem);
aoqi@0 221 return missed;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 E& at(int i) {
aoqi@0 225 assert(0 <= i && i < _len, "illegal index");
aoqi@0 226 return _data[i];
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 E const& at(int i) const {
aoqi@0 230 assert(0 <= i && i < _len, "illegal index");
aoqi@0 231 return _data[i];
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 E* adr_at(int i) const {
aoqi@0 235 assert(0 <= i && i < _len, "illegal index");
aoqi@0 236 return &_data[i];
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 E first() const {
aoqi@0 240 assert(_len > 0, "empty list");
aoqi@0 241 return _data[0];
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 E top() const {
aoqi@0 245 assert(_len > 0, "empty list");
aoqi@0 246 return _data[_len-1];
aoqi@0 247 }
aoqi@0 248
anoll@7028 249 GrowableArrayIterator<E> begin() const {
anoll@7028 250 return GrowableArrayIterator<E>(this, 0);
anoll@7028 251 }
anoll@7028 252
anoll@7028 253 GrowableArrayIterator<E> end() const {
anoll@7028 254 return GrowableArrayIterator<E>(this, length());
anoll@7028 255 }
anoll@7028 256
aoqi@0 257 void push(const E& elem) { append(elem); }
aoqi@0 258
aoqi@0 259 E pop() {
aoqi@0 260 assert(_len > 0, "empty list");
aoqi@0 261 return _data[--_len];
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 void at_put(int i, const E& elem) {
aoqi@0 265 assert(0 <= i && i < _len, "illegal index");
aoqi@0 266 _data[i] = elem;
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 E at_grow(int i, const E& fill = E()) {
aoqi@0 270 assert(0 <= i, "negative index");
aoqi@0 271 check_nesting();
aoqi@0 272 if (i >= _len) {
aoqi@0 273 if (i >= _max) grow(i);
aoqi@0 274 for (int j = _len; j <= i; j++)
aoqi@0 275 _data[j] = fill;
aoqi@0 276 _len = i+1;
aoqi@0 277 }
aoqi@0 278 return _data[i];
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 void at_put_grow(int i, const E& elem, const E& fill = E()) {
aoqi@0 282 assert(0 <= i, "negative index");
aoqi@0 283 check_nesting();
aoqi@0 284 raw_at_put_grow(i, elem, fill);
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 bool contains(const E& elem) const {
aoqi@0 288 for (int i = 0; i < _len; i++) {
aoqi@0 289 if (_data[i] == elem) return true;
aoqi@0 290 }
aoqi@0 291 return false;
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 int find(const E& elem) const {
aoqi@0 295 for (int i = 0; i < _len; i++) {
aoqi@0 296 if (_data[i] == elem) return i;
aoqi@0 297 }
aoqi@0 298 return -1;
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 int find_from_end(const E& elem) const {
aoqi@0 302 for (int i = _len-1; i >= 0; i--) {
aoqi@0 303 if (_data[i] == elem) return i;
aoqi@0 304 }
aoqi@0 305 return -1;
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 int find(void* token, bool f(void*, E)) const {
aoqi@0 309 for (int i = 0; i < _len; i++) {
aoqi@0 310 if (f(token, _data[i])) return i;
aoqi@0 311 }
aoqi@0 312 return -1;
aoqi@0 313 }
aoqi@0 314
aoqi@0 315 int find_from_end(void* token, bool f(void*, E)) const {
aoqi@0 316 // start at the end of the array
aoqi@0 317 for (int i = _len-1; i >= 0; i--) {
aoqi@0 318 if (f(token, _data[i])) return i;
aoqi@0 319 }
aoqi@0 320 return -1;
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 void remove(const E& elem) {
aoqi@0 324 for (int i = 0; i < _len; i++) {
aoqi@0 325 if (_data[i] == elem) {
aoqi@0 326 for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
aoqi@0 327 _len--;
aoqi@0 328 return;
aoqi@0 329 }
aoqi@0 330 }
aoqi@0 331 ShouldNotReachHere();
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 // The order is preserved.
aoqi@0 335 void remove_at(int index) {
aoqi@0 336 assert(0 <= index && index < _len, "illegal index");
aoqi@0 337 for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
aoqi@0 338 _len--;
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 // The order is changed.
aoqi@0 342 void delete_at(int index) {
aoqi@0 343 assert(0 <= index && index < _len, "illegal index");
aoqi@0 344 if (index < --_len) {
aoqi@0 345 // Replace removed element with last one.
aoqi@0 346 _data[index] = _data[_len];
aoqi@0 347 }
aoqi@0 348 }
aoqi@0 349
aoqi@0 350 // inserts the given element before the element at index i
aoqi@0 351 void insert_before(const int idx, const E& elem) {
roland@7041 352 assert(0 <= idx && idx <= _len, "illegal index");
aoqi@0 353 check_nesting();
aoqi@0 354 if (_len == _max) grow(_len);
aoqi@0 355 for (int j = _len - 1; j >= idx; j--) {
aoqi@0 356 _data[j + 1] = _data[j];
aoqi@0 357 }
aoqi@0 358 _len++;
aoqi@0 359 _data[idx] = elem;
aoqi@0 360 }
aoqi@0 361
aoqi@0 362 void appendAll(const GrowableArray<E>* l) {
aoqi@0 363 for (int i = 0; i < l->_len; i++) {
roland@7041 364 raw_at_put_grow(_len, l->_data[i], E());
aoqi@0 365 }
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 void sort(int f(E*,E*)) {
aoqi@0 369 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
aoqi@0 370 }
aoqi@0 371 // sort by fixed-stride sub arrays:
aoqi@0 372 void sort(int f(E*,E*), int stride) {
aoqi@0 373 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
aoqi@0 374 }
aoqi@0 375 };
aoqi@0 376
aoqi@0 377 // Global GrowableArray methods (one instance in the library per each 'E' type).
aoqi@0 378
aoqi@0 379 template<class E> void GrowableArray<E>::grow(int j) {
aoqi@0 380 // grow the array by doubling its size (amortized growth)
aoqi@0 381 int old_max = _max;
aoqi@0 382 if (_max == 0) _max = 1; // prevent endless loop
aoqi@0 383 while (j >= _max) _max = _max*2;
aoqi@0 384 // j < _max
aoqi@0 385 E* newData = (E*)raw_allocate(sizeof(E));
aoqi@0 386 int i = 0;
aoqi@0 387 for ( ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
aoqi@0 388 for ( ; i < _max; i++) ::new ((void*)&newData[i]) E();
aoqi@0 389 for (i = 0; i < old_max; i++) _data[i].~E();
aoqi@0 390 if (on_C_heap() && _data != NULL) {
aoqi@0 391 FreeHeap(_data);
aoqi@0 392 }
aoqi@0 393 _data = newData;
aoqi@0 394 }
aoqi@0 395
aoqi@0 396 template<class E> void GrowableArray<E>::raw_at_put_grow(int i, const E& p, const E& fill) {
aoqi@0 397 if (i >= _len) {
aoqi@0 398 if (i >= _max) grow(i);
aoqi@0 399 for (int j = _len; j < i; j++)
aoqi@0 400 _data[j] = fill;
aoqi@0 401 _len = i+1;
aoqi@0 402 }
aoqi@0 403 _data[i] = p;
aoqi@0 404 }
aoqi@0 405
aoqi@0 406 // This function clears and deallocate the data in the growable array that
aoqi@0 407 // has been allocated on the C heap. It's not public - called by the
aoqi@0 408 // destructor.
aoqi@0 409 template<class E> void GrowableArray<E>::clear_and_deallocate() {
aoqi@0 410 assert(on_C_heap(),
aoqi@0 411 "clear_and_deallocate should only be called when on C heap");
aoqi@0 412 clear();
aoqi@0 413 if (_data != NULL) {
aoqi@0 414 for (int i = 0; i < _max; i++) _data[i].~E();
aoqi@0 415 FreeHeap(_data);
aoqi@0 416 _data = NULL;
aoqi@0 417 }
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 template<class E> void GrowableArray<E>::print() {
aoqi@0 421 tty->print("Growable Array " INTPTR_FORMAT, this);
aoqi@0 422 tty->print(": length %ld (_max %ld) { ", _len, _max);
aoqi@0 423 for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
aoqi@0 424 tty->print("}\n");
aoqi@0 425 }
aoqi@0 426
anoll@7028 427 // Custom STL-style iterator to iterate over GrowableArrays
anoll@7028 428 // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
anoll@7028 429 template<class E> class GrowableArrayIterator : public StackObj {
anoll@7028 430 friend class GrowableArray<E>;
anoll@7028 431 template<class F, class UnaryPredicate> friend class GrowableArrayFilterIterator;
anoll@7028 432
anoll@7028 433 private:
anoll@7028 434 const GrowableArray<E>* _array; // GrowableArray we iterate over
anoll@7028 435 int _position; // The current position in the GrowableArray
anoll@7028 436
anoll@7028 437 // Private constructor used in GrowableArray::begin() and GrowableArray::end()
anoll@7028 438 GrowableArrayIterator(const GrowableArray<E>* array, int position) : _array(array), _position(position) {
anoll@7028 439 assert(0 <= position && position <= _array->length(), "illegal position");
anoll@7028 440 }
anoll@7028 441
anoll@7028 442 public:
anoll@7028 443 GrowableArrayIterator<E>& operator++() { ++_position; return *this; }
anoll@7028 444 E operator*() { return _array->at(_position); }
anoll@7028 445
anoll@7028 446 bool operator==(const GrowableArrayIterator<E>& rhs) {
anoll@7028 447 assert(_array == rhs._array, "iterator belongs to different array");
anoll@7028 448 return _position == rhs._position;
anoll@7028 449 }
anoll@7028 450
anoll@7028 451 bool operator!=(const GrowableArrayIterator<E>& rhs) {
anoll@7028 452 assert(_array == rhs._array, "iterator belongs to different array");
anoll@7028 453 return _position != rhs._position;
anoll@7028 454 }
anoll@7028 455 };
anoll@7028 456
anoll@7028 457 // Custom STL-style iterator to iterate over elements of a GrowableArray that satisfy a given predicate
anoll@7028 458 template<class E, class UnaryPredicate> class GrowableArrayFilterIterator : public StackObj {
anoll@7028 459 friend class GrowableArray<E>;
anoll@7028 460
anoll@7028 461 private:
anoll@7028 462 const GrowableArray<E>* _array; // GrowableArray we iterate over
anoll@7028 463 int _position; // Current position in the GrowableArray
anoll@7028 464 UnaryPredicate _predicate; // Unary predicate the elements of the GrowableArray should satisfy
anoll@7028 465
anoll@7028 466 public:
anoll@7028 467 GrowableArrayFilterIterator(const GrowableArrayIterator<E>& begin, UnaryPredicate filter_predicate)
anoll@7028 468 : _array(begin._array), _position(begin._position), _predicate(filter_predicate) {
anoll@7028 469 // Advance to first element satisfying the predicate
anoll@7028 470 while(_position != _array->length() && !_predicate(_array->at(_position))) {
anoll@7028 471 ++_position;
anoll@7028 472 }
anoll@7028 473 }
anoll@7028 474
anoll@7028 475 GrowableArrayFilterIterator<E, UnaryPredicate>& operator++() {
anoll@7028 476 do {
anoll@7028 477 // Advance to next element satisfying the predicate
anoll@7028 478 ++_position;
anoll@7028 479 } while(_position != _array->length() && !_predicate(_array->at(_position)));
anoll@7028 480 return *this;
anoll@7028 481 }
anoll@7028 482
anoll@7028 483 E operator*() { return _array->at(_position); }
anoll@7028 484
anoll@7028 485 bool operator==(const GrowableArrayIterator<E>& rhs) {
anoll@7028 486 assert(_array == rhs._array, "iterator belongs to different array");
anoll@7028 487 return _position == rhs._position;
anoll@7028 488 }
anoll@7028 489
anoll@7028 490 bool operator!=(const GrowableArrayIterator<E>& rhs) {
anoll@7028 491 assert(_array == rhs._array, "iterator belongs to different array");
anoll@7028 492 return _position != rhs._position;
anoll@7028 493 }
anoll@7028 494
anoll@7028 495 bool operator==(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs) {
anoll@7028 496 assert(_array == rhs._array, "iterator belongs to different array");
anoll@7028 497 return _position == rhs._position;
anoll@7028 498 }
anoll@7028 499
anoll@7028 500 bool operator!=(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs) {
anoll@7028 501 assert(_array == rhs._array, "iterator belongs to different array");
anoll@7028 502 return _position != rhs._position;
anoll@7028 503 }
anoll@7028 504 };
anoll@7028 505
aoqi@0 506 #endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP

mercurial