author | Claus Gittinger <cg@exept.de> |
Wed, 26 Jun 2019 22:06:12 +0200 | |
changeset 24369 | e796791ea838 |
parent 24181 | fbc74b464089 |
child 24621 | 10d24a1c67e4 |
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 |
||
18468 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
Collection subclass:#Bag |
1057 | 17 |
instanceVariableNames:'contents' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Unordered' |
|
1 | 21 |
! |
22 |
||
54 | 23 |
!Bag class methodsFor:'documentation'! |
24 |
||
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
345 | 28 |
All Rights Reserved |
88 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
54 | 39 |
documentation |
40 |
" |
|
20628 | 41 |
Bags are collections where the elements are unordered and have no external key. |
42 |
Elements may occur more than once in a bag. |
|
43 |
There is no defined order within a bag. |
|
44 |
The default implementation uses a dictionary to store each object's occurrence count, |
|
45 |
using the object itself as key |
|
46 |
(i.e. using = and hash for inclusion tests). |
|
399 | 47 |
|
20628 | 48 |
There is also an instance creation variant (#identityNew:) which creats a |
345 | 49 |
bag which compares using #== and hashes using #identityHash. |
20628 | 50 |
(I'd say that instantiating an IdentityBag explicitly is better, |
399 | 51 |
... but for compatibility ... we do it here as well) |
1 | 52 |
|
1283 | 53 |
[Instance variables:] |
54 |
||
20628 | 55 |
contents <Dictionary> for each element, the number of occurrences |
1283 | 56 |
|
1 | 57 |
|
1290 | 58 |
[author:] |
20628 | 59 |
Claus Gittinger |
1290 | 60 |
|
1283 | 61 |
[See also:] |
20628 | 62 |
Set IdentitySet |
63 |
Dictionary IdentityDictionary |
|
64 |
OrderedCollection Array |
|
54 | 65 |
" |
66 |
! ! |
|
1 | 67 |
|
68 |
!Bag class methodsFor:'instance creation'! |
|
69 |
||
14956 | 70 |
contentsClass |
71 |
"the default class to use for the underlying contents array, |
|
72 |
used when instantiated with new/new:" |
|
73 |
||
74 |
^ Dictionary |
|
75 |
! |
|
76 |
||
77 |
equalityNew |
|
78 |
"return a new empty Bag. |
|
79 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
80 |
||
81 |
^ self basicNew initContentsForEquality |
|
82 |
! |
|
83 |
||
345 | 84 |
equalityNew:size |
85 |
"return a new empty Bag with initial space for size elements. |
|
86 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 87 |
|
14956 | 88 |
^ self basicNew initContentsForEquality:size |
345 | 89 |
! |
90 |
||
5375 | 91 |
identityNew |
14956 | 92 |
"return a new empty Identity-Bag. |
5375 | 93 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
94 |
||
14956 | 95 |
^ self basicNew initContentsForIdentity |
5375 | 96 |
! |
97 |
||
345 | 98 |
identityNew:size |
99 |
"return a new empty Bag with initial space for size elements. |
|
100 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
101 |
||
5558 | 102 |
^ self basicNew initContentsForIdentity:size |
1 | 103 |
! |
104 |
||
609 | 105 |
new |
106 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 107 |
|
5558 | 108 |
^ self basicNew initContents |
345 | 109 |
! |
110 |
||
609 | 111 |
new:size |
112 |
"return a new empty Bag with initial space for size elements. |
|
113 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 114 |
|
609 | 115 |
^ self equalityNew:size |
22308 | 116 |
! |
117 |
||
118 |
newWithCapacity:size |
|
119 |
"return a new empty Collection with capacity for n elements." |
|
120 |
||
121 |
^ self new:size |
|
122 |
||
123 |
"Created: / 10-10-2017 / 17:49:15 / stefan" |
|
1 | 124 |
! ! |
125 |
||
7261 | 126 |
!Bag methodsFor:'Compatibility-Dolphin'! |
6411 | 127 |
|
128 |
asAssociations |
|
11935 | 129 |
"return the dictionary which associates occurrence-counts |
130 |
to the bags elements. |
|
131 |
Same as #contents for dolphin compatibility." |
|
132 |
||
14685 | 133 |
^ self contents |
6411 | 134 |
! ! |
135 |
||
1 | 136 |
!Bag methodsFor:'accessing'! |
137 |
||
138 |
at:index |
|
139 |
"report an error: at: is not allowed for Bags" |
|
140 |
||
141 |
^ self errorNotKeyed |
|
142 |
! |
|
143 |
||
144 |
at:index put:anObject |
|
145 |
"report an error: at:put: is not allowed for Bags" |
|
146 |
||
147 |
^ self errorNotKeyed |
|
345 | 148 |
! |
149 |
||
150 |
contents |
|
151 |
"return the dictionary which associates occurrence-counts |
|
152 |
to the bags elements." |
|
153 |
||
154 |
^ contents |
|
15732 | 155 |
|
156 |
" |
|
157 |
Bag new |
|
158 |
add:'abc'; |
|
159 |
add:'def'; |
|
160 |
add:'ghi'; |
|
161 |
add:'abc'; |
|
162 |
add:'def'; |
|
163 |
add:'abc'; |
|
164 |
add:'abc'; |
|
165 |
contents |
|
166 |
" |
|
15686 | 167 |
! |
168 |
||
169 |
sortedCounts |
|
15732 | 170 |
"Answer with a collection of counts associated to elements, sorted by decreasing count." |
15686 | 171 |
"Suggested by l. Uzonyi" |
172 |
||
173 |
^ (Array new:contents size |
|
174 |
streamContents:[:stream | |
|
175 |
contents associationsDo:[:each | stream nextPut:each value -> each key ] |
|
176 |
]) |
|
16766 | 177 |
sort:[:x :y | y < x]; |
15686 | 178 |
yourself |
15731 | 179 |
|
180 |
" |
|
181 |
Bag new |
|
182 |
add:'abc'; |
|
183 |
add:'def'; |
|
184 |
add:'ghi'; |
|
185 |
add:'abc'; |
|
186 |
add:'def'; |
|
187 |
add:'abc'; |
|
188 |
add:'abc'; |
|
189 |
sortedCounts |
|
190 |
" |
|
16098 | 191 |
! |
192 |
||
193 |
valuesSortedByCounts |
|
194 |
"Answer with a collection of values, sorted by decreasing count. |
|
195 |
Count informtion is lost in the result" |
|
196 |
||
197 |
^ self sortedCounts collect:[:assoc | assoc value]. |
|
198 |
||
199 |
" |
|
200 |
Bag new |
|
201 |
add:'abc'; |
|
202 |
add:'def'; |
|
203 |
add:'ghi'; |
|
204 |
add:'abc'; |
|
205 |
add:'def'; |
|
206 |
add:'abc'; |
|
207 |
add:'abc'; |
|
208 |
valuesSortedByCounts |
|
209 |
" |
|
1 | 210 |
! ! |
211 |
||
212 |
!Bag methodsFor:'adding & removing'! |
|
213 |
||
54 | 214 |
add:newObject |
345 | 215 |
"add the argument, anObject to the receiver. |
14896 | 216 |
Returns the object (sigh). |
1057 | 217 |
|
218 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
14896 | 219 |
Iterate over a copy to do this." |
1 | 220 |
|
54 | 221 |
|n| |
222 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
223 |
n := contents at:newObject ifAbsent:0. |
54 | 224 |
contents at:newObject put:(n + 1). |
225 |
^ newObject |
|
1057 | 226 |
|
227 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 228 |
! |
229 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
230 |
add:newObject withOccurrences:anInteger |
345 | 231 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 232 |
Returns the object. |
233 |
||
234 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 235 |
Iterate over a copy to do this." |
1 | 236 |
|
54 | 237 |
|n| |
238 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
239 |
n := contents at:newObject ifAbsent:0. |
54 | 240 |
contents at:newObject put:(n + anInteger). |
1 | 241 |
^ newObject |
1057 | 242 |
|
243 |
"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
|
244 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 245 |
! |
246 |
||
247 |
remove:oldObject ifAbsent:anExceptionBlock |
|
19081 | 248 |
"Remove one occurrence of oldObject from the collection. |
345 | 249 |
If it was not present, return the value of the exceptionBlock; |
1057 | 250 |
otherwise return the removed object. |
251 |
||
252 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
19081 | 253 |
Iterate over a copy to do this." |
1 | 254 |
|
255 |
|count| |
|
256 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
257 |
count := contents at:oldObject ifAbsent:0. |
1 | 258 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
259 |
(count == 1) ifTrue:[ |
|
19081 | 260 |
contents removeKey:oldObject |
8873 | 261 |
] ifFalse:[ |
19081 | 262 |
contents at:oldObject put:(count - 1) |
1 | 263 |
]. |
264 |
^ oldObject |
|
1057 | 265 |
|
266 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 267 |
! |
268 |
||
1110 | 269 |
removeAll |
1164 | 270 |
"remove all objects from the receiver collection (i.e. make it empty). |
271 |
Returns the receiver." |
|
1110 | 272 |
|
273 |
contents := contents species new. |
|
274 |
||
275 |
" |
|
276 |
|b| |
|
277 |
||
278 |
b := Bag new. |
|
279 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 280 |
Transcript showCR:b. |
1110 | 281 |
|
282 |
b removeAll. |
|
283 |
||
1422 | 284 |
Transcript showCR:b |
1110 | 285 |
" |
286 |
||
1164 | 287 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 288 |
! |
289 |
||
345 | 290 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
291 |
"Remove all occurrences of oldObject from the collection. |
|
292 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 293 |
otherwise return the number of removes. |
294 |
||
295 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 296 |
Iterate over a copy to do this." |
345 | 297 |
|
298 |
|count| |
|
299 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
300 |
count := contents at:oldObject ifAbsent:0. |
345 | 301 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
302 |
contents removeKey:oldObject. |
|
303 |
^ oldObject |
|
1057 | 304 |
|
305 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 306 |
! ! |
307 |
||
8860 | 308 |
!Bag methodsFor:'bulk operations'! |
309 |
||
310 |
sum |
|
11522 | 311 |
"sum up all elements; return 0 for an empty collection. |
312 |
can be done easier, using bags knowledge." |
|
8860 | 313 |
|
314 |
|accu| |
|
315 |
||
11522 | 316 |
self isEmpty ifTrue:[ ^ 0 ]. |
11474 | 317 |
|
318 |
accu := nil. |
|
319 |
self |
|
320 |
valuesAndCountsDo:[:n :count | |
|
321 |
|thisAmount| |
|
322 |
||
323 |
thisAmount := count * n. |
|
324 |
accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ]. |
|
8860 | 325 |
^ accu |
326 |
||
327 |
" |
|
11474 | 328 |
TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10). |
8860 | 329 |
" |
330 |
! ! |
|
331 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
332 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
333 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
334 |
= aBag |
11935 | 335 |
"Compare the receiver with the argument and return true if the |
336 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
337 |
Otherwise return false." |
|
338 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
339 |
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
|
340 |
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
|
341 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 342 |
(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
|
343 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
344 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
345 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
346 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
347 |
hash |
11935 | 348 |
"return an integer useful for hashing on the receiver; |
349 |
redefined since = is redefined here." |
|
350 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
351 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
352 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
353 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
354 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 355 |
h := h + cnt hash. |
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
356 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
357 |
^ h |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
358 |
! ! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
359 |
|
609 | 360 |
!Bag methodsFor:'converting'! |
361 |
||
362 |
asBag |
|
363 |
"return the receiver as a bag" |
|
364 |
||
365 |
"could be an instance of a subclass..." |
|
366 |
self class == Bag ifTrue:[ |
|
367 |
^ self |
|
368 |
]. |
|
369 |
^ super asBag |
|
7161 | 370 |
! |
371 |
||
372 |
asSet |
|
373 |
"return the receiver as a set" |
|
374 |
||
375 |
^ contents keys copy |
|
376 |
||
377 |
" |
|
378 |
|b| |
|
379 |
||
380 |
b := Bag new. |
|
381 |
b add:1; add:2; add:3; add:1; add:1. |
|
382 |
b asSet. |
|
383 |
" |
|
609 | 384 |
! ! |
385 |
||
24181 | 386 |
!Bag methodsFor:'copying-private'! |
609 | 387 |
|
388 |
postCopy |
|
389 |
"must copy the contents as well" |
|
390 |
||
391 |
contents := contents copy |
|
392 |
! ! |
|
393 |
||
1 | 394 |
!Bag methodsFor:'enumerating'! |
395 |
||
396 |
do:aBlock |
|
1057 | 397 |
"evaluate the block for all elements in the collection. |
398 |
||
399 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 400 |
Iterate over a copy to do this." |
1 | 401 |
|
54 | 402 |
contents keysAndValuesDo:[:key :value| |
8873 | 403 |
value timesRepeat:[ |
404 |
aBlock value:key |
|
405 |
] |
|
1 | 406 |
] |
1057 | 407 |
|
408 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 409 |
! |
410 |
||
16779 | 411 |
keysAndValuesDo:aTwoArgBlock |
412 |
"evaluate the block for all distinct elements in the collection, |
|
413 |
passing both the element and the occurrence count as arguments. |
|
414 |
||
415 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
416 |
Iterate over a copy to do this." |
|
417 |
||
418 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
419 |
! |
|
420 |
||
10835 | 421 |
valuesAndCounts |
11481 | 422 |
"return an orderedCollection containing value->count associations" |
10835 | 423 |
|
424 |
|coll| |
|
425 |
||
426 |
coll := OrderedCollection new. |
|
427 |
self valuesAndCountsDo:[:value :count | coll add:(value->count)]. |
|
428 |
^ coll |
|
429 |
||
430 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
431 |
! |
|
432 |
||
345 | 433 |
valuesAndCountsDo:aTwoArgBlock |
434 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 435 |
passing both the element and the occurrence count as arguments. |
1057 | 436 |
|
437 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 438 |
Iterate over a copy to do this." |
345 | 439 |
|
440 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 441 |
|
442 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
20324 | 443 |
! |
444 |
||
445 |
valuesAndCountsSelect:aTwoArgBlock |
|
446 |
"evaluate the block for all distinct elements in the collection, |
|
447 |
passing both the element and the occurrence count as arguments. |
|
448 |
If that returns true, add the element to the OrderedCollection. |
|
449 |
Answer the OrderedCollection. |
|
450 |
||
451 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
452 |
Iterate over a copy to do this." |
|
453 |
||
454 |
|collected| |
|
455 |
||
456 |
collected := OrderedCollection new. |
|
457 |
self valuesAndCountsDo:[:eachValue :eachCount| |
|
458 |
(aTwoArgBlock value:eachValue value:eachCount) ifTrue:[ |
|
459 |
collected add:eachValue |
|
460 |
]. |
|
461 |
]. |
|
462 |
^ collected |
|
463 |
||
464 |
" |
|
465 |
#(10 20 20 30 40) asBag |
|
466 |
valuesAndCountsSelect:[:eachValue :eachCount | eachCount > 1] |
|
467 |
" |
|
468 |
||
469 |
||
470 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 471 |
! ! |
609 | 472 |
|
16766 | 473 |
|
12823 | 474 |
!Bag methodsFor:'printing & storing'! |
475 |
||
476 |
printElementsDo:aBlock |
|
14685 | 477 |
self valuesAndCountsDo:[:value :count| |
478 |
aBlock value:('%1(*%2)' bindWith:value with:count). |
|
12823 | 479 |
] |
480 |
! ! |
|
481 |
||
609 | 482 |
!Bag methodsFor:'private'! |
483 |
||
16888 | 484 |
grow:newSize |
485 |
"/ ignored here |
|
486 |
! |
|
487 |
||
609 | 488 |
initContents |
14956 | 489 |
"set the contents to be an empty Dictionary. |
490 |
This is the default method for initialization, which can be redefined in subclasses." |
|
491 |
||
492 |
contents := self class contentsClass new |
|
493 |
! |
|
494 |
||
495 |
initContents:size |
|
496 |
"set the contents to be an empty Dictionary with initial size. |
|
497 |
This is the default method for initialization, which can be redefined in subclasses." |
|
498 |
||
499 |
contents := self class contentsClass new: size |
|
500 |
! |
|
501 |
||
502 |
initContentsForEquality |
|
609 | 503 |
"set the contents to be an empty Dictionary" |
504 |
||
505 |
contents := Dictionary new |
|
506 |
! |
|
507 |
||
14956 | 508 |
initContentsForEquality:size |
609 | 509 |
"set the contents to be an empty Dictionary with initial size" |
510 |
||
511 |
contents := Dictionary new:size |
|
512 |
! |
|
513 |
||
5375 | 514 |
initContentsForIdentity |
515 |
"set the contents to be an empty IdentityDictionary" |
|
516 |
||
517 |
contents := IdentityDictionary new |
|
518 |
! |
|
519 |
||
609 | 520 |
initContentsForIdentity:size |
521 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
522 |
||
523 |
contents := IdentityDictionary new:size |
|
524 |
! ! |
|
525 |
||
5557 | 526 |
!Bag methodsFor:'queries'! |
527 |
||
23699 | 528 |
includes:anObject |
529 |
"return true, if anObject is in the receiver" |
|
530 |
||
531 |
^ (self occurrencesOf:anObject) > 0 |
|
532 |
! |
|
533 |
||
534 |
occurrencesOf:anObject |
|
535 |
"return how many times anObject is in the receiver" |
|
536 |
||
537 |
^ contents at:anObject ifAbsent:0 |
|
538 |
! |
|
539 |
||
5557 | 540 |
size |
541 |
"return the number of bag elements" |
|
542 |
||
543 |
|count| |
|
544 |
||
545 |
count := 0. |
|
546 |
contents do:[:element | count := count + element]. |
|
547 |
^ count |
|
548 |
! ! |
|
549 |
||
15457 | 550 |
!Bag methodsFor:'statistical functions'! |
551 |
||
552 |
variance |
|
553 |
"compute the variance over a complete data set (and not of a sample)" |
|
554 |
||
555 |
|m sz sumDeltaSquares| |
|
556 |
||
557 |
m := self arithmeticMean. |
|
558 |
sumDeltaSquares := 0. |
|
559 |
sz := 0. |
|
560 |
self |
|
561 |
valuesAndCountsDo:[:val :count | |
|
562 |
sumDeltaSquares := sumDeltaSquares + ((val - m) squared). |
|
563 |
sz := sz + 1. |
|
564 |
]. |
|
565 |
||
566 |
^ sumDeltaSquares / sz |
|
567 |
||
568 |
" |
|
569 |
TestCase assert:( #(1 1 1 2 2 2 1 1 1 2 2 2) asBag variance = #(1 1 1 2 2 2 1 1 1 2 2 2) variance). |
|
570 |
TestCase assert:( #(1 1 1 1 1 0 0 0 0 0) asBag variance = #(1 1 1 1 1 0 0 0 0 0) variance). |
|
571 |
" |
|
572 |
! ! |
|
573 |
||
609 | 574 |
!Bag methodsFor:'testing'! |
575 |
||
15429 | 576 |
isFixedSize |
577 |
"return true if the receiver cannot grow" |
|
578 |
||
579 |
^ false |
|
580 |
! |
|
581 |
||
18468 | 582 |
isOrdered |
583 |
"return true, if the receiver's elements are ordered. |
|
584 |
Redefined to return false here, because the order of keys and values |
|
585 |
may change due to rehashing, when elements are added/removed" |
|
586 |
||
587 |
^ false |
|
609 | 588 |
! ! |
589 |
||
633 | 590 |
!Bag class methodsFor:'documentation'! |
591 |
||
592 |
version |
|
19081 | 593 |
^ '$Header$' |
12823 | 594 |
! |
595 |
||
596 |
version_CVS |
|
19081 | 597 |
^ '$Header$' |
633 | 598 |
! ! |
14685 | 599 |