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