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