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