1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
ArrayedCollection variableSubclass:#Array
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
155
|
17 |
category:'Collections-Arrayed'
|
1
|
18 |
!
|
|
19 |
|
|
20 |
Array comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
22 |
All Rights Reserved
|
92
|
23 |
|
357
|
24 |
$Header: /cvs/stx/stx/libbasic/Array.st,v 1.28 1995-06-06 03:52:50 claus Exp $
|
1
|
25 |
'!
|
|
26 |
|
77
|
27 |
!Array class methodsFor:'documentation'!
|
|
28 |
|
82
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
32 |
All Rights Reserved
|
82
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
357
|
45 |
$Header: /cvs/stx/stx/libbasic/Array.st,v 1.28 1995-06-06 03:52:50 claus Exp $
|
82
|
46 |
"
|
|
47 |
!
|
|
48 |
|
77
|
49 |
documentation
|
|
50 |
"
|
155
|
51 |
Instances of Array store general objects; the arrays size is fixed,
|
|
52 |
therefore add:/remove: are not allowed.
|
|
53 |
Access to the individual elements is via an integer index.
|
|
54 |
Since Arrays are used very often in the system, some methods have been tuned by
|
|
55 |
reimplementing them as primitive.
|
92
|
56 |
|
|
57 |
Notice that Array is a built-in class (i.e. the VM knows about its
|
|
58 |
representation). Therefore it is NOT possible to add named instance
|
|
59 |
variables or change its inheritance. Subclassing is allowed - of course.
|
|
60 |
|
|
61 |
Literal arrays (i.e. array-constants) are entered in source as:
|
155
|
62 |
#( element1 element2 ... element-n)
|
92
|
63 |
where each element must be itself a constant.
|
|
64 |
Examples:
|
155
|
65 |
#(1 2 3)
|
|
66 |
#('foo' 2 (1 2) 4)
|
|
67 |
#('foo' #(1 2))
|
92
|
68 |
|
77
|
69 |
"
|
|
70 |
! !
|
|
71 |
|
3
|
72 |
!Array class methodsFor:'queries'!
|
|
73 |
|
|
74 |
isBuiltInClass
|
|
75 |
"this class is known by the run-time-system"
|
|
76 |
|
|
77 |
^ self == Array
|
|
78 |
! !
|
|
79 |
|
249
|
80 |
!Array class methodsFor:'instance creation'!
|
|
81 |
|
345
|
82 |
new:anInteger
|
|
83 |
"return an instance of myself with anInteger indexed variables.
|
353
|
84 |
Since this is often called, its worth giving it an extra ilc-slot.
|
345
|
85 |
Future versions of stc will do this automatically."
|
|
86 |
|
|
87 |
^ self basicNew:anInteger
|
|
88 |
!
|
|
89 |
|
249
|
90 |
basicNew:anInteger
|
|
91 |
"return an instance of myself with anInteger indexed variables.
|
|
92 |
Since Array-creation is so common (all other collections use them),
|
|
93 |
it seems worth to have a specially tuned version here."
|
|
94 |
|
|
95 |
%{ /* NOCONTEXT */
|
|
96 |
|
|
97 |
OBJ newobj;
|
|
98 |
unsigned INT instsize, nInstVars;
|
|
99 |
INT nindexedinstvars;
|
|
100 |
REGISTER OBJ *op;
|
|
101 |
extern OBJ new();
|
|
102 |
|
|
103 |
if (__isSmallInteger(anInteger)) {
|
|
104 |
nindexedinstvars = _intVal(anInteger);
|
|
105 |
if (nindexedinstvars >= 0) {
|
|
106 |
nInstVars = _intVal(_ClassInstPtr(self)->c_ninstvars);
|
|
107 |
|
|
108 |
nInstVars += nindexedinstvars;
|
|
109 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars);
|
357
|
110 |
if (_CanDoQuickAlignedNew(instsize)) {
|
|
111 |
/*
|
|
112 |
* the most common case
|
|
113 |
*/
|
|
114 |
_qCheckedAlignedNew(newobj, instsize);
|
|
115 |
ok: ;
|
249
|
116 |
_InstPtr(newobj)->o_class = self;
|
|
117 |
#if defined(FAST_ARRAY_MEMSET4)
|
|
118 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars);
|
|
119 |
#else
|
|
120 |
# if !defined(NEGATIVE_ADDRESSES)
|
|
121 |
/*
|
|
122 |
* knowing that nil is 0
|
|
123 |
*/
|
|
124 |
#ifdef mips
|
|
125 |
# undef FAST_ARRAY_MEMSET_DOUBLES_UNROLLED
|
|
126 |
# undef FAST_ARRAY_MEMSET_LONGLONG_UNROLLED
|
|
127 |
/* seems to be slightly faster */
|
|
128 |
# define FAST_ARRAY_MEMSET
|
|
129 |
#endif
|
|
130 |
#ifdef sparc
|
|
131 |
# define FAST_ARRAY_MEMSET_DOUBLES_UNROLLED
|
|
132 |
#endif
|
|
133 |
|
|
134 |
# if defined(FAST_ARRAY_MEMSET_DOUBLES_UNROLLED)
|
|
135 |
op = _InstPtr(newobj)->i_instvars;
|
|
136 |
if (nInstVars > 8) {
|
|
137 |
*op++ = nil; /* for alignment */
|
|
138 |
nInstVars--;
|
|
139 |
while (nInstVars >= 8) {
|
|
140 |
*(double *)op = 0.0;
|
|
141 |
((double *)op)[1] = 0.0;
|
|
142 |
((double *)op)[2] = 0.0;
|
|
143 |
((double *)op)[3] = 0.0;
|
|
144 |
op += 8;
|
|
145 |
nInstVars -= 8;
|
|
146 |
}
|
|
147 |
}
|
|
148 |
while (nInstVars) {
|
|
149 |
*op++ = 0;
|
|
150 |
nInstVars--;
|
|
151 |
}
|
|
152 |
# else
|
|
153 |
# if defined(FAST_ARRAY_MEMSET_LONGLONG_UNROLLED)
|
|
154 |
op = _InstPtr(newobj)->i_instvars;
|
|
155 |
if (nInstVars > 8) {
|
|
156 |
*op++ = nil; /* for alignment */
|
|
157 |
nInstVars--;
|
|
158 |
while (nInstVars >= 8) {
|
|
159 |
*(long long *)op = 0;
|
|
160 |
((long long *)op)[1] = 0;
|
|
161 |
((long long *)op)[2] = 0;
|
|
162 |
((long long *)op)[3] = 0;
|
|
163 |
op += 8;
|
|
164 |
nInstVars -= 8;
|
|
165 |
}
|
|
166 |
}
|
|
167 |
while (nInstVars) {
|
|
168 |
*op++ = 0;
|
|
169 |
nInstVars--;
|
|
170 |
}
|
|
171 |
# else
|
|
172 |
# if defined(FAST_ARRAY_MEMSET)
|
|
173 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
174 |
# else
|
|
175 |
op = _InstPtr(newobj)->i_instvars;
|
|
176 |
while (nInstVars--)
|
|
177 |
*op++ = nil;
|
|
178 |
# endif
|
|
179 |
# endif
|
|
180 |
# endif
|
|
181 |
# else
|
|
182 |
op = _InstPtr(newobj)->i_instvars;
|
|
183 |
while (nInstVars--)
|
|
184 |
*op++ = nil;
|
|
185 |
# endif
|
|
186 |
#endif
|
|
187 |
RETURN ( newobj );
|
357
|
188 |
} else {
|
|
189 |
/*
|
|
190 |
* a GC will happen ...
|
|
191 |
* have to protect all context stuff
|
|
192 |
* (especially for self, but also for backtrace in case of
|
|
193 |
* allocation failure)
|
|
194 |
*/
|
|
195 |
PROTECT_CONTEXT
|
|
196 |
_qAlignedNew(newobj, instsize, SENDER);
|
|
197 |
UNPROTECT_CONTEXT
|
|
198 |
if (newobj != nil) {
|
|
199 |
goto ok;
|
|
200 |
}
|
249
|
201 |
}
|
|
202 |
}
|
|
203 |
}
|
|
204 |
%}.
|
|
205 |
"
|
|
206 |
arrive here if something went wrong ...
|
|
207 |
figure out what it was
|
|
208 |
"
|
|
209 |
(anInteger isMemberOf:SmallInteger) ifFalse:[
|
|
210 |
"
|
|
211 |
the argument is either not an integer,
|
|
212 |
or a LargeInteger (which means that its definitely too big)
|
|
213 |
"
|
|
214 |
self error:'argument to new: must be Integer'.
|
|
215 |
^ nil
|
|
216 |
].
|
|
217 |
(anInteger < 0) ifTrue:[
|
|
218 |
"
|
|
219 |
the argument is negative,
|
|
220 |
"
|
|
221 |
self error:'bad (negative) argument to new:'.
|
|
222 |
^ nil
|
|
223 |
].
|
|
224 |
"
|
|
225 |
memory allocation failed.
|
|
226 |
When we arrive here, there was no memory, even after
|
|
227 |
a garbage collect.
|
|
228 |
This means, that the VM wanted to get some more memory from the
|
|
229 |
Operatingsystem, which was not kind enough to give it.
|
|
230 |
Bad luck - you should increase the swap space on your machine.
|
|
231 |
"
|
|
232 |
^ ObjectMemory allocationFailureSignal raise.
|
345
|
233 |
!
|
|
234 |
|
|
235 |
with:one
|
|
236 |
%{ /* NOCONTEXT */
|
|
237 |
if (_ClassInstPtr(self)->c_ninstvars == __MKSMALLINT(0)) {
|
353
|
238 |
if (_CanDoQuickAlignedNew(sizeof(struct __arrayheader) + __OBJS2BYTES__(1))) {
|
|
239 |
OBJ newArray;
|
345
|
240 |
|
353
|
241 |
_qCheckedAlignedNew(newArray, sizeof(struct __arrayheader) + __OBJS2BYTES__(1));
|
|
242 |
_InstPtr(newArray)->o_class = self;
|
345
|
243 |
_ArrayInstPtr(newArray)->a_element[0] = one;
|
|
244 |
__STORE(newArray, one);
|
353
|
245 |
return newArray;
|
345
|
246 |
}
|
|
247 |
}
|
|
248 |
%}.
|
|
249 |
^ super with:one
|
|
250 |
!
|
|
251 |
|
|
252 |
with:one with:two
|
|
253 |
%{ /* NOCONTEXT */
|
|
254 |
if (_ClassInstPtr(self)->c_ninstvars == __MKSMALLINT(0)) {
|
353
|
255 |
if (_CanDoQuickAlignedNew(sizeof(struct __arrayheader) + __OBJS2BYTES__(2))) {
|
|
256 |
OBJ newArray;
|
345
|
257 |
|
353
|
258 |
_qCheckedAlignedNew(newArray, sizeof(struct __arrayheader) + __OBJS2BYTES__(2));
|
|
259 |
_InstPtr(newArray)->o_class = self;
|
|
260 |
_ArrayInstPtr(newArray)->a_element[0] = one;
|
|
261 |
_ArrayInstPtr(newArray)->a_element[1] = two;
|
|
262 |
__STORE(newArray, one);
|
|
263 |
__STORE(newArray, two);
|
|
264 |
return newArray;
|
|
265 |
}
|
345
|
266 |
}
|
|
267 |
%}.
|
|
268 |
^ super with:one with:two
|
|
269 |
!
|
|
270 |
|
|
271 |
with:one with:two with:three
|
|
272 |
%{ /* NOCONTEXT */
|
|
273 |
if (_ClassInstPtr(self)->c_ninstvars == __MKSMALLINT(0)) {
|
353
|
274 |
if (_CanDoQuickAlignedNew(sizeof(struct __arrayheader) + __OBJS2BYTES__(3))) {
|
|
275 |
OBJ newArray;
|
345
|
276 |
|
353
|
277 |
_qCheckedAlignedNew(newArray, sizeof(struct __arrayheader) + __OBJS2BYTES__(3));
|
|
278 |
_InstPtr(newArray)->o_class = self;
|
|
279 |
_ArrayInstPtr(newArray)->a_element[0] = one;
|
|
280 |
_ArrayInstPtr(newArray)->a_element[1] = two;
|
|
281 |
_ArrayInstPtr(newArray)->a_element[2] = three;
|
|
282 |
__STORE(newArray, one);
|
|
283 |
__STORE(newArray, two);
|
|
284 |
__STORE(newArray, three);
|
|
285 |
return newArray;
|
|
286 |
}
|
345
|
287 |
}
|
|
288 |
%}.
|
|
289 |
^ super with:one with:two with:three
|
|
290 |
!
|
|
291 |
|
|
292 |
with:one with:two with:three with:four
|
|
293 |
|
|
294 |
%{ /* NOCONTEXT */
|
|
295 |
if (_ClassInstPtr(self)->c_ninstvars == __MKSMALLINT(0)) {
|
353
|
296 |
if (_CanDoQuickAlignedNew(sizeof(struct __arrayheader) + __OBJS2BYTES__(4))) {
|
|
297 |
OBJ newArray;
|
345
|
298 |
|
353
|
299 |
_qCheckedAlignedNew(newArray, sizeof(struct __arrayheader) + __OBJS2BYTES__(4));
|
|
300 |
_InstPtr(newArray)->o_class = self;
|
|
301 |
_ArrayInstPtr(newArray)->a_element[0] = one;
|
|
302 |
_ArrayInstPtr(newArray)->a_element[1] = two;
|
|
303 |
_ArrayInstPtr(newArray)->a_element[2] = three;
|
|
304 |
_ArrayInstPtr(newArray)->a_element[3] = four;
|
|
305 |
__STORE(newArray, one);
|
|
306 |
__STORE(newArray, two);
|
|
307 |
__STORE(newArray, three);
|
|
308 |
__STORE(newArray, four);
|
|
309 |
return newArray;
|
|
310 |
}
|
345
|
311 |
}
|
|
312 |
%}.
|
|
313 |
^ super with:one with:two with:three with:four
|
249
|
314 |
! !
|
|
315 |
|
1
|
316 |
!Array methodsFor:'accessing'!
|
|
317 |
|
77
|
318 |
basicSize
|
1
|
319 |
"return the number of indexed elements in the receiver"
|
|
320 |
|
|
321 |
%{ /* NOCONTEXT */
|
|
322 |
|
345
|
323 |
RETURN ( __MKSMALLINT(_arraySize(self) - _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars) ));
|
1
|
324 |
%}
|
|
325 |
!
|
|
326 |
|
353
|
327 |
size
|
|
328 |
"return the number of indexed elements in the receiver.
|
|
329 |
Reimplemented here to avoid the additional size->basicSize send
|
|
330 |
(which we can do here, since size is obviously not redefined in a subclass).
|
|
331 |
This method is the same as basicSize."
|
|
332 |
|
|
333 |
%{ /* NOCONTEXT */
|
|
334 |
|
|
335 |
RETURN ( __MKSMALLINT(_arraySize(self) - _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars) ));
|
|
336 |
%}
|
|
337 |
!
|
|
338 |
|
77
|
339 |
basicAt:index
|
1
|
340 |
"return the indexed instance variable with index, anInteger
|
|
341 |
- added here for speed"
|
|
342 |
|
|
343 |
%{ /* NOCONTEXT */
|
|
344 |
|
|
345 |
REGISTER int indx;
|
92
|
346 |
REGISTER unsigned int nIndex;
|
11
|
347 |
OBJ cls;
|
1
|
348 |
|
249
|
349 |
if (__isSmallInteger(index)) {
|
155
|
350 |
indx = _intVal(index) - 1;
|
|
351 |
if (indx >= 0) {
|
328
|
352 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
|
353 |
if ((cls = __qClass(self)) != Array)
|
155
|
354 |
indx += _intVal(_ClassInstPtr(cls)->c_ninstvars);
|
|
355 |
if (indx < nIndex) {
|
|
356 |
RETURN ( _InstPtr(self)->i_instvars[indx] );
|
|
357 |
}
|
|
358 |
}
|
1
|
359 |
}
|
|
360 |
%}
|
|
361 |
.
|
77
|
362 |
^ super basicAt:index
|
1
|
363 |
!
|
|
364 |
|
353
|
365 |
at:index
|
|
366 |
"return the indexed instance variable with index, anInteger.
|
|
367 |
Reimplemented here to avoid the additional at:->basicAt: send
|
|
368 |
(which we can do here, since at: is obviously not redefined in a subclass).
|
|
369 |
This method is the same as at:."
|
|
370 |
|
|
371 |
%{ /* NOCONTEXT */
|
|
372 |
|
|
373 |
REGISTER int indx;
|
|
374 |
REGISTER unsigned int nIndex;
|
|
375 |
OBJ cls;
|
|
376 |
|
|
377 |
if (__isSmallInteger(index)) {
|
|
378 |
indx = _intVal(index) - 1;
|
|
379 |
if (indx >= 0) {
|
|
380 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
|
381 |
if ((cls = __qClass(self)) != Array)
|
|
382 |
indx += _intVal(_ClassInstPtr(cls)->c_ninstvars);
|
|
383 |
if (indx < nIndex) {
|
|
384 |
RETURN ( _InstPtr(self)->i_instvars[indx] );
|
|
385 |
}
|
|
386 |
}
|
|
387 |
}
|
|
388 |
%}.
|
|
389 |
^ super at:index
|
|
390 |
!
|
|
391 |
|
77
|
392 |
basicAt:index put:anObject
|
1
|
393 |
"store the 2nd arg, anObject as indexed instvar with index, anInteger.
|
|
394 |
- added here for speed"
|
|
395 |
|
|
396 |
%{ /* NOCONTEXT */
|
|
397 |
|
|
398 |
REGISTER int indx;
|
92
|
399 |
REGISTER unsigned int nIndex;
|
11
|
400 |
OBJ cls;
|
1
|
401 |
|
249
|
402 |
if (__isSmallInteger(index)) {
|
155
|
403 |
indx = _intVal(index) - 1;
|
|
404 |
if (indx >= 0) {
|
328
|
405 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
|
406 |
if ((cls = __qClass(self)) != Array)
|
155
|
407 |
indx += _intVal(_ClassInstPtr(cls)->c_ninstvars);
|
|
408 |
if (indx < nIndex) {
|
|
409 |
_InstPtr(self)->i_instvars[indx] = anObject;
|
|
410 |
__STORE(self, anObject);
|
|
411 |
RETURN ( anObject );
|
|
412 |
}
|
|
413 |
}
|
1
|
414 |
}
|
|
415 |
%}
|
|
416 |
.
|
77
|
417 |
^ super basicAt:index put:anObject
|
353
|
418 |
!
|
|
419 |
|
|
420 |
at:index put:anObject
|
|
421 |
"store the 2nd arg, anObject as indexed instvar with index, anInteger.
|
|
422 |
Reimplemented here to avoid the additional at:put:->basicAt:put: send
|
|
423 |
(which we can do here, since at: is obviously not redefined in a subclass).
|
|
424 |
This method is the same as at:."
|
|
425 |
|
|
426 |
%{ /* NOCONTEXT */
|
|
427 |
|
|
428 |
REGISTER int indx;
|
|
429 |
REGISTER unsigned int nIndex;
|
|
430 |
OBJ cls;
|
|
431 |
|
|
432 |
if (__isSmallInteger(index)) {
|
|
433 |
indx = _intVal(index) - 1;
|
|
434 |
if (indx >= 0) {
|
|
435 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
|
436 |
if ((cls = __qClass(self)) != Array)
|
|
437 |
indx += _intVal(_ClassInstPtr(cls)->c_ninstvars);
|
|
438 |
if (indx < nIndex) {
|
|
439 |
_InstPtr(self)->i_instvars[indx] = anObject;
|
|
440 |
__STORE(self, anObject);
|
|
441 |
RETURN ( anObject );
|
|
442 |
}
|
|
443 |
}
|
|
444 |
}
|
|
445 |
%}.
|
|
446 |
^ super at:index put:anObject
|
1
|
447 |
! !
|
|
448 |
|
11
|
449 |
!Array methodsFor:'converting'!
|
|
450 |
|
|
451 |
asArray
|
|
452 |
"return the receiver as an array"
|
|
453 |
|
|
454 |
"could be an instance of a subclass..."
|
|
455 |
self class == Array ifTrue:[
|
155
|
456 |
^ self
|
11
|
457 |
].
|
|
458 |
^ super asArray
|
|
459 |
! !
|
|
460 |
|
1
|
461 |
!Array methodsFor:'copying'!
|
|
462 |
|
|
463 |
copyWith:something
|
220
|
464 |
"return a new collection containing the receivers elements
|
|
465 |
and the single new element, newElement.
|
|
466 |
This is different from concatentation, which expects another collection
|
|
467 |
as argument, but equivalent to copy-and-addLast.
|
|
468 |
Reimplemented for speed if receiver is an Array"
|
1
|
469 |
%{
|
11
|
470 |
OBJ nObj, element;
|
92
|
471 |
unsigned int sz;
|
|
472 |
unsigned int nIndex;
|
11
|
473 |
REGISTER OBJ *srcP, *dstP;
|
293
|
474 |
REGISTER int spc;
|
1
|
475 |
|
328
|
476 |
if (__qClass(self) == Array) {
|
|
477 |
sz = __qSize(self) + sizeof(OBJ);
|
155
|
478 |
PROTECT(something);
|
|
479 |
_qAlignedNew(nObj, sz, __context);
|
|
480 |
UNPROTECT(something);
|
92
|
481 |
|
155
|
482 |
if (nObj) {
|
|
483 |
_InstPtr(nObj)->o_class = Array;
|
1
|
484 |
|
155
|
485 |
nIndex = __BYTES2OBJS__(sz - OHDR_SIZE - sizeof(OBJ));
|
|
486 |
/* sorry: must take care of stores ... */
|
|
487 |
srcP = _ArrayInstPtr(self)->a_element;
|
|
488 |
dstP = _ArrayInstPtr(nObj)->a_element;
|
325
|
489 |
spc = __qSpace(nObj);
|
155
|
490 |
while (nIndex--) {
|
|
491 |
element = *srcP++;
|
|
492 |
*dstP++ = element;
|
293
|
493 |
__STORE_SPC(nObj, element, spc);
|
155
|
494 |
}
|
|
495 |
*dstP = something;
|
293
|
496 |
__STORE_SPC(nObj, something, spc);
|
155
|
497 |
RETURN ( nObj );
|
|
498 |
}
|
1
|
499 |
}
|
|
500 |
%}
|
|
501 |
.
|
|
502 |
^ super copyWith:something
|
|
503 |
! !
|
|
504 |
|
|
505 |
!Array methodsFor:'filling & replacing'!
|
|
506 |
|
|
507 |
from:index1 to:index2 put:anObject
|
|
508 |
"reimplemented for speed if receiver is an Array"
|
|
509 |
|
|
510 |
%{ /* NOCONTEXT */
|
|
511 |
|
|
512 |
REGISTER int index;
|
92
|
513 |
unsigned int nIndex;
|
|
514 |
unsigned int endIndex;
|
1
|
515 |
REGISTER OBJ *dst;
|
|
516 |
|
328
|
517 |
if ((__qClass(self) == Array)
|
249
|
518 |
&& __bothSmallInteger(index1, index2)) {
|
155
|
519 |
index = _intVal(index1) - 1;
|
|
520 |
if (index >= 0) {
|
328
|
521 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
522 |
endIndex = _intVal(index2) - 1;
|
|
523 |
if (endIndex < nIndex) {
|
|
524 |
dst = &(_InstPtr(self)->i_instvars[index]);
|
1
|
525 |
#ifdef memset4
|
155
|
526 |
memset4(dst, anObject, (endIndex-index+1));
|
|
527 |
__STORE(self, anObject);
|
1
|
528 |
#else
|
155
|
529 |
if ((INT)anObject == 0) {
|
|
530 |
memset(dst, 0, __OBJS2BYTES__(endIndex-index+1));
|
|
531 |
} else {
|
|
532 |
for (; index <= endIndex; index++) {
|
|
533 |
*dst++ = anObject;
|
|
534 |
}
|
|
535 |
__STORE(self, anObject);
|
|
536 |
}
|
1
|
537 |
#endif
|
155
|
538 |
RETURN ( self );
|
|
539 |
}
|
|
540 |
}
|
1
|
541 |
}
|
|
542 |
%}
|
|
543 |
.
|
|
544 |
^ super from:index1 to:index2 put:anObject
|
|
545 |
!
|
|
546 |
|
|
547 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
548 |
"reimplemented for speed if both receiver and aCollection are Arrays"
|
|
549 |
|
|
550 |
%{ /* NOCONTEXT */
|
|
551 |
|
92
|
552 |
unsigned int nIndex;
|
|
553 |
unsigned int repNIndex;
|
1
|
554 |
int startIndex, stopIndex;
|
|
555 |
REGISTER OBJ *src;
|
|
556 |
REGISTER OBJ *dst;
|
|
557 |
int repStopIndex;
|
|
558 |
REGISTER int repStartIndex;
|
|
559 |
REGISTER OBJ t;
|
|
560 |
REGISTER int count;
|
|
561 |
|
302
|
562 |
|
345
|
563 |
if ((_ClassInstPtr(__qClass(self))->c_ninstvars == __MKSMALLINT(0))
|
328
|
564 |
&& (((t = __Class(aCollection)) == Array) || (t == __qClass(self)))
|
249
|
565 |
&& __bothSmallInteger(start, stop)
|
|
566 |
&& __isSmallInteger(repStart)) {
|
155
|
567 |
startIndex = _intVal(start) - 1;
|
|
568 |
if (startIndex >= 0) {
|
328
|
569 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
570 |
stopIndex = _intVal(stop) - 1;
|
|
571 |
count = stopIndex - startIndex + 1;
|
|
572 |
if (count == 0) {
|
|
573 |
RETURN ( self );
|
|
574 |
}
|
|
575 |
if ((count > 0) && (stopIndex < nIndex)) {
|
|
576 |
repStartIndex = _intVal(repStart) - 1;
|
|
577 |
if (repStartIndex >= 0) {
|
328
|
578 |
repNIndex = __BYTES2OBJS__(__qSize(aCollection)-OHDR_SIZE);
|
155
|
579 |
repStopIndex = repStartIndex + (stopIndex - startIndex);
|
|
580 |
if (repStopIndex < repNIndex) {
|
|
581 |
src = &(_InstPtr(aCollection)->i_instvars[repStartIndex]);
|
|
582 |
dst = &(_InstPtr(self)->i_instvars[startIndex]);
|
|
583 |
if (aCollection == self) {
|
|
584 |
/* no need to check stores */
|
|
585 |
/* take care of overlapping copy */
|
|
586 |
if (src < dst) {
|
|
587 |
/* must do a reverse copy */
|
|
588 |
src += count;
|
|
589 |
dst += count;
|
|
590 |
while (count-- > 0) {
|
|
591 |
*--dst = *--src;
|
|
592 |
}
|
|
593 |
RETURN ( self );
|
|
594 |
}
|
1
|
595 |
#ifdef bcopy4
|
155
|
596 |
bcopy4(src, dst, count);
|
1
|
597 |
#else
|
|
598 |
# ifdef FAST_MEMCPY
|
155
|
599 |
bcopy(src, dst, __OBJS2BYTES__(count));
|
1
|
600 |
# else
|
155
|
601 |
while (count--) {
|
|
602 |
*dst++ = *src++;
|
|
603 |
}
|
1
|
604 |
# endif
|
|
605 |
#endif
|
155
|
606 |
} else {
|
302
|
607 |
REGISTER int spc;
|
293
|
608 |
|
325
|
609 |
spc = __qSpace(self);
|
155
|
610 |
while (count-- > 0) {
|
|
611 |
t = *src++;
|
|
612 |
*dst++ = t;
|
293
|
613 |
__STORE_SPC(self, t, spc);
|
155
|
614 |
}
|
|
615 |
}
|
|
616 |
RETURN ( self );
|
|
617 |
}
|
|
618 |
}
|
|
619 |
}
|
|
620 |
}
|
1
|
621 |
}
|
|
622 |
%}
|
|
623 |
.
|
|
624 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
625 |
! !
|
|
626 |
|
275
|
627 |
!Array methodsFor:'queries'!
|
|
628 |
|
|
629 |
isLiteral
|
|
630 |
"return true, if the receiver can be used as a literal
|
|
631 |
(i.e. can be used in constant arrays)"
|
|
632 |
|
|
633 |
"no, subclasses of array are not"
|
|
634 |
self class == Array ifFalse:[^ false].
|
|
635 |
|
|
636 |
thisContext isRecursive ifTrue:[^ false].
|
|
637 |
|
|
638 |
self do:[:element |
|
|
639 |
element isLiteral ifFalse:[^ false]
|
|
640 |
].
|
|
641 |
^ true
|
328
|
642 |
!
|
|
643 |
|
|
644 |
refersToLiteral: aLiteral
|
|
645 |
self do: [ :el |
|
|
646 |
el == aLiteral ifTrue:[^true].
|
|
647 |
el class == Array ifTrue:[
|
|
648 |
(el refersToLiteral: aLiteral) ifTrue: [^true]
|
|
649 |
]
|
|
650 |
].
|
|
651 |
^ false
|
|
652 |
|
|
653 |
"
|
|
654 |
#(1 2 3) refersToLiteral:#foo
|
|
655 |
#(1 2 3 foo bar baz) refersToLiteral:#foo
|
|
656 |
#(1 2 3 (((bar foo))) bar baz) refersToLiteral:#foo
|
|
657 |
"
|
275
|
658 |
! !
|
|
659 |
|
1
|
660 |
!Array methodsFor:'testing'!
|
|
661 |
|
|
662 |
includes:anObject
|
|
663 |
"return true, if the argument, anObject is contained in the array
|
|
664 |
- reimplemented for speed"
|
|
665 |
|
|
666 |
|element|
|
|
667 |
|
|
668 |
%{ /* NOCONTEXT */
|
|
669 |
|
92
|
670 |
/*
|
|
671 |
* first, do a quick check using ==
|
302
|
672 |
* this does not need a context or message send.
|
92
|
673 |
* in many cases this will already find a match
|
|
674 |
*/
|
1
|
675 |
REGISTER int index;
|
|
676 |
REGISTER OBJ o;
|
92
|
677 |
unsigned int nIndex;
|
1
|
678 |
|
328
|
679 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
|
680 |
index = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
1
|
681 |
|
|
682 |
o = anObject;
|
|
683 |
while (index < nIndex) {
|
155
|
684 |
if (_InstPtr(self)->i_instvars[index++] == o) {
|
|
685 |
RETURN ( true );
|
|
686 |
}
|
1
|
687 |
}
|
|
688 |
if (o == nil) {
|
155
|
689 |
RETURN ( false );
|
1
|
690 |
}
|
|
691 |
%}
|
|
692 |
.
|
|
693 |
%{
|
92
|
694 |
/*
|
|
695 |
* then do a slow(er) check using =
|
|
696 |
*/
|
1
|
697 |
REGISTER int index;
|
92
|
698 |
unsigned int nIndex;
|
1
|
699 |
static struct inlineCache eq = _ILC1;
|
|
700 |
|
328
|
701 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
|
702 |
index = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
1
|
703 |
|
|
704 |
while (index < nIndex) {
|
155
|
705 |
element = _InstPtr(self)->i_instvars[index++];
|
|
706 |
if (element != nil) {
|
|
707 |
if ((*eq.ilc_func)(anObject,
|
|
708 |
@symbol(=),
|
|
709 |
CON_COMMA nil,&eq,
|
|
710 |
element)==true) {
|
|
711 |
RETURN ( true );
|
|
712 |
}
|
|
713 |
}
|
1
|
714 |
}
|
356
|
715 |
%}.
|
1
|
716 |
^ false
|
|
717 |
!
|
|
718 |
|
|
719 |
indexOf:anElement startingAt:start
|
|
720 |
"search the array for anElement; return index if found, 0 otherwise
|
|
721 |
- reimplemented for speed"
|
|
722 |
|
|
723 |
|element|
|
|
724 |
%{
|
|
725 |
REGISTER int index;
|
92
|
726 |
unsigned int nIndex, nInsts;
|
1
|
727 |
static struct inlineCache eq = _ILC1;
|
|
728 |
|
249
|
729 |
if (__isSmallInteger(start)) {
|
155
|
730 |
index = _intVal(start) - 1;
|
|
731 |
if (index >= 0) {
|
328
|
732 |
nInsts = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
155
|
733 |
index += nInsts;
|
328
|
734 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
735 |
if (anElement != nil) {
|
|
736 |
while (index < nIndex) {
|
|
737 |
element = _InstPtr(self)->i_instvars[index++];
|
|
738 |
if (element != nil) {
|
|
739 |
if ((element == anElement)
|
|
740 |
|| ((*eq.ilc_func)(anElement,
|
|
741 |
@symbol(=),
|
|
742 |
CON_COMMA nil,&eq,
|
|
743 |
element) == true)) {
|
345
|
744 |
RETURN ( __MKSMALLINT(index - nInsts) );
|
155
|
745 |
}
|
|
746 |
}
|
|
747 |
}
|
|
748 |
} else {
|
|
749 |
/* search for nil */
|
|
750 |
while (index < nIndex) {
|
|
751 |
if (_InstPtr(self)->i_instvars[index++] == nil) {
|
345
|
752 |
RETURN ( __MKSMALLINT(index - nInsts) );
|
155
|
753 |
}
|
|
754 |
}
|
|
755 |
}
|
|
756 |
}
|
1
|
757 |
}
|
356
|
758 |
%}.
|
1
|
759 |
^ 0
|
|
760 |
!
|
|
761 |
|
|
762 |
identityIndexOf:anElement startingAt:start
|
|
763 |
"search the array for anElement; return index if found, 0 otherwise
|
|
764 |
- reimplemented for speed"
|
|
765 |
|
|
766 |
%{ /* NOCONTEXT */
|
|
767 |
|
|
768 |
REGISTER int index;
|
|
769 |
REGISTER OBJ el;
|
|
770 |
REGISTER OBJ *op;
|
92
|
771 |
REGISTER unsigned int nIndex;
|
1
|
772 |
int nInsts;
|
|
773 |
|
249
|
774 |
if (__isSmallInteger(start)) {
|
155
|
775 |
index = _intVal(start) - 1;
|
|
776 |
if (index >= 0) {
|
328
|
777 |
nInsts = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
155
|
778 |
index += nInsts;
|
328
|
779 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
780 |
el = anElement;
|
|
781 |
op = & (_InstPtr(self)->i_instvars[index]);
|
|
782 |
while (index++ < nIndex) {
|
|
783 |
if (*op++ == el) {
|
345
|
784 |
RETURN ( __MKSMALLINT(index - nInsts) );
|
155
|
785 |
}
|
|
786 |
}
|
345
|
787 |
RETURN ( __MKSMALLINT(0) );
|
155
|
788 |
}
|
1
|
789 |
}
|
356
|
790 |
%}.
|
|
791 |
^ super identityIndexOf:anElement startingAt:start
|
|
792 |
!
|
|
793 |
|
|
794 |
identityIndexOf:anElement or:alternative
|
|
795 |
"search the array for anElement or alternative;
|
|
796 |
return the index of anElement if found, or the index of anAlternative,
|
|
797 |
if not found. If anAlternative is also not found, return 0.
|
|
798 |
This is a special interface for high-speed searching in an array
|
|
799 |
and at the same time searching for an empty slot.
|
|
800 |
Do not use this method for your application classes, since it is
|
|
801 |
not portable (i.e. other smalltalks do not offer this)"
|
|
802 |
|
|
803 |
%{ /* NOCONTEXT */
|
|
804 |
|
|
805 |
REGISTER int index;
|
|
806 |
REGISTER OBJ o, el1, el2;
|
|
807 |
REGISTER OBJ *op;
|
|
808 |
REGISTER unsigned int nIndex;
|
|
809 |
int altIndex = 0;
|
|
810 |
int nInsts;
|
|
811 |
|
|
812 |
index = 0;
|
|
813 |
nInsts = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
|
814 |
index += nInsts;
|
|
815 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
|
816 |
el1 = anElement; el2 = alternative;
|
|
817 |
op = & (_InstPtr(self)->i_instvars[index]);
|
|
818 |
while (index++ < nIndex) {
|
|
819 |
if ((o = *op++) == el1) {
|
|
820 |
RETURN ( __MKSMALLINT(index - nInsts) );
|
|
821 |
}
|
|
822 |
if (o == el2) {
|
|
823 |
if (altIndex == 0) {
|
|
824 |
altIndex = index;
|
|
825 |
}
|
|
826 |
}
|
|
827 |
}
|
|
828 |
RETURN ( __MKSMALLINT(altIndex) );
|
1
|
829 |
%}
|
356
|
830 |
|
|
831 |
"
|
|
832 |
#(1 2 3 4 5 6 7 8 9) identityIndexOf:3 or:5
|
|
833 |
#(1 2 0 4 5 6 7 8 9) identityIndexOf:3 or:5
|
|
834 |
#(1 2 0 4 5 6 7 3 9) identityIndexOf:3 or:5
|
|
835 |
#(1 2 3 4 5 nil 7 3 9) identityIndexOf:3 or:nil
|
|
836 |
#(1 2 nil 4 5 6 7 3 9) identityIndexOf:3 or:nil
|
|
837 |
#(1 2 nil 4 5 6 7 8 9) identityIndexOf:3 or:nil
|
|
838 |
#() identityIndexOf:3 or:nil
|
|
839 |
#(1 2) identityIndexOf:3 or:nil
|
|
840 |
"
|
1
|
841 |
! !
|
|
842 |
|
11
|
843 |
!Array methodsFor:'printing & storing'!
|
|
844 |
|
77
|
845 |
displayString
|
|
846 |
"return a printed representation of the receiver for displaying"
|
|
847 |
|
|
848 |
|s|
|
|
849 |
|
275
|
850 |
(self isLiteral) ifTrue:[
|
155
|
851 |
s := WriteStream on:String new.
|
|
852 |
s nextPutAll:'#('.
|
|
853 |
self do:[:element | s nextPutAll:element displayString. s space].
|
|
854 |
s nextPutAll:')'.
|
|
855 |
^ s contents
|
77
|
856 |
].
|
|
857 |
^ super displayString
|
|
858 |
!
|
|
859 |
|
11
|
860 |
storeOn:aStream
|
47
|
861 |
"append a printed representation of the receiver to aStream,
|
|
862 |
which allows reconstructing it via readFrom:.
|
281
|
863 |
Redefined to output a somewhat more user friendly string."
|
47
|
864 |
|
11
|
865 |
self isLiteral ifTrue:[
|
155
|
866 |
aStream nextPutAll:'#('.
|
|
867 |
self do:[:element | element storeOn:aStream. aStream space].
|
|
868 |
aStream nextPutAll:')'
|
11
|
869 |
] ifFalse:[
|
155
|
870 |
super storeOn:aStream
|
11
|
871 |
]
|
281
|
872 |
|
|
873 |
"
|
|
874 |
#(1 2 $a 'hello') storeString
|
|
875 |
"
|
11
|
876 |
! !
|
|
877 |
|
216
|
878 |
!Array methodsFor:'enumerating'!
|
1
|
879 |
|
|
880 |
do:aBlock
|
|
881 |
"evaluate the argument, aBlock for each element in the collection.
|
|
882 |
- reimplemented for speed"
|
|
883 |
|
302
|
884 |
|home|
|
1
|
885 |
%{
|
|
886 |
REGISTER OBJFUNC codeVal;
|
|
887 |
REGISTER int index;
|
92
|
888 |
unsigned int nIndex;
|
77
|
889 |
extern OBJ Block;
|
1
|
890 |
static struct inlineCache val = _ILC1;
|
2
|
891 |
REGISTER OBJ rHome;
|
1
|
892 |
|
328
|
893 |
index = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
|
894 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
895 |
if (__isBlockLike(aBlock)
|
1
|
896 |
&& ((codeVal = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
345
|
897 |
&& (_BlockInstPtr(aBlock)->b_nargs == __MKSMALLINT(1))) {
|
11
|
898 |
#ifdef NEW_BLOCK_CALL
|
155
|
899 |
for (; index < nIndex; index++) {
|
|
900 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
11
|
901 |
|
155
|
902 |
(*codeVal)(aBlock, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
903 |
}
|
11
|
904 |
#else
|
155
|
905 |
home = _BlockInstPtr(aBlock)->b_home;
|
|
906 |
rHome = home;
|
325
|
907 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) {
|
155
|
908 |
/*
|
|
909 |
* home will not move - keep in a fast register
|
|
910 |
*/
|
|
911 |
for (; index < nIndex; index++) {
|
|
912 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
1
|
913 |
|
155
|
914 |
(*codeVal)(rHome, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
915 |
}
|
|
916 |
} else {
|
|
917 |
for (; index < nIndex; index++) {
|
|
918 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
2
|
919 |
|
155
|
920 |
(*codeVal)(home, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
921 |
}
|
|
922 |
}
|
11
|
923 |
#endif
|
1
|
924 |
} else {
|
155
|
925 |
for (; index < nIndex; index++) {
|
|
926 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
1
|
927 |
|
155
|
928 |
(*val.ilc_func)(aBlock,
|
|
929 |
@symbol(value:),
|
|
930 |
CON_COMMA nil, &val,
|
|
931 |
_InstPtr(self)->i_instvars[index]);
|
|
932 |
}
|
1
|
933 |
}
|
|
934 |
%}
|
|
935 |
.
|
|
936 |
^ self
|
|
937 |
!
|
|
938 |
|
219
|
939 |
keysAndValuesDo:aBlock
|
|
940 |
"evaluate the argument, aBlock for each element in the collection.
|
|
941 |
Pass both index and element to the block.
|
|
942 |
- reimplemented for speed"
|
|
943 |
|
|
944 |
|home|
|
|
945 |
%{
|
|
946 |
REGISTER OBJFUNC codeVal;
|
|
947 |
REGISTER int index;
|
|
948 |
unsigned int nIndex;
|
|
949 |
extern OBJ Block;
|
|
950 |
static struct inlineCache val2 = _ILC2;
|
|
951 |
REGISTER OBJ rHome;
|
|
952 |
|
328
|
953 |
index = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
|
954 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
219
|
955 |
if (__isBlockLike(aBlock)
|
|
956 |
&& ((codeVal = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
345
|
957 |
&& (_BlockInstPtr(aBlock)->b_nargs == __MKSMALLINT(2))) {
|
219
|
958 |
#ifdef NEW_BLOCK_CALL
|
|
959 |
for (; index < nIndex; index++) {
|
|
960 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
961 |
|
345
|
962 |
(*codeVal)(aBlock, CON_COMMA __MKSMALLINT(index+1),
|
219
|
963 |
_InstPtr(self)->i_instvars[index]);
|
|
964 |
}
|
|
965 |
#else
|
|
966 |
home = _BlockInstPtr(aBlock)->b_home;
|
|
967 |
rHome = home;
|
325
|
968 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) {
|
219
|
969 |
/*
|
|
970 |
* home will not move - keep in a fast register
|
|
971 |
*/
|
|
972 |
for (; index < nIndex; index++) {
|
|
973 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
974 |
|
345
|
975 |
(*codeVal)(rHome, CON_COMMA __MKSMALLINT(index+1),
|
219
|
976 |
_InstPtr(self)->i_instvars[index]);
|
|
977 |
}
|
|
978 |
} else {
|
|
979 |
for (; index < nIndex; index++) {
|
|
980 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
981 |
|
345
|
982 |
(*codeVal)(home, CON_COMMA __MKSMALLINT(index+1),
|
219
|
983 |
_InstPtr(self)->i_instvars[index]);
|
|
984 |
}
|
|
985 |
}
|
|
986 |
#endif
|
|
987 |
} else {
|
|
988 |
for (; index < nIndex; index++) {
|
|
989 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
990 |
|
287
|
991 |
(*val2.ilc_func)(aBlock,
|
219
|
992 |
@symbol(value:value:),
|
|
993 |
CON_COMMA nil, &val2,
|
345
|
994 |
__MKSMALLINT(index+1),
|
219
|
995 |
_InstPtr(self)->i_instvars[index]);
|
|
996 |
}
|
|
997 |
}
|
|
998 |
%}
|
|
999 |
.
|
|
1000 |
^ self
|
|
1001 |
!
|
|
1002 |
|
1
|
1003 |
reverseDo:aBlock
|
|
1004 |
"evaluate the argument, aBlock for each element in the collection in reverse order.
|
|
1005 |
- reimplemented for speed"
|
|
1006 |
|
302
|
1007 |
|home|
|
1
|
1008 |
%{
|
|
1009 |
REGISTER OBJFUNC codeVal;
|
|
1010 |
REGISTER int index;
|
92
|
1011 |
unsigned int nIndex;
|
|
1012 |
int endIndex;
|
77
|
1013 |
extern OBJ Block;
|
1
|
1014 |
static struct inlineCache val = _ILC1;
|
|
1015 |
|
328
|
1016 |
endIndex = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
|
1017 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
1018 |
if (__isBlockLike(aBlock)
|
1
|
1019 |
&& ((codeVal = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
345
|
1020 |
&& (_BlockInstPtr(aBlock)->b_nargs == __MKSMALLINT(1))) {
|
11
|
1021 |
#ifdef NEW_BLOCK_CALL
|
155
|
1022 |
for (index=nIndex-1; index >= endIndex; index--) {
|
|
1023 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1024 |
(*codeVal)(aBlock, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1025 |
}
|
11
|
1026 |
#else
|
155
|
1027 |
home = _BlockInstPtr(aBlock)->b_home;
|
|
1028 |
for (index=nIndex-1; index >= endIndex; index--) {
|
|
1029 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1030 |
(*codeVal)(home, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1031 |
}
|
11
|
1032 |
#endif
|
1
|
1033 |
} else {
|
155
|
1034 |
for (index=nIndex=1; index >= endIndex; index--) {
|
|
1035 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1036 |
(*val.ilc_func)(aBlock,
|
|
1037 |
@symbol(value:),
|
|
1038 |
CON_COMMA nil, &val,
|
|
1039 |
_InstPtr(self)->i_instvars[index]);
|
|
1040 |
}
|
1
|
1041 |
}
|
|
1042 |
%}
|
|
1043 |
.
|
|
1044 |
^ self
|
|
1045 |
!
|
|
1046 |
|
|
1047 |
from:start to:stop do:aBlock
|
|
1048 |
"evaluate the argument, aBlock for the elements starting at index start
|
|
1049 |
up to (and including) stop in the collection.
|
|
1050 |
- reimplemented for speed"
|
|
1051 |
|
302
|
1052 |
|home|
|
1
|
1053 |
%{
|
|
1054 |
REGISTER OBJFUNC codeVal;
|
|
1055 |
REGISTER int index;
|
2
|
1056 |
REGISTER OBJ rHome;
|
1
|
1057 |
int nIndex, nInsts;
|
77
|
1058 |
extern OBJ Block;
|
1
|
1059 |
static struct inlineCache val = _ILC1;
|
|
1060 |
int indexLow, indexHigh;
|
|
1061 |
|
249
|
1062 |
if (__bothSmallInteger(start, stop)) {
|
155
|
1063 |
indexLow = _intVal(start);
|
|
1064 |
if (indexLow > 0) {
|
|
1065 |
indexHigh = _intVal(stop);
|
328
|
1066 |
if (__qClass(self) != Array) {
|
|
1067 |
nInsts = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
155
|
1068 |
indexLow += nInsts;
|
|
1069 |
indexHigh += nInsts;
|
|
1070 |
}
|
328
|
1071 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
1072 |
if (indexHigh <= nIndex) {
|
|
1073 |
indexLow--;
|
|
1074 |
indexHigh--;
|
|
1075 |
if (__isBlockLike(aBlock)
|
|
1076 |
&& ((codeVal = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
345
|
1077 |
&& (_BlockInstPtr(aBlock)->b_nargs == __MKSMALLINT(1))) {
|
11
|
1078 |
#ifdef NEW_BLOCK_CALL
|
155
|
1079 |
for (index=indexLow; index <= indexHigh; index++) {
|
|
1080 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1081 |
(*codeVal)(aBlock, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1082 |
}
|
11
|
1083 |
#else
|
155
|
1084 |
home = _BlockInstPtr(aBlock)->b_home;
|
|
1085 |
rHome = home;
|
325
|
1086 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) {
|
155
|
1087 |
for (index=indexLow; index <= indexHigh; index++) {
|
|
1088 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1089 |
(*codeVal)(rHome, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1090 |
}
|
|
1091 |
} else {
|
|
1092 |
for (index=indexLow; index <= indexHigh; index++) {
|
|
1093 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1094 |
(*codeVal)(home, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1095 |
}
|
|
1096 |
}
|
11
|
1097 |
#endif
|
155
|
1098 |
} else {
|
|
1099 |
for (index=indexLow; index <= indexHigh; index++) {
|
|
1100 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1101 |
(*val.ilc_func) (aBlock,
|
|
1102 |
@symbol(value:),
|
|
1103 |
CON_COMMA nil, &val,
|
302
|
1104 |
_InstPtr(self)->i_instvars[index]);
|
155
|
1105 |
}
|
|
1106 |
}
|
|
1107 |
}
|
|
1108 |
RETURN ( self );
|
|
1109 |
}
|
1
|
1110 |
}
|
|
1111 |
%}
|
|
1112 |
.
|
|
1113 |
^ super from:start to:stop do:aBlock
|
|
1114 |
!
|
|
1115 |
|
42
|
1116 |
from:start to:stop reverseDo:aBlock
|
|
1117 |
"evaluate the argument, aBlock for the elements starting at index start
|
|
1118 |
up to (and including) stop in the collection. Step in reverse order.
|
|
1119 |
- reimplemented for speed"
|
|
1120 |
|
302
|
1121 |
|home|
|
42
|
1122 |
%{
|
|
1123 |
REGISTER OBJFUNC codeVal;
|
|
1124 |
REGISTER int index;
|
|
1125 |
REGISTER OBJ rHome;
|
|
1126 |
int nIndex, nInsts;
|
77
|
1127 |
extern OBJ Block;
|
42
|
1128 |
static struct inlineCache val = _ILC1;
|
|
1129 |
int indexLow, indexHigh;
|
|
1130 |
|
249
|
1131 |
if (__bothSmallInteger(start, stop)) {
|
155
|
1132 |
indexLow = _intVal(start);
|
|
1133 |
if (indexLow > 0) {
|
|
1134 |
indexHigh = _intVal(stop);
|
328
|
1135 |
if (__qClass(self) != Array) {
|
|
1136 |
nInsts = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
155
|
1137 |
indexLow += nInsts;
|
|
1138 |
indexHigh += nInsts;
|
|
1139 |
}
|
328
|
1140 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
1141 |
if (indexHigh <= nIndex) {
|
|
1142 |
indexLow--;
|
|
1143 |
indexHigh--;
|
|
1144 |
if (__isBlockLike(aBlock)
|
|
1145 |
&& ((codeVal = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
345
|
1146 |
&& (_BlockInstPtr(aBlock)->b_nargs == __MKSMALLINT(1))) {
|
42
|
1147 |
#ifdef NEW_BLOCK_CALL
|
155
|
1148 |
for (index=indexHigh; index >= indexLow; index--) {
|
|
1149 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1150 |
(*codeVal)(aBlock, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1151 |
}
|
42
|
1152 |
#else
|
155
|
1153 |
home = _BlockInstPtr(aBlock)->b_home;
|
|
1154 |
rHome = home;
|
325
|
1155 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) {
|
155
|
1156 |
for (index=indexHigh; index >= indexLow; index--) {
|
|
1157 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1158 |
(*codeVal)(rHome, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1159 |
}
|
|
1160 |
} else {
|
|
1161 |
for (index=indexHigh; index >= indexLow; index--) {
|
|
1162 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1163 |
(*codeVal)(home, CON_COMMA _InstPtr(self)->i_instvars[index]);
|
|
1164 |
}
|
|
1165 |
}
|
42
|
1166 |
#endif
|
155
|
1167 |
} else {
|
|
1168 |
for (index=indexHigh; index >= indexLow; index--) {
|
|
1169 |
if (InterruptPending != nil) interruptL(__LINE__ COMMA_CON);
|
|
1170 |
(*val.ilc_func) (aBlock,
|
|
1171 |
@symbol(value:),
|
|
1172 |
CON_COMMA nil, &val,
|
302
|
1173 |
_InstPtr(self)->i_instvars[index]);
|
155
|
1174 |
}
|
|
1175 |
}
|
|
1176 |
}
|
|
1177 |
RETURN ( self );
|
|
1178 |
}
|
42
|
1179 |
}
|
|
1180 |
%}
|
|
1181 |
.
|
|
1182 |
^ super from:start to:stop reverseDo:aBlock
|
|
1183 |
!
|
|
1184 |
|
1
|
1185 |
nonNilElementsDo:aBlock
|
|
1186 |
"evaluate the argument, aBlock for each non-nil element"
|
|
1187 |
|
302
|
1188 |
|home|
|
1
|
1189 |
%{
|
|
1190 |
REGISTER OBJFUNC codeVal;
|
|
1191 |
REGISTER int index;
|
|
1192 |
int nIndex;
|
77
|
1193 |
extern OBJ Block;
|
1
|
1194 |
static struct inlineCache val = _ILC1;
|
302
|
1195 |
REGISTER OBJ rHome;
|
|
1196 |
REGISTER OBJ element;
|
1
|
1197 |
|
328
|
1198 |
index = _intVal(_ClassInstPtr(__qClass(self))->c_ninstvars);
|
|
1199 |
nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
|
155
|
1200 |
if (__isBlockLike(aBlock)
|
1
|
1201 |
&& ((codeVal = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
345
|
1202 |
&& (_BlockInstPtr(aBlock)->b_nargs == __MKSMALLINT(1))) {
|
11
|
1203 |
#ifdef NEW_BLOCK_CALL
|
155
|
1204 |
for (; index < nIndex; index++) {
|
|
1205 |
if (InterruptPending != nil) interrupt(CONARG);
|
11
|
1206 |
|
155
|
1207 |
element = _InstPtr(self)->i_instvars[index];
|
|
1208 |
if (element != nil)
|
|
1209 |
(*codeVal)(aBlock, CON_COMMA element);
|
|
1210 |
}
|
11
|
1211 |
#else
|
155
|
1212 |
home = _BlockInstPtr(aBlock)->b_home;
|
328
|
1213 |
rHome = home;
|
|
1214 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) {
|
|
1215 |
/*
|
|
1216 |
* home will not move - keep in in a register
|
|
1217 |
*/
|
302
|
1218 |
for (; index < nIndex; index++) {
|
328
|
1219 |
if (InterruptPending != nil) interrupt(CONARG);
|
1
|
1220 |
|
328
|
1221 |
element = _InstPtr(self)->i_instvars[index];
|
|
1222 |
if (element != nil)
|
302
|
1223 |
(*codeVal)(home, CON_COMMA element);
|
|
1224 |
}
|
|
1225 |
} else {
|
|
1226 |
for (; index < nIndex; index++) {
|
328
|
1227 |
if (InterruptPending != nil) interrupt(CONARG);
|
302
|
1228 |
|
328
|
1229 |
element = _InstPtr(self)->i_instvars[index];
|
|
1230 |
if (element != nil)
|
302
|
1231 |
(*codeVal)(home, CON_COMMA element);
|
|
1232 |
}
|
155
|
1233 |
}
|
11
|
1234 |
#endif
|
1
|
1235 |
} else {
|
155
|
1236 |
for (; index < nIndex; index++) {
|
|
1237 |
if (InterruptPending != nil) interrupt(CONARG);
|
1
|
1238 |
|
155
|
1239 |
element = _InstPtr(self)->i_instvars[index];
|
|
1240 |
if (element != nil)
|
|
1241 |
(*val.ilc_func)(aBlock,
|
|
1242 |
@symbol(value:),
|
|
1243 |
CON_COMMA nil, &val,
|
|
1244 |
element);
|
|
1245 |
}
|
1
|
1246 |
}
|
|
1247 |
%}
|
|
1248 |
.
|
|
1249 |
^ self
|
11
|
1250 |
!
|
|
1251 |
|
328
|
1252 |
traverse:aBlock
|
|
1253 |
"Evaluate aBlock for every element that is not an Array,
|
|
1254 |
and traverse Arrays.
|
|
1255 |
Implemented here to support better search for selectors in
|
|
1256 |
literal arrays - might be a good idea to move it up in the collection
|
|
1257 |
hierarchy, since this may be a useful method for other collections
|
|
1258 |
as well."
|
|
1259 |
|
|
1260 |
self do: [:el |
|
|
1261 |
(el isKindOf: Array)
|
|
1262 |
ifTrue: [el traverse: aBlock]
|
|
1263 |
ifFalse: [aBlock value: el]]
|
|
1264 |
|
|
1265 |
"
|
|
1266 |
example: flattening an Array:
|
|
1267 |
|
|
1268 |
|s|
|
|
1269 |
|
|
1270 |
s := WriteStream on:Array new.
|
|
1271 |
#(1 2 (3 (4 5 (6 7) 8) 9 10) 11 (12 (13)) 14) traverse:[:el | s nextPut:el].
|
|
1272 |
s contents
|
|
1273 |
"
|
|
1274 |
!
|
|
1275 |
|
11
|
1276 |
addAllTo:aCollection
|
|
1277 |
"add all elements of the receiver to aCollection.
|
|
1278 |
return aCollection."
|
|
1279 |
|
|
1280 |
|stop "{ Class: SmallInteger }"|
|
|
1281 |
|
|
1282 |
stop := self size.
|
|
1283 |
1 to:stop do:[:idx |
|
155
|
1284 |
aCollection add:(self at:idx)
|
11
|
1285 |
].
|
|
1286 |
^ aCollection
|
1
|
1287 |
! !
|