author | james |
Wed, 09 Apr 2003 14:29:15 +0200 | |
changeset 7182 | 42dce0ee1b98 |
parent 7161 | be2c068dbc81 |
child 7261 | f35fc9cee675 |
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 |
" |
|
12 |
||
5375 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
Collection subclass:#Bag |
1057 | 16 |
instanceVariableNames:'contents' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Unordered' |
|
1 | 20 |
! |
21 |
||
54 | 22 |
!Bag class methodsFor:'documentation'! |
23 |
||
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
345 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
54 | 38 |
documentation |
39 |
" |
|
88 | 40 |
Bag implements collections whose elements are unordered and have no |
154 | 41 |
external key. Elements may occur more than once in a bag. There is no defined |
42 |
order within a bag. |
|
345 | 43 |
The default implementation uses a dictionary to store each objects occurence |
44 |
count, using the object itself as key (i.e. using = and hash for inclusion |
|
45 |
tests). |
|
399 | 46 |
|
345 | 47 |
There is also an instance creation variant (#identityNew:) creating a |
48 |
bag which compares using #== and hashes using #identityHash. |
|
399 | 49 |
(I'd say that an IdentityBag was a better thing to implement ... |
50 |
... but for compatibility ... we do it here as well) |
|
1 | 51 |
|
1283 | 52 |
[Instance variables:] |
53 |
||
54 |
contents <Dictionary> for each element, the number of occurrences |
|
55 |
||
1 | 56 |
|
1290 | 57 |
[author:] |
58 |
Claus Gittinger |
|
59 |
||
1283 | 60 |
[See also:] |
61 |
Set IdentitySet |
|
62 |
Dictionary IdentityDictionary |
|
63 |
OrderedCollection Array |
|
54 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
67 |
!Bag class methodsFor:'instance creation'! |
|
68 |
||
345 | 69 |
equalityNew:size |
70 |
"return a new empty Bag with initial space for size elements. |
|
71 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 72 |
|
5558 | 73 |
^ self basicNew initContents:size |
345 | 74 |
! |
75 |
||
5375 | 76 |
identityNew |
77 |
"return a new empty Bag which compares for identity |
|
78 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
79 |
||
80 |
^ super new initContentsForIdentity |
|
81 |
! |
|
82 |
||
345 | 83 |
identityNew:size |
84 |
"return a new empty Bag with initial space for size elements. |
|
85 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
86 |
||
5558 | 87 |
^ self basicNew initContentsForIdentity:size |
1 | 88 |
! |
89 |
||
609 | 90 |
new |
91 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 92 |
|
5558 | 93 |
^ self basicNew initContents |
345 | 94 |
! |
95 |
||
609 | 96 |
new:size |
97 |
"return a new empty Bag with initial space for size elements. |
|
98 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 99 |
|
609 | 100 |
^ self equalityNew:size |
1 | 101 |
! ! |
102 |
||
6411 | 103 |
!Bag methodsFor:'Compatibility - Dolphin'! |
104 |
||
105 |
asAssociations |
|
106 |
^ contents |
|
107 |
! ! |
|
108 |
||
1 | 109 |
!Bag methodsFor:'accessing'! |
110 |
||
111 |
at:index |
|
112 |
"report an error: at: is not allowed for Bags" |
|
113 |
||
114 |
^ self errorNotKeyed |
|
115 |
! |
|
116 |
||
117 |
at:index put:anObject |
|
118 |
"report an error: at:put: is not allowed for Bags" |
|
119 |
||
120 |
^ self errorNotKeyed |
|
345 | 121 |
! |
122 |
||
123 |
contents |
|
124 |
"return the dictionary which associates occurrence-counts |
|
125 |
to the bags elements." |
|
126 |
||
127 |
^ contents |
|
1 | 128 |
! ! |
129 |
||
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. |
|
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. |
|
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. |
|
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:[ |
|
1057 | 178 |
contents removeKey:oldObject |
1 | 179 |
] ifFalse:[ |
1057 | 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. |
|
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 |
||
6735
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
226 |
!Bag methodsFor:'comparing'! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
227 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
228 |
= aBag |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
229 |
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
|
230 |
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
|
231 |
self valuesAndCountsDo:[:val :cnt | |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
232 |
(aBag occurrencesOf:val) == cnt ifFalse:[^ false] |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
233 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
234 |
^ true |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
235 |
! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
236 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
237 |
hash |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
238 |
|h| |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
239 |
|
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
240 |
h := self size. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
241 |
self valuesAndCountsDo:[:val :cnt | |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
242 |
h := h + cnt hash. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
243 |
]. |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
244 |
^ h |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
245 |
! ! |
fad09454a025
added #= and #hash (to put bags into Sets/Dictionaries)
Claus Gittinger <cg@exept.de>
parents:
6411
diff
changeset
|
246 |
|
609 | 247 |
!Bag methodsFor:'converting'! |
248 |
||
249 |
asBag |
|
250 |
"return the receiver as a bag" |
|
251 |
||
252 |
"could be an instance of a subclass..." |
|
253 |
self class == Bag ifTrue:[ |
|
254 |
^ self |
|
255 |
]. |
|
256 |
^ super asBag |
|
7161 | 257 |
! |
258 |
||
259 |
asSet |
|
260 |
"return the receiver as a set" |
|
261 |
||
262 |
^ contents keys copy |
|
263 |
||
264 |
" |
|
265 |
|b| |
|
266 |
||
267 |
b := Bag new. |
|
268 |
b add:1; add:2; add:3; add:1; add:1. |
|
269 |
b asSet. |
|
270 |
" |
|
609 | 271 |
! ! |
272 |
||
273 |
!Bag methodsFor:'copying'! |
|
274 |
||
275 |
postCopy |
|
276 |
"must copy the contents as well" |
|
277 |
||
278 |
contents := contents copy |
|
279 |
! ! |
|
280 |
||
1 | 281 |
!Bag methodsFor:'enumerating'! |
282 |
||
283 |
do:aBlock |
|
1057 | 284 |
"evaluate the block for all elements in the collection. |
285 |
||
286 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
287 |
Iterate over a copy to do this." |
|
1 | 288 |
|
54 | 289 |
contents keysAndValuesDo:[:key :value| |
1057 | 290 |
value timesRepeat:[ |
291 |
aBlock value:key |
|
292 |
] |
|
1 | 293 |
] |
1057 | 294 |
|
295 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 296 |
! |
297 |
||
298 |
valuesAndCountsDo:aTwoArgBlock |
|
299 |
"evaluate the block for all distinct elements in the collection, |
|
1057 | 300 |
passing both the element and the occurence count as arguments. |
301 |
||
302 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
303 |
Iterate over a copy to do this." |
|
345 | 304 |
|
305 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 306 |
|
307 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 308 |
! ! |
609 | 309 |
|
310 |
!Bag methodsFor:'private'! |
|
311 |
||
312 |
initContents |
|
313 |
"set the contents to be an empty Dictionary" |
|
314 |
||
315 |
contents := Dictionary new |
|
316 |
! |
|
317 |
||
318 |
initContents:size |
|
319 |
"set the contents to be an empty Dictionary with initial size" |
|
320 |
||
321 |
contents := Dictionary new:size |
|
322 |
! |
|
323 |
||
5375 | 324 |
initContentsForIdentity |
325 |
"set the contents to be an empty IdentityDictionary" |
|
326 |
||
327 |
contents := IdentityDictionary new |
|
328 |
! |
|
329 |
||
609 | 330 |
initContentsForIdentity:size |
331 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
332 |
||
333 |
contents := IdentityDictionary new:size |
|
334 |
! ! |
|
335 |
||
5557 | 336 |
!Bag methodsFor:'queries'! |
337 |
||
338 |
size |
|
339 |
"return the number of bag elements" |
|
340 |
||
341 |
|count| |
|
342 |
||
343 |
count := 0. |
|
344 |
contents do:[:element | count := count + element]. |
|
345 |
^ count |
|
346 |
! ! |
|
347 |
||
609 | 348 |
!Bag methodsFor:'testing'! |
349 |
||
350 |
includes:anObject |
|
351 |
"return true, if anObject is in the receiver" |
|
352 |
||
353 |
^ contents includesKey:anObject |
|
354 |
! |
|
355 |
||
356 |
occurrencesOf:anObject |
|
357 |
"return how many times anObject is in the receiver" |
|
358 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
359 |
^ contents at:anObject ifAbsent:0 |
609 | 360 |
! ! |
361 |
||
633 | 362 |
!Bag class methodsFor:'documentation'! |
363 |
||
364 |
version |
|
7161 | 365 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.30 2003-04-02 12:12:04 cg Exp $' |
633 | 366 |
! ! |