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