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