author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 5237 | 3c1a727eb448 |
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 |
4706 | 19 |
instanceVariableNames:'dependents optionalAccessLock' |
480 | 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 |
4815 | 47 |
a list's contents is changed. |
737 | 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 |
||
4706 | 57 |
New: |
58 |
if the optional optionalAccessLock is set, all operations |
|
59 |
are protected by a critical region. |
|
60 |
(i.e. the List is a SynchronizedCollection) |
|
61 |
||
4147 | 62 |
[caveat:] |
63 |
'List' is probably a bad name, which may confuse beginners. |
|
64 |
I have nothing in common with LinkedLists. |
|
65 |
Instances are just regular ordered collections, with the added benefit of |
|
4706 | 66 |
sending out information about changes, and an optional synchronization lock. |
4147 | 67 |
Thus, they can be used as a model of textviews or selection list views, |
68 |
which need to redraw whenever the contents of the list changes. |
|
69 |
(and Lists not only send out change notifications when modified, |
|
70 |
but also include information about the range of changed elements. |
|
71 |
So the view can optimize its redraws) |
|
72 |
||
480 | 73 |
[see also:] |
74 |
Array OrderedCollection |
|
75 |
||
76 |
[author:] |
|
77 |
Claus Gittinger |
|
78 |
" |
|
79 |
! ! |
|
80 |
||
81 |
!List methodsFor:'accessing'! |
|
82 |
||
577 | 83 |
at:anIndex put:anObject |
84 |
"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
|
85 |
Return anObject (sigh). |
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
|
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
91 |
|idx "{ Class: SmallInteger }"| |
577 | 92 |
|
4706 | 93 |
self possiblySynchronized:[ |
94 |
idx := anIndex + firstIndex - 1. |
|
95 |
((anIndex < 1) or:[idx > lastIndex]) ifTrue:[ |
|
96 |
idx == (lastIndex+1) ifTrue:[ |
|
97 |
self makeRoomAtLast. |
|
98 |
lastIndex := lastIndex + 1. |
|
99 |
] ifFalse:[ |
|
100 |
^ self subscriptBoundsError:anIndex |
|
101 |
] |
|
102 |
]. |
|
103 |
||
104 |
contentsArray basicAt:idx put:anObject. |
|
105 |
dependents notNil ifTrue:[self changed:#at: with:anIndex]. |
|
608
d7a0efe4105f
allow at:put:-ting right after the last element (VW compatbility)
Claus Gittinger <cg@exept.de>
parents:
606
diff
changeset
|
106 |
]. |
577 | 107 |
^ anObject |
108 |
||
4683 | 109 |
"Modified: / 28-01-1998 / 16:44:49 / cg" |
4706 | 110 |
"Modified: / 01-08-2018 / 11:46:40 / Claus Gittinger" |
577 | 111 |
! |
112 |
||
113 |
list |
|
114 |
^ self |
|
4707 | 115 |
! |
116 |
||
117 |
synchronizationSemaphore |
|
118 |
"return a synchronization semaphore for myself" |
|
119 |
||
120 |
optionalAccessLock notNil ifTrue:[ |
|
121 |
^ optionalAccessLock |
|
122 |
]. |
|
123 |
^ super synchronizationSemaphore. |
|
124 |
||
125 |
"Created: / 01-08-2018 / 13:22:45 / Claus Gittinger" |
|
577 | 126 |
! ! |
127 |
||
128 |
!List methodsFor:'adding & removing'! |
|
129 |
||
130 |
add:anObject |
|
131 |
"add the argument, anObject to the end of the collection |
|
132 |
Return the argument, anObject." |
|
133 |
||
4706 | 134 |
self possiblySynchronized:[ |
135 |
super add:anObject. |
|
136 |
dependents notNil ifTrue:[self changed:#insert: with:(self size)]. |
|
137 |
]. |
|
577 | 138 |
^ anObject |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
139 |
|
4706 | 140 |
"Modified: / 29-01-1998 / 10:52:32 / cg" |
141 |
"Modified: / 01-08-2018 / 11:46:58 / Claus Gittinger" |
|
577 | 142 |
! |
143 |
||
4816 | 144 |
add:anObject after:oldObject |
145 |
"insert the argument, newObject after oldObject. |
|
146 |
If oldObject is not in the receiver, report an error, |
|
147 |
otherwise return the argument, anObject." |
|
148 |
||
149 |
self possiblySynchronized:[ |
|
150 |
super add:anObject after:oldObject. |
|
151 |
]. |
|
152 |
||
153 |
"Created: / 19-02-2019 / 23:48:35 / Claus Gittinger" |
|
154 |
! |
|
155 |
||
577 | 156 |
add:anObject beforeIndex:index |
1922
be14f1a04953
#add:beforeIndex: - make immune against redefinition of #add: using
Stefan Vogel <sv@exept.de>
parents:
1916
diff
changeset
|
157 |
"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
|
158 |
Return the receiver (sigh - ST-80 compatibility)." |
577 | 159 |
|
4706 | 160 |
self possiblySynchronized:[ |
161 |
super add:anObject beforeIndex:index. |
|
162 |
dependents notNil ifTrue:[ |
|
163 |
self changed:#insert: with:index |
|
164 |
]. |
|
165 |
]. |
|
166 |
||
167 |
"Modified (format): / 01-08-2018 / 11:47:24 / Claus Gittinger" |
|
577 | 168 |
! |
169 |
||
170 |
addAll:aCollection beforeIndex:index |
|
5237 | 171 |
"insert all elements of the argument, anObject to become located at index. |
172 |
The collection may be unordered, but then the order of the sliced-in elements |
|
173 |
is undefined. |
|
577 | 174 |
Return the receiver." |
175 |
||
4706 | 176 |
self possiblySynchronized:[ |
177 |
super addAll:aCollection beforeIndex:index. |
|
178 |
dependents notNil ifTrue:[ |
|
179 |
self changed: #insertCollection: with:(Array with:index with:(aCollection size)) |
|
180 |
] |
|
611 | 181 |
] |
4706 | 182 |
|
183 |
"Modified: / 01-08-2018 / 11:47:17 / Claus Gittinger" |
|
577 | 184 |
! |
185 |
||
909
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
186 |
addAll:aCollection from:startIndex to:endIndex beforeIndex:index |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
187 |
"insert elements start to stop from the argument |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
188 |
Return the receiver." |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
189 |
|
4706 | 190 |
self possiblySynchronized:[ |
191 |
super addAll:aCollection from:startIndex to:endIndex beforeIndex:index. |
|
192 |
dependents notNil ifTrue:[ |
|
193 |
self changed: #insertCollection: with:(Array with:index with:(endIndex - startIndex + 1)) |
|
194 |
] |
|
909
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
195 |
] |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
196 |
|
4697 | 197 |
"Modified: / 29-01-1998 / 10:52:57 / cg" |
198 |
"Modified: / 30-07-2018 / 11:16:31 / Stefan Vogel" |
|
4706 | 199 |
"Modified: / 01-08-2018 / 11:47:38 / Claus Gittinger" |
909
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
200 |
! |
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
201 |
|
1142
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
202 |
addAllLast:aCollection |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
203 |
"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
|
204 |
Return the argument, aCollection." |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
205 |
|
4706 | 206 |
self possiblySynchronized:[ |
207 |
self addAll:aCollection beforeIndex:self size + 1. |
|
208 |
]. |
|
1142
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
209 |
^ aCollection |
4706 | 210 |
|
211 |
"Modified: / 01-08-2018 / 11:47:55 / Claus Gittinger" |
|
1142
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
212 |
! |
8b91f129aea7
addAllLast: - only one change message
Claus Gittinger <cg@exept.de>
parents:
1095
diff
changeset
|
213 |
|
577 | 214 |
addFirst:anObject |
215 |
"add the argument, anObject to the beginning of the collection. |
|
216 |
Return the argument, anObject." |
|
217 |
||
4706 | 218 |
self possiblySynchronized:[ |
219 |
super addFirst:anObject. |
|
220 |
dependents notNil ifTrue:[self changed:#insert: with:1]. |
|
221 |
]. |
|
577 | 222 |
^ anObject |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
223 |
|
4706 | 224 |
"Modified: / 29-01-1998 / 10:53:09 / cg" |
225 |
"Modified: / 01-08-2018 / 11:48:05 / Claus Gittinger" |
|
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
226 |
! |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
227 |
|
632 | 228 |
clearContents |
229 |
"remove all elements from the collection but keep the contentsArray. |
|
230 |
Useful for huge lists, if the contents will be rebuild soon (using #add:) |
|
231 |
to a size which is similar to the lists current size. |
|
232 |
Returns the receiver." |
|
233 |
||
4706 | 234 |
self possiblySynchronized:[ |
235 |
|prevSize| |
|
632 | 236 |
|
4706 | 237 |
prevSize := self size. |
238 |
super clearContents. |
|
632 | 239 |
|
240 |
dependents notNil ifTrue:[ |
|
4706 | 241 |
prevSize ~~ 0 ifTrue:[ |
242 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
243 |
] |
|
632 | 244 |
] |
245 |
] |
|
4706 | 246 |
|
247 |
"Modified (format): / 01-08-2018 / 11:51:29 / Claus Gittinger" |
|
632 | 248 |
! |
249 |
||
4934 | 250 |
dropLast:n |
251 |
"remove the last n elements from the receiver collection. |
|
252 |
Return the receiver." |
|
253 |
||
254 |
self possiblySynchronized:[ |
|
255 |
| stop | |
|
256 |
stop := self size. |
|
257 |
super dropLast:n. |
|
258 |
dependents notNil ifTrue:[ |
|
259 |
self changed:#removeFrom: with:(Array with:(stop - n + 1) with:stop). |
|
260 |
]. |
|
261 |
]. |
|
262 |
^ self |
|
263 |
||
264 |
"Created: / 03-04-2019 / 12:40:35 / Claus Gittinger" |
|
265 |
! |
|
266 |
||
577 | 267 |
removeAll |
268 |
"remove all elements from the collection. |
|
269 |
Returns the receiver." |
|
270 |
||
4706 | 271 |
self possiblySynchronized:[ |
272 |
|prevSize| |
|
577 | 273 |
|
4706 | 274 |
prevSize := self size. |
275 |
super removeAll. |
|
577 | 276 |
|
611 | 277 |
dependents notNil ifTrue:[ |
4706 | 278 |
prevSize ~~ 0 ifTrue:[ |
279 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
280 |
] |
|
611 | 281 |
] |
4706 | 282 |
] |
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
283 |
|
4706 | 284 |
"Modified: / 29-01-1998 / 10:53:28 / cg" |
285 |
"Modified (format): / 01-08-2018 / 11:51:21 / Claus Gittinger" |
|
577 | 286 |
! |
287 |
||
3301 | 288 |
removeAllSuchThat:aBlock |
289 |
"remove all elements that meet a test criteria as specified in aBlock. |
|
290 |
The argument, aBlock is evaluated for successive elements and all those, |
|
291 |
for which it returns true, are removed. |
|
292 |
Destructive: modifies the receiver. |
|
293 |
Return a collection containing the removed elements." |
|
294 |
||
295 |
|removedElements| |
|
296 |
||
4706 | 297 |
self possiblySynchronized:[ |
298 |
removedElements := super removeAllSuchThat:aBlock. |
|
299 |
removedElements notEmpty ifTrue:[ |
|
300 |
self changed. |
|
301 |
]. |
|
3836 | 302 |
]. |
3301 | 303 |
^ removedElements. |
4706 | 304 |
|
305 |
"Modified: / 01-08-2018 / 11:49:44 / Claus Gittinger" |
|
3301 | 306 |
! |
307 |
||
577 | 308 |
removeFirst |
309 |
"remove the first element from the collection; return the element." |
|
310 |
||
4706 | 311 |
|deletedObject| |
577 | 312 |
|
4706 | 313 |
self possiblySynchronized:[ |
314 |
deletedObject := super removeFirst. |
|
315 |
dependents notNil ifTrue:[self changed:#remove: with:1]. |
|
316 |
]. |
|
317 |
^ deletedObject |
|
577 | 318 |
|
4706 | 319 |
"Modified: / 29-01-1998 / 10:53:36 / cg" |
320 |
"Modified: / 01-08-2018 / 11:49:58 / Claus Gittinger" |
|
577 | 321 |
! |
322 |
||
323 |
removeFirst:n |
|
324 |
"remove the first n elements from the collection; |
|
325 |
Return a collection containing the removed elements." |
|
326 |
||
327 |
|deletedObjects| |
|
328 |
||
4706 | 329 |
self possiblySynchronized:[ |
330 |
deletedObjects := super removeFirst:n. |
|
331 |
dependents notNil ifTrue:[self changed:#removeFrom: with:(Array with:1 with:n)]. |
|
332 |
]. |
|
606
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
333 |
^ deletedObjects |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
334 |
|
4706 | 335 |
"Modified: / 29-01-1998 / 10:53:40 / cg" |
336 |
"Modified: / 01-08-2018 / 11:50:10 / Claus Gittinger" |
|
577 | 337 |
! |
338 |
||
1095 | 339 |
removeFirstIfAbsent:exceptionBlock |
340 |
"remove the first element from the collection; return the element. |
|
341 |
If there is no element in the receiver collection, return the value from |
|
342 |
exceptionBlock." |
|
343 |
||
4706 | 344 |
self possiblySynchronized:[ |
345 |
self notEmpty ifTrue:[ ^ self removeFirst ]. |
|
346 |
]. |
|
1778
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
347 |
^ exceptionBlock value |
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
348 |
|
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
349 |
"Modified: / 21-10-2006 / 23:03:46 / cg" |
4632 | 350 |
"Modified: / 11-04-2018 / 11:52:30 / stefan" |
4706 | 351 |
"Modified: / 01-08-2018 / 11:50:26 / Claus Gittinger" |
1095 | 352 |
! |
353 |
||
577 | 354 |
removeFromIndex:startIndex toIndex:stopIndex |
355 |
"remove the elements stored under startIndex up to and including |
|
742 | 356 |
the elements under stopIndex. |
357 |
Return the receiver. |
|
358 |
Returning the receiver here is a historic leftover - it may change." |
|
577 | 359 |
|
742 | 360 |
|ret| |
577 | 361 |
|
2004
e9ee1c2930b0
dont send out change-notifications, if there was no change
ab
parents:
1922
diff
changeset
|
362 |
stopIndex < startIndex ifTrue:[^ self]. |
e9ee1c2930b0
dont send out change-notifications, if there was no change
ab
parents:
1922
diff
changeset
|
363 |
|
4706 | 364 |
self possiblySynchronized:[ |
365 |
ret := super removeFromIndex:startIndex toIndex:stopIndex. |
|
366 |
dependents notNil ifTrue:[ |
|
367 |
self changed:#removeFrom: with:(Array with:startIndex with:stopIndex). |
|
368 |
]. |
|
369 |
]. |
|
742 | 370 |
^ ret |
577 | 371 |
|
4706 | 372 |
"Modified: / 29-01-1998 / 10:54:03 / cg" |
373 |
"Modified: / 01-08-2018 / 11:50:42 / Claus Gittinger" |
|
577 | 374 |
! |
375 |
||
1095 | 376 |
removeIdentical:anObject ifAbsent:exceptionBlock |
377 |
"remove the first element which is identical to anObject; |
|
378 |
if found, remove and return it; |
|
379 |
if not, return the value from evaluating exceptionBlock. |
|
380 |
Uses identity compare (==) to search for the element." |
|
381 |
||
4706 | 382 |
self possiblySynchronized:[ |
383 |
|index| |
|
1095 | 384 |
|
4706 | 385 |
index := self identityIndexOf:anObject. |
1095 | 386 |
|
4706 | 387 |
index == 0 ifTrue:[ ^ exceptionBlock value ]. |
388 |
self removeFromIndex:index toIndex:index. |
|
389 |
]. |
|
1778
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
390 |
^ anObject |
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
391 |
|
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
392 |
"Modified: / 21-10-2006 / 23:03:29 / cg" |
4706 | 393 |
"Modified (format): / 01-08-2018 / 11:51:07 / Claus Gittinger" |
1095 | 394 |
! |
395 |
||
577 | 396 |
removeLast |
397 |
"remove the last element from the collection; return the element" |
|
398 |
||
4706 | 399 |
|deletedObject| |
577 | 400 |
|
4706 | 401 |
self possiblySynchronized:[ |
402 |
deletedObject := super removeLast. |
|
403 |
dependents notNil ifTrue:[self changed:#remove: with:(1 + self size)]. |
|
404 |
]. |
|
405 |
^ deletedObject |
|
577 | 406 |
|
4706 | 407 |
"Modified: / 29-01-1998 / 10:54:15 / cg" |
408 |
"Modified: / 01-08-2018 / 11:51:51 / Claus Gittinger" |
|
577 | 409 |
! |
410 |
||
411 |
removeLast:n |
|
2054 | 412 |
"remove the last n elements from the receiver collection. |
413 |
Return a collection of removed elements." |
|
577 | 414 |
|
4706 | 415 |
|deletedObjects| |
577 | 416 |
|
4706 | 417 |
self possiblySynchronized:[ |
418 |
| stop | |
|
419 |
stop := self size. |
|
420 |
deletedObjects := super removeLast:n. |
|
421 |
dependents notNil ifTrue:[ |
|
422 |
self changed:#removeFrom: with:(Array with:(stop - n + 1) with:stop). |
|
423 |
]. |
|
611 | 424 |
]. |
577 | 425 |
^ deletedObjects |
426 |
||
4706 | 427 |
"Modified: / 29-01-1998 / 10:54:25 / cg" |
428 |
"Modified: / 01-08-2018 / 11:52:10 / Claus Gittinger" |
|
1095 | 429 |
! |
430 |
||
431 |
removeLastIfAbsent:exceptionBlock |
|
432 |
"remove the last element from the collection; return the element. |
|
433 |
If there is no element in the receiver collection, return the value from |
|
434 |
exceptionBlock." |
|
435 |
||
4706 | 436 |
self possiblySynchronized:[ |
437 |
self notEmpty ifTrue:[ ^ self removeLast ]. |
|
438 |
]. |
|
1778
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
439 |
^ exceptionBlock value |
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
440 |
|
1fc313500385
ca's ugly indentation removed
Claus Gittinger <cg@exept.de>
parents:
1638
diff
changeset
|
441 |
"Modified: / 21-10-2006 / 23:03:53 / cg" |
4697 | 442 |
"Modified: / 30-07-2018 / 11:12:56 / Stefan Vogel" |
4706 | 443 |
"Modified: / 01-08-2018 / 11:52:25 / Claus Gittinger" |
1095 | 444 |
! |
445 |
||
446 |
reset |
|
447 |
"logically remove all elements from the collection. |
|
4089 | 448 |
That's almost the same as #removeAll, but keeps the contentsArray. |
1095 | 449 |
Returns the receiver." |
450 |
||
4706 | 451 |
self possiblySynchronized:[ |
452 |
|prevSize| |
|
1095 | 453 |
|
4706 | 454 |
prevSize := self size. |
455 |
super reset. |
|
1095 | 456 |
|
457 |
dependents notNil ifTrue:[ |
|
4706 | 458 |
prevSize ~~ 0 ifTrue:[ |
459 |
self changed:#removeFrom: with:(Array with:1 with:prevSize) |
|
460 |
] |
|
461 |
]. |
|
462 |
]. |
|
463 |
||
464 |
"Modified: / 01-08-2018 / 11:52:46 / Claus Gittinger" |
|
577 | 465 |
! ! |
466 |
||
467 |
!List methodsFor:'converting'! |
|
468 |
||
469 |
asList |
|
470 |
^ self |
|
471 |
||
472 |
"Created: 14.2.1997 / 16:25:55 / cg" |
|
4706 | 473 |
! |
474 |
||
475 |
asSharedCollection |
|
5137 | 476 |
"return a shared collection on the receiver. |
477 |
If the receiver is already synchronized, itself is returned. |
|
478 |
This implements synchronized (i.e. mutually excluded) access to me. |
|
479 |
Use this for safe access when multiple processes access me concurrently." |
|
480 |
||
4708 | 481 |
optionalAccessLock notNil ifTrue:[ |
4793 | 482 |
"I am alreay protected against concurrent access" |
4708 | 483 |
^ self |
484 |
]. |
|
4706 | 485 |
|
4708 | 486 |
^ (List withAll:self) |
487 |
beSynchronized; |
|
488 |
yourself. |
|
4706 | 489 |
|
490 |
"Created: / 01-08-2018 / 11:54:26 / Claus Gittinger" |
|
4708 | 491 |
"Modified (format): / 01-08-2018 / 15:07:20 / Stefan Vogel" |
4793 | 492 |
"Modified (format): / 02-02-2019 / 19:26:04 / Claus Gittinger" |
577 | 493 |
! ! |
494 |
||
810
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
495 |
!List methodsFor:'copying'! |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
496 |
|
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
497 |
skipInstvarIndexInDeepCopy:index |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
498 |
"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
|
499 |
false are copied in a deep copy." |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
500 |
|
2858 | 501 |
^ index == 4 "/ skip dependents |
810
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
502 |
! ! |
b27913d14137
deepCopy fix (do not copy dependents)
Claus Gittinger <cg@exept.de>
parents:
742
diff
changeset
|
503 |
|
577 | 504 |
!List methodsFor:'dependents access'! |
505 |
||
1638
4ca339939383
dependents vs. weakDependents
Claus Gittinger <cg@exept.de>
parents:
1615
diff
changeset
|
506 |
addDependent:anObject |
1615 | 507 |
"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
|
508 |
|
1638
4ca339939383
dependents vs. weakDependents
Claus Gittinger <cg@exept.de>
parents:
1615
diff
changeset
|
509 |
^ self addNonWeakDependent:anObject |
1090
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
510 |
! |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
511 |
|
480 | 512 |
dependents |
513 |
"return the dependents of the receiver" |
|
514 |
||
584
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
515 |
^ dependents ? #() |
480 | 516 |
|
584
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
517 |
"Created: / 14.2.1997 / 16:05:49 / cg" |
319da69edba2
dependents now always returns a collection
Claus Gittinger <cg@exept.de>
parents:
577
diff
changeset
|
518 |
"Modified: / 27.10.1997 / 19:37:33 / cg" |
480 | 519 |
! |
520 |
||
521 |
dependents:aCollection |
|
522 |
"set the dependents of the receiver" |
|
523 |
||
611 | 524 |
aCollection size == 0 ifTrue:[ |
525 |
dependents := nil |
|
526 |
] ifFalse:[ |
|
527 |
dependents := aCollection. |
|
528 |
]. |
|
480 | 529 |
|
611 | 530 |
"Created: / 14.2.1997 / 16:05:58 / cg" |
531 |
"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
|
532 |
! |
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
533 |
|
4419919ae433
shortCut change-messages if there are no dependents
Claus Gittinger <cg@exept.de>
parents:
584
diff
changeset
|
534 |
nonWeakDependents |
612
cc2d6bd50cdc
#nonWeakDependents always returns a collection.
Claus Gittinger <cg@exept.de>
parents:
611
diff
changeset
|
535 |
"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
|
536 |
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
|
537 |
|
1604 | 538 |
^ dependents ? #() |
673 | 539 |
! |
540 |
||
541 |
nonWeakDependents:newDeps |
|
542 |
"return a Collection of dependents - empty if there is none. |
|
543 |
Since all dependencies are nonWeak in List, this is a dummy." |
|
544 |
||
545 |
^ self dependents:newDeps |
|
546 |
||
547 |
"Created: / 19.4.1996 / 10:29:43 / cg" |
|
548 |
"Modified: / 30.1.1998 / 14:06:12 / cg" |
|
1090
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
549 |
! |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
550 |
|
1638
4ca339939383
dependents vs. weakDependents
Claus Gittinger <cg@exept.de>
parents:
1615
diff
changeset
|
551 |
removeDependent:anObject |
1090
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
552 |
"make the argument, anObject be independent of the receiver. |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
553 |
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
|
554 |
forwarded to removeDependent:" |
979886a925c8
nonWeak dependents fix (aka: kludge)
Claus Gittinger <cg@exept.de>
parents:
1033
diff
changeset
|
555 |
|
1638
4ca339939383
dependents vs. weakDependents
Claus Gittinger <cg@exept.de>
parents:
1615
diff
changeset
|
556 |
^ self removeNonWeakDependent:anObject |
480 | 557 |
! ! |
558 |
||
577 | 559 |
!List methodsFor:'filling & replacing'! |
560 |
||
908 | 561 |
contents:aCollection |
562 |
"replace all elements in the receiver by aCollection, |
|
563 |
Redefined - can be done faster" |
|
564 |
||
4706 | 565 |
|oldSize newSize| |
908 | 566 |
|
4706 | 567 |
aCollection isSequenceable ifFalse:[ |
568 |
^ super contents:aCollection |
|
569 |
]. |
|
909
c4f759a283cf
fixed #contents: (empty case)
Claus Gittinger <cg@exept.de>
parents:
908
diff
changeset
|
570 |
|
4706 | 571 |
self possiblySynchronized:[ |
572 |
oldSize := self size. |
|
573 |
newSize := aCollection size. |
|
908 | 574 |
|
4706 | 575 |
newSize < oldSize ifTrue:[ |
576 |
self replaceFrom:1 to:newSize with:aCollection startingAt:1. |
|
577 |
self removeFromIndex:newSize+1 toIndex:oldSize. |
|
908 | 578 |
] ifFalse:[ |
4706 | 579 |
newSize > oldSize ifTrue:[ |
580 |
oldSize == 0 ifTrue:[ |
|
581 |
self addAll:aCollection |
|
582 |
] ifFalse:[ |
|
583 |
self replaceFrom:1 to:oldSize with:aCollection startingAt:1. |
|
584 |
self addAll:aCollection from:oldSize+1 to:newSize beforeIndex:oldSize+1 |
|
585 |
] |
|
586 |
] ifFalse:[ |
|
587 |
"/ same size |
|
588 |
oldSize ~~ 0 ifTrue:[ |
|
589 |
self replaceFrom:1 to:newSize with:aCollection startingAt:1. |
|
590 |
] |
|
591 |
] |
|
592 |
]. |
|
593 |
]. |
|
594 |
||
595 |
" |
|
908 | 596 |
|l| |
597 |
l := List new. |
|
598 |
l contents:#(1 2 3 4 5). |
|
599 |
l |
|
600 |
||
601 |
|l| |
|
602 |
l := List new. |
|
603 |
l addAll:#(1 2 3 4 5). |
|
604 |
l contents:#(10 20 30). |
|
605 |
l |
|
606 |
||
607 |
|l| |
|
608 |
l := List new. |
|
609 |
l addAll:#(1 2 3 4 5). |
|
610 |
l contents:#(10 20 30 40 50 60 70 80). |
|
611 |
l |
|
612 |
||
613 |
|l| |
|
614 |
l := List new. |
|
615 |
l addAll:#(1 2 3 4 5). |
|
616 |
l contents:#(10 20 30 40 50). |
|
617 |
l |
|
4706 | 618 |
" |
619 |
||
620 |
"Modified: / 01-08-2018 / 11:55:20 / Claus Gittinger" |
|
908 | 621 |
! |
622 |
||
1033 | 623 |
list:aCollection |
624 |
"replace all elements in the receiver by aCollection. |
|
625 |
For compatibility with other smalltalks |
|
626 |
(allows List to be sometimes used as a ListPresenter in ported Dolphin apps)" |
|
627 |
||
628 |
self contents:aCollection |
|
629 |
||
630 |
" |
|
631 |
|l| |
|
632 |
l := List new. |
|
633 |
l list:#(1 2 3 4 5). |
|
634 |
l |
|
635 |
||
636 |
|l| |
|
637 |
l := List new. |
|
638 |
l addAll:#(1 2 3 4 5). |
|
639 |
l list:#(10 20 30). |
|
640 |
l |
|
641 |
||
642 |
|l| |
|
643 |
l := List new. |
|
644 |
l addAll:#(1 2 3 4 5). |
|
645 |
l list:#(10 20 30 40 50 60 70 80). |
|
646 |
l |
|
647 |
||
648 |
|l| |
|
649 |
l := List new. |
|
650 |
l addAll:#(1 2 3 4 5). |
|
651 |
l list:#(10 20 30 40 50). |
|
652 |
l |
|
653 |
" |
|
654 |
! |
|
655 |
||
577 | 656 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
657 |
"replace elements in the receiver between index start and stop, |
|
658 |
with elements taken from replacementCollection starting at repStart. |
|
659 |
Redefined - can be done faster" |
|
481 | 660 |
|
2999 | 661 |
stop < start ifTrue:[^ self]. |
481 | 662 |
|
4706 | 663 |
self possiblySynchronized:[ |
664 |
"/ see if there is really any change involved |
|
665 |
(self sameContentsFrom:start to:stop as:aCollection startingAt:repStart) ifTrue:[ |
|
666 |
^ self "/ avoids useless change notifications |
|
667 |
]. |
|
668 |
||
669 |
super replaceFrom:start to:stop with:aCollection startingAt:repStart. |
|
670 |
||
671 |
dependents notNil ifTrue:[ |
|
672 |
self changed:#replace: with:(Array with:start with:stop) |
|
673 |
]. |
|
2999 | 674 |
]. |
675 |
||
4706 | 676 |
"Modified: / 20-05-1998 / 15:20:17 / cg" |
677 |
"Modified: / 01-08-2018 / 11:55:41 / Claus Gittinger" |
|
2911 | 678 |
! |
679 |
||
680 |
setContents:aCollection |
|
681 |
"replace the receiver's underlying collection by aCollection" |
|
682 |
||
4706 | 683 |
self possiblySynchronized:[ |
684 |
aCollection isSequenceable ifFalse:[ |
|
685 |
^ super contents:aCollection |
|
686 |
]. |
|
687 |
||
688 |
contentsArray := aCollection. |
|
689 |
firstIndex := 1. |
|
690 |
lastIndex := aCollection size. |
|
691 |
self changed. |
|
692 |
]. |
|
693 |
||
694 |
"Modified: / 01-08-2018 / 11:56:01 / Claus Gittinger" |
|
695 |
! ! |
|
696 |
||
697 |
!List methodsFor:'private'! |
|
2911 | 698 |
|
4706 | 699 |
possiblySynchronized:aBlock |
700 |
optionalAccessLock notNil ifTrue:[ |
|
701 |
^ optionalAccessLock synchronized:aBlock. |
|
702 |
]. |
|
703 |
^ aBlock value. |
|
704 |
||
705 |
"Created: / 01-08-2018 / 11:46:01 / Claus Gittinger" |
|
4793 | 706 |
! |
707 |
||
708 |
synchronized:aBlock |
|
5009 | 709 |
"a shortcut, if the sync-sema is already present; |
710 |
return the value from aBlock" |
|
4793 | 711 |
|
712 |
optionalAccessLock notNil ifTrue:[ |
|
713 |
^ optionalAccessLock synchronized:aBlock. |
|
714 |
]. |
|
715 |
^ super synchronized:aBlock. |
|
716 |
||
717 |
"Created: / 02-02-2019 / 19:23:27 / Claus Gittinger" |
|
5009 | 718 |
"Modified (comment): / 07-06-2019 / 13:51:10 / Claus Gittinger" |
4706 | 719 |
! ! |
720 |
||
721 |
!List methodsFor:'setup'! |
|
722 |
||
723 |
beSynchronized |
|
724 |
"make the receiver a synchronized List" |
|
725 |
||
4708 | 726 |
|wasBlocked| |
727 |
||
4706 | 728 |
optionalAccessLock isNil ifTrue:[ |
4708 | 729 |
wasBlocked := OperatingSystem blockInterrupts. |
730 |
optionalAccessLock isNil ifTrue:[ |
|
731 |
"already a registered synchronizationSemaphore for myself in Object? |
|
732 |
- then use it!!" |
|
733 |
optionalAccessLock := SynchronizationSemaphores removeKey:self ifAbsent:[]. |
|
734 |
optionalAccessLock isNil ifTrue:[ |
|
735 |
optionalAccessLock := RecursionLock name:self className. |
|
736 |
]. |
|
737 |
]. |
|
738 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
|
4706 | 739 |
]. |
740 |
||
4708 | 741 |
" |
742 |
|list oldSema| |
|
743 |
||
744 |
list := List new. |
|
745 |
list synchronized:[list add:1]. |
|
746 |
oldSema := list synchronizationSemaphore. |
|
747 |
list beSynchronized. |
|
748 |
self assert:oldSema == list synchronizationSemaphore. |
|
749 |
oldSema |
|
750 |
" |
|
751 |
||
4706 | 752 |
"Created: / 01-08-2018 / 11:44:37 / Claus Gittinger" |
4708 | 753 |
"Modified (comment): / 01-08-2018 / 15:10:45 / Stefan Vogel" |
481 | 754 |
! ! |
755 |
||
881 | 756 |
!List methodsFor:'testing'! |
757 |
||
758 |
isList |
|
759 |
"return true, if the receiver is some kind of list collection; |
|
760 |
true is returned here - the method is redefined from Object." |
|
761 |
||
762 |
^ true |
|
763 |
||
764 |
"Modified: / 11.2.2000 / 01:37:36 / cg" |
|
765 |
! ! |
|
766 |
||
480 | 767 |
!List class methodsFor:'documentation'! |
768 |
||
769 |
version |
|
3836 | 770 |
^ '$Header$' |
4707 | 771 |
! |
772 |
||
773 |
version_CVS |
|
774 |
^ '$Header$' |
|
480 | 775 |
! ! |
2911 | 776 |