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