1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:'contents'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Unordered'
|
|
18 |
!
|
|
19 |
|
|
20 |
Bag comment:'
|
5
|
21 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
1
|
22 |
All Rights Reserved
|
92
|
23 |
|
154
|
24 |
$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.9 1994-10-10 00:19:48 claus Exp $
|
54
|
25 |
'!
|
|
26 |
|
|
27 |
!Bag class methodsFor:'documentation'!
|
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
|
32 |
All Rights Reserved
|
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
154
|
45 |
$Header: /cvs/stx/stx/libbasic/Bag.st,v 1.9 1994-10-10 00:19:48 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
54
|
49 |
documentation
|
|
50 |
"
|
88
|
51 |
Bag implements collections whose elements are unordered and have no
|
154
|
52 |
external key. Elements may occur more than once in a bag. There is no defined
|
|
53 |
order within a bag.
|
|
54 |
The implementation uses a dictionary to store each objects occurence count,
|
|
55 |
using the object itself as key (i.e. using = and hash for inclusion tests).
|
1
|
56 |
|
88
|
57 |
Instance variables:
|
1
|
58 |
|
92
|
59 |
contents <Dictionary> for each element, the number of occurrences
|
54
|
60 |
"
|
|
61 |
! !
|
1
|
62 |
|
|
63 |
!Bag class methodsFor:'instance creation'!
|
|
64 |
|
|
65 |
new
|
|
66 |
"return a new empty Bag"
|
|
67 |
|
|
68 |
^ super new initContents
|
|
69 |
!
|
|
70 |
|
|
71 |
new:size
|
|
72 |
"return a new empty Bag with initial space for size elements"
|
|
73 |
|
|
74 |
^ super new initContents:size
|
|
75 |
! !
|
|
76 |
|
|
77 |
!Bag methodsFor:'private'!
|
|
78 |
|
|
79 |
initContents
|
|
80 |
"set the contents to be an empty Dictionary"
|
|
81 |
|
|
82 |
contents := Dictionary new
|
|
83 |
!
|
|
84 |
|
|
85 |
initContents:size
|
|
86 |
"set the contents to be an empty Dictionary with initial size"
|
|
87 |
|
|
88 |
contents := Dictionary new:size
|
|
89 |
! !
|
|
90 |
|
|
91 |
!Bag methodsFor:'accessing'!
|
|
92 |
|
|
93 |
at:index
|
|
94 |
"report an error: at: is not allowed for Bags"
|
|
95 |
|
|
96 |
^ self errorNotKeyed
|
|
97 |
!
|
|
98 |
|
|
99 |
at:index put:anObject
|
|
100 |
"report an error: at:put: is not allowed for Bags"
|
|
101 |
|
|
102 |
^ self errorNotKeyed
|
|
103 |
! !
|
|
104 |
|
|
105 |
!Bag methodsFor:'testing'!
|
|
106 |
|
|
107 |
size
|
|
108 |
"return the number of bag elements"
|
|
109 |
|
|
110 |
|count|
|
|
111 |
|
|
112 |
count := 0.
|
|
113 |
contents do:[:element | count := count + element].
|
|
114 |
^ count
|
|
115 |
!
|
|
116 |
|
|
117 |
occurrencesOf:anObject
|
|
118 |
"return how many times anObject is in the receiver"
|
|
119 |
|
|
120 |
^ contents at:anObject ifAbsent:[0]
|
|
121 |
!
|
|
122 |
|
|
123 |
includes:anObject
|
|
124 |
"return true, if anObject is in the receiver"
|
|
125 |
|
|
126 |
^ contents includesKey:anObject
|
|
127 |
! !
|
|
128 |
|
154
|
129 |
!Bag methodsFor:'copying'!
|
|
130 |
|
|
131 |
postCopy
|
|
132 |
"must copy the contents as well"
|
|
133 |
|
|
134 |
contents := contents copy
|
|
135 |
! !
|
|
136 |
|
11
|
137 |
!Bag methodsFor:'converting'!
|
|
138 |
|
|
139 |
asBag
|
|
140 |
"return the receiver as a bag"
|
|
141 |
|
|
142 |
"could be an instance of a subclass..."
|
|
143 |
self class == Bag ifTrue:[
|
|
144 |
^ self
|
|
145 |
].
|
|
146 |
^ super asBag
|
|
147 |
! !
|
|
148 |
|
1
|
149 |
!Bag methodsFor:'adding & removing'!
|
|
150 |
|
54
|
151 |
add:newObject
|
1
|
152 |
"add the argument, anObject to the receiver"
|
|
153 |
|
54
|
154 |
|n|
|
|
155 |
|
|
156 |
n := contents at:newObject ifAbsent:[0].
|
|
157 |
contents at:newObject put:(n + 1).
|
|
158 |
^ newObject
|
1
|
159 |
!
|
|
160 |
|
|
161 |
add:newObject withOccurences:anInteger
|
|
162 |
"add the argument, anObject anInteger times to the receiver"
|
|
163 |
|
54
|
164 |
|n|
|
|
165 |
|
|
166 |
n := contents at:newObject ifAbsent:[0].
|
|
167 |
contents at:newObject put:(n + anInteger).
|
1
|
168 |
^ newObject
|
|
169 |
!
|
|
170 |
|
|
171 |
remove:oldObject ifAbsent:anExceptionBlock
|
|
172 |
"Remove oldObject from the collection and return it
|
|
173 |
- if it was not present, return the value of the exceptionBlock."
|
|
174 |
|
|
175 |
|count|
|
|
176 |
|
54
|
177 |
count := contents at:oldObject ifAbsent:[0].
|
1
|
178 |
(count == 0) ifTrue:[^ anExceptionBlock value].
|
|
179 |
(count == 1) ifTrue:[
|
|
180 |
contents removeKey:oldObject
|
|
181 |
] ifFalse:[
|
|
182 |
contents at:oldObject put:(count - 1)
|
|
183 |
].
|
|
184 |
^ oldObject
|
|
185 |
! !
|
|
186 |
|
|
187 |
!Bag methodsFor:'enumerating'!
|
|
188 |
|
|
189 |
do:aBlock
|
|
190 |
"Perform the block for all members in the collection."
|
|
191 |
|
54
|
192 |
contents keysAndValuesDo:[:key :value|
|
|
193 |
value timesRepeat:[
|
|
194 |
aBlock value:key
|
1
|
195 |
]
|
|
196 |
]
|
|
197 |
! !
|