author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 11523 | d47e76cf572d |
child 17732 | a1892eeca6c0 |
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 |
||
345 | 68 |
equalityNew:size |
69 |
"return a new empty Bag with initial space for size elements. |
|
70 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 71 |
|
5558 | 72 |
^ self basicNew initContents:size |
345 | 73 |
! |
74 |
||
5375 | 75 |
identityNew |
76 |
"return a new empty Bag which compares for identity |
|
77 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
78 |
||
79 |
^ super new initContentsForIdentity |
|
80 |
! |
|
81 |
||
345 | 82 |
identityNew:size |
83 |
"return a new empty Bag with initial space for size elements. |
|
84 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
85 |
||
5558 | 86 |
^ self basicNew initContentsForIdentity:size |
1 | 87 |
! |
88 |
||
609 | 89 |
new |
90 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 91 |
|
5558 | 92 |
^ self basicNew initContents |
345 | 93 |
! |
94 |
||
609 | 95 |
new:size |
96 |
"return a new empty Bag with initial space for size elements. |
|
97 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 98 |
|
609 | 99 |
^ self equalityNew:size |
1 | 100 |
! ! |
101 |
||
7261 | 102 |
!Bag methodsFor:'Compatibility-Dolphin'! |
6411 | 103 |
|
104 |
asAssociations |
|
105 |
^ contents |
|
106 |
! ! |
|
107 |
||
1 | 108 |
!Bag methodsFor:'accessing'! |
109 |
||
110 |
at:index |
|
111 |
"report an error: at: is not allowed for Bags" |
|
112 |
||
113 |
^ self errorNotKeyed |
|
114 |
! |
|
115 |
||
116 |
at:index put:anObject |
|
117 |
"report an error: at:put: is not allowed for Bags" |
|
118 |
||
119 |
^ self errorNotKeyed |
|
345 | 120 |
! |
121 |
||
122 |
contents |
|
123 |
"return the dictionary which associates occurrence-counts |
|
124 |
to the bags elements." |
|
125 |
||
126 |
^ contents |
|
1 | 127 |
! ! |
128 |
||
129 |
!Bag methodsFor:'adding & removing'! |
|
130 |
||
54 | 131 |
add:newObject |
345 | 132 |
"add the argument, anObject to the receiver. |
1057 | 133 |
Returns the object. |
134 |
||
135 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 136 |
Iterate over a copy to do this." |
1 | 137 |
|
54 | 138 |
|n| |
139 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
140 |
n := contents at:newObject ifAbsent:0. |
54 | 141 |
contents at:newObject put:(n + 1). |
142 |
^ newObject |
|
1057 | 143 |
|
144 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 145 |
! |
146 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
147 |
add:newObject withOccurrences:anInteger |
345 | 148 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 149 |
Returns the object. |
150 |
||
151 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 152 |
Iterate over a copy to do this." |
1 | 153 |
|
54 | 154 |
|n| |
155 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
156 |
n := contents at:newObject ifAbsent:0. |
54 | 157 |
contents at:newObject put:(n + anInteger). |
1 | 158 |
^ newObject |
1057 | 159 |
|
160 |
"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
|
161 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 162 |
! |
163 |
||
164 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 165 |
"Remove oldObject from the collection. |
166 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 167 |
otherwise return the removed 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 |
|
172 |
|count| |
|
173 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
174 |
count := contents at:oldObject ifAbsent:0. |
1 | 175 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
176 |
(count == 1) ifTrue:[ |
|
8873 | 177 |
contents removeKey:oldObject |
178 |
] ifFalse:[ |
|
179 |
contents at:oldObject put:(count - 1) |
|
1 | 180 |
]. |
181 |
^ oldObject |
|
1057 | 182 |
|
183 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 184 |
! |
185 |
||
1110 | 186 |
removeAll |
1164 | 187 |
"remove all objects from the receiver collection (i.e. make it empty). |
188 |
Returns the receiver." |
|
1110 | 189 |
|
190 |
contents := contents species new. |
|
191 |
||
192 |
" |
|
193 |
|b| |
|
194 |
||
195 |
b := Bag new. |
|
196 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 197 |
Transcript showCR:b. |
1110 | 198 |
|
199 |
b removeAll. |
|
200 |
||
1422 | 201 |
Transcript showCR:b |
1110 | 202 |
" |
203 |
||
1164 | 204 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 205 |
! |
206 |
||
345 | 207 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
208 |
"Remove all occurrences of oldObject from the collection. |
|
209 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 210 |
otherwise return the number of removes. |
211 |
||
212 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 213 |
Iterate over a copy to do this." |
345 | 214 |
|
215 |
|count| |
|
216 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
217 |
count := contents at:oldObject ifAbsent:0. |
345 | 218 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
219 |
contents removeKey:oldObject. |
|
220 |
^ oldObject |
|
1057 | 221 |
|
222 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 223 |
! ! |
224 |
||
8860 | 225 |
!Bag methodsFor:'bulk operations'! |
226 |
||
227 |
sum |
|
11522 | 228 |
"sum up all elements; return 0 for an empty collection. |
229 |
can be done easier, using bags knowledge." |
|
8860 | 230 |
|
231 |
|accu| |
|
232 |
||
11522 | 233 |
self isEmpty ifTrue:[ ^ 0 ]. |
11474 | 234 |
|
235 |
accu := nil. |
|
236 |
self |
|
237 |
valuesAndCountsDo:[:n :count | |
|
238 |
|thisAmount| |
|
239 |
||
240 |
thisAmount := count * n. |
|
241 |
accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ]. |
|
8860 | 242 |
^ accu |
243 |
||
244 |
" |
|
11474 | 245 |
TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10). |
8860 | 246 |
" |
247 |
! ! |
|
248 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
249 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
250 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
251 |
= aBag |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
252 |
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
|
253 |
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
|
254 |
self valuesAndCountsDo:[:val :cnt | |
8873 | 255 |
(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
|
256 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
257 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
258 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
259 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
260 |
hash |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
261 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
262 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
263 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
264 |
self valuesAndCountsDo:[:val :cnt | |
8873 | 265 |
h := h + cnt hash. |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
266 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
267 |
^ h |
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 |
|
609 | 270 |
!Bag methodsFor:'converting'! |
271 |
||
272 |
asBag |
|
273 |
"return the receiver as a bag" |
|
274 |
||
275 |
"could be an instance of a subclass..." |
|
276 |
self class == Bag ifTrue:[ |
|
277 |
^ self |
|
278 |
]. |
|
279 |
^ super asBag |
|
7161 | 280 |
! |
281 |
||
282 |
asSet |
|
283 |
"return the receiver as a set" |
|
284 |
||
285 |
^ contents keys copy |
|
286 |
||
287 |
" |
|
288 |
|b| |
|
289 |
||
290 |
b := Bag new. |
|
291 |
b add:1; add:2; add:3; add:1; add:1. |
|
292 |
b asSet. |
|
293 |
" |
|
609 | 294 |
! ! |
295 |
||
296 |
!Bag methodsFor:'copying'! |
|
297 |
||
298 |
postCopy |
|
299 |
"must copy the contents as well" |
|
300 |
||
301 |
contents := contents copy |
|
302 |
! ! |
|
303 |
||
1 | 304 |
!Bag methodsFor:'enumerating'! |
305 |
||
306 |
do:aBlock |
|
1057 | 307 |
"evaluate the block for all elements in the collection. |
308 |
||
309 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 310 |
Iterate over a copy to do this." |
1 | 311 |
|
54 | 312 |
contents keysAndValuesDo:[:key :value| |
8873 | 313 |
value timesRepeat:[ |
314 |
aBlock value:key |
|
315 |
] |
|
1 | 316 |
] |
1057 | 317 |
|
318 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 319 |
! |
320 |
||
10835 | 321 |
valuesAndCounts |
11481 | 322 |
"return an orderedCollection containing value->count associations" |
10835 | 323 |
|
324 |
|coll| |
|
325 |
||
326 |
coll := OrderedCollection new. |
|
327 |
self valuesAndCountsDo:[:value :count | coll add:(value->count)]. |
|
328 |
^ coll |
|
329 |
||
330 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
331 |
! |
|
332 |
||
345 | 333 |
valuesAndCountsDo:aTwoArgBlock |
334 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 335 |
passing both the element and the occurrence count as arguments. |
1057 | 336 |
|
337 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 338 |
Iterate over a copy to do this." |
345 | 339 |
|
340 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 341 |
|
342 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 343 |
! ! |
609 | 344 |
|
345 |
!Bag methodsFor:'private'! |
|
346 |
||
347 |
initContents |
|
348 |
"set the contents to be an empty Dictionary" |
|
349 |
||
350 |
contents := Dictionary new |
|
351 |
! |
|
352 |
||
353 |
initContents:size |
|
354 |
"set the contents to be an empty Dictionary with initial size" |
|
355 |
||
356 |
contents := Dictionary new:size |
|
357 |
! |
|
358 |
||
5375 | 359 |
initContentsForIdentity |
360 |
"set the contents to be an empty IdentityDictionary" |
|
361 |
||
362 |
contents := IdentityDictionary new |
|
363 |
! |
|
364 |
||
609 | 365 |
initContentsForIdentity:size |
366 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
367 |
||
368 |
contents := IdentityDictionary new:size |
|
369 |
! ! |
|
370 |
||
5557 | 371 |
!Bag methodsFor:'queries'! |
372 |
||
373 |
size |
|
374 |
"return the number of bag elements" |
|
375 |
||
376 |
|count| |
|
377 |
||
378 |
count := 0. |
|
379 |
contents do:[:element | count := count + element]. |
|
380 |
^ count |
|
381 |
! ! |
|
382 |
||
609 | 383 |
!Bag methodsFor:'testing'! |
384 |
||
385 |
includes:anObject |
|
386 |
"return true, if anObject is in the receiver" |
|
387 |
||
388 |
^ contents includesKey:anObject |
|
389 |
! |
|
390 |
||
391 |
occurrencesOf:anObject |
|
392 |
"return how many times anObject is in the receiver" |
|
393 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
394 |
^ contents at:anObject ifAbsent:0 |
609 | 395 |
! ! |
396 |
||
633 | 397 |
!Bag class methodsFor:'documentation'! |
398 |
||
399 |
version |
|
17711 | 400 |
^ '$Id: Bag.st 10447 2009-06-14 13:09:55Z vranyj1 $' |
633 | 401 |
! ! |