author | Claus Gittinger <cg@exept.de> |
Tue, 22 Aug 2000 15:57:33 +0200 | |
changeset 5557 | f5f8d236027c |
parent 5375 | ed4815509a57 |
child 5558 | dfdb1198bf6a |
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 |
|
73 |
^ super new 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 |
||
87 |
^ super new initContentsForIdentity:size |
|
1 | 88 |
! |
89 |
||
609 | 90 |
new |
91 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 92 |
|
609 | 93 |
^ super new 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 |
||
103 |
!Bag methodsFor:'accessing'! |
|
104 |
||
105 |
at:index |
|
106 |
"report an error: at: is not allowed for Bags" |
|
107 |
||
108 |
^ self errorNotKeyed |
|
109 |
! |
|
110 |
||
111 |
at:index put:anObject |
|
112 |
"report an error: at:put: is not allowed for Bags" |
|
113 |
||
114 |
^ self errorNotKeyed |
|
345 | 115 |
! |
116 |
||
117 |
contents |
|
118 |
"return the dictionary which associates occurrence-counts |
|
119 |
to the bags elements." |
|
120 |
||
121 |
^ contents |
|
1 | 122 |
! ! |
123 |
||
124 |
!Bag methodsFor:'adding & removing'! |
|
125 |
||
54 | 126 |
add:newObject |
345 | 127 |
"add the argument, anObject to the receiver. |
1057 | 128 |
Returns the object. |
129 |
||
130 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
131 |
Iterate over a copy to do this." |
|
1 | 132 |
|
54 | 133 |
|n| |
134 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
135 |
n := contents at:newObject ifAbsent:0. |
54 | 136 |
contents at:newObject put:(n + 1). |
137 |
^ newObject |
|
1057 | 138 |
|
139 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 140 |
! |
141 |
||
1372
d7bd1b463a65
oops - typo; occurrences has two r's
Claus Gittinger <cg@exept.de>
parents:
1290
diff
changeset
|
142 |
add:newObject withOccurrences:anInteger |
345 | 143 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 144 |
Returns the object. |
145 |
||
146 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
147 |
Iterate over a copy to do this." |
|
1 | 148 |
|
54 | 149 |
|n| |
150 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
151 |
n := contents at:newObject ifAbsent:0. |
54 | 152 |
contents at:newObject put:(n + anInteger). |
1 | 153 |
^ newObject |
1057 | 154 |
|
155 |
"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
|
156 |
"Created: 11.5.1996 / 12:13:43 / cg" |
1 | 157 |
! |
158 |
||
159 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 160 |
"Remove oldObject from the collection. |
161 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 162 |
otherwise return the removed object. |
163 |
||
164 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
165 |
Iterate over a copy to do this." |
|
1 | 166 |
|
167 |
|count| |
|
168 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
169 |
count := contents at:oldObject ifAbsent:0. |
1 | 170 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
171 |
(count == 1) ifTrue:[ |
|
1057 | 172 |
contents removeKey:oldObject |
1 | 173 |
] ifFalse:[ |
1057 | 174 |
contents at:oldObject put:(count - 1) |
1 | 175 |
]. |
176 |
^ oldObject |
|
1057 | 177 |
|
178 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 179 |
! |
180 |
||
1110 | 181 |
removeAll |
1164 | 182 |
"remove all objects from the receiver collection (i.e. make it empty). |
183 |
Returns the receiver." |
|
1110 | 184 |
|
185 |
contents := contents species new. |
|
186 |
||
187 |
" |
|
188 |
|b| |
|
189 |
||
190 |
b := Bag new. |
|
191 |
b add:1; add:2; add:3; add:2; add:1. |
|
1422 | 192 |
Transcript showCR:b. |
1110 | 193 |
|
194 |
b removeAll. |
|
195 |
||
1422 | 196 |
Transcript showCR:b |
1110 | 197 |
" |
198 |
||
1164 | 199 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 200 |
! |
201 |
||
345 | 202 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
203 |
"Remove all occurrences of oldObject from the collection. |
|
204 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 205 |
otherwise return the number of removes. |
206 |
||
207 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
208 |
Iterate over a copy to do this." |
|
345 | 209 |
|
210 |
|count| |
|
211 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
212 |
count := contents at:oldObject ifAbsent:0. |
345 | 213 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
214 |
contents removeKey:oldObject. |
|
215 |
^ oldObject |
|
1057 | 216 |
|
217 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 218 |
! ! |
219 |
||
609 | 220 |
!Bag methodsFor:'converting'! |
221 |
||
222 |
asBag |
|
223 |
"return the receiver as a bag" |
|
224 |
||
225 |
"could be an instance of a subclass..." |
|
226 |
self class == Bag ifTrue:[ |
|
227 |
^ self |
|
228 |
]. |
|
229 |
^ super asBag |
|
230 |
! ! |
|
231 |
||
232 |
!Bag methodsFor:'copying'! |
|
233 |
||
234 |
postCopy |
|
235 |
"must copy the contents as well" |
|
236 |
||
237 |
contents := contents copy |
|
238 |
! ! |
|
239 |
||
1 | 240 |
!Bag methodsFor:'enumerating'! |
241 |
||
242 |
do:aBlock |
|
1057 | 243 |
"evaluate the block for all elements in the collection. |
244 |
||
245 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
246 |
Iterate over a copy to do this." |
|
1 | 247 |
|
54 | 248 |
contents keysAndValuesDo:[:key :value| |
1057 | 249 |
value timesRepeat:[ |
250 |
aBlock value:key |
|
251 |
] |
|
1 | 252 |
] |
1057 | 253 |
|
254 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 255 |
! |
256 |
||
257 |
valuesAndCountsDo:aTwoArgBlock |
|
258 |
"evaluate the block for all distinct elements in the collection, |
|
1057 | 259 |
passing both the element and the occurence count as arguments. |
260 |
||
261 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
262 |
Iterate over a copy to do this." |
|
345 | 263 |
|
264 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 265 |
|
266 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 267 |
! ! |
609 | 268 |
|
269 |
!Bag methodsFor:'private'! |
|
270 |
||
271 |
initContents |
|
272 |
"set the contents to be an empty Dictionary" |
|
273 |
||
274 |
contents := Dictionary new |
|
275 |
! |
|
276 |
||
277 |
initContents:size |
|
278 |
"set the contents to be an empty Dictionary with initial size" |
|
279 |
||
280 |
contents := Dictionary new:size |
|
281 |
! |
|
282 |
||
5375 | 283 |
initContentsForIdentity |
284 |
"set the contents to be an empty IdentityDictionary" |
|
285 |
||
286 |
contents := IdentityDictionary new |
|
287 |
! |
|
288 |
||
609 | 289 |
initContentsForIdentity:size |
290 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
291 |
||
292 |
contents := IdentityDictionary new:size |
|
293 |
! ! |
|
294 |
||
5557 | 295 |
!Bag methodsFor:'queries'! |
296 |
||
297 |
size |
|
298 |
"return the number of bag elements" |
|
299 |
||
300 |
|count| |
|
301 |
||
302 |
count := 0. |
|
303 |
contents do:[:element | count := count + element]. |
|
304 |
^ count |
|
305 |
! ! |
|
306 |
||
609 | 307 |
!Bag methodsFor:'testing'! |
308 |
||
309 |
includes:anObject |
|
310 |
"return true, if anObject is in the receiver" |
|
311 |
||
312 |
^ contents includesKey:anObject |
|
313 |
! |
|
314 |
||
315 |
occurrencesOf:anObject |
|
316 |
"return how many times anObject is in the receiver" |
|
317 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
318 |
^ contents at:anObject ifAbsent:0 |
609 | 319 |
! ! |
320 |
||
633 | 321 |
!Bag class methodsFor:'documentation'! |
322 |
||
323 |
version |
|
5557 | 324 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.26 2000-08-22 13:56:00 cg Exp $' |
633 | 325 |
! ! |