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