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