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