author | Claus Gittinger <cg@exept.de> |
Tue, 11 Feb 2003 00:26:26 +0100 | |
changeset 1142 | 8b91f129aea7 |
parent 1095 | adf956ca6cda |
child 1567 | f4cc74d17aa7 |
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 |
||
908 | 14 |
"{ Package: 'stx:libbasic2' }" |
15 |
||
480 | 16 |
OrderedCollection subclass:#List |
17 |
instanceVariableNames:'dependents' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Sequenceable' |
|
21 |
! |
|
22 |
||
23 |
!List class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
737 | 42 |
Lists are mostly like OrderedCollections, but keep their dependents |
480 | 43 |
locally (which is adding a bit of performance - not functionality). |
737 | 44 |
In addition, special change notifications are emitted, whenever |
45 |
a lists contents is changed. |
|
46 |
Some views (SelectionIn*View and DataSetView) react specially on |
|
47 |
on those messages and perform optimized updates. |
|
48 |
(the change messages pass the range-of-change as parameter). |
|
480 | 49 |
|
50 |
In ST/X, most functionality is already provided by OrderedCollection, |
|
51 |
so there is not much new stuff found here. |
|
52 |
It has been mostly provided, for ST-80 compatibility, |
|
53 |
where it adds sorting capabilities. |
|
54 |
||
55 |
[see also:] |
|
56 |
Array OrderedCollection |
|
57 |
||
58 |
[author:] |
|
59 |
Claus Gittinger |
|
60 |
" |
|
61 |
||
62 |
! ! |
|
63 |
||
64 |
!List methodsFor:'accessing'! |
|
65 |
||
577 | 66 |
at:anIndex put:anObject |
67 |
"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
|
68 |
Return anObject (sigh). |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
|
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
74 |
|idx "{ Class: SmallInteger }"| |
577 | 75 |
|
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
76 |
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
|
77 |
((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
|
78 |
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
|
79 |
self makeRoomAtLast. |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
80 |
lastIndex := lastIndex + 1. |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
81 |
] ifFalse:[ |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
82 |
^ self subscriptBoundsError:anIndex |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
83 |
] |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
84 |
]. |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
85 |
|
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
86 |
contentsArray basicAt:idx put:anObject. |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
87 |
dependents size ~~ 0 ifTrue:[self changed:#at: with:anIndex]. |
577 | 88 |
^ anObject |
89 |
||
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
90 |
"Modified: / 28.1.1998 / 16:44:49 / cg" |
577 | 91 |
! |
92 |
||
93 |
list |
|
94 |
^ self |
|
95 |
! ! |
|
96 |
||
97 |
!List methodsFor:'adding & removing'! |
|
98 |
||
99 |
add:anObject |
|
100 |
"add the argument, anObject to the end of the collection |
|
101 |
Return the argument, anObject." |
|
102 |
||
103 |
super add:anObject. |
|
611 | 104 |
dependents notNil ifTrue:[self changed:#insert: with:(self size)]. |
577 | 105 |
^ anObject |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
106 |
|
611 | 107 |
"Modified: / 29.1.1998 / 10:52:32 / cg" |
577 | 108 |
! |
109 |
||
110 |
add:anObject beforeIndex:index |
|
610 | 111 |
"add the argument, anObject to the end of the collection |
112 |
Return the argument, anObject." |
|
113 |
||
114 |
|idx physicalIndex| |
|
577 | 115 |
|
610 | 116 |
physicalIndex := index + firstIndex - 1. |
117 |
physicalIndex > lastIndex ifTrue:[ |
|
118 |
physicalIndex == (lastIndex+1) ifTrue:[ |
|
119 |
self addLast:anObject. |
|
120 |
^ self |
|
121 |
]. |
|
122 |
^ self subscriptBoundsError:index |
|
123 |
]. |
|
577 | 124 |
|
610 | 125 |
idx := self makeRoomAtIndex:physicalIndex. |
126 |
"/ notice: the above may change firstIndex |
|
127 |
contentsArray basicAt:idx put:anObject. |
|
128 |
||
613 | 129 |
dependents notNil ifTrue:[self changed:#insert: with:index]. |
610 | 130 |
^ anObject |
131 |
||
613 | 132 |
"Modified: / 2.2.1998 / 19:06:21 / cg" |
577 | 133 |
! |
134 |
||
135 |
addAll:aCollection beforeIndex:index |
|
136 |
"insert all elements of the argument |
|
137 |
Return the receiver." |
|
138 |
||
610 | 139 |
|count idx| |
577 | 140 |
|
610 | 141 |
count := aCollection size. |
142 |
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
|
143 |
|
577 | 144 |
aCollection isSequenceable ifTrue:[ |
610 | 145 |
"/ we are lucky - that thing can count & do bulk copies |
146 |
"/ notice: the above may change firstIndex |
|
147 |
contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1. |
|
148 |
] ifFalse:[ |
|
149 |
"/ avoid zillions of change messages ... |
|
150 |
aCollection do:[:element | |
|
151 |
contentsArray at:idx put:element. |
|
152 |
idx := idx + 1. |
|
153 |
]. |
|
154 |
]. |
|
611 | 155 |
dependents notNil ifTrue:[ |
156 |
self changed: #insertCollection: with:(Array with:index with:(aCollection size)) |
|
157 |
] |
|
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
158 |
|
611 | 159 |
"Modified: / 29.1.1998 / 10:52:57 / cg" |
577 | 160 |
! |
161 |
||
909
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
162 |
addAll:aCollection from:startIndex to:endIndex beforeIndex:index |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
163 |
"insert elements start to stop from the argument |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
164 |
Return the receiver." |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
165 |
|
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
166 |
|count idx| |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
167 |
|
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
168 |
count := endIndex - startIndex + 1. |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
169 |
idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count. |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
170 |
|
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
171 |
aCollection isSequenceable ifTrue:[ |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
172 |
"/ we are lucky - that thing can count & do bulk copies |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
173 |
"/ notice: the above may change firstIndex |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
174 |
contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:startIndex. |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
175 |
] ifFalse:[ |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
176 |
self error:'collection must be sequenceable' |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
177 |
]. |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
178 |
dependents notNil ifTrue:[ |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
179 |
self changed: #insertCollection: with:(Array with:index with:count) |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
180 |
] |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
181 |
|
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
182 |
"Modified: / 29.1.1998 / 10:52:57 / cg" |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
183 |
! |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
184 |
|
1142
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
185 |
addAllLast:aCollection |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
186 |
"add all elements of the argument, aCollection to the end of the collection. |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
187 |
Return the argument, aCollection." |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
188 |
|
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
189 |
self addAll:aCollection beforeIndex:self size + 1. |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
190 |
^ aCollection |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
191 |
! |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
192 |
|
577 | 193 |
addFirst:anObject |
194 |
"add the argument, anObject to the beginning of the collection. |
|
195 |
Return the argument, anObject." |
|
196 |
||
197 |
super addFirst:anObject. |
|
611 | 198 |
dependents notNil ifTrue:[self changed:#insert: with:1]. |
577 | 199 |
^ anObject |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
200 |
|
611 | 201 |
"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
|
202 |
! |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
203 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
204 |
addLast:anObject |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
205 |
"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
|
206 |
Return the argument, anObject." |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
207 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
208 |
self add:anObject. |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
209 |
^ anObject |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
210 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
211 |
"Created: / 26.1.1998 / 11:10:55 / cg" |
577 | 212 |
! |
213 |
||
632 | 214 |
clearContents |
215 |
"remove all elements from the collection but keep the contentsArray. |
|
216 |
Useful for huge lists, if the contents will be rebuild soon (using #add:) |
|
217 |
to a size which is similar to the lists current size. |
|
218 |
Returns the receiver." |
|
219 |
||
220 |
|prevSize| |
|
221 |
||
222 |
prevSize := self size. |
|
223 |
super clearContents. |
|
224 |
||
225 |
prevSize ~~ 0 ifTrue:[ |
|
226 |
dependents notNil ifTrue:[ |
|
227 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
228 |
] |
|
229 |
] |
|
230 |
||
231 |
! |
|
232 |
||
577 | 233 |
removeAll |
234 |
"remove all elements from the collection. |
|
235 |
Returns the receiver." |
|
236 |
||
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
237 |
|prevSize| |
577 | 238 |
|
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
239 |
prevSize := self size. |
577 | 240 |
super removeAll. |
241 |
||
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
242 |
prevSize ~~ 0 ifTrue:[ |
611 | 243 |
dependents notNil ifTrue:[ |
244 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
245 |
] |
|
577 | 246 |
] |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
247 |
|
611 | 248 |
"Modified: / 29.1.1998 / 10:53:28 / cg" |
577 | 249 |
! |
250 |
||
251 |
removeFirst |
|
252 |
"remove the first element from the collection; return the element." |
|
253 |
||
254 |
|deletedObject| |
|
255 |
||
256 |
deletedObject := super removeFirst. |
|
611 | 257 |
dependents notNil ifTrue:[self changed:#remove: with:1]. |
577 | 258 |
^ deletedObject |
259 |
||
611 | 260 |
"Modified: / 29.1.1998 / 10:53:36 / cg" |
577 | 261 |
! |
262 |
||
263 |
removeFirst:n |
|
264 |
"remove the first n elements from the collection; |
|
265 |
Return a collection containing the removed elements." |
|
266 |
||
267 |
|deletedObjects| |
|
268 |
||
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
269 |
deletedObjects := super removeFirst:n. |
611 | 270 |
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
|
271 |
^ deletedObjects |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
272 |
|
611 | 273 |
"Modified: / 29.1.1998 / 10:53:40 / cg" |
577 | 274 |
! |
275 |
||
1095 | 276 |
removeFirstIfAbsent:exceptionBlock |
277 |
"remove the first element from the collection; return the element. |
|
278 |
If there is no element in the receiver collection, return the value from |
|
279 |
exceptionBlock." |
|
280 |
||
281 |
self size > 0 ifTrue:[ ^ self removeFirst ]. |
|
282 |
^ exceptionBlock value |
|
283 |
! |
|
284 |
||
577 | 285 |
removeFromIndex:startIndex toIndex:stopIndex |
286 |
"remove the elements stored under startIndex up to and including |
|
742 | 287 |
the elements under stopIndex. |
288 |
Return the receiver. |
|
289 |
Returning the receiver here is a historic leftover - it may change." |
|
577 | 290 |
|
742 | 291 |
|ret| |
577 | 292 |
|
742 | 293 |
ret := super removeFromIndex:startIndex toIndex:stopIndex. |
611 | 294 |
dependents notNil ifTrue:[ |
742 | 295 |
self changed:#removeFrom: with:(Array with:startIndex with:stopIndex). |
611 | 296 |
]. |
742 | 297 |
^ ret |
577 | 298 |
|
611 | 299 |
"Modified: / 29.1.1998 / 10:54:03 / cg" |
577 | 300 |
! |
301 |
||
1095 | 302 |
removeIdentical:anObject ifAbsent:exceptionBlock |
303 |
"remove the first element which is identical to anObject; |
|
304 |
if found, remove and return it; |
|
305 |
if not, return the value from evaluating exceptionBlock. |
|
306 |
Uses identity compare (==) to search for the element." |
|
307 |
||
308 |
|index| |
|
309 |
||
310 |
index := self identityIndexOf:anObject. |
|
311 |
||
312 |
index == 0 ifTrue:[ ^ exceptionBlock value ]. |
|
313 |
self removeFromIndex:index toIndex:index. |
|
314 |
^ anObject |
|
315 |
! |
|
316 |
||
577 | 317 |
removeLast |
318 |
"remove the last element from the collection; return the element" |
|
319 |
||
320 |
|deletedObject| |
|
321 |
||
322 |
deletedObject := super removeLast. |
|
611 | 323 |
dependents notNil ifTrue:[self changed:#remove: with:(1 + self size)]. |
577 | 324 |
^ deletedObject |
325 |
||
611 | 326 |
"Modified: / 29.1.1998 / 10:54:15 / cg" |
577 | 327 |
! |
328 |
||
329 |
removeLast:n |
|
330 |
"remove the last n elements from the collection; |
|
331 |
Return a collection of removed elements." |
|
332 |
||
333 |
|deletedObjects stop| |
|
334 |
||
335 |
stop := self size. |
|
336 |
deletedObjects := super removeLast:n. |
|
611 | 337 |
dependents notNil ifTrue:[ |
338 |
self changed:#removeFrom: with:(Array with:(stop - n + 1) with:stop). |
|
339 |
]. |
|
577 | 340 |
^ deletedObjects |
341 |
||
611 | 342 |
"Modified: / 29.1.1998 / 10:54:25 / cg" |
1095 | 343 |
! |
344 |
||
345 |
removeLastIfAbsent:exceptionBlock |
|
346 |
"remove the last element from the collection; return the element. |
|
347 |
If there is no element in the receiver collection, return the value from |
|
348 |
exceptionBlock." |
|
349 |
||
350 |
self size > 0 ifTrue:[ ^ self removeLast ]. |
|
351 |
^ exceptionBlock value |
|
352 |
! |
|
353 |
||
354 |
reset |
|
355 |
"logically remove all elements from the collection. |
|
356 |
Thats almost the same as #removeAll, but keeps the contentsArray. |
|
357 |
Returns the receiver." |
|
358 |
||
359 |
|prevSize| |
|
360 |
||
361 |
prevSize := self size. |
|
362 |
super reset. |
|
363 |
||
364 |
prevSize ~~ 0 ifTrue:[ |
|
365 |
dependents notNil ifTrue:[ |
|
366 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
367 |
] |
|
368 |
]. |
|
577 | 369 |
! ! |
370 |
||
371 |
!List methodsFor:'converting'! |
|
372 |
||
373 |
asList |
|
374 |
^ self |
|
375 |
||
376 |
"Created: 14.2.1997 / 16:25:55 / cg" |
|
377 |
! ! |
|
378 |
||
810
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
379 |
!List methodsFor:'copying'! |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
380 |
|
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
381 |
skipInstvarIndexInDeepCopy:index |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
382 |
"a helper for deepCopy; only indices for which this method returns |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
383 |
false are copied in a deep copy." |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
384 |
|
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
385 |
index == 4 ifTrue:[ |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
386 |
^ true "/ skip dependents |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
387 |
]. |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
388 |
^ false |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
389 |
|
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
390 |
|
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
391 |
! ! |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
392 |
|
577 | 393 |
!List methodsFor:'dependents access'! |
394 |
||
1090
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
395 |
addNonWeakDependent:anObject |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
396 |
"make the argument, anObject be a dependent of the receiver. |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
397 |
Since all dependencies are nonWeak in Model, this is simply |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
398 |
forwarded to addDependent:" |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
399 |
|
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
400 |
^ self addDependent:anObject |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
401 |
! |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
402 |
|
480 | 403 |
dependents |
404 |
"return the dependents of the receiver" |
|
405 |
||
584
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
406 |
^ dependents ? #() |
480 | 407 |
|
584
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
408 |
"Created: / 14.2.1997 / 16:05:49 / cg" |
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
409 |
"Modified: / 27.10.1997 / 19:37:33 / cg" |
480 | 410 |
! |
411 |
||
412 |
dependents:aCollection |
|
413 |
"set the dependents of the receiver" |
|
414 |
||
611 | 415 |
aCollection size == 0 ifTrue:[ |
416 |
dependents := nil |
|
417 |
] ifFalse:[ |
|
418 |
dependents := aCollection. |
|
419 |
]. |
|
480 | 420 |
|
611 | 421 |
"Created: / 14.2.1997 / 16:05:58 / cg" |
422 |
"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
|
423 |
! |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
424 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
425 |
nonWeakDependents |
612
cc2d6bd50cdc
#nonWeakDependents always returns a collection.
Claus Gittinger <cg@exept.de>
parents:
611
diff
changeset
|
426 |
"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
|
427 |
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
|
428 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
429 |
^ self dependents |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
430 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
431 |
"Created: / 19.4.1996 / 10:29:43 / cg" |
612
cc2d6bd50cdc
#nonWeakDependents always returns a collection.
Claus Gittinger <cg@exept.de>
parents:
611
diff
changeset
|
432 |
"Modified: / 30.1.1998 / 14:06:12 / cg" |
673 | 433 |
! |
434 |
||
435 |
nonWeakDependents:newDeps |
|
436 |
"return a Collection of dependents - empty if there is none. |
|
437 |
Since all dependencies are nonWeak in List, this is a dummy." |
|
438 |
||
439 |
^ self dependents:newDeps |
|
440 |
||
441 |
"Created: / 19.4.1996 / 10:29:43 / cg" |
|
442 |
"Modified: / 30.1.1998 / 14:06:12 / cg" |
|
1090
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
443 |
! |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
444 |
|
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
445 |
removeNonWeakDependent:anObject |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
446 |
"make the argument, anObject be independent of the receiver. |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
447 |
Since all dependencies are nonWeak in Model, this is simply |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
448 |
forwarded to removeDependent:" |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
449 |
|
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
450 |
^ self removeDependent:anObject |
480 | 451 |
! ! |
452 |
||
577 | 453 |
!List methodsFor:'filling & replacing'! |
454 |
||
908 | 455 |
contents:aCollection |
456 |
"replace all elements in the receiver by aCollection, |
|
457 |
Redefined - can be done faster" |
|
458 |
||
459 |
|oldSize newSize| |
|
460 |
||
909
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
461 |
aCollection isSequenceable ifFalse:[ |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
462 |
^ super contents:aCollection |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
463 |
]. |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
464 |
|
908 | 465 |
oldSize := self size. |
466 |
newSize := aCollection size. |
|
467 |
||
468 |
newSize < oldSize ifTrue:[ |
|
469 |
self replaceFrom:1 to:newSize with:aCollection startingAt:1. |
|
470 |
self removeFromIndex:newSize+1 toIndex:oldSize. |
|
471 |
] ifFalse:[ |
|
472 |
newSize > oldSize ifTrue:[ |
|
909
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
473 |
oldSize == 0 ifTrue:[ |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
474 |
self addAll:aCollection |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
475 |
] ifFalse:[ |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
476 |
self replaceFrom:1 to:oldSize with:aCollection startingAt:1. |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
477 |
self addAll:aCollection from:oldSize+1 to:newSize beforeIndex:oldSize+1 |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
478 |
] |
908 | 479 |
] ifFalse:[ |
480 |
self replaceFrom:1 to:newSize with:aCollection startingAt:1. |
|
481 |
] |
|
482 |
]. |
|
483 |
||
484 |
" |
|
485 |
|l| |
|
486 |
l := List new. |
|
487 |
l contents:#(1 2 3 4 5). |
|
488 |
l |
|
489 |
||
490 |
|l| |
|
491 |
l := List new. |
|
492 |
l addAll:#(1 2 3 4 5). |
|
493 |
l contents:#(10 20 30). |
|
494 |
l |
|
495 |
||
496 |
|l| |
|
497 |
l := List new. |
|
498 |
l addAll:#(1 2 3 4 5). |
|
499 |
l contents:#(10 20 30 40 50 60 70 80). |
|
500 |
l |
|
501 |
||
502 |
|l| |
|
503 |
l := List new. |
|
504 |
l addAll:#(1 2 3 4 5). |
|
505 |
l contents:#(10 20 30 40 50). |
|
506 |
l |
|
507 |
" |
|
508 |
! |
|
509 |
||
1033 | 510 |
list:aCollection |
511 |
"replace all elements in the receiver by aCollection. |
|
512 |
For compatibility with other smalltalks |
|
513 |
(allows List to be sometimes used as a ListPresenter in ported Dolphin apps)" |
|
514 |
||
515 |
self contents:aCollection |
|
516 |
||
517 |
" |
|
518 |
|l| |
|
519 |
l := List new. |
|
520 |
l list:#(1 2 3 4 5). |
|
521 |
l |
|
522 |
||
523 |
|l| |
|
524 |
l := List new. |
|
525 |
l addAll:#(1 2 3 4 5). |
|
526 |
l list:#(10 20 30). |
|
527 |
l |
|
528 |
||
529 |
|l| |
|
530 |
l := List new. |
|
531 |
l addAll:#(1 2 3 4 5). |
|
532 |
l list:#(10 20 30 40 50 60 70 80). |
|
533 |
l |
|
534 |
||
535 |
|l| |
|
536 |
l := List new. |
|
537 |
l addAll:#(1 2 3 4 5). |
|
538 |
l list:#(10 20 30 40 50). |
|
539 |
l |
|
540 |
" |
|
541 |
! |
|
542 |
||
577 | 543 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
544 |
"replace elements in the receiver between index start and stop, |
|
545 |
with elements taken from replacementCollection starting at repStart. |
|
546 |
Redefined - can be done faster" |
|
481 | 547 |
|
646 | 548 |
super replaceFrom:start to:stop with:aCollection startingAt:repStart. |
481 | 549 |
|
577 | 550 |
dependents notNil ifTrue:[ |
551 |
self changed:#replace: with:(Array with:start with:stop) |
|
552 |
]. |
|
553 |
||
646 | 554 |
"Modified: / 20.5.1998 / 15:20:17 / cg" |
481 | 555 |
! ! |
556 |
||
881 | 557 |
!List methodsFor:'testing'! |
558 |
||
559 |
isList |
|
560 |
"return true, if the receiver is some kind of list collection; |
|
561 |
true is returned here - the method is redefined from Object." |
|
562 |
||
563 |
^ true |
|
564 |
||
565 |
"Modified: / 11.2.2000 / 01:37:36 / cg" |
|
566 |
! ! |
|
567 |
||
480 | 568 |
!List class methodsFor:'documentation'! |
569 |
||
570 |
version |
|
1142
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
571 |
^ '$Header: /cvs/stx/stx/libbasic2/List.st,v 1.23 2003-02-10 23:26:26 cg Exp $' |
480 | 572 |
! ! |