38 // creation |
38 // creation |
39 ResourceArray() { |
39 ResourceArray() { |
40 _length = 0; |
40 _length = 0; |
41 _data = NULL; |
41 _data = NULL; |
42 DEBUG_ONLY(init_nesting();) |
42 DEBUG_ONLY(init_nesting();) |
|
43 // client may call initialize, at most once |
43 } |
44 } |
44 |
45 |
45 |
46 |
46 ResourceArray(size_t esize, int length) { |
47 ResourceArray(size_t esize, int length) { |
|
48 DEBUG_ONLY(_data = NULL); |
|
49 initialize(esize, length); |
|
50 } |
|
51 |
|
52 void initialize(size_t esize, int length) { |
47 assert(length >= 0, "illegal length"); |
53 assert(length >= 0, "illegal length"); |
|
54 assert(_data == NULL, "must be new object"); |
48 _length = length; |
55 _length = length; |
49 _data = resource_allocate_bytes(esize * length); |
56 _data = resource_allocate_bytes(esize * length); |
50 DEBUG_ONLY(init_nesting();) |
57 DEBUG_ONLY(init_nesting();) |
51 } |
58 } |
52 |
59 |
109 \ |
116 \ |
110 public: \ |
117 public: \ |
111 /* creation */ \ |
118 /* creation */ \ |
112 array_name() : base_class() {} \ |
119 array_name() : base_class() {} \ |
113 array_name(const int length) : base_class(esize, length) {} \ |
120 array_name(const int length) : base_class(esize, length) {} \ |
114 array_name(const int length, const etype fx) : base_class(esize, length) { \ |
121 array_name(const int length, const etype fx) { initialize(length, fx); } \ |
|
122 void initialize(const int length) { base_class::initialize(esize, length); } \ |
|
123 void initialize(const int length, const etype fx) { \ |
|
124 initialize(length); \ |
115 for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx; \ |
125 for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx; \ |
116 } \ |
126 } \ |
117 \ |
127 \ |
118 /* standard operations */ \ |
128 /* standard operations */ \ |
119 etype& operator [] (const int i) const { \ |
129 etype& operator [] (const int i) const { \ |
155 _length = i+1; \ |
165 _length = i+1; \ |
156 } \ |
166 } \ |
157 \ |
167 \ |
158 public: \ |
168 public: \ |
159 /* creation */ \ |
169 /* creation */ \ |
160 stack_name() : array_name() { _size = 0; } \ |
170 stack_name() : array_name() { _size = 0; } \ |
161 stack_name(const int size) : array_name(size){ _length = 0; _size = size; } \ |
171 stack_name(const int size) { initialize(size); } \ |
162 stack_name(const int size, const etype fx) : array_name(size, fx) { _size = size; } \ |
172 stack_name(const int size, const etype fx) { initialize(size, fx); } \ |
|
173 void initialize(const int size, const etype fx) { \ |
|
174 _size = size; \ |
|
175 array_name::initialize(size, fx); \ |
|
176 /* _length == size, allocation and size are the same */ \ |
|
177 } \ |
|
178 void initialize(const int size) { \ |
|
179 _size = size; \ |
|
180 array_name::initialize(size); \ |
|
181 _length = 0; /* reset length to zero; _size records the allocation */ \ |
|
182 } \ |
163 \ |
183 \ |
164 /* standard operations */ \ |
184 /* standard operations */ \ |
165 int size() const { return _size; } \ |
185 int size() const { return _size; } \ |
166 \ |
186 \ |
167 void push(const etype x) { \ |
187 int push(const etype x) { \ |
168 if (length() >= size()) expand(esize, length(), _size); \ |
188 int len = length(); \ |
169 ((etype*)_data)[_length++] = x; \ |
189 if (len >= size()) expand(esize, len, _size); \ |
|
190 ((etype*)_data)[len] = x; \ |
|
191 _length = len+1; \ |
|
192 return len; \ |
170 } \ |
193 } \ |
171 \ |
194 \ |
172 etype pop() { \ |
195 etype pop() { \ |
173 assert(!is_empty(), "stack is empty"); \ |
196 assert(!is_empty(), "stack is empty"); \ |
174 return ((etype*)_data)[--_length]; \ |
197 return ((etype*)_data)[--_length]; \ |
233 \ |
256 \ |
234 /* deprecated operations - for compatibility with GrowableArray only */ \ |
257 /* deprecated operations - for compatibility with GrowableArray only */ \ |
235 int capacity() const { return size(); } \ |
258 int capacity() const { return size(); } \ |
236 void clear() { truncate(0); } \ |
259 void clear() { truncate(0); } \ |
237 void trunc_to(const int length) { truncate(length); } \ |
260 void trunc_to(const int length) { truncate(length); } \ |
238 void append(const etype x) { push(x); } \ |
261 int append(const etype x) { return push(x); } \ |
239 void appendAll(const stack_name* stack) { push_all(stack); } \ |
262 void appendAll(const stack_name* stack) { push_all(stack); } \ |
240 etype last() const { return top(); } \ |
263 etype last() const { return top(); } \ |
241 }; \ |
264 }; \ |
242 |
265 |
243 |
266 |