author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 18:20:46 +0200 | |
changeset 1290 | 15ba3221b89b |
parent 1283 | 2c533653efa3 |
child 1372 | d7bd1b463a65 |
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 |
||
133 |
add:newObject withOccurences: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" |
|
1 | 147 |
! |
148 |
||
149 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 150 |
"Remove oldObject from the collection. |
151 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 152 |
otherwise return the removed object. |
153 |
||
154 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
155 |
Iterate over a copy to do this." |
|
1 | 156 |
|
157 |
|count| |
|
158 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
159 |
count := contents at:oldObject ifAbsent:0. |
1 | 160 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
161 |
(count == 1) ifTrue:[ |
|
1057 | 162 |
contents removeKey:oldObject |
1 | 163 |
] ifFalse:[ |
1057 | 164 |
contents at:oldObject put:(count - 1) |
1 | 165 |
]. |
166 |
^ oldObject |
|
1057 | 167 |
|
168 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 169 |
! |
170 |
||
1110 | 171 |
removeAll |
1164 | 172 |
"remove all objects from the receiver collection (i.e. make it empty). |
173 |
Returns the receiver." |
|
1110 | 174 |
|
175 |
contents := contents species new. |
|
176 |
||
177 |
" |
|
178 |
|b| |
|
179 |
||
180 |
b := Bag new. |
|
181 |
b add:1; add:2; add:3; add:2; add:1. |
|
182 |
Transcript showCr:b. |
|
183 |
||
184 |
b removeAll. |
|
185 |
||
186 |
Transcript showCr:b |
|
187 |
" |
|
188 |
||
1164 | 189 |
"Modified: 12.4.1996 / 13:34:34 / cg" |
1110 | 190 |
! |
191 |
||
345 | 192 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
193 |
"Remove all occurrences of oldObject from the collection. |
|
194 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 195 |
otherwise return the number of removes. |
196 |
||
197 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
198 |
Iterate over a copy to do this." |
|
345 | 199 |
|
200 |
|count| |
|
201 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
202 |
count := contents at:oldObject ifAbsent:0. |
345 | 203 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
204 |
contents removeKey:oldObject. |
|
205 |
^ oldObject |
|
1057 | 206 |
|
207 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 208 |
! ! |
209 |
||
609 | 210 |
!Bag methodsFor:'converting'! |
211 |
||
212 |
asBag |
|
213 |
"return the receiver as a bag" |
|
214 |
||
215 |
"could be an instance of a subclass..." |
|
216 |
self class == Bag ifTrue:[ |
|
217 |
^ self |
|
218 |
]. |
|
219 |
^ super asBag |
|
220 |
! ! |
|
221 |
||
222 |
!Bag methodsFor:'copying'! |
|
223 |
||
224 |
postCopy |
|
225 |
"must copy the contents as well" |
|
226 |
||
227 |
contents := contents copy |
|
228 |
! ! |
|
229 |
||
1 | 230 |
!Bag methodsFor:'enumerating'! |
231 |
||
232 |
do:aBlock |
|
1057 | 233 |
"evaluate the block for all elements in the collection. |
234 |
||
235 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
236 |
Iterate over a copy to do this." |
|
1 | 237 |
|
54 | 238 |
contents keysAndValuesDo:[:key :value| |
1057 | 239 |
value timesRepeat:[ |
240 |
aBlock value:key |
|
241 |
] |
|
1 | 242 |
] |
1057 | 243 |
|
244 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 245 |
! |
246 |
||
247 |
valuesAndCountsDo:aTwoArgBlock |
|
248 |
"evaluate the block for all distinct elements in the collection, |
|
1057 | 249 |
passing both the element and the occurence count as arguments. |
250 |
||
251 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
252 |
Iterate over a copy to do this." |
|
345 | 253 |
|
254 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 255 |
|
256 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 257 |
! ! |
609 | 258 |
|
259 |
!Bag methodsFor:'private'! |
|
260 |
||
261 |
initContents |
|
262 |
"set the contents to be an empty Dictionary" |
|
263 |
||
264 |
contents := Dictionary new |
|
265 |
! |
|
266 |
||
267 |
initContents:size |
|
268 |
"set the contents to be an empty Dictionary with initial size" |
|
269 |
||
270 |
contents := Dictionary new:size |
|
271 |
! |
|
272 |
||
273 |
initContentsForIdentity:size |
|
274 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
275 |
||
276 |
contents := IdentityDictionary new:size |
|
277 |
! ! |
|
278 |
||
279 |
!Bag methodsFor:'testing'! |
|
280 |
||
281 |
includes:anObject |
|
282 |
"return true, if anObject is in the receiver" |
|
283 |
||
284 |
^ contents includesKey:anObject |
|
285 |
! |
|
286 |
||
287 |
occurrencesOf:anObject |
|
288 |
"return how many times anObject is in the receiver" |
|
289 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
290 |
^ contents at:anObject ifAbsent:0 |
609 | 291 |
! |
292 |
||
293 |
size |
|
294 |
"return the number of bag elements" |
|
295 |
||
296 |
|count| |
|
297 |
||
298 |
count := 0. |
|
299 |
contents do:[:element | count := count + element]. |
|
300 |
^ count |
|
301 |
! ! |
|
302 |
||
633 | 303 |
!Bag class methodsFor:'documentation'! |
304 |
||
305 |
version |
|
1290 | 306 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.22 1996-04-25 16:19:12 cg Exp $' |
633 | 307 |
! ! |