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