author | Claus Gittinger <cg@exept.de> |
Tue, 28 Dec 1999 15:03:48 +0100 | |
changeset 5150 | d7f854b5ede8 |
parent 1422 | 9a0b792f2953 |
child 5375 | ed4815509a57 |
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 |
||
13 |
Collection subclass:#Bag |
|
1057 | 14 |
instanceVariableNames:'contents' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Collections-Unordered' |
|
1 | 18 |
! |
19 |
||
54 | 20 |
!Bag class methodsFor:'documentation'! |
21 |
||
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
345 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
54 | 36 |
documentation |
37 |
" |
|
88 | 38 |
Bag implements collections whose elements are unordered and have no |
154 | 39 |
external key. Elements may occur more than once in a bag. There is no defined |
40 |
order within a bag. |
|
345 | 41 |
The default implementation uses a dictionary to store each objects occurence |
42 |
count, using the object itself as key (i.e. using = and hash for inclusion |
|
43 |
tests). |
|
399 | 44 |
|
345 | 45 |
There is also an instance creation variant (#identityNew:) creating a |
46 |
bag which compares using #== and hashes using #identityHash. |
|
399 | 47 |
(I'd say that an IdentityBag was a better thing to implement ... |
48 |
... but for compatibility ... we do it here as well) |
|
1 | 49 |
|
1283 | 50 |
[Instance variables:] |
51 |
||
52 |
contents <Dictionary> for each element, the number of occurrences |
|
53 |
||
1 | 54 |
|
1290 | 55 |
[author:] |
56 |
Claus Gittinger |
|
57 |
||
1283 | 58 |
[See also:] |
59 |
Set IdentitySet |
|
60 |
Dictionary IdentityDictionary |
|
61 |
OrderedCollection Array |
|
54 | 62 |
" |
63 |
! ! |
|
1 | 64 |
|
65 |
!Bag class methodsFor:'instance creation'! |
|
66 |
||
345 | 67 |
equalityNew:size |
68 |
"return a new empty Bag with initial space for size elements. |
|
69 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 70 |
|
71 |
^ super new initContents:size |
|
345 | 72 |
! |
73 |
||
74 |
identityNew:size |
|
75 |
"return a new empty Bag with initial space for size elements. |
|
76 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
77 |
||
78 |
^ super new initContentsForIdentity:size |
|
1 | 79 |
! |
80 |
||
609 | 81 |
new |
82 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 83 |
|
609 | 84 |
^ super new initContents |
345 | 85 |
! |
86 |
||
609 | 87 |
new:size |
88 |
"return a new empty Bag with initial space for size elements. |
|
89 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 90 |
|
609 | 91 |
^ self equalityNew:size |
1 | 92 |
! ! |
93 |
||
94 |
!Bag methodsFor:'accessing'! |
|
95 |
||
96 |
at:index |
|
97 |
"report an error: at: is not allowed for Bags" |
|
98 |
||
99 |
^ self errorNotKeyed |
|
100 |
! |
|
101 |
||
102 |
at:index put:anObject |
|
103 |
"report an error: at:put: is not allowed for Bags" |
|
104 |
||
105 |
^ self errorNotKeyed |
|
345 | 106 |
! |
107 |
||
108 |
contents |
|
109 |
"return the dictionary which associates occurrence-counts |
|
110 |
to the bags elements." |
|
111 |
||
112 |
^ contents |
|
1 | 113 |
! ! |
114 |
||
115 |
!Bag methodsFor:'adding & removing'! |
|
116 |
||
54 | 117 |
add:newObject |
345 | 118 |
"add the argument, anObject to the receiver. |
1057 | 119 |
Returns the object. |
120 |
||
121 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
122 |
Iterate over a copy to do this." |
|
1 | 123 |
|
54 | 124 |
|n| |
125 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
126 |
n := contents at:newObject ifAbsent:0. |
54 | 127 |
contents at:newObject put:(n + 1). |
128 |
^ newObject |
|
1057 | 129 |
|
130 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 131 |
! |
132 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
133 |
add:newObject withOccurrences:anInteger |
345 | 134 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 135 |
Returns the object. |
136 |
||
137 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
138 |
Iterate over a copy to do this." |
|
1 | 139 |
|
54 | 140 |
|n| |
141 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
142 |
n := contents at:newObject ifAbsent:0. |
54 | 143 |
contents at:newObject put:(n + anInteger). |
1 | 144 |
^ newObject |
1057 | 145 |
|
146 |
"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
|
147 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 148 |
! |
149 |
||
150 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 151 |
"Remove oldObject from the collection. |
152 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 153 |
otherwise return the removed object. |
154 |
||
155 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
156 |
Iterate over a copy to do this." |
|
1 | 157 |
|
158 |
|count| |
|
159 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
160 |
count := contents at:oldObject ifAbsent:0. |
1 | 161 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
162 |
(count == 1) ifTrue:[ |
|
1057 | 163 |
contents removeKey:oldObject |
1 | 164 |
] ifFalse:[ |
1057 | 165 |
contents at:oldObject put:(count - 1) |
1 | 166 |
]. |
167 |
^ oldObject |
|
1057 | 168 |
|
169 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 170 |
! |
171 |
||
1110 | 172 |
removeAll |
1164 | 173 |
"remove all objects from the receiver collection (i.e. make it empty). |
174 |
Returns the receiver." |
|
1110 | 175 |
|
176 |
contents := contents species new. |
|
177 |
||
178 |
" |
|
179 |
|b| |
|
180 |
||
181 |
b := Bag new. |
|
182 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 183 |
Transcript showCR:b. |
1110 | 184 |
|
185 |
b removeAll. |
|
186 |
||
1422 | 187 |
Transcript showCR:b |
1110 | 188 |
" |
189 |
||
1164 | 190 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 191 |
! |
192 |
||
345 | 193 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
194 |
"Remove all occurrences of oldObject from the collection. |
|
195 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 196 |
otherwise return the number of removes. |
197 |
||
198 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
199 |
Iterate over a copy to do this." |
|
345 | 200 |
|
201 |
|count| |
|
202 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
203 |
count := contents at:oldObject ifAbsent:0. |
345 | 204 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
205 |
contents removeKey:oldObject. |
|
206 |
^ oldObject |
|
1057 | 207 |
|
208 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 209 |
! ! |
210 |
||
609 | 211 |
!Bag methodsFor:'converting'! |
212 |
||
213 |
asBag |
|
214 |
"return the receiver as a bag" |
|
215 |
||
216 |
"could be an instance of a subclass..." |
|
217 |
self class == Bag ifTrue:[ |
|
218 |
^ self |
|
219 |
]. |
|
220 |
^ super asBag |
|
221 |
! ! |
|
222 |
||
223 |
!Bag methodsFor:'copying'! |
|
224 |
||
225 |
postCopy |
|
226 |
"must copy the contents as well" |
|
227 |
||
228 |
contents := contents copy |
|
229 |
! ! |
|
230 |
||
1 | 231 |
!Bag methodsFor:'enumerating'! |
232 |
||
233 |
do:aBlock |
|
1057 | 234 |
"evaluate the block for all elements in the collection. |
235 |
||
236 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
237 |
Iterate over a copy to do this." |
|
1 | 238 |
|
54 | 239 |
contents keysAndValuesDo:[:key :value| |
1057 | 240 |
value timesRepeat:[ |
241 |
aBlock value:key |
|
242 |
] |
|
1 | 243 |
] |
1057 | 244 |
|
245 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 246 |
! |
247 |
||
248 |
valuesAndCountsDo:aTwoArgBlock |
|
249 |
"evaluate the block for all distinct elements in the collection, |
|
1057 | 250 |
passing both the element and the occurence count as arguments. |
251 |
||
252 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
253 |
Iterate over a copy to do this." |
|
345 | 254 |
|
255 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 256 |
|
257 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 258 |
! ! |
609 | 259 |
|
260 |
!Bag methodsFor:'private'! |
|
261 |
||
262 |
initContents |
|
263 |
"set the contents to be an empty Dictionary" |
|
264 |
||
265 |
contents := Dictionary new |
|
266 |
! |
|
267 |
||
268 |
initContents:size |
|
269 |
"set the contents to be an empty Dictionary with initial size" |
|
270 |
||
271 |
contents := Dictionary new:size |
|
272 |
! |
|
273 |
||
274 |
initContentsForIdentity:size |
|
275 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
276 |
||
277 |
contents := IdentityDictionary new:size |
|
278 |
! ! |
|
279 |
||
280 |
!Bag methodsFor:'testing'! |
|
281 |
||
282 |
includes:anObject |
|
283 |
"return true, if anObject is in the receiver" |
|
284 |
||
285 |
^ contents includesKey:anObject |
|
286 |
! |
|
287 |
||
288 |
occurrencesOf:anObject |
|
289 |
"return how many times anObject is in the receiver" |
|
290 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
291 |
^ contents at:anObject ifAbsent:0 |
609 | 292 |
! |
293 |
||
294 |
size |
|
295 |
"return the number of bag elements" |
|
296 |
||
297 |
|count| |
|
298 |
||
299 |
count := 0. |
|
300 |
contents do:[:element | count := count + element]. |
|
301 |
^ count |
|
302 |
! ! |
|
303 |
||
633 | 304 |
!Bag class methodsFor:'documentation'! |
305 |
||
306 |
version |
|
1422 | 307 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.24 1996-05-18 15:24:28 cg Exp $' |
633 | 308 |
! ! |