author | Claus Gittinger <cg@exept.de> |
Tue, 09 Apr 1996 22:49:28 +0200 | |
changeset 1145 | a094d90e11bf |
parent 1110 | c425b5d28a89 |
child 1164 | 38c54a4f1273 |
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 |
|
88 | 50 |
Instance variables: |
1 | 51 |
|
345 | 52 |
contents <Dictionary> for each element, the number of occurrences |
54 | 53 |
" |
54 |
! ! |
|
1 | 55 |
|
56 |
!Bag class methodsFor:'instance creation'! |
|
57 |
||
345 | 58 |
equalityNew:size |
59 |
"return a new empty Bag with initial space for size elements. |
|
60 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
1 | 61 |
|
62 |
^ super new initContents:size |
|
345 | 63 |
! |
64 |
||
65 |
identityNew:size |
|
66 |
"return a new empty Bag with initial space for size elements. |
|
67 |
Elements will be compared using identity compare (i.e. #== not #= equality)." |
|
68 |
||
69 |
^ super new initContentsForIdentity:size |
|
1 | 70 |
! |
71 |
||
609 | 72 |
new |
73 |
"return a new empty Bag which compares for equality (i.e. not identity)" |
|
1 | 74 |
|
609 | 75 |
^ super new initContents |
345 | 76 |
! |
77 |
||
609 | 78 |
new:size |
79 |
"return a new empty Bag with initial space for size elements. |
|
80 |
Elements will be compared using equality compare (i.e. #= not #== identity)." |
|
345 | 81 |
|
609 | 82 |
^ self equalityNew:size |
1 | 83 |
! ! |
84 |
||
85 |
!Bag methodsFor:'accessing'! |
|
86 |
||
87 |
at:index |
|
88 |
"report an error: at: is not allowed for Bags" |
|
89 |
||
90 |
^ self errorNotKeyed |
|
91 |
! |
|
92 |
||
93 |
at:index put:anObject |
|
94 |
"report an error: at:put: is not allowed for Bags" |
|
95 |
||
96 |
^ self errorNotKeyed |
|
345 | 97 |
! |
98 |
||
99 |
contents |
|
100 |
"return the dictionary which associates occurrence-counts |
|
101 |
to the bags elements." |
|
102 |
||
103 |
^ contents |
|
1 | 104 |
! ! |
105 |
||
106 |
!Bag methodsFor:'adding & removing'! |
|
107 |
||
54 | 108 |
add:newObject |
345 | 109 |
"add the argument, anObject to the receiver. |
1057 | 110 |
Returns the object. |
111 |
||
112 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
113 |
Iterate over a copy to do this." |
|
1 | 114 |
|
54 | 115 |
|n| |
116 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
117 |
n := contents at:newObject ifAbsent:0. |
54 | 118 |
contents at:newObject put:(n + 1). |
119 |
^ newObject |
|
1057 | 120 |
|
121 |
"Modified: 1.3.1996 / 21:43:06 / cg" |
|
1 | 122 |
! |
123 |
||
124 |
add:newObject withOccurences:anInteger |
|
345 | 125 |
"add the argument, anObject anInteger times to the receiver. |
1057 | 126 |
Returns the object. |
127 |
||
128 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
129 |
Iterate over a copy to do this." |
|
1 | 130 |
|
54 | 131 |
|n| |
132 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
133 |
n := contents at:newObject ifAbsent:0. |
54 | 134 |
contents at:newObject put:(n + anInteger). |
1 | 135 |
^ newObject |
1057 | 136 |
|
137 |
"Modified: 1.3.1996 / 21:43:12 / cg" |
|
1 | 138 |
! |
139 |
||
140 |
remove:oldObject ifAbsent:anExceptionBlock |
|
345 | 141 |
"Remove oldObject from the collection. |
142 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 143 |
otherwise return the removed object. |
144 |
||
145 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
146 |
Iterate over a copy to do this." |
|
1 | 147 |
|
148 |
|count| |
|
149 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
150 |
count := contents at:oldObject ifAbsent:0. |
1 | 151 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
152 |
(count == 1) ifTrue:[ |
|
1057 | 153 |
contents removeKey:oldObject |
1 | 154 |
] ifFalse:[ |
1057 | 155 |
contents at:oldObject put:(count - 1) |
1 | 156 |
]. |
157 |
^ oldObject |
|
1057 | 158 |
|
159 |
"Modified: 1.3.1996 / 21:43:18 / cg" |
|
345 | 160 |
! |
161 |
||
1110 | 162 |
removeAll |
163 |
"remove all objects from the receiver collection (i.e. make it empty)." |
|
164 |
||
165 |
contents := contents species new. |
|
166 |
||
167 |
" |
|
168 |
|b| |
|
169 |
||
170 |
b := Bag new. |
|
171 |
b add:1; add:2; add:3; add:2; add:1. |
|
172 |
Transcript showCr:b. |
|
173 |
||
174 |
b removeAll. |
|
175 |
||
176 |
Transcript showCr:b |
|
177 |
" |
|
178 |
||
179 |
"Modified: 21.3.1996 / 15:23:35 / cg" |
|
180 |
! |
|
181 |
||
345 | 182 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock |
183 |
"Remove all occurrences of oldObject from the collection. |
|
184 |
If it was not present, return the value of the exceptionBlock; |
|
1057 | 185 |
otherwise return the number of removes. |
186 |
||
187 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
188 |
Iterate over a copy to do this." |
|
345 | 189 |
|
190 |
|count| |
|
191 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
192 |
count := contents at:oldObject ifAbsent:0. |
345 | 193 |
(count == 0) ifTrue:[^ anExceptionBlock value]. |
194 |
contents removeKey:oldObject. |
|
195 |
^ oldObject |
|
1057 | 196 |
|
197 |
"Modified: 1.3.1996 / 21:43:26 / cg" |
|
1 | 198 |
! ! |
199 |
||
609 | 200 |
!Bag methodsFor:'converting'! |
201 |
||
202 |
asBag |
|
203 |
"return the receiver as a bag" |
|
204 |
||
205 |
"could be an instance of a subclass..." |
|
206 |
self class == Bag ifTrue:[ |
|
207 |
^ self |
|
208 |
]. |
|
209 |
^ super asBag |
|
210 |
! ! |
|
211 |
||
212 |
!Bag methodsFor:'copying'! |
|
213 |
||
214 |
postCopy |
|
215 |
"must copy the contents as well" |
|
216 |
||
217 |
contents := contents copy |
|
218 |
! ! |
|
219 |
||
1 | 220 |
!Bag methodsFor:'enumerating'! |
221 |
||
222 |
do:aBlock |
|
1057 | 223 |
"evaluate the block for all elements in the collection. |
224 |
||
225 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
226 |
Iterate over a copy to do this." |
|
1 | 227 |
|
54 | 228 |
contents keysAndValuesDo:[:key :value| |
1057 | 229 |
value timesRepeat:[ |
230 |
aBlock value:key |
|
231 |
] |
|
1 | 232 |
] |
1057 | 233 |
|
234 |
"Modified: 1.3.1996 / 21:42:39 / cg" |
|
345 | 235 |
! |
236 |
||
237 |
valuesAndCountsDo:aTwoArgBlock |
|
238 |
"evaluate the block for all distinct elements in the collection, |
|
1057 | 239 |
passing both the element and the occurence count as arguments. |
240 |
||
241 |
WARNING: do not add/remove elements while iterating over the receiver. |
|
242 |
Iterate over a copy to do this." |
|
345 | 243 |
|
244 |
^ contents keysAndValuesDo:aTwoArgBlock |
|
1057 | 245 |
|
246 |
"Modified: 1.3.1996 / 21:42:44 / cg" |
|
1 | 247 |
! ! |
609 | 248 |
|
249 |
!Bag methodsFor:'private'! |
|
250 |
||
251 |
initContents |
|
252 |
"set the contents to be an empty Dictionary" |
|
253 |
||
254 |
contents := Dictionary new |
|
255 |
! |
|
256 |
||
257 |
initContents:size |
|
258 |
"set the contents to be an empty Dictionary with initial size" |
|
259 |
||
260 |
contents := Dictionary new:size |
|
261 |
! |
|
262 |
||
263 |
initContentsForIdentity:size |
|
264 |
"set the contents to be an empty IdentityDictionary with initial size" |
|
265 |
||
266 |
contents := IdentityDictionary new:size |
|
267 |
! ! |
|
268 |
||
269 |
!Bag methodsFor:'testing'! |
|
270 |
||
271 |
includes:anObject |
|
272 |
"return true, if anObject is in the receiver" |
|
273 |
||
274 |
^ contents includesKey:anObject |
|
275 |
! |
|
276 |
||
277 |
occurrencesOf:anObject |
|
278 |
"return how many times anObject is in the receiver" |
|
279 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
280 |
^ contents at:anObject ifAbsent:0 |
609 | 281 |
! |
282 |
||
283 |
size |
|
284 |
"return the number of bag elements" |
|
285 |
||
286 |
|count| |
|
287 |
||
288 |
count := 0. |
|
289 |
contents do:[:element | count := count + element]. |
|
290 |
^ count |
|
291 |
! ! |
|
292 |
||
633 | 293 |
!Bag class methodsFor:'documentation'! |
294 |
||
295 |
version |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1110
diff
changeset
|
296 |
^ '$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.19 1996-04-09 20:48:44 cg Exp $' |
633 | 297 |
! ! |