annotate src/share/vm/utilities/growableArray.hpp @ 196:d1605aabd0a1 jdk7-b30

6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
author xdono
date Wed, 02 Jul 2008 12:55:16 -0700
parents a61af66fc99e
children 275a3b7ff0d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
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 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // A growable array.
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 /*************************************************************************/
a61af66fc99e Initial load
duke
parents:
diff changeset
28 /* */
a61af66fc99e Initial load
duke
parents:
diff changeset
29 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
a61af66fc99e Initial load
duke
parents:
diff changeset
30 /* */
a61af66fc99e Initial load
duke
parents:
diff changeset
31 /* Should you use GrowableArrays to contain handles you must be certain */
a61af66fc99e Initial load
duke
parents:
diff changeset
32 /* the the GrowableArray does not outlive the HandleMark that contains */
a61af66fc99e Initial load
duke
parents:
diff changeset
33 /* the handles. Since GrowableArrays are typically resource allocated */
a61af66fc99e Initial load
duke
parents:
diff changeset
34 /* the following is an example of INCORRECT CODE, */
a61af66fc99e Initial load
duke
parents:
diff changeset
35 /* */
a61af66fc99e Initial load
duke
parents:
diff changeset
36 /* ResourceMark rm; */
a61af66fc99e Initial load
duke
parents:
diff changeset
37 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */
a61af66fc99e Initial load
duke
parents:
diff changeset
38 /* if (blah) { */
a61af66fc99e Initial load
duke
parents:
diff changeset
39 /* while (...) { */
a61af66fc99e Initial load
duke
parents:
diff changeset
40 /* HandleMark hm; */
a61af66fc99e Initial load
duke
parents:
diff changeset
41 /* ... */
a61af66fc99e Initial load
duke
parents:
diff changeset
42 /* Handle h(THREAD, some_oop); */
a61af66fc99e Initial load
duke
parents:
diff changeset
43 /* arr->append(h); */
a61af66fc99e Initial load
duke
parents:
diff changeset
44 /* } */
a61af66fc99e Initial load
duke
parents:
diff changeset
45 /* } */
a61af66fc99e Initial load
duke
parents:
diff changeset
46 /* if (arr->length() != 0 ) { */
a61af66fc99e Initial load
duke
parents:
diff changeset
47 /* oop bad_oop = arr->at(0)(); // Handle is BAD HERE. */
a61af66fc99e Initial load
duke
parents:
diff changeset
48 /* ... */
a61af66fc99e Initial load
duke
parents:
diff changeset
49 /* } */
a61af66fc99e Initial load
duke
parents:
diff changeset
50 /* */
a61af66fc99e Initial load
duke
parents:
diff changeset
51 /* If the GrowableArrays you are creating is C_Heap allocated then it */
a61af66fc99e Initial load
duke
parents:
diff changeset
52 /* hould not old handles since the handles could trivially try and */
a61af66fc99e Initial load
duke
parents:
diff changeset
53 /* outlive their HandleMark. In some situations you might need to do */
a61af66fc99e Initial load
duke
parents:
diff changeset
54 /* this and it would be legal but be very careful and see if you can do */
a61af66fc99e Initial load
duke
parents:
diff changeset
55 /* the code in some other manner. */
a61af66fc99e Initial load
duke
parents:
diff changeset
56 /* */
a61af66fc99e Initial load
duke
parents:
diff changeset
57 /*************************************************************************/
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // To call default constructor the placement operator new() is used.
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // It should be empty (it only returns the passed void* pointer).
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // The definition of placement operator new(size_t, void*) in the <new>.
a61af66fc99e Initial load
duke
parents:
diff changeset
62
a61af66fc99e Initial load
duke
parents:
diff changeset
63 #include <new>
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 // Need the correct linkage to call qsort without warnings
a61af66fc99e Initial load
duke
parents:
diff changeset
66 extern "C" {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 typedef int (*_sort_Fn)(const void *, const void *);
a61af66fc99e Initial load
duke
parents:
diff changeset
68 }
a61af66fc99e Initial load
duke
parents:
diff changeset
69
a61af66fc99e Initial load
duke
parents:
diff changeset
70 class GenericGrowableArray : public ResourceObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
72 int _len; // current length
a61af66fc99e Initial load
duke
parents:
diff changeset
73 int _max; // maximum length
a61af66fc99e Initial load
duke
parents:
diff changeset
74 Arena* _arena; // Indicates where allocation occurs:
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // 0 means default ResourceArea
a61af66fc99e Initial load
duke
parents:
diff changeset
76 // 1 means on C heap
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // otherwise, allocate in _arena
a61af66fc99e Initial load
duke
parents:
diff changeset
78 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
79 int _nesting; // resource area nesting at creation
a61af66fc99e Initial load
duke
parents:
diff changeset
80 void set_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
81 void check_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
82 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
83 #define set_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
84 #define check_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
85 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 // Where are we going to allocate memory?
a61af66fc99e Initial load
duke
parents:
diff changeset
88 bool on_C_heap() { return _arena == (Arena*)1; }
a61af66fc99e Initial load
duke
parents:
diff changeset
89 bool on_stack () { return _arena == NULL; }
a61af66fc99e Initial load
duke
parents:
diff changeset
90 bool on_arena () { return _arena > (Arena*)1; }
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // This GA will use the resource stack for storage if c_heap==false,
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // Else it will use the C heap. Use clear_and_deallocate to avoid leaks.
a61af66fc99e Initial load
duke
parents:
diff changeset
94 GenericGrowableArray(int initial_size, int initial_len, bool c_heap) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 _len = initial_len;
a61af66fc99e Initial load
duke
parents:
diff changeset
96 _max = initial_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 assert(_len >= 0 && _len <= _max, "initial_len too big");
a61af66fc99e Initial load
duke
parents:
diff changeset
98 _arena = (c_heap ? (Arena*)1 : NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
99 set_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
100 assert(!c_heap || allocated_on_C_heap(), "growable array must be on C heap if elements are");
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // This GA will use the given arena for storage.
a61af66fc99e Initial load
duke
parents:
diff changeset
104 // Consider using new(arena) GrowableArray<T> to allocate the header.
a61af66fc99e Initial load
duke
parents:
diff changeset
105 GenericGrowableArray(Arena* arena, int initial_size, int initial_len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 _len = initial_len;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 _max = initial_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
108 assert(_len >= 0 && _len <= _max, "initial_len too big");
a61af66fc99e Initial load
duke
parents:
diff changeset
109 _arena = arena;
a61af66fc99e Initial load
duke
parents:
diff changeset
110 assert(on_arena(), "arena has taken on reserved value 0 or 1");
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112
a61af66fc99e Initial load
duke
parents:
diff changeset
113 void* raw_allocate(int elementSize);
a61af66fc99e Initial load
duke
parents:
diff changeset
114 };
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 template<class E> class GrowableArray : public GenericGrowableArray {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
118 E* _data; // data array
a61af66fc99e Initial load
duke
parents:
diff changeset
119
a61af66fc99e Initial load
duke
parents:
diff changeset
120 void grow(int j);
a61af66fc99e Initial load
duke
parents:
diff changeset
121 void raw_at_put_grow(int i, const E& p, const E& fill);
a61af66fc99e Initial load
duke
parents:
diff changeset
122 void clear_and_deallocate();
a61af66fc99e Initial load
duke
parents:
diff changeset
123 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
124 GrowableArray(int initial_size, bool C_heap = false) : GenericGrowableArray(initial_size, 0, C_heap) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 _data = (E*)raw_allocate(sizeof(E));
a61af66fc99e Initial load
duke
parents:
diff changeset
126 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129 GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false) : GenericGrowableArray(initial_size, initial_len, C_heap) {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 _data = (E*)raw_allocate(sizeof(E));
a61af66fc99e Initial load
duke
parents:
diff changeset
131 int i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 for (; i < _max; i++) ::new ((void*)&_data[i]) E();
a61af66fc99e Initial load
duke
parents:
diff changeset
134 }
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 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
137 _data = (E*)raw_allocate(sizeof(E));
a61af66fc99e Initial load
duke
parents:
diff changeset
138 int i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
139 for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
a61af66fc99e Initial load
duke
parents:
diff changeset
140 for (; i < _max; i++) ::new ((void*)&_data[i]) E();
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }
a61af66fc99e Initial load
duke
parents:
diff changeset
142
a61af66fc99e Initial load
duke
parents:
diff changeset
143 GrowableArray() : GenericGrowableArray(2, 0, false) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 _data = (E*)raw_allocate(sizeof(E));
a61af66fc99e Initial load
duke
parents:
diff changeset
145 ::new ((void*)&_data[0]) E();
a61af66fc99e Initial load
duke
parents:
diff changeset
146 ::new ((void*)&_data[1]) E();
a61af66fc99e Initial load
duke
parents:
diff changeset
147 }
a61af66fc99e Initial load
duke
parents:
diff changeset
148
a61af66fc99e Initial load
duke
parents:
diff changeset
149 // Does nothing for resource and arena objects
a61af66fc99e Initial load
duke
parents:
diff changeset
150 ~GrowableArray() { if (on_C_heap()) clear_and_deallocate(); }
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 void clear() { _len = 0; }
a61af66fc99e Initial load
duke
parents:
diff changeset
153 int length() const { return _len; }
a61af66fc99e Initial load
duke
parents:
diff changeset
154 void trunc_to(int l) { assert(l <= _len,"cannot increase length"); _len = l; }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 bool is_empty() const { return _len == 0; }
a61af66fc99e Initial load
duke
parents:
diff changeset
156 bool is_nonempty() const { return _len != 0; }
a61af66fc99e Initial load
duke
parents:
diff changeset
157 bool is_full() const { return _len == _max; }
a61af66fc99e Initial load
duke
parents:
diff changeset
158 DEBUG_ONLY(E* data_addr() const { return _data; })
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 void print();
a61af66fc99e Initial load
duke
parents:
diff changeset
161
a61af66fc99e Initial load
duke
parents:
diff changeset
162 void append(const E& elem) {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 check_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
164 if (_len == _max) grow(_len);
a61af66fc99e Initial load
duke
parents:
diff changeset
165 _data[_len++] = elem;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 void append_if_missing(const E& elem) {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 if (!contains(elem)) append(elem);
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 E at(int i) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 assert(0 <= i && i < _len, "illegal index");
a61af66fc99e Initial load
duke
parents:
diff changeset
174 return _data[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176
a61af66fc99e Initial load
duke
parents:
diff changeset
177 E* adr_at(int i) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 assert(0 <= i && i < _len, "illegal index");
a61af66fc99e Initial load
duke
parents:
diff changeset
179 return &_data[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 E first() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
183 assert(_len > 0, "empty list");
a61af66fc99e Initial load
duke
parents:
diff changeset
184 return _data[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186
a61af66fc99e Initial load
duke
parents:
diff changeset
187 E top() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 assert(_len > 0, "empty list");
a61af66fc99e Initial load
duke
parents:
diff changeset
189 return _data[_len-1];
a61af66fc99e Initial load
duke
parents:
diff changeset
190 }
a61af66fc99e Initial load
duke
parents:
diff changeset
191
a61af66fc99e Initial load
duke
parents:
diff changeset
192 void push(const E& elem) { append(elem); }
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 E pop() {
a61af66fc99e Initial load
duke
parents:
diff changeset
195 assert(_len > 0, "empty list");
a61af66fc99e Initial load
duke
parents:
diff changeset
196 return _data[--_len];
a61af66fc99e Initial load
duke
parents:
diff changeset
197 }
a61af66fc99e Initial load
duke
parents:
diff changeset
198
a61af66fc99e Initial load
duke
parents:
diff changeset
199 void at_put(int i, const E& elem) {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 assert(0 <= i && i < _len, "illegal index");
a61af66fc99e Initial load
duke
parents:
diff changeset
201 _data[i] = elem;
a61af66fc99e Initial load
duke
parents:
diff changeset
202 }
a61af66fc99e Initial load
duke
parents:
diff changeset
203
a61af66fc99e Initial load
duke
parents:
diff changeset
204 E at_grow(int i, const E& fill = E()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 assert(0 <= i, "negative index");
a61af66fc99e Initial load
duke
parents:
diff changeset
206 check_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
207 if (i >= _len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 if (i >= _max) grow(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
209 for (int j = _len; j <= i; j++)
a61af66fc99e Initial load
duke
parents:
diff changeset
210 _data[j] = fill;
a61af66fc99e Initial load
duke
parents:
diff changeset
211 _len = i+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
212 }
a61af66fc99e Initial load
duke
parents:
diff changeset
213 return _data[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215
a61af66fc99e Initial load
duke
parents:
diff changeset
216 void at_put_grow(int i, const E& elem, const E& fill = E()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 assert(0 <= i, "negative index");
a61af66fc99e Initial load
duke
parents:
diff changeset
218 check_nesting();
a61af66fc99e Initial load
duke
parents:
diff changeset
219 raw_at_put_grow(i, elem, fill);
a61af66fc99e Initial load
duke
parents:
diff changeset
220 }
a61af66fc99e Initial load
duke
parents:
diff changeset
221
a61af66fc99e Initial load
duke
parents:
diff changeset
222 bool contains(const E& elem) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
223 for (int i = 0; i < _len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
224 if (_data[i] == elem) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
a61af66fc99e Initial load
duke
parents:
diff changeset
226 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
227 }
a61af66fc99e Initial load
duke
parents:
diff changeset
228
a61af66fc99e Initial load
duke
parents:
diff changeset
229 int find(const E& elem) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
230 for (int i = 0; i < _len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
231 if (_data[i] == elem) return i;
a61af66fc99e Initial load
duke
parents:
diff changeset
232 }
a61af66fc99e Initial load
duke
parents:
diff changeset
233 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235
a61af66fc99e Initial load
duke
parents:
diff changeset
236 int find(void* token, bool f(void*, E)) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
237 for (int i = 0; i < _len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
238 if (f(token, _data[i])) return i;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 int find_at_end(void* token, bool f(void*, E)) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // start at the end of the array
a61af66fc99e Initial load
duke
parents:
diff changeset
245 for (int i = _len-1; i >= 0; i--) {
a61af66fc99e Initial load
duke
parents:
diff changeset
246 if (f(token, _data[i])) return i;
a61af66fc99e Initial load
duke
parents:
diff changeset
247 }
a61af66fc99e Initial load
duke
parents:
diff changeset
248 return -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
249 }
a61af66fc99e Initial load
duke
parents:
diff changeset
250
a61af66fc99e Initial load
duke
parents:
diff changeset
251 void remove(const E& elem) {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 for (int i = 0; i < _len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
253 if (_data[i] == elem) {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
a61af66fc99e Initial load
duke
parents:
diff changeset
255 _len--;
a61af66fc99e Initial load
duke
parents:
diff changeset
256 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258 }
a61af66fc99e Initial load
duke
parents:
diff changeset
259 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
260 }
a61af66fc99e Initial load
duke
parents:
diff changeset
261
a61af66fc99e Initial load
duke
parents:
diff changeset
262 void remove_at(int index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 assert(0 <= index && index < _len, "illegal index");
a61af66fc99e Initial load
duke
parents:
diff changeset
264 for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
a61af66fc99e Initial load
duke
parents:
diff changeset
265 _len--;
a61af66fc99e Initial load
duke
parents:
diff changeset
266 }
a61af66fc99e Initial load
duke
parents:
diff changeset
267
a61af66fc99e Initial load
duke
parents:
diff changeset
268 void appendAll(const GrowableArray<E>* l) {
a61af66fc99e Initial load
duke
parents:
diff changeset
269 for (int i = 0; i < l->_len; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
270 raw_at_put_grow(_len, l->_data[i], 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
271 }
a61af66fc99e Initial load
duke
parents:
diff changeset
272 }
a61af66fc99e Initial load
duke
parents:
diff changeset
273
a61af66fc99e Initial load
duke
parents:
diff changeset
274 void sort(int f(E*,E*)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
275 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
a61af66fc99e Initial load
duke
parents:
diff changeset
276 }
a61af66fc99e Initial load
duke
parents:
diff changeset
277 // sort by fixed-stride sub arrays:
a61af66fc99e Initial load
duke
parents:
diff changeset
278 void sort(int f(E*,E*), int stride) {
a61af66fc99e Initial load
duke
parents:
diff changeset
279 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
a61af66fc99e Initial load
duke
parents:
diff changeset
280 }
a61af66fc99e Initial load
duke
parents:
diff changeset
281 };
a61af66fc99e Initial load
duke
parents:
diff changeset
282
a61af66fc99e Initial load
duke
parents:
diff changeset
283 // Global GrowableArray methods (one instance in the library per each 'E' type).
a61af66fc99e Initial load
duke
parents:
diff changeset
284
a61af66fc99e Initial load
duke
parents:
diff changeset
285 template<class E> void GrowableArray<E>::grow(int j) {
a61af66fc99e Initial load
duke
parents:
diff changeset
286 // grow the array by doubling its size (amortized growth)
a61af66fc99e Initial load
duke
parents:
diff changeset
287 int old_max = _max;
a61af66fc99e Initial load
duke
parents:
diff changeset
288 if (_max == 0) _max = 1; // prevent endless loop
a61af66fc99e Initial load
duke
parents:
diff changeset
289 while (j >= _max) _max = _max*2;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 // j < _max
a61af66fc99e Initial load
duke
parents:
diff changeset
291 E* newData = (E*)raw_allocate(sizeof(E));
a61af66fc99e Initial load
duke
parents:
diff changeset
292 int i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
293 for ( ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
a61af66fc99e Initial load
duke
parents:
diff changeset
294 for ( ; i < _max; i++) ::new ((void*)&newData[i]) E();
a61af66fc99e Initial load
duke
parents:
diff changeset
295 for (i = 0; i < old_max; i++) _data[i].~E();
a61af66fc99e Initial load
duke
parents:
diff changeset
296 if (on_C_heap() && _data != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 FreeHeap(_data);
a61af66fc99e Initial load
duke
parents:
diff changeset
298 }
a61af66fc99e Initial load
duke
parents:
diff changeset
299 _data = newData;
a61af66fc99e Initial load
duke
parents:
diff changeset
300 }
a61af66fc99e Initial load
duke
parents:
diff changeset
301
a61af66fc99e Initial load
duke
parents:
diff changeset
302 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
303 if (i >= _len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
304 if (i >= _max) grow(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
305 for (int j = _len; j < i; j++)
a61af66fc99e Initial load
duke
parents:
diff changeset
306 _data[j] = fill;
a61af66fc99e Initial load
duke
parents:
diff changeset
307 _len = i+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
308 }
a61af66fc99e Initial load
duke
parents:
diff changeset
309 _data[i] = p;
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311
a61af66fc99e Initial load
duke
parents:
diff changeset
312 // This function clears and deallocate the data in the growable array that
a61af66fc99e Initial load
duke
parents:
diff changeset
313 // has been allocated on the C heap. It's not public - called by the
a61af66fc99e Initial load
duke
parents:
diff changeset
314 // destructor.
a61af66fc99e Initial load
duke
parents:
diff changeset
315 template<class E> void GrowableArray<E>::clear_and_deallocate() {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 assert(on_C_heap(),
a61af66fc99e Initial load
duke
parents:
diff changeset
317 "clear_and_deallocate should only be called when on C heap");
a61af66fc99e Initial load
duke
parents:
diff changeset
318 clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
319 if (_data != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
320 for (int i = 0; i < _max; i++) _data[i].~E();
a61af66fc99e Initial load
duke
parents:
diff changeset
321 FreeHeap(_data);
a61af66fc99e Initial load
duke
parents:
diff changeset
322 _data = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
323 }
a61af66fc99e Initial load
duke
parents:
diff changeset
324 }
a61af66fc99e Initial load
duke
parents:
diff changeset
325
a61af66fc99e Initial load
duke
parents:
diff changeset
326 template<class E> void GrowableArray<E>::print() {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 tty->print("Growable Array " INTPTR_FORMAT, this);
a61af66fc99e Initial load
duke
parents:
diff changeset
328 tty->print(": length %ld (_max %ld) { ", _len, _max);
a61af66fc99e Initial load
duke
parents:
diff changeset
329 for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
a61af66fc99e Initial load
duke
parents:
diff changeset
330 tty->print("}\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
331 }