author | tz |
Fri, 07 Aug 1998 16:44:44 +0200 | |
changeset 673 | 530ca09ba8d1 |
parent 646 | 613ef9177393 |
child 737 | c76130b05afb |
permissions | -rw-r--r-- |
480 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
||
14 |
OrderedCollection subclass:#List |
|
15 |
instanceVariableNames:'dependents' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Sequenceable' |
|
19 |
! |
|
20 |
||
21 |
!List class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
Lists are mostly OrderedCollection, but keep their dependents |
|
41 |
locally (which is adding a bit of performance - not functionality). |
|
42 |
||
43 |
In ST/X, most functionality is already provided by OrderedCollection, |
|
44 |
so there is not much new stuff found here. |
|
45 |
It has been mostly provided, for ST-80 compatibility, |
|
46 |
where it adds sorting capabilities. |
|
47 |
||
48 |
[see also:] |
|
49 |
Array OrderedCollection |
|
50 |
||
51 |
[author:] |
|
52 |
Claus Gittinger |
|
53 |
" |
|
54 |
||
55 |
! ! |
|
56 |
||
57 |
!List methodsFor:'accessing'! |
|
58 |
||
577 | 59 |
at:anIndex put:anObject |
60 |
"set the element at index, to be anIndex. |
|
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
61 |
Return anObject (sigh). |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
62 |
In contrast to OrderedCollection, Lists allow putting an object |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
63 |
right after the last element and auto-grow in this case; |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
64 |
however, putting 2 or more indices after the last element is |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
65 |
reported as an error." |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
66 |
|
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
67 |
|idx "{ Class: SmallInteger }"| |
577 | 68 |
|
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
69 |
idx := anIndex + firstIndex - 1. |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
70 |
((anIndex < 1) or:[idx > lastIndex]) ifTrue:[ |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
71 |
idx == (lastIndex+1) ifTrue:[ |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
72 |
self makeRoomAtLast. |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
73 |
lastIndex := lastIndex + 1. |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
74 |
] ifFalse:[ |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
75 |
^ self subscriptBoundsError:anIndex |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
76 |
] |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
77 |
]. |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
78 |
|
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
79 |
contentsArray basicAt:idx put:anObject. |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
80 |
dependents size ~~ 0 ifTrue:[self changed:#at: with:anIndex]. |
577 | 81 |
^ anObject |
82 |
||
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
83 |
"Modified: / 28.1.1998 / 16:44:49 / cg" |
577 | 84 |
! |
85 |
||
86 |
list |
|
87 |
^ self |
|
88 |
! ! |
|
89 |
||
90 |
!List methodsFor:'adding & removing'! |
|
91 |
||
92 |
add:anObject |
|
93 |
"add the argument, anObject to the end of the collection |
|
94 |
Return the argument, anObject." |
|
95 |
||
96 |
super add:anObject. |
|
611 | 97 |
dependents notNil ifTrue:[self changed:#insert: with:(self size)]. |
577 | 98 |
^ anObject |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
99 |
|
611 | 100 |
"Modified: / 29.1.1998 / 10:52:32 / cg" |
577 | 101 |
! |
102 |
||
103 |
add:anObject beforeIndex:index |
|
610 | 104 |
"add the argument, anObject to the end of the collection |
105 |
Return the argument, anObject." |
|
106 |
||
107 |
|idx physicalIndex| |
|
577 | 108 |
|
610 | 109 |
physicalIndex := index + firstIndex - 1. |
110 |
physicalIndex > lastIndex ifTrue:[ |
|
111 |
physicalIndex == (lastIndex+1) ifTrue:[ |
|
112 |
self addLast:anObject. |
|
113 |
^ self |
|
114 |
]. |
|
115 |
^ self subscriptBoundsError:index |
|
116 |
]. |
|
577 | 117 |
|
610 | 118 |
idx := self makeRoomAtIndex:physicalIndex. |
119 |
"/ notice: the above may change firstIndex |
|
120 |
contentsArray basicAt:idx put:anObject. |
|
121 |
||
613 | 122 |
dependents notNil ifTrue:[self changed:#insert: with:index]. |
610 | 123 |
^ anObject |
124 |
||
613 | 125 |
"Modified: / 2.2.1998 / 19:06:21 / cg" |
577 | 126 |
! |
127 |
||
128 |
addAll:aCollection beforeIndex:index |
|
129 |
"insert all elements of the argument |
|
130 |
Return the receiver." |
|
131 |
||
610 | 132 |
|count idx| |
577 | 133 |
|
610 | 134 |
count := aCollection size. |
135 |
idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count. |
|
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
136 |
|
577 | 137 |
aCollection isSequenceable ifTrue:[ |
610 | 138 |
"/ we are lucky - that thing can count & do bulk copies |
139 |
"/ notice: the above may change firstIndex |
|
140 |
contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1. |
|
141 |
] ifFalse:[ |
|
142 |
"/ avoid zillions of change messages ... |
|
143 |
aCollection do:[:element | |
|
144 |
contentsArray at:idx put:element. |
|
145 |
idx := idx + 1. |
|
146 |
]. |
|
147 |
]. |
|
611 | 148 |
dependents notNil ifTrue:[ |
149 |
self changed: #insertCollection: with:(Array with:index with:(aCollection size)) |
|
150 |
] |
|
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
151 |
|
611 | 152 |
"Modified: / 29.1.1998 / 10:52:57 / cg" |
577 | 153 |
! |
154 |
||
155 |
addFirst:anObject |
|
156 |
"add the argument, anObject to the beginning of the collection. |
|
157 |
Return the argument, anObject." |
|
158 |
||
159 |
super addFirst:anObject. |
|
611 | 160 |
dependents notNil ifTrue:[self changed:#insert: with:1]. |
577 | 161 |
^ anObject |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
162 |
|
611 | 163 |
"Modified: / 29.1.1998 / 10:53:09 / cg" |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
164 |
! |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
165 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
166 |
addLast:anObject |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
167 |
"add the argument, anObject to the end of the collection. |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
168 |
Return the argument, anObject." |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
169 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
170 |
self add:anObject. |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
171 |
^ anObject |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
172 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
173 |
"Created: / 26.1.1998 / 11:10:55 / cg" |
577 | 174 |
! |
175 |
||
632 | 176 |
clearContents |
177 |
"remove all elements from the collection but keep the contentsArray. |
|
178 |
Useful for huge lists, if the contents will be rebuild soon (using #add:) |
|
179 |
to a size which is similar to the lists current size. |
|
180 |
Returns the receiver." |
|
181 |
||
182 |
|prevSize| |
|
183 |
||
184 |
prevSize := self size. |
|
185 |
super clearContents. |
|
186 |
||
187 |
prevSize ~~ 0 ifTrue:[ |
|
188 |
dependents notNil ifTrue:[ |
|
189 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
190 |
] |
|
191 |
] |
|
192 |
||
193 |
! |
|
194 |
||
577 | 195 |
removeAll |
196 |
"remove all elements from the collection. |
|
197 |
Returns the receiver." |
|
198 |
||
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
199 |
|prevSize| |
577 | 200 |
|
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
201 |
prevSize := self size. |
577 | 202 |
super removeAll. |
203 |
||
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
204 |
prevSize ~~ 0 ifTrue:[ |
611 | 205 |
dependents notNil ifTrue:[ |
206 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
207 |
] |
|
577 | 208 |
] |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
209 |
|
611 | 210 |
"Modified: / 29.1.1998 / 10:53:28 / cg" |
577 | 211 |
! |
212 |
||
213 |
removeFirst |
|
214 |
"remove the first element from the collection; return the element." |
|
215 |
||
216 |
|deletedObject| |
|
217 |
||
218 |
deletedObject := super removeFirst. |
|
611 | 219 |
dependents notNil ifTrue:[self changed:#remove: with:1]. |
577 | 220 |
^ deletedObject |
221 |
||
611 | 222 |
"Modified: / 29.1.1998 / 10:53:36 / cg" |
577 | 223 |
! |
224 |
||
225 |
removeFirst:n |
|
226 |
"remove the first n elements from the collection; |
|
227 |
Return a collection containing the removed elements." |
|
228 |
||
229 |
|deletedObjects| |
|
230 |
||
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
231 |
deletedObjects := super removeFirst:n. |
611 | 232 |
dependents notNil ifTrue:[self changed:#removeFrom: with:(Array with:1 with:n)]. |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
233 |
^ deletedObjects |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
234 |
|
611 | 235 |
"Modified: / 29.1.1998 / 10:53:40 / cg" |
577 | 236 |
! |
237 |
||
238 |
removeFromIndex:startIndex toIndex:stopIndex |
|
239 |
"remove the elements stored under startIndex up to and including |
|
240 |
the elements under stopIndex" |
|
241 |
||
611 | 242 |
|deletedObjects| |
577 | 243 |
|
611 | 244 |
deletedObjects := super removeFromIndex:startIndex toIndex:stopIndex. |
245 |
dependents notNil ifTrue:[ |
|
246 |
self changed:#removeFrom: with:(Array with:startIndex with:stopIndex). |
|
247 |
]. |
|
248 |
^ deletedObjects |
|
577 | 249 |
|
611 | 250 |
"Modified: / 29.1.1998 / 10:54:03 / cg" |
577 | 251 |
! |
252 |
||
253 |
removeLast |
|
254 |
"remove the last element from the collection; return the element" |
|
255 |
||
256 |
|deletedObject| |
|
257 |
||
258 |
deletedObject := super removeLast. |
|
611 | 259 |
dependents notNil ifTrue:[self changed:#remove: with:(1 + self size)]. |
577 | 260 |
^ deletedObject |
261 |
||
611 | 262 |
"Modified: / 29.1.1998 / 10:54:15 / cg" |
577 | 263 |
! |
264 |
||
265 |
removeLast:n |
|
266 |
"remove the last n elements from the collection; |
|
267 |
Return a collection of removed elements." |
|
268 |
||
269 |
|deletedObjects stop| |
|
270 |
||
271 |
stop := self size. |
|
272 |
deletedObjects := super removeLast:n. |
|
611 | 273 |
dependents notNil ifTrue:[ |
274 |
self changed:#removeFrom: with:(Array with:(stop - n + 1) with:stop). |
|
275 |
]. |
|
577 | 276 |
^ deletedObjects |
277 |
||
611 | 278 |
"Modified: / 29.1.1998 / 10:54:25 / cg" |
577 | 279 |
! ! |
280 |
||
281 |
!List methodsFor:'converting'! |
|
282 |
||
283 |
asList |
|
284 |
^ self |
|
285 |
||
286 |
"Created: 14.2.1997 / 16:25:55 / cg" |
|
287 |
! ! |
|
288 |
||
289 |
!List methodsFor:'dependents access'! |
|
290 |
||
480 | 291 |
dependents |
292 |
"return the dependents of the receiver" |
|
293 |
||
584
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
294 |
^ dependents ? #() |
480 | 295 |
|
584
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
296 |
"Created: / 14.2.1997 / 16:05:49 / cg" |
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
297 |
"Modified: / 27.10.1997 / 19:37:33 / cg" |
480 | 298 |
! |
299 |
||
300 |
dependents:aCollection |
|
301 |
"set the dependents of the receiver" |
|
302 |
||
611 | 303 |
aCollection size == 0 ifTrue:[ |
304 |
dependents := nil |
|
305 |
] ifFalse:[ |
|
306 |
dependents := aCollection. |
|
307 |
]. |
|
480 | 308 |
|
611 | 309 |
"Created: / 14.2.1997 / 16:05:58 / cg" |
310 |
"Modified: / 29.1.1998 / 10:54:52 / cg" |
|
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
311 |
! |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
312 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
313 |
nonWeakDependents |
612
cc2d6bd50cdc
#nonWeakDependents always returns a collection.
Claus Gittinger <cg@exept.de>
parents:
611
diff
changeset
|
314 |
"return a Collection of dependents - empty if there is none. |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
315 |
Since all dependencies are nonWeak in List, this is a dummy." |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
316 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
317 |
^ self dependents |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
318 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
319 |
"Created: / 19.4.1996 / 10:29:43 / cg" |
612
cc2d6bd50cdc
#nonWeakDependents always returns a collection.
Claus Gittinger <cg@exept.de>
parents:
611
diff
changeset
|
320 |
"Modified: / 30.1.1998 / 14:06:12 / cg" |
673 | 321 |
! |
322 |
||
323 |
nonWeakDependents:newDeps |
|
324 |
"return a Collection of dependents - empty if there is none. |
|
325 |
Since all dependencies are nonWeak in List, this is a dummy." |
|
326 |
||
327 |
^ self dependents:newDeps |
|
328 |
||
329 |
"Created: / 19.4.1996 / 10:29:43 / cg" |
|
330 |
"Modified: / 30.1.1998 / 14:06:12 / cg" |
|
480 | 331 |
! ! |
332 |
||
577 | 333 |
!List methodsFor:'filling & replacing'! |
334 |
||
335 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
336 |
"replace elements in the receiver between index start and stop, |
|
337 |
with elements taken from replacementCollection starting at repStart. |
|
338 |
Redefined - can be done faster" |
|
481 | 339 |
|
646 | 340 |
super replaceFrom:start to:stop with:aCollection startingAt:repStart. |
481 | 341 |
|
577 | 342 |
dependents notNil ifTrue:[ |
343 |
self changed:#replace: with:(Array with:start with:stop) |
|
344 |
]. |
|
345 |
||
646 | 346 |
"Modified: / 20.5.1998 / 15:20:17 / cg" |
481 | 347 |
! ! |
348 |
||
480 | 349 |
!List class methodsFor:'documentation'! |
350 |
||
351 |
version |
|
673 | 352 |
^ '$Header: /cvs/stx/stx/libbasic2/List.st,v 1.13 1998-08-07 14:44:44 tz Exp $' |
480 | 353 |
! ! |