author | Claus Gittinger <cg@exept.de> |
Fri, 16 Jan 2009 14:35:44 +0100 | |
changeset 11474 | f4c8118aab2c |
parent 10835 | e23097df1639 |
child 11481 | 9da83b7e8c57 |
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 |
||
10835 | 129 |
|
1 | 130 |
!Bag methodsFor:'adding & removing'! |
131 |
||
54 | 132 |
add:newObject |
345 | 133 |
"add the argument, anObject to the receiver. |
1057 | 134 |
Returns the object. |
135 |
||
136 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 137 |
Iterate over a copy to do this." |
1 | 138 |
|
54 | 139 |
|n| |
140 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
141 |
n := contents at:newObject ifAbsent:0. |
54 | 142 |
contents at:newObject put:(n + 1). |
143 |
^ newObject |
|
1057 | 144 |
|
145 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 146 |
! |
147 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
148 |
add:newObject withOccurrences:anInteger |
345 | 149 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 150 |
Returns the object. |
151 |
||
152 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 153 |
Iterate over a copy to do this." |
1 | 154 |
|
54 | 155 |
|n| |
156 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
157 |
n := contents at:newObject ifAbsent:0. |
54 | 158 |
contents at:newObject put:(n + anInteger). |
1 | 159 |
^ newObject |
1057 | 160 |
|
161 |
"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
|
162 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 163 |
! |
164 |
||
165 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 166 |
"Remove oldObject from the collection. |
167 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 168 |
otherwise return the removed object. |
169 |
||
170 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 171 |
Iterate over a copy to do this." |
1 | 172 |
|
173 |
|count| |
|
174 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
175 |
count := contents at:oldObject ifAbsent:0. |
1 | 176 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
177 |
(count == 1) ifTrue:[ |
|
8873 | 178 |
contents removeKey:oldObject |
179 |
] ifFalse:[ |
|
180 |
contents at:oldObject put:(count - 1) |
|
1 | 181 |
]. |
182 |
^ oldObject |
|
1057 | 183 |
|
184 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 185 |
! |
186 |
||
1110 | 187 |
removeAll |
1164 | 188 |
"remove all objects from the receiver collection (i.e. make it empty). |
189 |
Returns the receiver." |
|
1110 | 190 |
|
191 |
contents := contents species new. |
|
192 |
||
193 |
" |
|
194 |
|b| |
|
195 |
||
196 |
b := Bag new. |
|
197 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 198 |
Transcript showCR:b. |
1110 | 199 |
|
200 |
b removeAll. |
|
201 |
||
1422 | 202 |
Transcript showCR:b |
1110 | 203 |
" |
204 |
||
1164 | 205 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 206 |
! |
207 |
||
345 | 208 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
209 |
"Remove all occurrences of oldObject from the collection. |
|
210 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 211 |
otherwise return the number of removes. |
212 |
||
213 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 214 |
Iterate over a copy to do this." |
345 | 215 |
|
216 |
|count| |
|
217 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
218 |
count := contents at:oldObject ifAbsent:0. |
345 | 219 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
220 |
contents removeKey:oldObject. |
|
221 |
^ oldObject |
|
1057 | 222 |
|
223 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 224 |
! ! |
225 |
||
8860 | 226 |
!Bag methodsFor:'bulk operations'! |
227 |
||
228 |
sum |
|
11474 | 229 |
"sum up all elements - can be done easier, using bags knowledge." |
8860 | 230 |
|
231 |
|accu| |
|
232 |
||
11474 | 233 |
self emptyCheck. |
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 should: [ Bag new sum ] raise:Error. |
246 |
||
247 |
TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10). |
|
8860 | 248 |
" |
249 |
! ! |
|
250 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
251 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
252 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
253 |
= aBag |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
254 |
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
|
255 |
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
|
256 |
self valuesAndCountsDo:[:val :cnt | |
8873 | 257 |
(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
|
258 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
259 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
260 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
261 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
262 |
hash |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
263 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
264 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
265 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
266 |
self valuesAndCountsDo:[:val :cnt | |
8873 | 267 |
h := h + cnt hash. |
6735
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 |
^ h |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
270 |
! ! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
271 |
|
609 | 272 |
!Bag methodsFor:'converting'! |
273 |
||
274 |
asBag |
|
275 |
"return the receiver as a bag" |
|
276 |
||
277 |
"could be an instance of a subclass..." |
|
278 |
self class == Bag ifTrue:[ |
|
279 |
^ self |
|
280 |
]. |
|
281 |
^ super asBag |
|
7161 | 282 |
! |
283 |
||
284 |
asSet |
|
285 |
"return the receiver as a set" |
|
286 |
||
287 |
^ contents keys copy |
|
288 |
||
289 |
" |
|
290 |
|b| |
|
291 |
||
292 |
b := Bag new. |
|
293 |
b add:1; add:2; add:3; add:1; add:1. |
|
294 |
b asSet. |
|
295 |
" |
|
609 | 296 |
! ! |
297 |
||
298 |
!Bag methodsFor:'copying'! |
|
299 |
||
300 |
postCopy |
|
301 |
"must copy the contents as well" |
|
302 |
||
303 |
contents := contents copy |
|
304 |
! ! |
|
305 |
||
1 | 306 |
!Bag methodsFor:'enumerating'! |
307 |
||
308 |
do:aBlock |
|
1057 | 309 |
"evaluate the block for all elements in the collection. |
310 |
||
311 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 312 |
Iterate over a copy to do this." |
1 | 313 |
|
54 | 314 |
contents keysAndValuesDo:[:key :value| |
8873 | 315 |
value timesRepeat:[ |
316 |
aBlock value:key |
|
317 |
] |
|
1 | 318 |
] |
1057 | 319 |
|
320 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 321 |
! |
322 |
||
10835 | 323 |
valuesAndCounts |
324 |
"return an orderedCollection containing value->count assocuations" |
|
325 |
||
326 |
|coll| |
|
327 |
||
328 |
coll := OrderedCollection new. |
|
329 |
self valuesAndCountsDo:[:value :count | coll add:(value->count)]. |
|
330 |
^ coll |
|
331 |
||
332 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
333 |
! |
|
334 |
||
345 | 335 |
valuesAndCountsDo:aTwoArgBlock |
336 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 337 |
passing both the element and the occurrence count as arguments. |
1057 | 338 |
|
339 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 340 |
Iterate over a copy to do this." |
345 | 341 |
|
342 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 343 |
|
344 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 345 |
! ! |
609 | 346 |
|
347 |
!Bag methodsFor:'private'! |
|
348 |
||
349 |
initContents |
|
350 |
"set the contents to be an empty Dictionary" |
|
351 |
||
352 |
contents := Dictionary new |
|
353 |
! |
|
354 |
||
355 |
initContents:size |
|
356 |
"set the contents to be an empty Dictionary with initial size" |
|
357 |
||
358 |
contents := Dictionary new:size |
|
359 |
! |
|
360 |
||
5375 | 361 |
initContentsForIdentity |
362 |
"set the contents to be an empty IdentityDictionary" |
|
363 |
||
364 |
contents := IdentityDictionary new |
|
365 |
! |
|
366 |
||
609 | 367 |
initContentsForIdentity:size |
368 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
369 |
||
370 |
contents := IdentityDictionary new:size |
|
371 |
! ! |
|
372 |
||
5557 | 373 |
!Bag methodsFor:'queries'! |
374 |
||
375 |
size |
|
376 |
"return the number of bag elements" |
|
377 |
||
378 |
|count| |
|
379 |
||
380 |
count := 0. |
|
381 |
contents do:[:element | count := count + element]. |
|
382 |
^ count |
|
383 |
! ! |
|
384 |
||
609 | 385 |
!Bag methodsFor:'testing'! |
386 |
||
387 |
includes:anObject |
|
388 |
"return true, if anObject is in the receiver" |
|
389 |
||
390 |
^ contents includesKey:anObject |
|
391 |
! |
|
392 |
||
393 |
occurrencesOf:anObject |
|
394 |
"return how many times anObject is in the receiver" |
|
395 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
396 |
^ contents at:anObject ifAbsent:0 |
609 | 397 |
! ! |
398 |
||
633 | 399 |
!Bag class methodsFor:'documentation'! |
400 |
||
401 |
version |
|
11474 | 402 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.35 2009-01-16 13:35:44 cg Exp $' |
633 | 403 |
! ! |