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