author | Merge Script |
Mon, 08 Jun 2015 06:58:54 +0200 | |
branch | jv |
changeset 18461 | bc3d3101c493 |
parent 18120 | e3a375d5f6a8 |
child 18473 | ff8f0323cadd |
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 |
|
15732 | 145 |
|
146 |
" |
|
147 |
Bag new |
|
148 |
add:'abc'; |
|
149 |
add:'def'; |
|
150 |
add:'ghi'; |
|
151 |
add:'abc'; |
|
152 |
add:'def'; |
|
153 |
add:'abc'; |
|
154 |
add:'abc'; |
|
155 |
contents |
|
156 |
" |
|
15686 | 157 |
! |
158 |
||
159 |
sortedCounts |
|
15732 | 160 |
"Answer with a collection of counts associated to elements, sorted by decreasing count." |
15686 | 161 |
"Suggested by l. Uzonyi" |
162 |
||
163 |
^ (Array new:contents size |
|
164 |
streamContents:[:stream | |
|
165 |
contents associationsDo:[:each | stream nextPut:each value -> each key ] |
|
166 |
]) |
|
16766 | 167 |
sort:[:x :y | y < x]; |
15686 | 168 |
yourself |
15731 | 169 |
|
170 |
" |
|
171 |
Bag new |
|
172 |
add:'abc'; |
|
173 |
add:'def'; |
|
174 |
add:'ghi'; |
|
175 |
add:'abc'; |
|
176 |
add:'def'; |
|
177 |
add:'abc'; |
|
178 |
add:'abc'; |
|
179 |
sortedCounts |
|
180 |
" |
|
16098 | 181 |
! |
182 |
||
183 |
valuesSortedByCounts |
|
184 |
"Answer with a collection of values, sorted by decreasing count. |
|
185 |
Count informtion is lost in the result" |
|
186 |
||
187 |
^ self sortedCounts collect:[:assoc | assoc value]. |
|
188 |
||
189 |
" |
|
190 |
Bag new |
|
191 |
add:'abc'; |
|
192 |
add:'def'; |
|
193 |
add:'ghi'; |
|
194 |
add:'abc'; |
|
195 |
add:'def'; |
|
196 |
add:'abc'; |
|
197 |
add:'abc'; |
|
198 |
valuesSortedByCounts |
|
199 |
" |
|
1 | 200 |
! ! |
201 |
||
202 |
!Bag methodsFor:'adding & removing'! |
|
203 |
||
54 | 204 |
add:newObject |
345 | 205 |
"add the argument, anObject to the receiver. |
14896 | 206 |
Returns the object (sigh). |
1057 | 207 |
|
208 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
14896 | 209 |
Iterate over a copy to do this." |
1 | 210 |
|
54 | 211 |
|n| |
212 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
213 |
n := contents at:newObject ifAbsent:0. |
54 | 214 |
contents at:newObject put:(n + 1). |
215 |
^ newObject |
|
1057 | 216 |
|
217 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 218 |
! |
219 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
220 |
add:newObject withOccurrences:anInteger |
345 | 221 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 222 |
Returns the object. |
223 |
||
224 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 225 |
Iterate over a copy to do this." |
1 | 226 |
|
54 | 227 |
|n| |
228 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
229 |
n := contents at:newObject ifAbsent:0. |
54 | 230 |
contents at:newObject put:(n + anInteger). |
1 | 231 |
^ newObject |
1057 | 232 |
|
233 |
"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
|
234 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 235 |
! |
236 |
||
237 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 238 |
"Remove oldObject from the collection. |
239 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 240 |
otherwise return the removed object. |
241 |
||
242 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 243 |
Iterate over a copy to do this." |
1 | 244 |
|
245 |
|count| |
|
246 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
247 |
count := contents at:oldObject ifAbsent:0. |
1 | 248 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
249 |
(count == 1) ifTrue:[ |
|
8873 | 250 |
contents removeKey:oldObject |
251 |
] ifFalse:[ |
|
252 |
contents at:oldObject put:(count - 1) |
|
1 | 253 |
]. |
254 |
^ oldObject |
|
1057 | 255 |
|
256 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 257 |
! |
258 |
||
1110 | 259 |
removeAll |
1164 | 260 |
"remove all objects from the receiver collection (i.e. make it empty). |
261 |
Returns the receiver." |
|
1110 | 262 |
|
263 |
contents := contents species new. |
|
264 |
||
265 |
" |
|
266 |
|b| |
|
267 |
||
268 |
b := Bag new. |
|
269 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 270 |
Transcript showCR:b. |
1110 | 271 |
|
272 |
b removeAll. |
|
273 |
||
1422 | 274 |
Transcript showCR:b |
1110 | 275 |
" |
276 |
||
1164 | 277 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 278 |
! |
279 |
||
345 | 280 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
281 |
"Remove all occurrences of oldObject from the collection. |
|
282 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 283 |
otherwise return the number of removes. |
284 |
||
285 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 286 |
Iterate over a copy to do this." |
345 | 287 |
|
288 |
|count| |
|
289 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
290 |
count := contents at:oldObject ifAbsent:0. |
345 | 291 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
292 |
contents removeKey:oldObject. |
|
293 |
^ oldObject |
|
1057 | 294 |
|
295 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 296 |
! ! |
297 |
||
8860 | 298 |
!Bag methodsFor:'bulk operations'! |
299 |
||
300 |
sum |
|
11522 | 301 |
"sum up all elements; return 0 for an empty collection. |
302 |
can be done easier, using bags knowledge." |
|
8860 | 303 |
|
304 |
|accu| |
|
305 |
||
11522 | 306 |
self isEmpty ifTrue:[ ^ 0 ]. |
11474 | 307 |
|
308 |
accu := nil. |
|
309 |
self |
|
310 |
valuesAndCountsDo:[:n :count | |
|
311 |
|thisAmount| |
|
312 |
||
313 |
thisAmount := count * n. |
|
314 |
accu := (accu isNil ifTrue:[thisAmount] ifFalse:[accu + thisAmount]) ]. |
|
8860 | 315 |
^ accu |
316 |
||
317 |
" |
|
11474 | 318 |
TestCase assert:((Bag new add:1; add:2; add:3; add:1; add:2; add:1; yourself) sum = 10). |
8860 | 319 |
" |
320 |
! ! |
|
321 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
322 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
323 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
324 |
= aBag |
11935 | 325 |
"Compare the receiver with the argument and return true if the |
326 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
327 |
Otherwise return false." |
|
328 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
329 |
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
|
330 |
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
|
331 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 332 |
(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
|
333 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
334 |
^ true |
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 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
337 |
hash |
11935 | 338 |
"return an integer useful for hashing on the receiver; |
339 |
redefined since = is redefined here." |
|
340 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
341 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
342 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
343 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
344 |
self valuesAndCountsDo:[:val :cnt | |
11935 | 345 |
h := h + cnt hash. |
6735
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 |
^ h |
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 |
|
609 | 350 |
!Bag methodsFor:'converting'! |
351 |
||
352 |
asBag |
|
353 |
"return the receiver as a bag" |
|
354 |
||
355 |
"could be an instance of a subclass..." |
|
356 |
self class == Bag ifTrue:[ |
|
357 |
^ self |
|
358 |
]. |
|
359 |
^ super asBag |
|
7161 | 360 |
! |
361 |
||
362 |
asSet |
|
363 |
"return the receiver as a set" |
|
364 |
||
365 |
^ contents keys copy |
|
366 |
||
367 |
" |
|
368 |
|b| |
|
369 |
||
370 |
b := Bag new. |
|
371 |
b add:1; add:2; add:3; add:1; add:1. |
|
372 |
b asSet. |
|
373 |
" |
|
609 | 374 |
! ! |
375 |
||
376 |
!Bag methodsFor:'copying'! |
|
377 |
||
378 |
postCopy |
|
379 |
"must copy the contents as well" |
|
380 |
||
381 |
contents := contents copy |
|
382 |
! ! |
|
383 |
||
1 | 384 |
!Bag methodsFor:'enumerating'! |
385 |
||
386 |
do:aBlock |
|
1057 | 387 |
"evaluate the block for all elements in the collection. |
388 |
||
389 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 390 |
Iterate over a copy to do this." |
1 | 391 |
|
54 | 392 |
contents keysAndValuesDo:[:key :value| |
8873 | 393 |
value timesRepeat:[ |
394 |
aBlock value:key |
|
395 |
] |
|
1 | 396 |
] |
1057 | 397 |
|
398 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 399 |
! |
400 |
||
16779 | 401 |
keysAndValuesDo:aTwoArgBlock |
402 |
"evaluate the block for all distinct elements in the collection, |
|
403 |
passing both the element and the occurrence count as arguments. |
|
404 |
||
405 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
406 |
Iterate over a copy to do this." |
|
407 |
||
408 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
409 |
! |
|
410 |
||
10835 | 411 |
valuesAndCounts |
11481 | 412 |
"return an orderedCollection containing value->count associations" |
10835 | 413 |
|
414 |
|coll| |
|
415 |
||
416 |
coll := OrderedCollection new. |
|
417 |
self valuesAndCountsDo:[:value :count | coll add:(value->count)]. |
|
418 |
^ coll |
|
419 |
||
420 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
421 |
! |
|
422 |
||
345 | 423 |
valuesAndCountsDo:aTwoArgBlock |
424 |
"evaluate the block for all distinct elements in the collection, |
|
8873 | 425 |
passing both the element and the occurrence count as arguments. |
1057 | 426 |
|
427 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
8873 | 428 |
Iterate over a copy to do this." |
345 | 429 |
|
430 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 431 |
|
432 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 433 |
! ! |
609 | 434 |
|
16766 | 435 |
|
12823 | 436 |
!Bag methodsFor:'printing & storing'! |
437 |
||
438 |
printElementsDo:aBlock |
|
14685 | 439 |
self valuesAndCountsDo:[:value :count| |
440 |
aBlock value:('%1(*%2)' bindWith:value with:count). |
|
12823 | 441 |
] |
442 |
! ! |
|
443 |
||
609 | 444 |
!Bag methodsFor:'private'! |
445 |
||
16888 | 446 |
grow:newSize |
447 |
"/ ignored here |
|
448 |
! |
|
449 |
||
609 | 450 |
initContents |
14956 | 451 |
"set the contents to be an empty Dictionary. |
452 |
This is the default method for initialization, which can be redefined in subclasses." |
|
453 |
||
454 |
contents := self class contentsClass new |
|
455 |
! |
|
456 |
||
457 |
initContents:size |
|
458 |
"set the contents to be an empty Dictionary with initial size. |
|
459 |
This is the default method for initialization, which can be redefined in subclasses." |
|
460 |
||
461 |
contents := self class contentsClass new: size |
|
462 |
! |
|
463 |
||
464 |
initContentsForEquality |
|
609 | 465 |
"set the contents to be an empty Dictionary" |
466 |
||
467 |
contents := Dictionary new |
|
468 |
! |
|
469 |
||
14956 | 470 |
initContentsForEquality:size |
609 | 471 |
"set the contents to be an empty Dictionary with initial size" |
472 |
||
473 |
contents := Dictionary new:size |
|
474 |
! |
|
475 |
||
5375 | 476 |
initContentsForIdentity |
477 |
"set the contents to be an empty IdentityDictionary" |
|
478 |
||
479 |
contents := IdentityDictionary new |
|
480 |
! |
|
481 |
||
609 | 482 |
initContentsForIdentity:size |
483 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
484 |
||
485 |
contents := IdentityDictionary new:size |
|
486 |
! ! |
|
487 |
||
5557 | 488 |
!Bag methodsFor:'queries'! |
489 |
||
490 |
size |
|
491 |
"return the number of bag elements" |
|
492 |
||
493 |
|count| |
|
494 |
||
495 |
count := 0. |
|
496 |
contents do:[:element | count := count + element]. |
|
497 |
^ count |
|
498 |
! ! |
|
499 |
||
15457 | 500 |
!Bag methodsFor:'statistical functions'! |
501 |
||
502 |
variance |
|
503 |
"compute the variance over a complete data set (and not of a sample)" |
|
504 |
||
505 |
|m sz sumDeltaSquares| |
|
506 |
||
507 |
m := self arithmeticMean. |
|
508 |
sumDeltaSquares := 0. |
|
509 |
sz := 0. |
|
510 |
self |
|
511 |
valuesAndCountsDo:[:val :count | |
|
512 |
sumDeltaSquares := sumDeltaSquares + ((val - m) squared). |
|
513 |
sz := sz + 1. |
|
514 |
]. |
|
515 |
||
516 |
^ sumDeltaSquares / sz |
|
517 |
||
518 |
" |
|
519 |
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). |
|
520 |
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). |
|
521 |
" |
|
522 |
! ! |
|
523 |
||
609 | 524 |
!Bag methodsFor:'testing'! |
525 |
||
526 |
includes:anObject |
|
527 |
"return true, if anObject is in the receiver" |
|
528 |
||
14685 | 529 |
^ (self occurrencesOf:anObject) > 0 |
609 | 530 |
! |
531 |
||
15429 | 532 |
isFixedSize |
533 |
"return true if the receiver cannot grow" |
|
534 |
||
535 |
^ false |
|
536 |
! |
|
537 |
||
609 | 538 |
occurrencesOf:anObject |
539 |
"return how many times anObject is in the receiver" |
|
540 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
541 |
^ contents at:anObject ifAbsent:0 |
609 | 542 |
! ! |
543 |
||
633 | 544 |
!Bag class methodsFor:'documentation'! |
545 |
||
546 |
version |
|
16888 | 547 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.53 2014-10-08 10:29:29 cg Exp $' |
12823 | 548 |
! |
549 |
||
550 |
version_CVS |
|
16888 | 551 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.53 2014-10-08 10:29:29 cg Exp $' |
633 | 552 |
! ! |
14685 | 553 |