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