author | Claus Gittinger <cg@exept.de> |
Sat, 14 Sep 2002 13:37:53 +0200 | |
changeset 6755 | a5704d23e08f |
parent 6700 | e4254778c239 |
child 6756 | e3ad68595086 |
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:[ |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
884 |
contentsArray at:firstIndex put:nil. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
885 |
firstIndex := firstIndex + 1. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
886 |
] ifFalse:[ |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
887 |
index == lastIndex ifTrue:[ |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
888 |
contentsArray at:lastIndex put:nil. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
889 |
lastIndex := lastIndex - 1. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
890 |
] ifFalse:[ |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
891 |
index := index - firstIndex + 1. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
892 |
self removeFromIndex:index toIndex:index. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
893 |
] |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
894 |
]. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
895 |
^ anObject |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
896 |
]. |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
897 |
^ exceptionBlock value |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
898 |
|
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
899 |
" |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
900 |
#(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
|
901 |
#(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
|
902 |
#(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
|
903 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#fee; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
904 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#foo; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
905 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#baz; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
906 |
#(fee) asOrderedCollection removeIdentical:#fee; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
907 |
#(fee) asOrderedCollection removeIdentical:#foo; yourself |
2348
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 |
|
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
910 |
"Modified: 8.2.1997 / 18:57:43 / cg" |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
911 |
! |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
912 |
|
606 | 913 |
removeLast |
914 |
"remove the last element from the collection; return the element" |
|
915 |
||
3107 | 916 |
|anObject |
917 |
idx "{ Class: SmallInteger }" | |
|
217 | 918 |
|
3107 | 919 |
idx := lastIndex. |
920 |
firstIndex > idx ifTrue:[ |
|
921 |
"error if collection is empty" |
|
922 |
^ self emptyCollectionError. |
|
606 | 923 |
]. |
3107 | 924 |
anObject := contentsArray basicAt:idx. |
1496 | 925 |
|
926 |
"/ nil it (helps GC) |
|
3107 | 927 |
contentsArray basicAt:idx put:nil. |
928 |
lastIndex := idx := idx - 1. |
|
606 | 929 |
|
3107 | 930 |
firstIndex > idx ifTrue:[ |
931 |
"reset to avoid ever growing" |
|
932 |
firstIndex := 1. |
|
933 |
lastIndex := 0 |
|
606 | 934 |
]. |
935 |
^ anObject |
|
217 | 936 |
|
937 |
" |
|
606 | 938 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself |
939 |
OrderedCollection new removeLast |
|
940 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself |
|
217 | 941 |
" |
3107 | 942 |
|
943 |
"Modified: / 12.11.1997 / 17:58:57 / cg" |
|
13 | 944 |
! |
945 |
||
606 | 946 |
removeLast:n |
947 |
"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
|
948 |
Return a collection of removed elements." |
606 | 949 |
|
950 |
|mySize ret| |
|
951 |
||
952 |
mySize := self size. |
|
953 |
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
|
954 |
"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
|
955 |
^ self notEnoughElementsError. |
606 | 956 |
]. |
13 | 957 |
|
606 | 958 |
ret := Array new:n. |
959 |
ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1. |
|
1496 | 960 |
|
606 | 961 |
"/ |
962 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
963 |
"/ |
|
964 |
contentsArray from:lastIndex - n + 1 to:lastIndex put:nil. |
|
965 |
lastIndex := lastIndex - n. |
|
13 | 966 |
|
606 | 967 |
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
|
968 |
"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
|
969 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
970 |
lastIndex := 0 |
13 | 971 |
]. |
606 | 972 |
^ ret |
13 | 973 |
|
974 |
" |
|
606 | 975 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself |
976 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself |
|
977 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself |
|
978 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself |
|
13 | 979 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
980 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
981 |
"Modified: 12.4.1996 / 13:39:12 / cg" |
4862 | 982 |
! |
983 |
||
984 |
reset |
|
985 |
"logically remove all elements from the collection. |
|
986 |
Thats almost the same as #removeAll, but keeps the contentsArray. |
|
987 |
Returns the receiver." |
|
988 |
||
989 |
firstIndex := contentsArray size // 3 max: 1. |
|
990 |
lastIndex := firstIndex - 1 |
|
1 | 991 |
! ! |
992 |
||
606 | 993 |
!OrderedCollection methodsFor:'converting'! |
1 | 994 |
|
1170 | 995 |
asArray |
996 |
"return the receiver as an array." |
|
997 |
||
4153 | 998 |
^ contentsArray copyFrom:firstIndex to:lastIndex |
1170 | 999 |
|
4153 | 1000 |
"/ |newArray sz| |
1001 |
"/ |
|
1002 |
"/ sz := self size. |
|
1003 |
"/ newArray := Array new:sz. |
|
1004 |
"/ newArray replaceFrom:1 to:sz with:contentsArray startingAt:firstIndex. |
|
1005 |
"/ ^ newArray |
|
1006 |
||
1007 |
" |
|
1008 |
OrderedCollection new asArray |
|
1009 |
OrderedCollection new add:1;add:2;asArray |
|
1010 |
OrderedCollection new add:1;removeFirst;asArray |
|
1011 |
" |
|
1170 | 1012 |
|
1013 |
" |
|
1014 |
|o rnd| |
|
1015 |
||
1016 |
o := OrderedCollection new. |
|
1017 |
rnd := Random new. |
|
1018 |
10000 timesRepeat:[ |
|
1019 |
o add:rnd next. |
|
1020 |
]. |
|
1021 |
Time millisecondsToRun:[o asArray] |
|
1022 |
" |
|
1023 |
||
1024 |
"Modified: 13.4.1996 / 12:10:56 / cg" |
|
1025 |
! |
|
1026 |
||
606 | 1027 |
asOrderedCollection |
1028 |
"return the receiver as an ordered collection" |
|
1 | 1029 |
|
606 | 1030 |
"could be an instance of a subclass..." |
1031 |
self class == OrderedCollection ifTrue:[ |
|
1032 |
^ self |
|
1033 |
]. |
|
1034 |
^ super asOrderedCollection |
|
1 | 1035 |
! ! |
1036 |
||
606 | 1037 |
!OrderedCollection methodsFor:'copying'! |
13 | 1038 |
|
606 | 1039 |
, aCollection |
1040 |
"return a new collection formed from concatenating the receiver with |
|
1041 |
the argument" |
|
1 | 1042 |
|
606 | 1043 |
|newCollection| |
1 | 1044 |
|
606 | 1045 |
newCollection := self copyEmpty:(self size + aCollection size). |
1046 |
self do:[:element | |
|
1047 |
newCollection add:element |
|
42 | 1048 |
]. |
606 | 1049 |
aCollection do:[:element | |
1050 |
newCollection add:element |
|
1051 |
]. |
|
1052 |
^ newCollection |
|
42 | 1053 |
|
253 | 1054 |
" |
606 | 1055 |
#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection |
1056 |
#(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection |
|
253 | 1057 |
" |
42 | 1058 |
! |
1059 |
||
606 | 1060 |
copy |
1061 |
"return a new OrderedCollection containing the elements of the receiver." |
|
1062 |
||
1063 |
"redefinition is a consequence of the implementation with a |
|
1064 |
separate array - otherwise we get a shallow copy of the |
|
1065 |
contents array, which is not what we want here" |
|
1066 |
||
1067 |
^ self copyFrom:1 to:self size |
|
1068 |
! |
|
1069 |
||
1070 |
copyEmpty |
|
1071 |
"return a copy of the receiver without any elements." |
|
1072 |
||
1073 |
^ self copyEmpty:10 |
|
1074 |
! |
|
1075 |
||
1076 |
copyFrom:start to:stop |
|
1077 |
"return a new OrderedCollection containing the elements |
|
1078 |
from start to stop." |
|
42 | 1079 |
|
606 | 1080 |
|newCollection sz| |
1081 |
||
1082 |
sz := stop - start + 1. |
|
1083 |
newCollection := self copyEmptyAndGrow:sz. "must grow, otherwise replace fails" |
|
1084 |
newCollection replaceFrom:1 to:sz with:self startingAt:start. |
|
1085 |
^ newCollection |
|
1086 |
! |
|
1087 |
||
1088 |
copyWith:newElement |
|
1089 |
"return a new collection containing the receivers elements |
|
1090 |
and the single new element, newElement. |
|
1091 |
This is different from concatentation, which expects another collection |
|
1092 |
as argument, but equivalent to copy-and-addLast." |
|
1093 |
||
1094 |
|newCollection mySize newSize| |
|
1095 |
||
1096 |
mySize := self size. |
|
1097 |
newSize := mySize + 1. |
|
1098 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
|
1099 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
1100 |
newCollection at:newSize put:newElement. |
|
1101 |
^newCollection |
|
42 | 1102 |
|
253 | 1103 |
" |
606 | 1104 |
#(1 2 3 4 5) copyWith:$a |
1105 |
'abcdefg' copyWith:$h |
|
1106 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
1107 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
1108 |
" |
|
1109 |
! |
|
1110 |
||
1111 |
postCopy |
|
1112 |
"have to copy the contentsArray too" |
|
1113 |
||
1114 |
contentsArray := contentsArray shallowCopy |
|
1115 |
! ! |
|
1116 |
||
1117 |
!OrderedCollection methodsFor:'enumerating'! |
|
1118 |
||
1119 |
collect:aBlock |
|
1120 |
"evaluate the argument, aBlock for every element in the collection |
|
1121 |
and return a collection of the results" |
|
1122 |
||
1123 |
|newCollection |
|
1124 |
start "{ Class:SmallInteger }" |
|
1125 |
stop "{ Class:SmallInteger }" | |
|
1126 |
||
1127 |
newCollection := self copyEmpty:(self size). |
|
1128 |
stop := lastIndex. |
|
1129 |
start := firstIndex. |
|
1130 |
start to:stop do:[:index | |
|
1131 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
1132 |
]. |
|
1133 |
^ newCollection |
|
1134 |
||
253 | 1135 |
" |
606 | 1136 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] |
1137 |
#(1 2 3 4) asOrderedCollection collect:[:i | i even] |
|
1138 |
" |
|
1139 |
! |
|
1140 |
||
5508 | 1141 |
collect:collectBlock thenSelect:selectBlock |
5874 | 1142 |
"combination of collect followed by select; |
1143 |
redefined to avoid the creation of an intermediate garbage collection." |
|
5508 | 1144 |
|
1145 |
|newCollection newElement |
|
1146 |
start "{ Class:SmallInteger }" |
|
1147 |
stop "{ Class:SmallInteger }" | |
|
1148 |
||
1149 |
newCollection := self copyEmpty:(self size). |
|
1150 |
stop := lastIndex. |
|
1151 |
start := firstIndex. |
|
1152 |
start to:stop do:[:index | |
|
1153 |
newElement := collectBlock value:(contentsArray at:index). |
|
1154 |
(selectBlock value:newElement) ifTrue:[ |
|
1155 |
newCollection add:newElement. |
|
1156 |
] |
|
1157 |
]. |
|
1158 |
^ newCollection |
|
1159 |
||
1160 |
" |
|
1161 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] thenSelect:[:each | each > 5] |
|
1162 |
( #(1 2 3 4) asOrderedCollection collect:[:i | i * i]) select:[:each | each > 5] |
|
1163 |
||
1164 |
Time millisecondsToRun:[ |
|
1165 |
100000 timesRepeat:[ |
|
1166 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] thenSelect:[:each | each > 5] |
|
1167 |
] |
|
1168 |
] |
|
1169 |
||
1170 |
Time millisecondsToRun:[ |
|
1171 |
100000 timesRepeat:[ |
|
1172 |
( #(1 2 3 4) asOrderedCollection collect:[:i | i * i]) select:[:each | each > 5] |
|
1173 |
] |
|
1174 |
] |
|
1175 |
" |
|
1176 |
! |
|
1177 |
||
606 | 1178 |
do:aBlock |
1179 |
"evaluate the argument, aBlock for every element in the collection." |
|
1180 |
||
1181 |
contentsArray from:firstIndex to:lastIndex do:aBlock |
|
1182 |
! |
|
1183 |
||
1184 |
keysAndValuesDo:aTwoArgBlock |
|
1185 |
"evaluate the argument, aBlock for every element in the collection, |
|
1186 |
passing both index and element as arguments." |
|
1187 |
||
1188 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1189 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1190 |
idx "{ Class:SmallInteger }" | |
606 | 1191 |
|
1192 |
stop := lastIndex. |
|
1193 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1194 |
idx := 1. |
606 | 1195 |
start to:stop do:[:index | |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1196 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1197 |
idx := idx + 1. |
606 | 1198 |
] |
1199 |
||
1200 |
" |
|
1201 |
#(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value | |
|
1422 | 1202 |
Transcript show:index; show:' '; showCR:value |
606 | 1203 |
] |
1204 |
" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1205 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1206 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1207 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1208 |
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
|
1209 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1210 |
oc keysAndValuesDo:[:index :value | |
1422 | 1211 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1212 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1213 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1214 |
|
606 | 1215 |
! |
1216 |
||
679 | 1217 |
keysAndValuesReverseDo:aTwoArgBlock |
1218 |
"evaluate the argument, aBlock for every element in the collection, |
|
1219 |
passing both index and element as arguments." |
|
1220 |
||
1221 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1222 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1223 |
idx "{ Class:SmallInteger }"| |
679 | 1224 |
|
1225 |
stop := lastIndex. |
|
1226 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1227 |
idx := (stop - start + 1). |
679 | 1228 |
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
|
1229 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1230 |
idx := idx - 1. |
679 | 1231 |
] |
1232 |
||
1233 |
" |
|
1234 |
#(10 20 30 40) asOrderedCollection keysAndValuesReverseDo:[:index :value | |
|
1422 | 1235 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1236 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1237 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1238 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1239 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1240 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1241 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1242 |
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
|
1243 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1244 |
oc keysAndValuesReverseDo:[:index :value | |
1422 | 1245 |
Transcript show:index; show:' '; showCR:value |
679 | 1246 |
] |
1247 |
" |
|
1071 | 1248 |
! |
1249 |
||
1250 |
reverseDo:aBlock |
|
1251 |
"evaluate the argument, aBlock for every element in the collection |
|
1252 |
procesing elements in reverse direction (i.e. starting with the last)" |
|
1253 |
||
1254 |
contentsArray from:firstIndex to:lastIndex reverseDo:aBlock |
|
1 | 1255 |
! ! |
1256 |
||
56 | 1257 |
!OrderedCollection methodsFor:'filling & replacing'! |
13 | 1258 |
|
1259 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1170 | 1260 |
"replace elements in the receiver between index start and stop, |
1261 |
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
|
1262 |
Return the receiver. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1263 |
Redefined here; can be done faster as the inherited operation." |
13 | 1264 |
|
1265 |
|end| |
|
1266 |
||
1267 |
end := stop + firstIndex - 1. |
|
1268 |
((start >= 1) and:[end <= lastIndex]) ifTrue:[ |
|
1170 | 1269 |
aCollection class == self class ifTrue:[ |
1270 |
contentsArray |
|
1271 |
replaceFrom:(start + firstIndex - 1) |
|
1272 |
to:end |
|
1273 |
with:aCollection contentsArray |
|
1274 |
startingAt:(repStart + aCollection firstIndex - 1). |
|
1275 |
] ifFalse:[ |
|
1276 |
contentsArray |
|
1277 |
replaceFrom:(start + firstIndex - 1) |
|
1278 |
to:end |
|
1279 |
with:aCollection |
|
1280 |
startingAt:repStart. |
|
1281 |
]. |
|
1282 |
^ self |
|
13 | 1283 |
]. |
1284 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
293 | 1285 |
|
1286 |
" |
|
1287 |
|c| |
|
1288 |
||
1289 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1290 |
c replaceFrom:3 to:6 with:c startingAt:2. |
|
1291 |
c |
|
1292 |
" |
|
1293 |
" |
|
1294 |
|c| |
|
1295 |
||
1296 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1297 |
c replaceFrom:3 to:5 with:c startingAt:4. |
|
1298 |
c |
|
1299 |
" |
|
1170 | 1300 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1301 |
"Modified: / 28.1.1998 / 16:49:31 / cg" |
13 | 1302 |
! ! |
1303 |
||
606 | 1304 |
!OrderedCollection methodsFor:'grow & shrink'! |
77 | 1305 |
|
606 | 1306 |
grow:newSize |
1307 |
"grow the receiver to newSize" |
|
77 | 1308 |
|
606 | 1309 |
|oldSize newContents oldLast newLast| |
77 | 1310 |
|
606 | 1311 |
oldSize := lastIndex - firstIndex + 1. |
1312 |
newSize ~~ oldSize ifTrue:[ |
|
1313 |
newLast := firstIndex + newSize - 1. |
|
1314 |
newSize < oldSize ifTrue:[ |
|
1315 |
oldLast := lastIndex. |
|
1316 |
lastIndex := newLast. |
|
1317 |
" |
|
1318 |
nil out rest, to give GC a chance to reclaim things |
|
1319 |
" |
|
1320 |
contentsArray from:lastIndex + 1 to:oldLast put:nil. |
|
1321 |
] ifFalse:[ |
|
1322 |
newLast <= contentsArray size ifTrue:[ |
|
1323 |
lastIndex := newLast. |
|
1324 |
^ self |
|
1325 |
]. |
|
77 | 1326 |
|
606 | 1327 |
newContents := Array basicNew:newSize. |
1328 |
newContents replaceFrom:1 to:oldSize with:contentsArray startingAt:firstIndex. |
|
1329 |
contentsArray := newContents. |
|
1330 |
firstIndex := 1. |
|
1331 |
lastIndex := newSize |
|
1332 |
] |
|
1333 |
] |
|
1334 |
! ! |
|
77 | 1335 |
|
606 | 1336 |
!OrderedCollection methodsFor:'inspecting'! |
1337 |
||
1338 |
inspectorClass |
|
1339 |
"redefined to launch an OrderedCollectionInspector |
|
1340 |
(instead of the default InspectorView)." |
|
1341 |
||
1342 |
^ OrderedCollectionInspectorView |
|
77 | 1343 |
|
1344 |
" |
|
606 | 1345 |
(OrderedCollection withAll:#(3 2 1)) inspect |
1346 |
(OrderedCollection withAll:#(3 2 1)) removeFirst; yourself; inspect |
|
1347 |
#(0 8 15 3 99 2) asSortedCollection inspect |
|
77 | 1348 |
" |
1349 |
! ! |
|
1350 |
||
1 | 1351 |
!OrderedCollection methodsFor:'private'! |
1352 |
||
606 | 1353 |
initContents:size |
1354 |
"setup the receiver-collection to hold size entries" |
|
33 | 1355 |
|
606 | 1356 |
contentsArray := Array basicNew:size. |
1357 |
firstIndex := 1. |
|
1358 |
lastIndex := 0 |
|
1 | 1359 |
! |
1360 |
||
1361 |
makeRoomAtFront |
|
282 | 1362 |
"grow/shift the contents for more room at the beginning. |
1363 |
Does not change the logical size. |
|
1364 |
i.e. |
|
1365 |
#(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6)" |
|
1 | 1366 |
|
1367 |
|newContents |
|
33 | 1368 |
oldSize "{ Class:SmallInteger }" |
1369 |
newSize "{ Class:SmallInteger }" |
|
1370 |
startIndex "{ Class:SmallInteger }" |
|
1371 |
sz "{ Class:SmallInteger }"| |
|
1 | 1372 |
|
1373 |
oldSize := contentsArray size. |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1374 |
sz := lastIndex - firstIndex + 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1375 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1376 |
((oldSize == 0) or:[sz == 0]) ifTrue:[ |
253 | 1377 |
contentsArray := Array basicNew:3. |
159 | 1378 |
firstIndex := 2. lastIndex := 1. |
1379 |
^ self |
|
93 | 1380 |
]. |
1381 |
||
329 | 1382 |
" |
1383 |
if there is lots of room at the end (> 50%), |
|
1384 |
shift instead of growing. This helps collections |
|
1385 |
which get elements removed at the end and added at front. |
|
1386 |
" |
|
33 | 1387 |
oldSize > (sz * 2) ifTrue:[ |
508 | 1388 |
startIndex := oldSize // 4. |
1389 |
startIndex > 1 ifTrue:[ |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1390 |
contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1391 |
replaceFrom:startIndex |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1392 |
to:startIndex + sz - 1 |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1393 |
with:contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1394 |
startingAt:1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1395 |
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
|
1396 |
firstIndex := startIndex. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1397 |
lastIndex := startIndex + sz - 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1398 |
^ self |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1399 |
] |
33 | 1400 |
]. |
2 | 1401 |
newSize := oldSize * 2. |
329 | 1402 |
newSize == 0 ifTrue:[ newSize := 3]. |
253 | 1403 |
newContents := Array basicNew:newSize. |
77 | 1404 |
newContents |
159 | 1405 |
replaceFrom:(oldSize + 1) |
1406 |
to:newSize |
|
1407 |
with:contentsArray |
|
1408 |
startingAt:1. |
|
1 | 1409 |
contentsArray := newContents. |
1410 |
firstIndex := firstIndex + oldSize. |
|
1411 |
lastIndex := lastIndex + oldSize |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1412 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1413 |
"Created: 8.11.1995 / 12:47:49 / cg" |
1 | 1414 |
! |
1415 |
||
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1416 |
makeRoomAtIndex:whereToMakeEmptySlot |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1417 |
"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
|
1418 |
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
|
1419 |
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
|
1420 |
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
|
1421 |
Since this changes the logical size, the modified index is returned. |
1 | 1422 |
i.e. |
1423 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6) |
|
1424 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6) |
|
1425 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)" |
|
1426 |
||
1427 |
|newContents |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1428 |
newSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1429 |
oldSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1430 |
oneFourthOfSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1431 |
first "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1432 |
last "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1433 |
index "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1434 |
shiftLeft shiftRight| |
1 | 1435 |
|
1436 |
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
|
1437 |
oneFourthOfSize := oldSize // 4. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1438 |
index := whereToMakeEmptySlot. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1439 |
first := firstIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1440 |
last := lastIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1441 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1442 |
((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
|
1443 |
"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
|
1444 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1445 |
shiftLeft := true. |
33 | 1446 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1447 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1448 |
"/ 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
|
1449 |
"/ 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
|
1450 |
"/ 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
|
1451 |
"/ 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
|
1452 |
"/ 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
|
1453 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1454 |
(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
|
1455 |
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
|
1456 |
shiftLeft := false. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1457 |
shiftRight := true. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1458 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1459 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1460 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1461 |
] ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1462 |
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
|
1463 |
shiftRight := true |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1464 |
] |
33 | 1465 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1466 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1467 |
shiftLeft == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1468 |
"there is room (>25%) at the beginning" |
33 | 1469 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1470 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1471 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1472 |
replaceFrom:(first - 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1473 |
to:(index - 2) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1474 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1475 |
startingAt:first. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1476 |
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
|
1477 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1478 |
firstIndex := first - 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1479 |
^ index - 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1480 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1481 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1482 |
shiftRight == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1483 |
"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
|
1484 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1485 |
last := last + 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1486 |
index == last ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1487 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1488 |
replaceFrom:(index + 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1489 |
to:last |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1490 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1491 |
startingAt:index. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1492 |
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
|
1493 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1494 |
lastIndex := last. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1495 |
^ index |
33 | 1496 |
]. |
1497 |
||
1498 |
newSize := oldSize * 2. |
|
253 | 1499 |
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
|
1500 |
index ~~ first ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1501 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1502 |
replaceFrom:1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1503 |
to:index - first |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1504 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1505 |
startingAt:first. |
1 | 1506 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1507 |
index <= last ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1508 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1509 |
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
|
1510 |
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
|
1511 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1512 |
startingAt:index. |
1 | 1513 |
]. |
1514 |
contentsArray := newContents. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1515 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1516 |
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
|
1517 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1518 |
"/ 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
|
1519 |
^ index - (first - firstIndex). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1520 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1521 |
"Modified: 12.4.1996 / 17:18:58 / cg" |
1 | 1522 |
! |
1523 |
||
2222 | 1524 |
makeRoomAtIndex:whereToMakeEmptySlots for:howMany |
1525 |
"grow the contents for inserting at whereToMakeEmptySlot. |
|
1526 |
The whereToMakeEmptySlot argument must be a physical index within the contentsArray. |
|
1527 |
If there is (plenty of) room at either end, elements are shifted inplace to create |
|
1528 |
an empty slot; otherwise, a new contentsArray is allocated. |
|
1529 |
Since this changes the logical size, the modified index is returned. |
|
1530 |
i.e. |
|
1531 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 for:2 -> #(1 2 nil nil 3 4 5 6) |
|
1532 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 for:2 -> #(nil nil 1 2 3 4 5 6) |
|
1533 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 for:2 -> #(1 2 3 4 5 6 nil nil)" |
|
1534 |
||
1535 |
|newContents |
|
1536 |
newSize "{ Class:SmallInteger }" |
|
1537 |
oldSize "{ Class:SmallInteger }" |
|
1538 |
oneFourthOfSize "{ Class:SmallInteger }" |
|
1539 |
first "{ Class:SmallInteger }" |
|
1540 |
last "{ Class:SmallInteger }" |
|
1541 |
index "{ Class:SmallInteger }" |
|
1542 |
shiftLeft shiftRight| |
|
1543 |
||
1544 |
oldSize := contentsArray size. |
|
1545 |
oneFourthOfSize := oldSize // 4. |
|
1546 |
index := whereToMakeEmptySlots. |
|
1547 |
first := firstIndex. |
|
1548 |
last := lastIndex. |
|
1549 |
||
2549 | 1550 |
shiftLeft := shiftRight := false. |
2222 | 1551 |
((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[ |
1552 |
"there is room (>25%) at the beginning" |
|
1553 |
||
1554 |
shiftLeft := true. |
|
1555 |
] ifFalse:[ |
|
2549 | 1556 |
((last + howMany) <= oldSize |
1557 |
and:[last < (oneFourthOfSize * 3)]) ifTrue:[ |
|
2222 | 1558 |
shiftRight := true |
1559 |
] |
|
1560 |
]. |
|
1561 |
||
1562 |
shiftLeft == true ifTrue:[ |
|
2549 | 1563 |
"there is room at the beginning" |
2222 | 1564 |
|
1565 |
index == first ifFalse:[ |
|
1566 |
contentsArray |
|
1567 |
replaceFrom:(first - howMany) |
|
1568 |
to:(index - howMany - 1) |
|
1569 |
with:contentsArray |
|
1570 |
startingAt:first. |
|
1571 |
contentsArray from:index-howMany to:index-1 put:nil. |
|
1572 |
]. |
|
1573 |
firstIndex := first - howMany. |
|
1574 |
^ index - howMany |
|
1575 |
]. |
|
1576 |
||
1577 |
shiftRight == true ifTrue:[ |
|
2549 | 1578 |
"there is room at the end" |
2222 | 1579 |
|
1580 |
last := last + howMany. |
|
1581 |
index == last ifFalse:[ |
|
1582 |
contentsArray |
|
1583 |
replaceFrom:(index + howMany) |
|
1584 |
to:last |
|
1585 |
with:contentsArray |
|
1586 |
startingAt:index. |
|
1587 |
contentsArray from:index to:index+howMany-1 put:nil |
|
1588 |
]. |
|
1589 |
lastIndex := last. |
|
1590 |
^ index |
|
1591 |
]. |
|
1592 |
||
1593 |
newSize := oldSize * 2. |
|
1594 |
[newSize < (oldSize+howMany)] whileTrue:[ |
|
1595 |
newSize := newSize * 2 |
|
1596 |
]. |
|
1597 |
||
1598 |
newContents := Array basicNew:newSize. |
|
1599 |
index > first ifTrue:[ |
|
1600 |
newContents |
|
1601 |
replaceFrom:1 |
|
1602 |
to:index - first |
|
1603 |
with:contentsArray |
|
1604 |
startingAt:first. |
|
1605 |
]. |
|
1606 |
index <= last ifTrue:[ |
|
1607 |
newContents |
|
1608 |
replaceFrom:index - first + howMany + 1 |
|
1609 |
to:(last - first + howMany + 1) |
|
1610 |
with:contentsArray |
|
1611 |
startingAt:(index). |
|
1612 |
]. |
|
1613 |
contentsArray := newContents. |
|
1614 |
firstIndex := 1. |
|
1615 |
lastIndex := last - first + howMany + 1. |
|
1616 |
||
1617 |
"/ return the modified index |
|
1618 |
^ index - (first - firstIndex). |
|
1619 |
||
2549 | 1620 |
"Modified: 15.4.1997 / 12:34:16 / cg" |
2222 | 1621 |
! |
1622 |
||
606 | 1623 |
makeRoomAtLast |
1624 |
"grow/shift the contents for more room at the end. |
|
1625 |
Does not change the logical size. |
|
1626 |
i.e. |
|
1627 |
#(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)" |
|
1628 |
||
1629 |
|newContents |
|
1630 |
oldSize "{ Class:SmallInteger }" |
|
1631 |
newSize "{ Class:SmallInteger }" |
|
1632 |
startIndex "{ Class:SmallInteger }" |
|
1633 |
sz "{ Class:SmallInteger }"| |
|
1634 |
||
1635 |
oldSize := contentsArray size. |
|
1636 |
sz := lastIndex - firstIndex + 1. |
|
1637 |
||
1638 |
" |
|
1639 |
if there is lots of room at the beginning (> 50%), |
|
1640 |
shift instead of growing. This helps collections which get |
|
1641 |
elements removed at front and added at the end. |
|
1642 |
" |
|
1643 |
oldSize > (sz * 2) ifTrue:[ |
|
1644 |
startIndex := firstIndex // 4. |
|
1645 |
startIndex == 0 ifTrue:[ |
|
1646 |
startIndex := 1 |
|
1647 |
]. |
|
1648 |
contentsArray |
|
1649 |
replaceFrom:startIndex |
|
1650 |
to:startIndex + sz - 1 |
|
1651 |
with:contentsArray |
|
1652 |
startingAt:firstIndex. |
|
1653 |
contentsArray from:startIndex + sz to:lastIndex put:nil. |
|
1654 |
firstIndex := startIndex. |
|
1655 |
lastIndex := startIndex + sz - 1. |
|
1656 |
^ self |
|
1657 |
]. |
|
1658 |
newSize := oldSize * 2. |
|
1659 |
newSize == 0 ifTrue:[newSize := 3]. |
|
1660 |
newContents := Array basicNew:newSize. |
|
1661 |
newContents |
|
1662 |
replaceFrom:1 |
|
1663 |
to:oldSize |
|
1664 |
with:contentsArray |
|
1665 |
startingAt:1. |
|
1666 |
contentsArray := newContents |
|
1667 |
! |
|
1668 |
||
1669 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex |
|
1670 |
"set first and last index" |
|
1671 |
||
1672 |
firstIndex := newFirstIndex. |
|
1673 |
lastIndex := newLastIndex. |
|
6088
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
1674 |
! |
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
1675 |
|
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
1676 |
setIndices |
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
1677 |
"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
|
1678 |
|
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
1679 |
firstIndex := 1. |
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
1680 |
lastIndex := 0. |
606 | 1681 |
! ! |
1682 |
||
1683 |
!OrderedCollection methodsFor:'private accessing'! |
|
1684 |
||
1685 |
contentsArray |
|
4153 | 1686 |
"return the orderedCollections underlying contentsArray. |
1687 |
The actual elements are found here starting at firstIndex, |
|
1688 |
and ending at lastIndex." |
|
1689 |
||
606 | 1690 |
^ contentsArray |
1691 |
! |
|
1692 |
||
1693 |
firstIndex |
|
4153 | 1694 |
"return the index of my first element in my underlying contentsArray. |
1695 |
The actual elements are found starting this index, |
|
1696 |
and ending at lastIndex." |
|
1697 |
||
606 | 1698 |
^ firstIndex |
1699 |
! ! |
|
1700 |
||
1701 |
!OrderedCollection methodsFor:'queries'! |
|
1702 |
||
1703 |
capacity |
|
1704 |
"return the number of elements, that the receiver is |
|
1705 |
prepared to take. |
|
1706 |
Not used by the system; added for ST-80 compatibility." |
|
1707 |
||
1708 |
^ contentsArray size |
|
1709 |
! |
|
1710 |
||
1711 |
size |
|
1712 |
"return the number of elements in the collection" |
|
1713 |
||
1714 |
^ lastIndex - firstIndex + 1 |
|
1715 |
! ! |
|
1716 |
||
3095 | 1717 |
!OrderedCollection methodsFor:'searching'! |
1 | 1718 |
|
1719 |
identityIndexOf:anObject |
|
33 | 1720 |
"return the index of anObject or 0 if not found. Compare using ==" |
1 | 1721 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1722 |
|index| |
1 | 1723 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1724 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1725 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1726 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1727 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1728 |
index == 0 ifTrue:[^ 0]. |
1 | 1729 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1730 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1731 |
"Modified: 12.4.1996 / 17:58:25 / cg" |
1 | 1732 |
! |
1733 |
||
1734 |
identityIndexOf:anObject startingAt:startIndex |
|
33 | 1735 |
"return the index of anObject, starting search at startIndex. |
1736 |
Compare using ==; return 0 if not found in the collection" |
|
1 | 1737 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1738 |
|index| |
1 | 1739 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1740 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1741 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1742 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1743 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1744 |
index == 0 ifTrue:[^ 0]. |
1 | 1745 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1746 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1747 |
"Modified: 12.4.1996 / 17:58:19 / cg" |
1 | 1748 |
! |
1749 |
||
1750 |
indexOf:anObject |
|
33 | 1751 |
"return the index of anObject or 0 if not found in the collection. |
1752 |
Compare using =" |
|
1 | 1753 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1754 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1755 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1756 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1757 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1758 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1759 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1760 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1761 |
^ index - firstIndex + 1 |
1 | 1762 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1763 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1764 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1765 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1766 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1767 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1768 |
c indexOf:99 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1769 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1770 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1771 |
"Modified: 12.4.1996 / 17:57:54 / cg" |
1 | 1772 |
! |
1773 |
||
1774 |
indexOf:anObject startingAt:startIndex |
|
33 | 1775 |
"return the index of anObject, starting search at startIndex. |
1776 |
Compare using =; return 0 if not found in the collection" |
|
1 | 1777 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1778 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1779 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1780 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1781 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1782 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1783 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1784 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1785 |
^ index - firstIndex + 1 |
1 | 1786 |
|
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 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1789 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1790 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1791 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1792 |
c indexOf:4 startingAt:5 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1793 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1794 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1795 |
"Modified: 12.4.1996 / 17:58:53 / cg" |
1 | 1796 |
! ! |
1797 |
||
3095 | 1798 |
!OrderedCollection methodsFor:'testing'! |
1799 |
||
1800 |
includes:anObject |
|
1801 |
"return true if anObject is in the collection. Compare using =" |
|
1802 |
||
1803 |
^ (contentsArray |
|
1804 |
indexOf:anObject |
|
1805 |
startingAt:firstIndex |
|
1806 |
endingAt:lastIndex) ~~ 0 |
|
1807 |
||
1808 |
"Modified: 12.4.1996 / 17:57:27 / cg" |
|
1809 |
! |
|
1810 |
||
1811 |
includesIdentical:anObject |
|
1812 |
"return true if anObject is in the collection. Compare using ==" |
|
1813 |
||
1814 |
^ (contentsArray |
|
1815 |
identityIndexOf:anObject |
|
1816 |
startingAt:firstIndex |
|
1817 |
endingAt:lastIndex) ~~ 0 |
|
1818 |
||
1819 |
"Modified: 12.4.1996 / 17:57:09 / cg" |
|
5401 | 1820 |
! |
1821 |
||
5556 | 1822 |
isEmpty |
1823 |
"return true, if the receiver has no elements" |
|
1824 |
||
1825 |
^ lastIndex < firstIndex |
|
1826 |
! |
|
1827 |
||
1828 |
isFixedSize |
|
1829 |
"return true if the receiver cannot grow - this will vanish once |
|
1830 |
Arrays and Strings learn how to grow ..." |
|
1831 |
||
1832 |
^ false |
|
1833 |
! |
|
1834 |
||
5401 | 1835 |
isOrderedCollection |
1836 |
"return true, if the receiver is some kind of ordered collection (or list etc); |
|
1837 |
true is returned here - the method is only redefined in Object." |
|
1838 |
||
1839 |
^ true |
|
1840 |
||
1841 |
||
5556 | 1842 |
! |
1843 |
||
1844 |
notEmpty |
|
1845 |
"return true, if the receiver has any elements" |
|
1846 |
||
1847 |
^ lastIndex >= firstIndex |
|
3095 | 1848 |
! ! |
1849 |
||
629 | 1850 |
!OrderedCollection class methodsFor:'documentation'! |
1851 |
||
1852 |
version |
|
6755
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1853 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.81 2002-09-14 11:37:53 cg Exp $' |
629 | 1854 |
! ! |