src/share/vm/utilities/growableArray.hpp

Wed, 14 Oct 2020 17:44:48 +0800

author
aoqi
date
Wed, 14 Oct 2020 17:44:48 +0800
changeset 9931
fd44df5e3bc3
parent 7535
7ae4e26cb1e0
parent 9858
b985cbb00e68
permissions
-rw-r--r--

Merge

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

mercurial