author | Claus Gittinger <cg@exept.de> |
Tue, 22 Aug 2000 15:49:36 +0200 | |
changeset 5556 | 1056cc5d6ce0 |
parent 5508 | caabcccaf926 |
child 5874 | bc4d80e3700f |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 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 |
||
5401 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
SequenceableCollection subclass:#OrderedCollection |
1071 | 16 |
instanceVariableNames:'contentsArray firstIndex lastIndex' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Sequenceable' |
|
1 | 20 |
! |
21 |
||
33 | 22 |
!OrderedCollection class methodsFor:'documentation'! |
23 |
||
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
33 | 38 |
documentation |
39 |
" |
|
1387 | 40 |
OrderedCollections (OCs) have their elements ordered as they were |
41 |
added. In addition, they provide all indexing access protocol |
|
42 |
and bulk copying (much like Arrays). |
|
43 |
||
1324 | 44 |
Insertion and removal at both ends is possible and reasonably |
45 |
fast - therefore they can be used for queues and stacks. |
|
46 |
||
1387 | 47 |
[Instance variables:] |
48 |
contentsArray <Array> the actual contents |
|
49 |
||
50 |
firstIndex <SmallInteger> index of first valid element |
|
51 |
||
52 |
lastIndex <SmallInteger> index of last valid element |
|
53 |
||
54 |
||
55 |
[performance hint:] |
|
1324 | 56 |
Although insertion and removal of inner elements is possible and supported, |
57 |
it may be slow, because remaining elements have to be copied. |
|
1387 | 58 |
If many elements have to be removed out of the middle of a big OC, |
59 |
it is often faster to create a new OC from scratch. |
|
1324 | 60 |
|
1 | 61 |
|
1387 | 62 |
[beginners bug hint:] |
63 |
notice that: |
|
64 |
Array new:n |
|
65 |
is quite different from: |
|
66 |
OrderedCollection new:n |
|
1290 | 67 |
|
1387 | 68 |
The later creates an OC which is prepared to hold <n> elements, |
69 |
but has a logical size of 0 (zero). To get an OC containing <n> nils, |
|
70 |
use: |
|
71 |
(OrderedCollection new) grow:n |
|
72 |
||
1 | 73 |
|
4064 | 74 |
[memory requirements:] |
75 |
OBJ-HEADER + (3 * ptr-size) |
|
76 |
+ (size-roundedUpToNextPowerOf2 * ptr-size) |
|
77 |
||
1290 | 78 |
[see also:] |
79 |
Array |
|
80 |
||
81 |
[author:] |
|
82 |
Claus Gittinger |
|
33 | 83 |
" |
1321 | 84 |
! |
85 |
||
86 |
examples |
|
87 |
" |
|
1387 | 88 |
using OC as a stack: |
1321 | 89 |
[exBegin] |
1387 | 90 |
|stack top| |
1321 | 91 |
|
1387 | 92 |
stack := OrderedCollection new. |
1321 | 93 |
|
1387 | 94 |
1 to:10 do:[:i | |
95 |
stack add:i |
|
96 |
]. |
|
1321 | 97 |
|
1387 | 98 |
10 timesRepeat:[ |
99 |
top := stack removeLast. |
|
1422 | 100 |
Transcript showCR:top |
1387 | 101 |
] |
1321 | 102 |
[exEnd] |
1387 | 103 |
using OC as a queue (you should use Queue right away ..): |
1321 | 104 |
[exBegin] |
1387 | 105 |
|queue dequeued| |
1321 | 106 |
|
1387 | 107 |
queue := OrderedCollection new. |
1321 | 108 |
|
1387 | 109 |
1 to:10 do:[:i | |
110 |
queue addLast:i |
|
111 |
]. |
|
1321 | 112 |
|
1387 | 113 |
10 timesRepeat:[ |
114 |
dequeued := queue removeFirst. |
|
1422 | 115 |
Transcript showCR:dequeued |
1387 | 116 |
] |
1321 | 117 |
[exEnd] |
1324 | 118 |
|
119 |
||
1387 | 120 |
examples to support the performance hint (see documentation) |
1324 | 121 |
|
122 |
timing removal of all odd elements in a collection of 10000: |
|
123 |
(940 ms on P5/133) |
|
124 |
[exBegin] |
|
125 |
|coll time| |
|
126 |
||
127 |
coll := (1 to:10000) asOrderedCollection. |
|
128 |
time := Time millisecondsToRun:[ |
|
129 |
coll removeAllSuchThat:[:el | el even] |
|
130 |
]. |
|
1422 | 131 |
Transcript show:'time is '; show:time; showCR:' ms'. |
1324 | 132 |
[exEnd] |
133 |
||
1387 | 134 |
tuning the removal by doing it reverse |
135 |
(less copying in #removeAtIndex:) speeds it up by a factor of 2: |
|
1324 | 136 |
[exBegin] |
137 |
|coll time| |
|
138 |
||
139 |
coll := (1 to:10000) asOrderedCollection. |
|
140 |
time := Time millisecondsToRun:[ |
|
141 |
coll size to:1 by:-1 do:[:index | |
|
142 |
(coll at:index) even ifTrue:[ |
|
143 |
coll removeAtIndex:index |
|
144 |
] |
|
145 |
] |
|
146 |
]. |
|
1422 | 147 |
Transcript show:'time is '; show:time; showCR:' ms'. |
1324 | 148 |
[exEnd] |
149 |
||
1387 | 150 |
rebuilding a new collection: |
1324 | 151 |
(64 ms on P5/133) |
152 |
[exBegin] |
|
153 |
|coll time| |
|
154 |
||
155 |
coll := (1 to:10000) asOrderedCollection. |
|
156 |
time := Time millisecondsToRun:[ |
|
157 |
coll := coll select:[:el | el odd] |
|
158 |
]. |
|
1422 | 159 |
Transcript show:'time is '; show:time; showCR:' ms'. |
1324 | 160 |
[exEnd] |
1321 | 161 |
" |
33 | 162 |
! ! |
1 | 163 |
|
164 |
!OrderedCollection class methodsFor:'instance creation'! |
|
165 |
||
166 |
new |
|
1106 | 167 |
"create a new, empty OrderedCollection" |
1 | 168 |
|
169 |
^ (self basicNew) initContents:10 |
|
1106 | 170 |
|
171 |
"Modified: 19.3.1996 / 17:53:12 / cg" |
|
293 | 172 |
! |
173 |
||
606 | 174 |
new:size |
1106 | 175 |
"create a new, empty OrderedCollection with a preallocated physical |
176 |
size. |
|
177 |
NOTICE: |
|
178 |
the logical size of the returned collection is 0 (i.e. it is empty). |
|
179 |
This may be confusing, in that it is different from what Array>>new: |
|
180 |
returns. |
|
181 |
" |
|
606 | 182 |
|
183 |
^ (self basicNew) initContents:(size max:3) |
|
1106 | 184 |
|
185 |
"Modified: 19.3.1996 / 17:53:47 / cg" |
|
4427 | 186 |
! |
187 |
||
188 |
newFrom:aCollection |
|
189 |
"return a new OrderedCollection filled with all elements from the argument, |
|
190 |
aCollection" |
|
191 |
||
192 |
|newColl| |
|
193 |
||
194 |
newColl := self new:(aCollection size). |
|
195 |
newColl addAll:aCollection. |
|
196 |
^ newColl |
|
197 |
||
198 |
" |
|
199 |
OrderedCollection newFrom:#(1 2 3 4) |
|
200 |
OrderedCollection newFrom:(Set with:1 with:2 with:3) |
|
201 |
" |
|
293 | 202 |
! ! |
203 |
||
606 | 204 |
!OrderedCollection methodsFor:'accessing'! |
1 | 205 |
|
606 | 206 |
at:anInteger |
207 |
"return the element at index, anInteger" |
|
1 | 208 |
|
3107 | 209 |
|idx "{ Class: SmallInteger }"| |
387 | 210 |
|
606 | 211 |
idx := anInteger + firstIndex - 1. |
212 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
3107 | 213 |
^ self subscriptBoundsError:anInteger |
214 |
]. |
|
215 |
^ contentsArray basicAt:idx |
|
216 |
||
217 |
"Modified: / 12.11.1997 / 17:56:05 / cg" |
|
360 | 218 |
! |
219 |
||
606 | 220 |
at:anInteger put:anObject |
1221 | 221 |
"set the element at index, to be anInteger. |
222 |
Return anObject (sigh)." |
|
1 | 223 |
|
3107 | 224 |
|idx "{ Class: SmallInteger }"| |
1 | 225 |
|
606 | 226 |
idx := anInteger + firstIndex - 1. |
227 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
1221 | 228 |
^ self subscriptBoundsError:anInteger |
3107 | 229 |
]. |
230 |
^ contentsArray basicAt:idx put:anObject |
|
1221 | 231 |
|
3107 | 232 |
"Modified: / 12.11.1997 / 17:56:32 / cg" |
202 | 233 |
! |
234 |
||
606 | 235 |
first |
236 |
"return the first element" |
|
1 | 237 |
|
606 | 238 |
firstIndex <= lastIndex ifTrue:[ |
239 |
^ contentsArray at:firstIndex |
|
1 | 240 |
]. |
606 | 241 |
"error if collection is empty" |
242 |
^ self emptyCollectionError |
|
2 | 243 |
|
93 | 244 |
" |
606 | 245 |
(OrderedCollection withAll:#(1 2 3 4 5)) first |
246 |
(SortedCollection withAll:#(5 4 3 2 1)) first |
|
93 | 247 |
" |
13 | 248 |
! |
249 |
||
606 | 250 |
last |
251 |
"return the last element" |
|
68 | 252 |
|
606 | 253 |
firstIndex <= lastIndex ifTrue:[ |
254 |
^ contentsArray at:lastIndex |
|
255 |
]. |
|
256 |
"error if collection is empty" |
|
257 |
^ self emptyCollectionError |
|
68 | 258 |
|
93 | 259 |
" |
606 | 260 |
(OrderedCollection withAll:#(1 2 3 4 5)) last |
261 |
(SortedCollection withAll:#(5 4 3 2 1)) last |
|
93 | 262 |
" |
1 | 263 |
! ! |
264 |
||
265 |
!OrderedCollection methodsFor:'adding & removing'! |
|
266 |
||
606 | 267 |
add:anObject |
268 |
"add the argument, anObject to the end of the collection |
|
269 |
Return the argument, anObject." |
|
270 |
||
271 |
|idx "{ Class:SmallInteger }"| |
|
1 | 272 |
|
606 | 273 |
idx := lastIndex. |
1 | 274 |
|
606 | 275 |
(idx == contentsArray size) ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
276 |
self makeRoomAtLast. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
277 |
idx := lastIndex. |
42 | 278 |
]. |
3107 | 279 |
lastIndex := idx := idx + 1. |
280 |
contentsArray basicAt:idx put:anObject. |
|
1 | 281 |
^ anObject |
42 | 282 |
|
348 | 283 |
" |
606 | 284 |
|c| |
285 |
c := #(4 3 2 1) asOrderedCollection. |
|
286 |
c add:'here' |
|
348 | 287 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
288 |
|
3107 | 289 |
"Modified: / 12.11.1997 / 17:49:47 / cg" |
1 | 290 |
! |
291 |
||
606 | 292 |
add:newObject after:oldObject |
293 |
"insert the argument, newObject after oldObject. |
|
294 |
If oldObject is not in the receiver, report an error, |
|
295 |
otherwise return the argument, anObject." |
|
1 | 296 |
|
606 | 297 |
|idx| |
1 | 298 |
|
606 | 299 |
idx := self indexOf:oldObject. |
300 |
idx ~~ 0 ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
301 |
self add:newObject beforeIndex:(idx + 1). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
302 |
^ newObject |
42 | 303 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
304 |
^ self errorValueNotFound:oldObject |
42 | 305 |
|
348 | 306 |
" |
606 | 307 |
|c| |
308 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
309 |
c add:'here' after:3; yourself. |
606 | 310 |
" |
311 |
" |
|
312 |
|c| |
|
313 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
314 |
c add:'here' after:1; yourself. |
606 | 315 |
" |
316 |
" |
|
317 |
|c| |
|
318 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
319 |
c add:'here' after:5; yourself |
606 | 320 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
321 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
322 |
"Modified: 12.4.1996 / 13:51:56 / cg" |
606 | 323 |
! |
324 |
||
325 |
add:anObject afterIndex:index |
|
326 |
"insert the argument, anObject to become located at index. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
327 |
Return the receiver (sigh - ST-80 compatibility)." |
606 | 328 |
|
329 |
^ self add:anObject beforeIndex:(index + 1) |
|
330 |
||
331 |
" |
|
332 |
|c| |
|
333 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
334 |
c add:'here' afterIndex:2 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
335 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
336 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
337 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
338 |
c := #(4 3 2 1) asOrderedCollection. |
606 | 339 |
c add:'here' afterIndex:4 |
348 | 340 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
341 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
342 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
343 |
c := #(4 3 2 1) asOrderedCollection. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
344 |
c add:'here' afterIndex:0 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
345 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
346 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
347 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
348 |
c := #(4 3 2 1) asOrderedCollection. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
349 |
c add:'here' afterIndex:5 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
350 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
351 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
352 |
"Modified: 12.4.1996 / 13:52:41 / cg" |
348 | 353 |
! |
354 |
||
606 | 355 |
add:newObject before:oldObject |
356 |
"insert the argument, newObject before oldObject. |
|
357 |
If oldObject is not in the receiver, report an error, |
|
358 |
otherwise return the argument, anObject." |
|
348 | 359 |
|
606 | 360 |
|idx| |
348 | 361 |
|
606 | 362 |
idx := self indexOf:oldObject. |
363 |
idx ~~ 0 ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
364 |
self add:newObject beforeIndex:idx. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
365 |
^ newObject |
348 | 366 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
367 |
^ self errorValueNotFound:oldObject |
348 | 368 |
|
369 |
" |
|
606 | 370 |
|c| |
371 |
c := #(4 3 2 1) asOrderedCollection. |
|
372 |
c add:'here' before:3. |
|
373 |
c add:'here' before:5 |
|
348 | 374 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
375 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
376 |
"Modified: 12.4.1996 / 13:25:47 / cg" |
348 | 377 |
! |
378 |
||
606 | 379 |
add:anObject beforeIndex:index |
380 |
"insert the argument, anObject to become located at index. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
381 |
Return the receiver (sigh - ST-80 compatibility)." |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
382 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
383 |
|idx physicalIndex| |
348 | 384 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
385 |
physicalIndex := index + firstIndex - 1. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
386 |
physicalIndex > lastIndex ifTrue:[ |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
387 |
physicalIndex == (lastIndex+1) ifTrue:[ |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
388 |
self addLast:anObject. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
389 |
^ self |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
390 |
]. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
391 |
^ self subscriptBoundsError:index |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
392 |
]. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
393 |
|
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
394 |
idx := self makeRoomAtIndex:physicalIndex. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
395 |
"/ notice: the above may change firstIndex |
3107 | 396 |
contentsArray basicAt:idx put:anObject. |
348 | 397 |
|
398 |
" |
|
606 | 399 |
|c| |
400 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
401 |
c add:'here' beforeIndex:3 |
606 | 402 |
" |
403 |
" |
|
404 |
|c| |
|
405 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
406 |
c add:'here' beforeIndex:1 |
606 | 407 |
" |
408 |
" |
|
409 |
|c| |
|
410 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
411 |
c add:'here' beforeIndex:5 |
348 | 412 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
413 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
414 |
"Modified: / 28.1.1998 / 17:17:48 / cg" |
1 | 415 |
! |
416 |
||
3109 | 417 |
addAll:aCollection |
418 |
"add all elements of the argument, aCollection to the |
|
419 |
receiver. Returns the argument, aCollection." |
|
420 |
||
421 |
self addAll:aCollection beforeIndex:(1 + self size). |
|
422 |
^ aCollection |
|
423 |
! |
|
424 |
||
2222 | 425 |
addAll:aCollection beforeIndex:index |
426 |
"insert all elements of the argument, anObject to become located at index. |
|
427 |
The collection may be unordered, but then order of the sliced-in elements |
|
428 |
is undefined. |
|
429 |
Return the receiver." |
|
430 |
||
431 |
|idx count| |
|
432 |
||
433 |
aCollection isSequenceable ifTrue:[ |
|
434 |
"/ we are lucky - that thing can count & do bulk copies |
|
435 |
||
436 |
count := aCollection size. |
|
2549 | 437 |
idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count. |
2222 | 438 |
"/ notice: the above may change firstIndex |
439 |
contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1. |
|
440 |
^ self |
|
441 |
]. |
|
442 |
||
443 |
idx := index. |
|
444 |
aCollection do:[:element | |
|
445 |
self add:element beforeIndex:idx. |
|
446 |
idx := idx + 1. |
|
447 |
]. |
|
448 |
||
449 |
" |
|
450 |
|c| |
|
451 |
c := #(1 2 3 4) asOrderedCollection. |
|
452 |
c addAll:'here' beforeIndex:3 |
|
453 |
" |
|
454 |
" |
|
455 |
|c| |
|
456 |
c := #(1 2 3 4) asOrderedCollection. |
|
2549 | 457 |
c removeFirst. |
458 |
c addAll:'here' beforeIndex:3 |
|
459 |
" |
|
460 |
" |
|
461 |
|c| |
|
462 |
c := #(1 2 3 4) asOrderedCollection. |
|
2222 | 463 |
c addAll:'here' beforeIndex:1 |
464 |
" |
|
465 |
" |
|
466 |
|c| |
|
467 |
c := #(1 2 3 4) asOrderedCollection. |
|
468 |
c addAll:'here' beforeIndex:5 |
|
469 |
" |
|
470 |
||
471 |
" |
|
472 |
|c| |
|
473 |
c := #(1 2 3 4) asOrderedCollection. |
|
474 |
c addAll:('hello' asSet) beforeIndex:3 |
|
475 |
" |
|
476 |
||
2549 | 477 |
"Modified: 15.4.1997 / 12:43:59 / cg" |
2222 | 478 |
! |
479 |
||
606 | 480 |
addFirst:anObject |
481 |
"add the argument, anObject to the beginning of the collection. |
|
482 |
Return the argument, anObject." |
|
56 | 483 |
|
3107 | 484 |
|idx "{ Class:SmallInteger }"| |
485 |
||
486 |
(idx := firstIndex) == 1 ifTrue:[ |
|
487 |
self makeRoomAtFront. |
|
488 |
idx := firstIndex. |
|
348 | 489 |
]. |
3107 | 490 |
firstIndex := idx := idx - 1. |
491 |
contentsArray basicAt:idx put:anObject. |
|
606 | 492 |
^ anObject |
56 | 493 |
|
494 |
" |
|
606 | 495 |
|c| |
496 |
c := #(1 2 3 4) asOrderedCollection. |
|
497 |
c addFirst:'here'. |
|
498 |
c |
|
499 |
" |
|
500 |
||
501 |
" |
|
502 |
|c| |
|
503 |
c := #() asOrderedCollection. |
|
504 |
c addFirst:'here'. |
|
505 |
c |
|
56 | 506 |
" |
3107 | 507 |
|
508 |
"Modified: / 12.11.1997 / 17:58:05 / cg" |
|
509 |
! |
|
510 |
||
511 |
addLast:anObject |
|
512 |
"add the argument, anObject to the end of the collection |
|
513 |
Return the argument, anObject." |
|
514 |
||
515 |
|idx "{ Class:SmallInteger }"| |
|
516 |
||
517 |
idx := lastIndex. |
|
518 |
||
519 |
(idx == contentsArray size) ifTrue:[ |
|
520 |
self makeRoomAtLast. |
|
521 |
idx := lastIndex. |
|
522 |
]. |
|
523 |
lastIndex := idx := idx + 1. |
|
524 |
contentsArray basicAt:idx put:anObject. |
|
525 |
^ anObject |
|
526 |
||
527 |
" |
|
528 |
|c| |
|
529 |
c := #(4 3 2 1) asOrderedCollection. |
|
530 |
c add:'here' |
|
531 |
" |
|
532 |
||
533 |
"Modified: / 12.4.1996 / 13:26:57 / cg" |
|
534 |
"Created: / 12.11.1997 / 17:46:29 / cg" |
|
56 | 535 |
! |
536 |
||
3368 | 537 |
clearContents |
538 |
"remove all elements from the collection but keep the contentsArray. |
|
539 |
Useful for huge lists, if the contents will be rebuild soon (using #add:) |
|
540 |
to a size which is similar to the lists current size. |
|
541 |
Returns the receiver." |
|
542 |
||
543 |
"/ clear those references, to give the garbage collector |
|
544 |
"/ a chance ... |
|
545 |
||
546 |
firstIndex >= lastIndex ifTrue:[ |
|
547 |
contentsArray from:firstIndex to:lastIndex put:nil. |
|
548 |
]. |
|
549 |
firstIndex := 1. |
|
550 |
lastIndex := 0. |
|
551 |
||
552 |
"/ this is the same as: |
|
553 |
"/ self initContents:(self size) |
|
554 |
||
555 |
"Modified: 12.4.1996 / 13:34:19 / cg" |
|
556 |
! |
|
557 |
||
1 | 558 |
remove:anObject ifAbsent:exceptionBlock |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
559 |
"remove the first element which is equal to anObject; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
560 |
if found, remove and return it; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
561 |
if not, return the value from evaluating exceptionBlock. |
2362 | 562 |
Uses equality compare (=) to search for the element." |
1 | 563 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
564 |
|index retVal| |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
565 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
566 |
index := contentsArray indexOf:anObject startingAt:firstIndex endingAt:lastIndex. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
567 |
index ~~ 0 ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
568 |
retVal := contentsArray at:index. |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
569 |
index := index - firstIndex + 1. |
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
570 |
self removeFromIndex:index toIndex:index. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
571 |
^ retVal |
1 | 572 |
]. |
573 |
^ exceptionBlock value |
|
348 | 574 |
|
575 |
" |
|
1496 | 576 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt] |
577 |
#(1 2 3 4 5) asOrderedCollection remove:3 ifAbsent:[self halt] |
|
578 |
||
348 | 579 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops' |
580 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' |
|
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
581 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
582 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' |
348 | 583 |
" |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
584 |
|
2362 | 585 |
"Modified: 8.2.1997 / 19:17:19 / cg" |
1 | 586 |
! |
587 |
||
114 | 588 |
removeAll |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
589 |
"remove all elements from the collection. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
590 |
Returns the receiver." |
114 | 591 |
|
592 |
self initContents:10 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
593 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
594 |
"Modified: 12.4.1996 / 13:34:19 / cg" |
114 | 595 |
! |
596 |
||
348 | 597 |
removeAllSuchThat:aBlock |
598 |
"remove all elements that meet a test criteria as specified in aBlock. |
|
599 |
The argument, aBlock is evaluated for successive elements and all those, |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
600 |
for which it returns true, are removed. |
1496 | 601 |
Return a collection containing the removed elements. |
602 |
||
603 |
Performance hint: |
|
2362 | 604 |
if a large number of objects is to be removed this way, |
1496 | 605 |
it may be better to rebuild a new collection via #select:, |
606 |
since removing elements out of the middle is somewhat slow |
|
2362 | 607 |
due to the need to copy over remaining elements within the collection." |
348 | 608 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
609 |
"/ this is a q&d implementation (possibly slow). |
348 | 610 |
|
2362 | 611 |
|runIndex removed element sz| |
348 | 612 |
|
613 |
removed := self species new. |
|
614 |
||
615 |
runIndex := 1. |
|
2362 | 616 |
sz := self size. |
617 |
[runIndex <= sz] whileTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
618 |
element := self at:runIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
619 |
(aBlock value:element) ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
620 |
removed add:element. |
2362 | 621 |
self removeAtIndex:runIndex. |
622 |
sz := sz - 1. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
623 |
] ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
624 |
runIndex := runIndex + 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
625 |
] |
348 | 626 |
]. |
627 |
^ removed |
|
628 |
||
629 |
" |
|
630 |
|coll| |
|
631 |
||
632 |
coll := OrderedCollection withAll:(1 to:10). |
|
1422 | 633 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
634 |
Transcript showCR:coll |
|
348 | 635 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
636 |
|
2362 | 637 |
"Modified: 8.2.1997 / 19:19:00 / cg" |
348 | 638 |
! |
639 |
||
606 | 640 |
removeFirst |
3695 | 641 |
"remove the first element from the collection; return the element. |
642 |
If there is no element in the receiver collection, raise an error." |
|
202 | 643 |
|
3968 | 644 |
|anObject |
645 |
fI "{ Class: SmallInteger }" | |
|
13 | 646 |
|
2362 | 647 |
fI := firstIndex. |
648 |
||
649 |
fI > lastIndex ifTrue:[ |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
650 |
"error if collection is empty" |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
651 |
^ self emptyCollectionError. |
606 | 652 |
]. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
653 |
|
3107 | 654 |
anObject := contentsArray basicAt:fI. |
1496 | 655 |
|
656 |
"/ nil it out, to allow GC to reclaim it. |
|
3107 | 657 |
contentsArray basicAt:fI put:nil. |
1496 | 658 |
|
2362 | 659 |
fI := fI + 1. |
1 | 660 |
|
2362 | 661 |
fI > lastIndex ifTrue:[ |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
662 |
"reset to avoid ever growing" |
2362 | 663 |
fI := 1. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
664 |
lastIndex := 0 |
1 | 665 |
]. |
2362 | 666 |
firstIndex := fI. |
1 | 667 |
^ anObject |
13 | 668 |
|
669 |
" |
|
606 | 670 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
671 |
OrderedCollection new removeFirst |
|
672 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
|
93 | 673 |
" |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
674 |
|
3968 | 675 |
"Modified: / 5.2.1999 / 23:22:58 / cg" |
606 | 676 |
! |
677 |
||
678 |
removeFirst:n |
|
679 |
"remove the first n elements from the collection; |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
680 |
Return a collection containing the removed elements." |
606 | 681 |
|
2362 | 682 |
|mySize ret newFirstIndex| |
606 | 683 |
|
684 |
mySize := self size. |
|
685 |
mySize < n ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
686 |
"error if collection has not enough elements" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
687 |
^ self notEnoughElementsError. |
606 | 688 |
]. |
689 |
||
690 |
ret := Array new:n. |
|
691 |
ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex. |
|
1496 | 692 |
|
606 | 693 |
"/ |
694 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
695 |
"/ |
|
2362 | 696 |
newFirstIndex := firstIndex + n. |
697 |
contentsArray from:firstIndex to:newFirstIndex - 1 put:nil. |
|
606 | 698 |
|
2362 | 699 |
newFirstIndex > lastIndex ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
700 |
"reset to avoid ever growing" |
2362 | 701 |
newFirstIndex := 1. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
702 |
lastIndex := 0 |
606 | 703 |
]. |
2362 | 704 |
firstIndex := newFirstIndex. |
606 | 705 |
^ ret |
93 | 706 |
|
707 |
" |
|
606 | 708 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself |
709 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself |
|
710 |
OrderedCollection new removeFirst:2 |
|
711 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 |
|
712 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself |
|
13 | 713 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
714 |
|
2362 | 715 |
"Modified: 8.2.1997 / 19:20:18 / cg" |
1 | 716 |
! |
717 |
||
3695 | 718 |
removeFirstIfAbsent:exceptionBlock |
719 |
"remove the first element from the collection; return the element. |
|
720 |
If there is no element in the receiver collection, return the value from |
|
721 |
exceptionBlock." |
|
722 |
||
723 |
|anObject fI "{ Class: SmallInteger }" | |
|
724 |
||
725 |
fI := firstIndex. |
|
726 |
||
727 |
fI > lastIndex ifTrue:[ |
|
728 |
"error if collection is empty" |
|
729 |
^ exceptionBlock value. |
|
730 |
]. |
|
731 |
||
732 |
anObject := contentsArray basicAt:fI. |
|
733 |
||
734 |
"/ nil it out, to allow GC to reclaim it. |
|
735 |
contentsArray basicAt:fI put:nil. |
|
736 |
||
737 |
fI := fI + 1. |
|
738 |
||
739 |
fI > lastIndex ifTrue:[ |
|
740 |
"reset to avoid ever growing" |
|
741 |
fI := 1. |
|
742 |
lastIndex := 0 |
|
743 |
]. |
|
744 |
firstIndex := fI. |
|
745 |
^ anObject |
|
746 |
||
747 |
" |
|
748 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
|
749 |
OrderedCollection new removeFirst |
|
750 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
|
751 |
" |
|
752 |
||
753 |
"Modified: / 12.11.1997 / 17:58:43 / cg" |
|
754 |
"Created: / 30.7.1998 / 13:19:42 / cg" |
|
755 |
! |
|
756 |
||
2549 | 757 |
removeFrom:startIndex to:stopIndex |
758 |
"added for ST-80 compatibility. |
|
759 |
Same as removeFromIndex:toIndex:." |
|
760 |
||
761 |
^ self removeFromIndex:startIndex toIndex:stopIndex |
|
762 |
||
763 |
"Created: 15.4.1997 / 12:39:00 / cg" |
|
764 |
! |
|
765 |
||
606 | 766 |
removeFromIndex:startIndex toIndex:stopIndex |
767 |
"remove the elements stored under startIndex up to and including |
|
768 |
the elements under stopIndex. |
|
769 |
Return the receiver. |
|
770 |
Returning the receiver here is a historic leftover - it may change. |
|
771 |
Please use yourself in a cascade, if you need the receivers value |
|
772 |
when using this method." |
|
773 |
||
3968 | 774 |
|nDeleted "{ Class: SmallInteger }" |
775 |
fI "{ Class: SmallInteger }" |
|
776 |
lI "{ Class: SmallInteger }" |
|
2364 | 777 |
newLastIndex sz| |
1 | 778 |
|
2364 | 779 |
sz := self size. |
780 |
||
781 |
(startIndex < 1 or:[stopIndex > sz]) ifTrue:[ |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
782 |
^ self notEnoughElementsError |
606 | 783 |
]. |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
784 |
|
2362 | 785 |
nDeleted := stopIndex - startIndex + 1. |
786 |
nDeleted < 0 ifTrue:[ |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
787 |
"/ mhmh - what should be done here ? |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
788 |
^ self error:'bad index range' |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
789 |
]. |
2362 | 790 |
nDeleted == 0 ifTrue:[^ self]. |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
791 |
|
3968 | 792 |
fI := firstIndex. |
793 |
lI := lastIndex. |
|
794 |
||
2362 | 795 |
"/ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
796 |
"/ can be done faster, when removing the first elements |
2362 | 797 |
"/ |
2364 | 798 |
startIndex == 1 ifTrue:[ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
799 |
"/ nil out (helps GC) |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
800 |
contentsArray |
3968 | 801 |
from:fI |
802 |
to:fI + nDeleted - 1 |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
803 |
put:nil. |
3968 | 804 |
firstIndex := fI := fI + nDeleted |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
805 |
] ifFalse:[ |
2362 | 806 |
"/ |
807 |
"/ can be done faster, when removing the last elements |
|
808 |
"/ |
|
2364 | 809 |
stopIndex == sz ifTrue:[ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
810 |
"/ nil out (helps GC) |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
811 |
contentsArray |
3968 | 812 |
from:lI - nDeleted + 1 |
813 |
to:lI |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
814 |
put:nil. |
3968 | 815 |
lastIndex := lI := lI - nDeleted |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
816 |
] ifFalse:[ |
2362 | 817 |
"/ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
818 |
"/ must shuffle |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
819 |
"/ TODO: |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
820 |
"/ for big collections, try to copy the smallest |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
821 |
"/ possible number of elements |
606 | 822 |
|
3968 | 823 |
newLastIndex := lI - nDeleted. |
2362 | 824 |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
825 |
contentsArray |
3968 | 826 |
replaceFrom:(fI + startIndex - 1) |
2362 | 827 |
to:newLastIndex |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
828 |
with:contentsArray |
3968 | 829 |
startingAt:(fI + stopIndex). |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
830 |
|
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
831 |
"/ nil out rest (helps GC) |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
832 |
contentsArray |
2362 | 833 |
from:(newLastIndex + 1) |
3968 | 834 |
to:lI |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
835 |
put:nil. |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
836 |
|
3968 | 837 |
lastIndex := lI := newLastIndex. |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
838 |
] |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
839 |
]. |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
840 |
|
3968 | 841 |
fI > lI ifTrue:[ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
842 |
"reset to avoid ever growing" |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
843 |
firstIndex := 1. |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
844 |
lastIndex := 0 |
606 | 845 |
] |
13 | 846 |
|
847 |
" |
|
606 | 848 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6 |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
849 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:8 |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
850 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3 |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
851 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9 |
606 | 852 |
#(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6 |
217 | 853 |
" |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
854 |
|
3968 | 855 |
"Modified: / 5.2.1999 / 23:22:07 / cg" |
217 | 856 |
! |
857 |
||
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
858 |
removeIdentical:anObject ifAbsent:exceptionBlock |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
859 |
"remove the first element which is identical to anObject; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
860 |
if found, remove and return it; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
861 |
if not, return the value from evaluating exceptionBlock. |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
862 |
Uses identity compare (==) to search for the element." |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
863 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
864 |
|index| |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
865 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
866 |
index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
867 |
index ~~ 0 ifTrue:[ |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
868 |
index := index - firstIndex + 1. |
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
869 |
self removeFromIndex:index toIndex:index. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
870 |
^ anObject |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
871 |
]. |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
872 |
^ exceptionBlock value |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
873 |
|
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
874 |
" |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
875 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
876 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'; yourself |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
877 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
878 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#fee; yourself |
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
879 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#foo; yourself |
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
880 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#baz; yourself |
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
881 |
#(fee) asOrderedCollection removeIdentical:#fee; yourself |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
882 |
" |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
883 |
|
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
884 |
"Modified: 8.2.1997 / 18:57:43 / cg" |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
885 |
! |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
886 |
|
606 | 887 |
removeLast |
888 |
"remove the last element from the collection; return the element" |
|
889 |
||
3107 | 890 |
|anObject |
891 |
idx "{ Class: SmallInteger }" | |
|
217 | 892 |
|
3107 | 893 |
idx := lastIndex. |
894 |
firstIndex > idx ifTrue:[ |
|
895 |
"error if collection is empty" |
|
896 |
^ self emptyCollectionError. |
|
606 | 897 |
]. |
3107 | 898 |
anObject := contentsArray basicAt:idx. |
1496 | 899 |
|
900 |
"/ nil it (helps GC) |
|
3107 | 901 |
contentsArray basicAt:idx put:nil. |
902 |
lastIndex := idx := idx - 1. |
|
606 | 903 |
|
3107 | 904 |
firstIndex > idx ifTrue:[ |
905 |
"reset to avoid ever growing" |
|
906 |
firstIndex := 1. |
|
907 |
lastIndex := 0 |
|
606 | 908 |
]. |
909 |
^ anObject |
|
217 | 910 |
|
911 |
" |
|
606 | 912 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself |
913 |
OrderedCollection new removeLast |
|
914 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself |
|
217 | 915 |
" |
3107 | 916 |
|
917 |
"Modified: / 12.11.1997 / 17:58:57 / cg" |
|
13 | 918 |
! |
919 |
||
606 | 920 |
removeLast:n |
921 |
"remove the last n elements from the collection; |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
922 |
Return a collection of removed elements." |
606 | 923 |
|
924 |
|mySize ret| |
|
925 |
||
926 |
mySize := self size. |
|
927 |
mySize < n ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
928 |
"error if collection has not enough elements" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
929 |
^ self notEnoughElementsError. |
606 | 930 |
]. |
13 | 931 |
|
606 | 932 |
ret := Array new:n. |
933 |
ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1. |
|
1496 | 934 |
|
606 | 935 |
"/ |
936 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
937 |
"/ |
|
938 |
contentsArray from:lastIndex - n + 1 to:lastIndex put:nil. |
|
939 |
lastIndex := lastIndex - n. |
|
13 | 940 |
|
606 | 941 |
firstIndex > lastIndex ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
942 |
"reset to avoid ever growing" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
943 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
944 |
lastIndex := 0 |
13 | 945 |
]. |
606 | 946 |
^ ret |
13 | 947 |
|
948 |
" |
|
606 | 949 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself |
950 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself |
|
951 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself |
|
952 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself |
|
13 | 953 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
954 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
955 |
"Modified: 12.4.1996 / 13:39:12 / cg" |
4862 | 956 |
! |
957 |
||
958 |
reset |
|
959 |
"logically remove all elements from the collection. |
|
960 |
Thats almost the same as #removeAll, but keeps the contentsArray. |
|
961 |
Returns the receiver." |
|
962 |
||
963 |
firstIndex := contentsArray size // 3 max: 1. |
|
964 |
lastIndex := firstIndex - 1 |
|
1 | 965 |
! ! |
966 |
||
606 | 967 |
!OrderedCollection methodsFor:'converting'! |
1 | 968 |
|
1170 | 969 |
asArray |
970 |
"return the receiver as an array." |
|
971 |
||
4153 | 972 |
^ contentsArray copyFrom:firstIndex to:lastIndex |
1170 | 973 |
|
4153 | 974 |
"/ |newArray sz| |
975 |
"/ |
|
976 |
"/ sz := self size. |
|
977 |
"/ newArray := Array new:sz. |
|
978 |
"/ newArray replaceFrom:1 to:sz with:contentsArray startingAt:firstIndex. |
|
979 |
"/ ^ newArray |
|
980 |
||
981 |
" |
|
982 |
OrderedCollection new asArray |
|
983 |
OrderedCollection new add:1;add:2;asArray |
|
984 |
OrderedCollection new add:1;removeFirst;asArray |
|
985 |
" |
|
1170 | 986 |
|
987 |
" |
|
988 |
|o rnd| |
|
989 |
||
990 |
o := OrderedCollection new. |
|
991 |
rnd := Random new. |
|
992 |
10000 timesRepeat:[ |
|
993 |
o add:rnd next. |
|
994 |
]. |
|
995 |
Time millisecondsToRun:[o asArray] |
|
996 |
" |
|
997 |
||
998 |
"Modified: 13.4.1996 / 12:10:56 / cg" |
|
999 |
! |
|
1000 |
||
606 | 1001 |
asOrderedCollection |
1002 |
"return the receiver as an ordered collection" |
|
1 | 1003 |
|
606 | 1004 |
"could be an instance of a subclass..." |
1005 |
self class == OrderedCollection ifTrue:[ |
|
1006 |
^ self |
|
1007 |
]. |
|
1008 |
^ super asOrderedCollection |
|
1 | 1009 |
! ! |
1010 |
||
606 | 1011 |
!OrderedCollection methodsFor:'copying'! |
13 | 1012 |
|
606 | 1013 |
, aCollection |
1014 |
"return a new collection formed from concatenating the receiver with |
|
1015 |
the argument" |
|
1 | 1016 |
|
606 | 1017 |
|newCollection| |
1 | 1018 |
|
606 | 1019 |
newCollection := self copyEmpty:(self size + aCollection size). |
1020 |
self do:[:element | |
|
1021 |
newCollection add:element |
|
42 | 1022 |
]. |
606 | 1023 |
aCollection do:[:element | |
1024 |
newCollection add:element |
|
1025 |
]. |
|
1026 |
^ newCollection |
|
42 | 1027 |
|
253 | 1028 |
" |
606 | 1029 |
#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection |
1030 |
#(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection |
|
253 | 1031 |
" |
42 | 1032 |
! |
1033 |
||
606 | 1034 |
copy |
1035 |
"return a new OrderedCollection containing the elements of the receiver." |
|
1036 |
||
1037 |
"redefinition is a consequence of the implementation with a |
|
1038 |
separate array - otherwise we get a shallow copy of the |
|
1039 |
contents array, which is not what we want here" |
|
1040 |
||
1041 |
^ self copyFrom:1 to:self size |
|
1042 |
! |
|
1043 |
||
1044 |
copyEmpty |
|
1045 |
"return a copy of the receiver without any elements." |
|
1046 |
||
1047 |
^ self copyEmpty:10 |
|
1048 |
! |
|
1049 |
||
1050 |
copyFrom:start to:stop |
|
1051 |
"return a new OrderedCollection containing the elements |
|
1052 |
from start to stop." |
|
42 | 1053 |
|
606 | 1054 |
|newCollection sz| |
1055 |
||
1056 |
sz := stop - start + 1. |
|
1057 |
newCollection := self copyEmptyAndGrow:sz. "must grow, otherwise replace fails" |
|
1058 |
newCollection replaceFrom:1 to:sz with:self startingAt:start. |
|
1059 |
^ newCollection |
|
1060 |
! |
|
1061 |
||
1062 |
copyWith:newElement |
|
1063 |
"return a new collection containing the receivers elements |
|
1064 |
and the single new element, newElement. |
|
1065 |
This is different from concatentation, which expects another collection |
|
1066 |
as argument, but equivalent to copy-and-addLast." |
|
1067 |
||
1068 |
|newCollection mySize newSize| |
|
1069 |
||
1070 |
mySize := self size. |
|
1071 |
newSize := mySize + 1. |
|
1072 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
|
1073 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
1074 |
newCollection at:newSize put:newElement. |
|
1075 |
^newCollection |
|
42 | 1076 |
|
253 | 1077 |
" |
606 | 1078 |
#(1 2 3 4 5) copyWith:$a |
1079 |
'abcdefg' copyWith:$h |
|
1080 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
1081 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
1082 |
" |
|
1083 |
! |
|
1084 |
||
1085 |
postCopy |
|
1086 |
"have to copy the contentsArray too" |
|
1087 |
||
1088 |
contentsArray := contentsArray shallowCopy |
|
1089 |
! ! |
|
1090 |
||
1091 |
!OrderedCollection methodsFor:'enumerating'! |
|
1092 |
||
1093 |
collect:aBlock |
|
1094 |
"evaluate the argument, aBlock for every element in the collection |
|
1095 |
and return a collection of the results" |
|
1096 |
||
1097 |
|newCollection |
|
1098 |
start "{ Class:SmallInteger }" |
|
1099 |
stop "{ Class:SmallInteger }" | |
|
1100 |
||
1101 |
newCollection := self copyEmpty:(self size). |
|
1102 |
stop := lastIndex. |
|
1103 |
start := firstIndex. |
|
1104 |
start to:stop do:[:index | |
|
1105 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
1106 |
]. |
|
1107 |
^ newCollection |
|
1108 |
||
253 | 1109 |
" |
606 | 1110 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] |
1111 |
#(1 2 3 4) asOrderedCollection collect:[:i | i even] |
|
1112 |
" |
|
1113 |
! |
|
1114 |
||
5508 | 1115 |
collect:collectBlock thenSelect:selectBlock |
1116 |
"combination of collect followed by select; redefined to avoid creation of intermediate garbage" |
|
1117 |
||
1118 |
|newCollection newElement |
|
1119 |
start "{ Class:SmallInteger }" |
|
1120 |
stop "{ Class:SmallInteger }" | |
|
1121 |
||
1122 |
newCollection := self copyEmpty:(self size). |
|
1123 |
stop := lastIndex. |
|
1124 |
start := firstIndex. |
|
1125 |
start to:stop do:[:index | |
|
1126 |
newElement := collectBlock value:(contentsArray at:index). |
|
1127 |
(selectBlock value:newElement) ifTrue:[ |
|
1128 |
newCollection add:newElement. |
|
1129 |
] |
|
1130 |
]. |
|
1131 |
^ newCollection |
|
1132 |
||
1133 |
" |
|
1134 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] thenSelect:[:each | each > 5] |
|
1135 |
( #(1 2 3 4) asOrderedCollection collect:[:i | i * i]) select:[:each | each > 5] |
|
1136 |
||
1137 |
Time millisecondsToRun:[ |
|
1138 |
100000 timesRepeat:[ |
|
1139 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] thenSelect:[:each | each > 5] |
|
1140 |
] |
|
1141 |
] |
|
1142 |
||
1143 |
Time millisecondsToRun:[ |
|
1144 |
100000 timesRepeat:[ |
|
1145 |
( #(1 2 3 4) asOrderedCollection collect:[:i | i * i]) select:[:each | each > 5] |
|
1146 |
] |
|
1147 |
] |
|
1148 |
" |
|
1149 |
! |
|
1150 |
||
606 | 1151 |
do:aBlock |
1152 |
"evaluate the argument, aBlock for every element in the collection." |
|
1153 |
||
1154 |
contentsArray from:firstIndex to:lastIndex do:aBlock |
|
1155 |
! |
|
1156 |
||
1157 |
keysAndValuesDo:aTwoArgBlock |
|
1158 |
"evaluate the argument, aBlock for every element in the collection, |
|
1159 |
passing both index and element as arguments." |
|
1160 |
||
1161 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1162 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1163 |
idx "{ Class:SmallInteger }" | |
606 | 1164 |
|
1165 |
stop := lastIndex. |
|
1166 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1167 |
idx := 1. |
606 | 1168 |
start to:stop do:[:index | |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1169 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1170 |
idx := idx + 1. |
606 | 1171 |
] |
1172 |
||
1173 |
" |
|
1174 |
#(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value | |
|
1422 | 1175 |
Transcript show:index; show:' '; showCR:value |
606 | 1176 |
] |
1177 |
" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1178 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1179 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1180 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1181 |
oc := #(10 20 30 40 50 60 70 80) asOrderedCollection. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1182 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1183 |
oc keysAndValuesDo:[:index :value | |
1422 | 1184 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1185 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1186 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1187 |
|
606 | 1188 |
! |
1189 |
||
679 | 1190 |
keysAndValuesReverseDo:aTwoArgBlock |
1191 |
"evaluate the argument, aBlock for every element in the collection, |
|
1192 |
passing both index and element as arguments." |
|
1193 |
||
1194 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1195 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1196 |
idx "{ Class:SmallInteger }"| |
679 | 1197 |
|
1198 |
stop := lastIndex. |
|
1199 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1200 |
idx := (stop - start + 1). |
679 | 1201 |
stop to:start by: -1 do:[:index | |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1202 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1203 |
idx := idx - 1. |
679 | 1204 |
] |
1205 |
||
1206 |
" |
|
1207 |
#(10 20 30 40) asOrderedCollection keysAndValuesReverseDo:[:index :value | |
|
1422 | 1208 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1209 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1210 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1211 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1212 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1213 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1214 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1215 |
oc := #(10 20 30 40 50 60 70 80) asOrderedCollection. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1216 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1217 |
oc keysAndValuesReverseDo:[:index :value | |
1422 | 1218 |
Transcript show:index; show:' '; showCR:value |
679 | 1219 |
] |
1220 |
" |
|
1071 | 1221 |
! |
1222 |
||
1223 |
reverseDo:aBlock |
|
1224 |
"evaluate the argument, aBlock for every element in the collection |
|
1225 |
procesing elements in reverse direction (i.e. starting with the last)" |
|
1226 |
||
1227 |
contentsArray from:firstIndex to:lastIndex reverseDo:aBlock |
|
1 | 1228 |
! ! |
1229 |
||
56 | 1230 |
!OrderedCollection methodsFor:'filling & replacing'! |
13 | 1231 |
|
1232 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1170 | 1233 |
"replace elements in the receiver between index start and stop, |
1234 |
with elements taken from replacementCollection starting at repStart. |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1235 |
Return the receiver. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1236 |
Redefined here; can be done faster as the inherited operation." |
13 | 1237 |
|
1238 |
|end| |
|
1239 |
||
1240 |
end := stop + firstIndex - 1. |
|
1241 |
((start >= 1) and:[end <= lastIndex]) ifTrue:[ |
|
1170 | 1242 |
aCollection class == self class ifTrue:[ |
1243 |
contentsArray |
|
1244 |
replaceFrom:(start + firstIndex - 1) |
|
1245 |
to:end |
|
1246 |
with:aCollection contentsArray |
|
1247 |
startingAt:(repStart + aCollection firstIndex - 1). |
|
1248 |
] ifFalse:[ |
|
1249 |
contentsArray |
|
1250 |
replaceFrom:(start + firstIndex - 1) |
|
1251 |
to:end |
|
1252 |
with:aCollection |
|
1253 |
startingAt:repStart. |
|
1254 |
]. |
|
1255 |
^ self |
|
13 | 1256 |
]. |
1257 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
293 | 1258 |
|
1259 |
" |
|
1260 |
|c| |
|
1261 |
||
1262 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1263 |
c replaceFrom:3 to:6 with:c startingAt:2. |
|
1264 |
c |
|
1265 |
" |
|
1266 |
" |
|
1267 |
|c| |
|
1268 |
||
1269 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1270 |
c replaceFrom:3 to:5 with:c startingAt:4. |
|
1271 |
c |
|
1272 |
" |
|
1170 | 1273 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1274 |
"Modified: / 28.1.1998 / 16:49:31 / cg" |
13 | 1275 |
! ! |
1276 |
||
606 | 1277 |
!OrderedCollection methodsFor:'grow & shrink'! |
77 | 1278 |
|
606 | 1279 |
grow:newSize |
1280 |
"grow the receiver to newSize" |
|
77 | 1281 |
|
606 | 1282 |
|oldSize newContents oldLast newLast| |
77 | 1283 |
|
606 | 1284 |
oldSize := lastIndex - firstIndex + 1. |
1285 |
newSize ~~ oldSize ifTrue:[ |
|
1286 |
newLast := firstIndex + newSize - 1. |
|
1287 |
newSize < oldSize ifTrue:[ |
|
1288 |
oldLast := lastIndex. |
|
1289 |
lastIndex := newLast. |
|
1290 |
" |
|
1291 |
nil out rest, to give GC a chance to reclaim things |
|
1292 |
" |
|
1293 |
contentsArray from:lastIndex + 1 to:oldLast put:nil. |
|
1294 |
] ifFalse:[ |
|
1295 |
newLast <= contentsArray size ifTrue:[ |
|
1296 |
lastIndex := newLast. |
|
1297 |
^ self |
|
1298 |
]. |
|
77 | 1299 |
|
606 | 1300 |
newContents := Array basicNew:newSize. |
1301 |
newContents replaceFrom:1 to:oldSize with:contentsArray startingAt:firstIndex. |
|
1302 |
contentsArray := newContents. |
|
1303 |
firstIndex := 1. |
|
1304 |
lastIndex := newSize |
|
1305 |
] |
|
1306 |
] |
|
1307 |
! ! |
|
77 | 1308 |
|
606 | 1309 |
!OrderedCollection methodsFor:'inspecting'! |
1310 |
||
1311 |
inspectorClass |
|
1312 |
"redefined to launch an OrderedCollectionInspector |
|
1313 |
(instead of the default InspectorView)." |
|
1314 |
||
1315 |
^ OrderedCollectionInspectorView |
|
77 | 1316 |
|
1317 |
" |
|
606 | 1318 |
(OrderedCollection withAll:#(3 2 1)) inspect |
1319 |
(OrderedCollection withAll:#(3 2 1)) removeFirst; yourself; inspect |
|
1320 |
#(0 8 15 3 99 2) asSortedCollection inspect |
|
77 | 1321 |
" |
1322 |
! ! |
|
1323 |
||
1 | 1324 |
!OrderedCollection methodsFor:'private'! |
1325 |
||
606 | 1326 |
initContents:size |
1327 |
"setup the receiver-collection to hold size entries" |
|
33 | 1328 |
|
606 | 1329 |
contentsArray := Array basicNew:size. |
1330 |
firstIndex := 1. |
|
1331 |
lastIndex := 0 |
|
1 | 1332 |
! |
1333 |
||
1334 |
makeRoomAtFront |
|
282 | 1335 |
"grow/shift the contents for more room at the beginning. |
1336 |
Does not change the logical size. |
|
1337 |
i.e. |
|
1338 |
#(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6)" |
|
1 | 1339 |
|
1340 |
|newContents |
|
33 | 1341 |
oldSize "{ Class:SmallInteger }" |
1342 |
newSize "{ Class:SmallInteger }" |
|
1343 |
startIndex "{ Class:SmallInteger }" |
|
1344 |
sz "{ Class:SmallInteger }"| |
|
1 | 1345 |
|
1346 |
oldSize := contentsArray size. |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1347 |
sz := lastIndex - firstIndex + 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1348 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1349 |
((oldSize == 0) or:[sz == 0]) ifTrue:[ |
253 | 1350 |
contentsArray := Array basicNew:3. |
159 | 1351 |
firstIndex := 2. lastIndex := 1. |
1352 |
^ self |
|
93 | 1353 |
]. |
1354 |
||
329 | 1355 |
" |
1356 |
if there is lots of room at the end (> 50%), |
|
1357 |
shift instead of growing. This helps collections |
|
1358 |
which get elements removed at the end and added at front. |
|
1359 |
" |
|
33 | 1360 |
oldSize > (sz * 2) ifTrue:[ |
508 | 1361 |
startIndex := oldSize // 4. |
1362 |
startIndex > 1 ifTrue:[ |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1363 |
contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1364 |
replaceFrom:startIndex |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1365 |
to:startIndex + sz - 1 |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1366 |
with:contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1367 |
startingAt:1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1368 |
contentsArray from:1 to:(startIndex - 1) put:nil. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1369 |
firstIndex := startIndex. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1370 |
lastIndex := startIndex + sz - 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1371 |
^ self |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1372 |
] |
33 | 1373 |
]. |
2 | 1374 |
newSize := oldSize * 2. |
329 | 1375 |
newSize == 0 ifTrue:[ newSize := 3]. |
253 | 1376 |
newContents := Array basicNew:newSize. |
77 | 1377 |
newContents |
159 | 1378 |
replaceFrom:(oldSize + 1) |
1379 |
to:newSize |
|
1380 |
with:contentsArray |
|
1381 |
startingAt:1. |
|
1 | 1382 |
contentsArray := newContents. |
1383 |
firstIndex := firstIndex + oldSize. |
|
1384 |
lastIndex := lastIndex + oldSize |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1385 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1386 |
"Created: 8.11.1995 / 12:47:49 / cg" |
1 | 1387 |
! |
1388 |
||
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1389 |
makeRoomAtIndex:whereToMakeEmptySlot |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1390 |
"grow the contents for inserting at whereToMakeEmptySlot. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1391 |
The whereToMakeEmptySlot argument must be a physical index within the contentsArray. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1392 |
If there is (plenty of) room at either end, elements are shifted inplace to create |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1393 |
an empty slot; otherwise, a new contentsArray is allocated. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1394 |
Since this changes the logical size, the modified index is returned. |
1 | 1395 |
i.e. |
1396 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6) |
|
1397 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6) |
|
1398 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)" |
|
1399 |
||
1400 |
|newContents |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1401 |
newSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1402 |
oldSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1403 |
oneFourthOfSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1404 |
first "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1405 |
last "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1406 |
index "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1407 |
shiftLeft shiftRight| |
1 | 1408 |
|
1409 |
oldSize := contentsArray size. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1410 |
oneFourthOfSize := oldSize // 4. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1411 |
index := whereToMakeEmptySlot. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1412 |
first := firstIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1413 |
last := lastIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1414 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1415 |
((first > 1) and:[first > oneFourthOfSize]) ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1416 |
"there is room (>25%) at the beginning" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1417 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1418 |
shiftLeft := true. |
33 | 1419 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1420 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1421 |
"/ so, we'd have to copy all elements before that index |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1422 |
"/ one slot towards the containers beginning ... |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1423 |
"/ if there is also space at the end, AND the number of |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1424 |
"/ elements after the index is smaller than the number before, |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1425 |
"/ copy the remaining elements. To copy the least possible number. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1426 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1427 |
(last - index) < (index - first) ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1428 |
last < (oneFourthOfSize * 3) ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1429 |
shiftLeft := false. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1430 |
shiftRight := true. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1431 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1432 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1433 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1434 |
] ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1435 |
last < (oneFourthOfSize * 3) ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1436 |
shiftRight := true |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1437 |
] |
33 | 1438 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1439 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1440 |
shiftLeft == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1441 |
"there is room (>25%) at the beginning" |
33 | 1442 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1443 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1444 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1445 |
replaceFrom:(first - 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1446 |
to:(index - 2) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1447 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1448 |
startingAt:first. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1449 |
contentsArray at:index-1 put:nil. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1450 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1451 |
firstIndex := first - 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1452 |
^ index - 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1453 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1454 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1455 |
shiftRight == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1456 |
"there is room (>25%) at the end" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1457 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1458 |
last := last + 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1459 |
index == last ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1460 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1461 |
replaceFrom:(index + 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1462 |
to:last |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1463 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1464 |
startingAt:index. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1465 |
contentsArray at:index put:nil |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1466 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1467 |
lastIndex := last. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1468 |
^ index |
33 | 1469 |
]. |
1470 |
||
1471 |
newSize := oldSize * 2. |
|
253 | 1472 |
newContents := Array basicNew:newSize. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1473 |
index ~~ first ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1474 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1475 |
replaceFrom:1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1476 |
to:index - first |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1477 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1478 |
startingAt:first. |
1 | 1479 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1480 |
index <= last ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1481 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1482 |
replaceFrom:index - first + 2 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1483 |
to:(last - first + 2) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1484 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1485 |
startingAt:index. |
1 | 1486 |
]. |
1487 |
contentsArray := newContents. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1488 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1489 |
lastIndex := last - first + 2. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1490 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1491 |
"/ return the modified index |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1492 |
^ index - (first - firstIndex). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1493 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1494 |
"Modified: 12.4.1996 / 17:18:58 / cg" |
1 | 1495 |
! |
1496 |
||
2222 | 1497 |
makeRoomAtIndex:whereToMakeEmptySlots for:howMany |
1498 |
"grow the contents for inserting at whereToMakeEmptySlot. |
|
1499 |
The whereToMakeEmptySlot argument must be a physical index within the contentsArray. |
|
1500 |
If there is (plenty of) room at either end, elements are shifted inplace to create |
|
1501 |
an empty slot; otherwise, a new contentsArray is allocated. |
|
1502 |
Since this changes the logical size, the modified index is returned. |
|
1503 |
i.e. |
|
1504 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 for:2 -> #(1 2 nil nil 3 4 5 6) |
|
1505 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 for:2 -> #(nil nil 1 2 3 4 5 6) |
|
1506 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 for:2 -> #(1 2 3 4 5 6 nil nil)" |
|
1507 |
||
1508 |
|newContents |
|
1509 |
newSize "{ Class:SmallInteger }" |
|
1510 |
oldSize "{ Class:SmallInteger }" |
|
1511 |
oneFourthOfSize "{ Class:SmallInteger }" |
|
1512 |
first "{ Class:SmallInteger }" |
|
1513 |
last "{ Class:SmallInteger }" |
|
1514 |
index "{ Class:SmallInteger }" |
|
1515 |
shiftLeft shiftRight| |
|
1516 |
||
1517 |
oldSize := contentsArray size. |
|
1518 |
oneFourthOfSize := oldSize // 4. |
|
1519 |
index := whereToMakeEmptySlots. |
|
1520 |
first := firstIndex. |
|
1521 |
last := lastIndex. |
|
1522 |
||
2549 | 1523 |
shiftLeft := shiftRight := false. |
2222 | 1524 |
((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[ |
1525 |
"there is room (>25%) at the beginning" |
|
1526 |
||
1527 |
shiftLeft := true. |
|
1528 |
] ifFalse:[ |
|
2549 | 1529 |
((last + howMany) <= oldSize |
1530 |
and:[last < (oneFourthOfSize * 3)]) ifTrue:[ |
|
2222 | 1531 |
shiftRight := true |
1532 |
] |
|
1533 |
]. |
|
1534 |
||
1535 |
shiftLeft == true ifTrue:[ |
|
2549 | 1536 |
"there is room at the beginning" |
2222 | 1537 |
|
1538 |
index == first ifFalse:[ |
|
1539 |
contentsArray |
|
1540 |
replaceFrom:(first - howMany) |
|
1541 |
to:(index - howMany - 1) |
|
1542 |
with:contentsArray |
|
1543 |
startingAt:first. |
|
1544 |
contentsArray from:index-howMany to:index-1 put:nil. |
|
1545 |
]. |
|
1546 |
firstIndex := first - howMany. |
|
1547 |
^ index - howMany |
|
1548 |
]. |
|
1549 |
||
1550 |
shiftRight == true ifTrue:[ |
|
2549 | 1551 |
"there is room at the end" |
2222 | 1552 |
|
1553 |
last := last + howMany. |
|
1554 |
index == last ifFalse:[ |
|
1555 |
contentsArray |
|
1556 |
replaceFrom:(index + howMany) |
|
1557 |
to:last |
|
1558 |
with:contentsArray |
|
1559 |
startingAt:index. |
|
1560 |
contentsArray from:index to:index+howMany-1 put:nil |
|
1561 |
]. |
|
1562 |
lastIndex := last. |
|
1563 |
^ index |
|
1564 |
]. |
|
1565 |
||
1566 |
newSize := oldSize * 2. |
|
1567 |
[newSize < (oldSize+howMany)] whileTrue:[ |
|
1568 |
newSize := newSize * 2 |
|
1569 |
]. |
|
1570 |
||
1571 |
newContents := Array basicNew:newSize. |
|
1572 |
index > first ifTrue:[ |
|
1573 |
newContents |
|
1574 |
replaceFrom:1 |
|
1575 |
to:index - first |
|
1576 |
with:contentsArray |
|
1577 |
startingAt:first. |
|
1578 |
]. |
|
1579 |
index <= last ifTrue:[ |
|
1580 |
newContents |
|
1581 |
replaceFrom:index - first + howMany + 1 |
|
1582 |
to:(last - first + howMany + 1) |
|
1583 |
with:contentsArray |
|
1584 |
startingAt:(index). |
|
1585 |
]. |
|
1586 |
contentsArray := newContents. |
|
1587 |
firstIndex := 1. |
|
1588 |
lastIndex := last - first + howMany + 1. |
|
1589 |
||
1590 |
"/ return the modified index |
|
1591 |
^ index - (first - firstIndex). |
|
1592 |
||
2549 | 1593 |
"Modified: 15.4.1997 / 12:34:16 / cg" |
2222 | 1594 |
! |
1595 |
||
606 | 1596 |
makeRoomAtLast |
1597 |
"grow/shift the contents for more room at the end. |
|
1598 |
Does not change the logical size. |
|
1599 |
i.e. |
|
1600 |
#(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)" |
|
1601 |
||
1602 |
|newContents |
|
1603 |
oldSize "{ Class:SmallInteger }" |
|
1604 |
newSize "{ Class:SmallInteger }" |
|
1605 |
startIndex "{ Class:SmallInteger }" |
|
1606 |
sz "{ Class:SmallInteger }"| |
|
1607 |
||
1608 |
oldSize := contentsArray size. |
|
1609 |
sz := lastIndex - firstIndex + 1. |
|
1610 |
||
1611 |
" |
|
1612 |
if there is lots of room at the beginning (> 50%), |
|
1613 |
shift instead of growing. This helps collections which get |
|
1614 |
elements removed at front and added at the end. |
|
1615 |
" |
|
1616 |
oldSize > (sz * 2) ifTrue:[ |
|
1617 |
startIndex := firstIndex // 4. |
|
1618 |
startIndex == 0 ifTrue:[ |
|
1619 |
startIndex := 1 |
|
1620 |
]. |
|
1621 |
contentsArray |
|
1622 |
replaceFrom:startIndex |
|
1623 |
to:startIndex + sz - 1 |
|
1624 |
with:contentsArray |
|
1625 |
startingAt:firstIndex. |
|
1626 |
contentsArray from:startIndex + sz to:lastIndex put:nil. |
|
1627 |
firstIndex := startIndex. |
|
1628 |
lastIndex := startIndex + sz - 1. |
|
1629 |
^ self |
|
1630 |
]. |
|
1631 |
newSize := oldSize * 2. |
|
1632 |
newSize == 0 ifTrue:[newSize := 3]. |
|
1633 |
newContents := Array basicNew:newSize. |
|
1634 |
newContents |
|
1635 |
replaceFrom:1 |
|
1636 |
to:oldSize |
|
1637 |
with:contentsArray |
|
1638 |
startingAt:1. |
|
1639 |
contentsArray := newContents |
|
1640 |
! |
|
1641 |
||
1642 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex |
|
1643 |
"set first and last index" |
|
1644 |
||
1645 |
firstIndex := newFirstIndex. |
|
1646 |
lastIndex := newLastIndex. |
|
1647 |
! ! |
|
1648 |
||
1649 |
!OrderedCollection methodsFor:'private accessing'! |
|
1650 |
||
1651 |
contentsArray |
|
4153 | 1652 |
"return the orderedCollections underlying contentsArray. |
1653 |
The actual elements are found here starting at firstIndex, |
|
1654 |
and ending at lastIndex." |
|
1655 |
||
606 | 1656 |
^ contentsArray |
1657 |
! |
|
1658 |
||
1659 |
firstIndex |
|
4153 | 1660 |
"return the index of my first element in my underlying contentsArray. |
1661 |
The actual elements are found starting this index, |
|
1662 |
and ending at lastIndex." |
|
1663 |
||
606 | 1664 |
^ firstIndex |
1665 |
! ! |
|
1666 |
||
1667 |
!OrderedCollection methodsFor:'queries'! |
|
1668 |
||
1669 |
capacity |
|
1670 |
"return the number of elements, that the receiver is |
|
1671 |
prepared to take. |
|
1672 |
Not used by the system; added for ST-80 compatibility." |
|
1673 |
||
1674 |
^ contentsArray size |
|
1675 |
! |
|
1676 |
||
1677 |
size |
|
1678 |
"return the number of elements in the collection" |
|
1679 |
||
1680 |
^ lastIndex - firstIndex + 1 |
|
1681 |
! ! |
|
1682 |
||
3095 | 1683 |
!OrderedCollection methodsFor:'searching'! |
1 | 1684 |
|
1685 |
identityIndexOf:anObject |
|
33 | 1686 |
"return the index of anObject or 0 if not found. Compare using ==" |
1 | 1687 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1688 |
|index| |
1 | 1689 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1690 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1691 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1692 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1693 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1694 |
index == 0 ifTrue:[^ 0]. |
1 | 1695 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1696 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1697 |
"Modified: 12.4.1996 / 17:58:25 / cg" |
1 | 1698 |
! |
1699 |
||
1700 |
identityIndexOf:anObject startingAt:startIndex |
|
33 | 1701 |
"return the index of anObject, starting search at startIndex. |
1702 |
Compare using ==; return 0 if not found in the collection" |
|
1 | 1703 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1704 |
|index| |
1 | 1705 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1706 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1707 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1708 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1709 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1710 |
index == 0 ifTrue:[^ 0]. |
1 | 1711 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1712 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1713 |
"Modified: 12.4.1996 / 17:58:19 / cg" |
1 | 1714 |
! |
1715 |
||
1716 |
indexOf:anObject |
|
33 | 1717 |
"return the index of anObject or 0 if not found in the collection. |
1718 |
Compare using =" |
|
1 | 1719 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1720 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1721 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1722 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1723 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1724 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1725 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1726 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1727 |
^ index - firstIndex + 1 |
1 | 1728 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1729 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1730 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1731 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1732 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1733 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1734 |
c indexOf:99 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1735 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1736 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1737 |
"Modified: 12.4.1996 / 17:57:54 / cg" |
1 | 1738 |
! |
1739 |
||
1740 |
indexOf:anObject startingAt:startIndex |
|
33 | 1741 |
"return the index of anObject, starting search at startIndex. |
1742 |
Compare using =; return 0 if not found in the collection" |
|
1 | 1743 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1744 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1745 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1746 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1747 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1748 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1749 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1750 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1751 |
^ index - firstIndex + 1 |
1 | 1752 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1753 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1754 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1755 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1756 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1757 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1758 |
c indexOf:4 startingAt:5 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1759 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1760 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1761 |
"Modified: 12.4.1996 / 17:58:53 / cg" |
1 | 1762 |
! ! |
1763 |
||
3095 | 1764 |
!OrderedCollection methodsFor:'testing'! |
1765 |
||
1766 |
includes:anObject |
|
1767 |
"return true if anObject is in the collection. Compare using =" |
|
1768 |
||
1769 |
^ (contentsArray |
|
1770 |
indexOf:anObject |
|
1771 |
startingAt:firstIndex |
|
1772 |
endingAt:lastIndex) ~~ 0 |
|
1773 |
||
1774 |
"Modified: 12.4.1996 / 17:57:27 / cg" |
|
1775 |
! |
|
1776 |
||
1777 |
includesIdentical:anObject |
|
1778 |
"return true if anObject is in the collection. Compare using ==" |
|
1779 |
||
1780 |
^ (contentsArray |
|
1781 |
identityIndexOf:anObject |
|
1782 |
startingAt:firstIndex |
|
1783 |
endingAt:lastIndex) ~~ 0 |
|
1784 |
||
1785 |
"Modified: 12.4.1996 / 17:57:09 / cg" |
|
5401 | 1786 |
! |
1787 |
||
5556 | 1788 |
isEmpty |
1789 |
"return true, if the receiver has no elements" |
|
1790 |
||
1791 |
^ lastIndex < firstIndex |
|
1792 |
! |
|
1793 |
||
1794 |
isFixedSize |
|
1795 |
"return true if the receiver cannot grow - this will vanish once |
|
1796 |
Arrays and Strings learn how to grow ..." |
|
1797 |
||
1798 |
^ false |
|
1799 |
! |
|
1800 |
||
5401 | 1801 |
isOrderedCollection |
1802 |
"return true, if the receiver is some kind of ordered collection (or list etc); |
|
1803 |
true is returned here - the method is only redefined in Object." |
|
1804 |
||
1805 |
^ true |
|
1806 |
||
1807 |
||
5556 | 1808 |
! |
1809 |
||
1810 |
notEmpty |
|
1811 |
"return true, if the receiver has any elements" |
|
1812 |
||
1813 |
^ lastIndex >= firstIndex |
|
3095 | 1814 |
! ! |
1815 |
||
629 | 1816 |
!OrderedCollection class methodsFor:'documentation'! |
1817 |
||
1818 |
version |
|
5556 | 1819 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.77 2000-08-22 13:49:01 cg Exp $' |
629 | 1820 |
! ! |