annotate src/share/vm/utilities/growableArray.hpp @ 20543:e7d0505c8a30

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