comparison src/share/vm/utilities/array.hpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 275a3b7ff0d6
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 // correct linkage required to compile w/o warnings
26 // (must be on file level - cannot be local)
27 extern "C" { typedef int (*ftype)(const void*, const void*); }
28
29
30 class ResourceArray: public ResourceObj {
31 protected:
32 int _length; // the number of array elements
33 void* _data; // the array memory
34 #ifdef ASSERT
35 int _nesting; // the resource area nesting level
36 #endif
37
38 // creation
39 ResourceArray() {
40 _length = 0;
41 _data = NULL;
42 DEBUG_ONLY(init_nesting();)
43 }
44
45
46 ResourceArray(size_t esize, int length) {
47 assert(length >= 0, "illegal length");
48 _length = length;
49 _data = resource_allocate_bytes(esize * length);
50 DEBUG_ONLY(init_nesting();)
51 }
52
53 #ifdef ASSERT
54 void init_nesting();
55 #endif
56
57 // helper functions
58 void sort (size_t esize, ftype f); // sort the array
59 void expand (size_t esize, int i, int& size);// expand the array to include slot i
60 void remove_at(size_t esize, int i); // remove the element in slot i
61
62 public:
63 // standard operations
64 int length() const { return _length; }
65 bool is_empty() const { return length() == 0; }
66 };
67
68
69 class CHeapArray: public CHeapObj {
70 protected:
71 int _length; // the number of array elements
72 void* _data; // the array memory
73
74 // creation
75 CHeapArray() {
76 _length = 0;
77 _data = NULL;
78 }
79
80
81 CHeapArray(size_t esize, int length) {
82 assert(length >= 0, "illegal length");
83 _length = length;
84 _data = (void*) NEW_C_HEAP_ARRAY(char *, esize * length);
85 }
86
87 #ifdef ASSERT
88 void init_nesting();
89 #endif
90
91 // helper functions
92 void sort (size_t esize, ftype f); // sort the array
93 void expand (size_t esize, int i, int& size);// expand the array to include slot i
94 void remove_at(size_t esize, int i); // remove the element in slot i
95
96 public:
97 // standard operations
98 int length() const { return _length; }
99 bool is_empty() const { return length() == 0; }
100 };
101
102 #define define_generic_array(array_name,element_type, base_class) \
103 class array_name: public base_class { \
104 protected: \
105 typedef element_type etype; \
106 enum { esize = sizeof(etype) }; \
107 \
108 void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); } \
109 \
110 public: \
111 /* creation */ \
112 array_name() : base_class() {} \
113 array_name(const int length) : base_class(esize, length) {} \
114 array_name(const int length, const etype fx) : base_class(esize, length) { \
115 for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx; \
116 } \
117 \
118 /* standard operations */ \
119 etype& operator [] (const int i) const { \
120 assert(0 <= i && i < length(), "index out of bounds"); \
121 return ((etype*)_data)[i]; \
122 } \
123 \
124 int index_of(const etype x) const { \
125 int i = length(); \
126 while (i-- > 0 && ((etype*)_data)[i] != x) ; \
127 /* i < 0 || ((etype*)_data)_data[i] == x */ \
128 return i; \
129 } \
130 \
131 void sort(int f(etype*, etype*)) { base_class::sort(esize, (ftype)f); } \
132 bool contains(const etype x) const { return index_of(x) >= 0; } \
133 \
134 /* deprecated operations - for compatibility with GrowableArray only */ \
135 etype at(const int i) const { return (*this)[i]; } \
136 void at_put(const int i, const etype x) { (*this)[i] = x; } \
137 etype* adr_at(const int i) { return &(*this)[i]; } \
138 int find(const etype x) { return index_of(x); } \
139 }; \
140
141
142 #define define_array(array_name,element_type) \
143 define_generic_array(array_name, element_type, ResourceArray)
144
145
146 #define define_stack(stack_name,array_name) \
147 class stack_name: public array_name { \
148 protected: \
149 int _size; \
150 \
151 void grow(const int i, const etype fx) { \
152 assert(i >= length(), "index too small"); \
153 if (i >= size()) expand(esize, i, _size); \
154 for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx; \
155 _length = i+1; \
156 } \
157 \
158 public: \
159 /* creation */ \
160 stack_name() : array_name() { _size = 0; } \
161 stack_name(const int size) : array_name(size){ _length = 0; _size = size; } \
162 stack_name(const int size, const etype fx) : array_name(size, fx) { _size = size; } \
163 \
164 /* standard operations */ \
165 int size() const { return _size; } \
166 \
167 void push(const etype x) { \
168 if (length() >= size()) expand(esize, length(), _size); \
169 ((etype*)_data)[_length++] = x; \
170 } \
171 \
172 etype pop() { \
173 assert(!is_empty(), "stack is empty"); \
174 return ((etype*)_data)[--_length]; \
175 } \
176 \
177 etype top() const { \
178 assert(!is_empty(), "stack is empty"); \
179 return ((etype*)_data)[length() - 1]; \
180 } \
181 \
182 void push_all(const stack_name* stack) { \
183 const int l = stack->length(); \
184 for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]); \
185 } \
186 \
187 etype at_grow(const int i, const etype fx) { \
188 if (i >= length()) grow(i, fx); \
189 return ((etype*)_data)[i]; \
190 } \
191 \
192 void at_put_grow(const int i, const etype x, const etype fx) { \
193 if (i >= length()) grow(i, fx); \
194 ((etype*)_data)[i] = x; \
195 } \
196 \
197 void truncate(const int length) { \
198 assert(0 <= length && length <= this->length(), "illegal length"); \
199 _length = length; \
200 } \
201 \
202 void remove_at(int i) { base_remove_at(esize, i); } \
203 void remove(etype x) { remove_at(index_of(x)); } \
204 \
205 /* inserts the given element before the element at index i */ \
206 void insert_before(const int i, const etype el) { \
207 int len = length(); \
208 int new_length = len + 1; \
209 if (new_length >= size()) expand(esize, new_length, _size); \
210 for (int j = len - 1; j >= i; j--) { \
211 ((etype*)_data)[j + 1] = ((etype*)_data)[j]; \
212 } \
213 _length = new_length; \
214 at_put(i, el); \
215 } \
216 \
217 /* inserts contents of the given stack before the element at index i */ \
218 void insert_before(const int i, const stack_name *st) { \
219 if (st->length() == 0) return; \
220 int len = length(); \
221 int st_len = st->length(); \
222 int new_length = len + st_len; \
223 if (new_length >= size()) expand(esize, new_length, _size); \
224 int j; \
225 for (j = len - 1; j >= i; j--) { \
226 ((etype*)_data)[j + st_len] = ((etype*)_data)[j]; \
227 } \
228 for (j = 0; j < st_len; j++) { \
229 ((etype*)_data)[i + j] = ((etype*)st->_data)[j]; \
230 } \
231 _length = new_length; \
232 } \
233 \
234 /* deprecated operations - for compatibility with GrowableArray only */ \
235 int capacity() const { return size(); } \
236 void clear() { truncate(0); } \
237 void trunc_to(const int length) { truncate(length); } \
238 void append(const etype x) { push(x); } \
239 void appendAll(const stack_name* stack) { push_all(stack); } \
240 etype last() const { return top(); } \
241 }; \
242
243
244 #define define_resource_list(element_type) \
245 define_generic_array(element_type##Array, element_type, ResourceArray) \
246 define_stack(element_type##List, element_type##Array)
247
248 #define define_resource_pointer_list(element_type) \
249 define_generic_array(element_type##Array, element_type *, ResourceArray) \
250 define_stack(element_type##List, element_type##Array)
251
252 #define define_c_heap_list(element_type) \
253 define_generic_array(element_type##Array, element_type, CHeapArray) \
254 define_stack(element_type##List, element_type##Array)
255
256 #define define_c_heap_pointer_list(element_type) \
257 define_generic_array(element_type##Array, element_type *, CHeapArray) \
258 define_stack(element_type##List, element_type##Array)
259
260
261 // Arrays for basic types
262
263 define_array(boolArray, bool) define_stack(boolStack, boolArray)
264 define_array(intArray , int ) define_stack(intStack , intArray )