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