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