author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2013 13:23:48 +0200 | |
changeset 15429 | 087ad8e8da0c |
parent 14956 | bda5a86c5525 |
child 15457 | af0513df339f |
child 18070 | d262e3aecaca |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1991 by Claus Gittinger |
345 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5375 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Collection subclass:#Bag |
1057 | 15 |
instanceVariableNames:'contents' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Unordered' |
|
1 | 19 |
! |
20 |
||
54 | 21 |
!Bag class methodsFor:'documentation'! |
22 |
||
88 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
345 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
54 | 37 |
documentation |
38 |
" |
|
88 | 39 |
Bag implements collections whose elements are unordered and have no |
154 | 40 |
external key. Elements may occur more than once in a bag. There is no defined |
8873 | 41 |
order within a bag. |
42 |
The default implementation uses a dictionary to store each objects occurrence |
|
43 |
count, using the object itself as key (i.e. using = and hash for inclusion |
|
345 | 44 |
tests). |
399 | 45 |
|
345 | 46 |
There is also an instance creation variant (#identityNew:) creating a |
47 |
bag which compares using #== and hashes using #identityHash. |
|
8873 | 48 |
(I'd say that an IdentityBag was a better thing to implement ... |
399 | 49 |
... but for compatibility ... we do it here as well) |
1 | 50 |
|
1283 | 51 |
[Instance variables:] |
52 |
||
8873 | 53 |
contents <Dictionary> for each element, the number of occurrences |
1283 | 54 |
|
1 | 55 |
|
1290 | 56 |
[author:] |
8873 | 57 |
Claus Gittinger |
1290 | 58 |
|
1283 | 59 |
[See also:] |
8873 | 60 |
Set IdentitySet |
61 |
Dictionary IdentityDictionary |
|
62 |
OrderedCollection Array |
|
54 | 63 |
" |
64 |
! ! |
|
1 | 65 |
|
66 |
!Bag class methodsFor:'instance creation'! |
|
67 |
||
14956 | 68 |
contentsClass |
69 |
"the default class to use for the underlying contents array, |
|
70 |
used when instantiated with new/new:" |
|
71 |
||
72 |
^ Dictionary |
|
73 |
! |
|
74 |
||
75 |
equalityNew |
|
76 |
"return a new empty Bag. |
|
77 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
78 |
||
79 |
^ self basicNew initContentsForEquality |
|
80 |
! |
|
81 |
||
345 | 82 |
equalityNew:size |
83 |
"return a new empty Bag with initial space for size elements. |
|
84 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 85 |
|
14956 | 86 |
^ self basicNew initContentsForEquality:size |
345 | 87 |
! |
88 |
||
5375 | 89 |
identityNew |
14956 | 90 |
"return a new empty Identity-Bag. |
5375 | 91 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
92 |
||
14956 | 93 |
^ self basicNew initContentsForIdentity |
5375 | 94 |
! |
95 |
||
345 | 96 |
identityNew:size |
97 |
"return a new empty Bag with initial space for size elements. |
|
98 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
99 |
||
5558 | 100 |
^ self basicNew initContentsForIdentity:size |
1 | 101 |
! |
102 |
||
609 | 103 |
new |
104 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 105 |
|
5558 | 106 |
^ self basicNew initContents |
345 | 107 |
! |
108 |
||
609 | 109 |
new:size |
110 |
"return a new empty Bag with initial space for size elements. |
|
111 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 112 |
|
609 | 113 |
^ self equalityNew:size |
1 | 114 |
! ! |
115 |
||
7261 | 116 |
!Bag methodsFor:'Compatibility-Dolphin'! |
6411 | 117 |
|
118 |
asAssociations |
|
11935 | 119 |
"return the dictionary which associates occurrence-counts |
120 |
to the bags elements. |
|
121 |
Same as #contents for dolphin compatibility." |
|
122 |
||
14685 | 123 |
^ self contents |
6411 | 124 |
! ! |
125 |
||
1 | 126 |
!Bag methodsFor:'accessing'! |
127 |
||
128 |
at:index |
|
129 |
"report an error: at: is not allowed for Bags" |
|
130 |
||
131 |
^ self errorNotKeyed |
|
132 |
! |
|
133 |
||
134 |
at:index put:anObject |
|
135 |
"report an error: at:put: is not allowed for Bags" |
|
136 |
||
137 |
^ self errorNotKeyed |
|
345 | 138 |
! |
139 |
||
140 |
contents |
|
141 |
"return the dictionary which associates occurrence-counts |
|
142 |
to the bags elements." |
|
143 |
||
144 |
^ contents |
|
1 | 145 |
! ! |
146 |
||
147 |
!Bag methodsFor:'adding & removing'! |
|
148 |
||
54 | 149 |
add:newObject |
345 | 150 |
"add the argument, anObject to the receiver. |
14896 | 151 |
Returns the object (sigh). |
1057 | 152 |
|
153 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
14896 | 154 |
Iterate over a copy to do this." |
1 | 155 |
|
54 | 156 |
|n| |
157 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
158 |
n := contents at:newObject ifAbsent:0. |
54 | 159 |
contents at:newObject put:(n + 1). |
160 |
^ newObject |
|
1057 | 161 |
|
162 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 163 |
! |
164 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
165 |
add:newObject withOccurrences:anInteger |
345 | 166 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 167 |
Returns the object. |
168 |
||
169 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 170 |
Iterate over a copy to do this." |
1 | 171 |
|
54 | 172 |
|n| |
173 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
174 |
n := contents at:newObject ifAbsent:0. |
54 | 175 |
contents at:newObject put:(n + anInteger). |
1 | 176 |
^ newObject |
1057 | 177 |
|
178 |
"Modified: 1.3.1996 / 21:43:12 / cg" |
|
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
179 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 180 |
! |
181 |
||
182 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 183 |
"Remove oldObject from the collection. |
184 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 185 |
otherwise return the removed object. |
186 |
||
187 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 188 |
Iterate over a copy to do this." |
1 | 189 |
|
190 |
|count| |
|
191 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
192 |
count := contents at:oldObject ifAbsent:0. |
1 | 193 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
194 |
(count == 1) ifTrue:[ |
|
8873 | 195 |
contents removeKey:oldObject |
196 |
] ifFalse:[ |
|
197 |
contents at:oldObject put:(count - 1) |
|
1 | 198 |
]. |
199 |
^ oldObject |
|
1057 | 200 |
|
201 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 202 |
! |
203 |
||
1110 | 204 |
removeAll |
1164 | 205 |
"remove all objects from the receiver collection (i.e. make it empty). |
206 |
Returns the receiver." |
|
1110 | 207 |
|
208 |
contents := contents species new. |
|
209 |
||
210 |
" |
|
211 |
|b| |
|
212 |
||
213 |
b := Bag new. |
|
214 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 215 |
Transcript showCR:b. |
1110 | 216 |
|
217 |
b removeAll. |
|
218 |
||
1422 | 219 |
Transcript showCR:b |
1110 | 220 |
" |
221 |
||
1164 | 222 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 223 |
! |
224 |
||
345 | 225 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
226 |
"Remove all occurrences of oldObject from the collection. |
|
227 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 228 |
otherwise return the number of removes. |
229 |
||
230 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 231 |
Iterate over a copy to do this." |
345 | 232 |
|
233 |
|count| |
|
234 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
235 |
count := contents at:oldObject ifAbsent:0. |
345 | 236 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
237 |
contents removeKey:oldObject. |
|
238 |
^ oldObject |
|
1057 | 239 |
|
240 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 241 |
! ! |
242 |
||
8860 | 243 |
!Bag methodsFor:'bulk operations'! |
244 |
||
245 |
sum |
|
11522 | 246 |
"sum up all elements; return 0 for an empty collection. |
247 |
can be done easier, using bags knowledge." |
|
8860 | 248 |
|
249 |
|accu| |
|
250 |
||
11522 | 251 |
self isEmpty ifTrue:[ ^ 0 ]. |
11474 | 252 |
|
253 |
accu := nil. |
|
254 |
self |
|
255 |
valuesAndCountsDo:[:n :count | |
|
256 |
|thisAmount| |
|
257 |
||
258 |
thisAmount := count * n. |
|
259 |
accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ]. |
|
8860 | 260 |
^ accu |
261 |
||
262 |
" |
|
11474 | 263 |
TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10). |
8860 | 264 |
" |
265 |
! ! |
|
266 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
267 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
268 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
269 |
= aBag |
11935 | 270 |
"Compare the receiver with the argument and return true if the |
271 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
272 |
Otherwise return false." |
|
273 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
274 |
aBag species == self species ifFalse:[^ false]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
275 |
self size == aBag size ifFalse:[^ false]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
276 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 277 |
(aBag occurrencesOf:val) == cnt ifFalse:[^ false] |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
278 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
279 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
280 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
281 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
282 |
hash |
11935 | 283 |
"return an integer useful for hashing on the receiver; |
284 |
redefined since = is redefined here." |
|
285 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
286 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
287 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
288 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
289 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 290 |
h := h + cnt hash. |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
291 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
292 |
^ h |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
293 |
! ! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
294 |
|
609 | 295 |
!Bag methodsFor:'converting'! |
296 |
||
297 |
asBag |
|
298 |
"return the receiver as a bag" |
|
299 |
||
300 |
"could be an instance of a subclass..." |
|
301 |
self class == Bag ifTrue:[ |
|
302 |
^ self |
|
303 |
]. |
|
304 |
^ super asBag |
|
7161 | 305 |
! |
306 |
||
307 |
asSet |
|
308 |
"return the receiver as a set" |
|
309 |
||
310 |
^ contents keys copy |
|
311 |
||
312 |
" |
|
313 |
|b| |
|
314 |
||
315 |
b := Bag new. |
|
316 |
b add:1; add:2; add:3; add:1; add:1. |
|
317 |
b asSet. |
|
318 |
" |
|
609 | 319 |
! ! |
320 |
||
321 |
!Bag methodsFor:'copying'! |
|
322 |
||
323 |
postCopy |
|
324 |
"must copy the contents as well" |
|
325 |
||
326 |
contents := contents copy |
|
327 |
! ! |
|
328 |
||
1 | 329 |
!Bag methodsFor:'enumerating'! |
330 |
||
331 |
do:aBlock |
|
1057 | 332 |
"evaluate the block for all elements in the collection. |
333 |
||
334 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 335 |
Iterate over a copy to do this." |
1 | 336 |
|
54 | 337 |
contents keysAndValuesDo:[:key :value| |
8873 | 338 |
value timesRepeat:[ |
339 |
aBlock value:key |
|
340 |
] |
|
1 | 341 |
] |
1057 | 342 |
|
343 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 344 |
! |
345 |
||
10835 | 346 |
valuesAndCounts |
11481 | 347 |
"return an orderedCollection containing value->count associations" |
10835 | 348 |
|
349 |
|coll| |
|
350 |
||
351 |
coll := OrderedCollection new. |
|
352 |
self valuesAndCountsDo:[:value :count | coll add:(value->count)]. |
|
353 |
^ coll |
|
354 |
||
355 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
356 |
! |
|
357 |
||
345 | 358 |
valuesAndCountsDo:aTwoArgBlock |
359 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 360 |
passing both the element and the occurrence count as arguments. |
1057 | 361 |
|
362 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 363 |
Iterate over a copy to do this." |
345 | 364 |
|
365 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 366 |
|
367 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 368 |
! ! |
609 | 369 |
|
12823 | 370 |
!Bag methodsFor:'printing & storing'! |
371 |
||
372 |
printElementsDo:aBlock |
|
14685 | 373 |
self valuesAndCountsDo:[:value :count| |
374 |
aBlock value:('%1(*%2)' bindWith:value with:count). |
|
12823 | 375 |
] |
376 |
! ! |
|
377 |
||
609 | 378 |
!Bag methodsFor:'private'! |
379 |
||
380 |
initContents |
|
14956 | 381 |
"set the contents to be an empty Dictionary. |
382 |
This is the default method for initialization, which can be redefined in subclasses." |
|
383 |
||
384 |
contents := self class contentsClass new |
|
385 |
! |
|
386 |
||
387 |
initContents:size |
|
388 |
"set the contents to be an empty Dictionary with initial size. |
|
389 |
This is the default method for initialization, which can be redefined in subclasses." |
|
390 |
||
391 |
contents := self class contentsClass new: size |
|
392 |
! |
|
393 |
||
394 |
initContentsForEquality |
|
609 | 395 |
"set the contents to be an empty Dictionary" |
396 |
||
397 |
contents := Dictionary new |
|
398 |
! |
|
399 |
||
14956 | 400 |
initContentsForEquality:size |
609 | 401 |
"set the contents to be an empty Dictionary with initial size" |
402 |
||
403 |
contents := Dictionary new:size |
|
404 |
! |
|
405 |
||
5375 | 406 |
initContentsForIdentity |
407 |
"set the contents to be an empty IdentityDictionary" |
|
408 |
||
409 |
contents := IdentityDictionary new |
|
410 |
! |
|
411 |
||
609 | 412 |
initContentsForIdentity:size |
413 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
414 |
||
415 |
contents := IdentityDictionary new:size |
|
416 |
! ! |
|
417 |
||
5557 | 418 |
!Bag methodsFor:'queries'! |
419 |
||
420 |
size |
|
421 |
"return the number of bag elements" |
|
422 |
||
423 |
|count| |
|
424 |
||
425 |
count := 0. |
|
426 |
contents do:[:element | count := count + element]. |
|
427 |
^ count |
|
428 |
! ! |
|
429 |
||
609 | 430 |
!Bag methodsFor:'testing'! |
431 |
||
432 |
includes:anObject |
|
433 |
"return true, if anObject is in the receiver" |
|
434 |
||
14685 | 435 |
^ (self occurrencesOf:anObject) > 0 |
609 | 436 |
! |
437 |
||
15429 | 438 |
isFixedSize |
439 |
"return true if the receiver cannot grow" |
|
440 |
||
441 |
^ false |
|
442 |
! |
|
443 |
||
609 | 444 |
occurrencesOf:anObject |
445 |
"return how many times anObject is in the receiver" |
|
446 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
447 |
^ contents at:anObject ifAbsent:0 |
609 | 448 |
! ! |
449 |
||
633 | 450 |
!Bag class methodsFor:'documentation'! |
451 |
||
452 |
version |
|
15429 | 453 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.45 2013-06-25 11:23:48 cg Exp $' |
12823 | 454 |
! |
455 |
||
456 |
version_CVS |
|
15429 | 457 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.45 2013-06-25 11:23:48 cg Exp $' |
633 | 458 |
! ! |
14685 | 459 |