2876
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2013 by Claus Gittinger
|
|
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 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
4802
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
2876
|
16 |
Bag subclass:#SmallBag
|
|
17 |
instanceVariableNames:''
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Collections-Unordered'
|
|
21 |
!
|
|
22 |
|
|
23 |
!SmallBag class methodsFor:'documentation'!
|
|
24 |
|
|
25 |
copyright
|
|
26 |
"
|
|
27 |
COPYRIGHT (c) 2013 by Claus Gittinger
|
|
28 |
All Rights Reserved
|
|
29 |
|
|
30 |
This software is furnished under a license and may be used
|
|
31 |
only in accordance with the terms of that license and with the
|
|
32 |
inclusion of the above copyright notice. This software may not
|
|
33 |
be provided or otherwise made available to, or used by, any
|
|
34 |
other person. No title to or ownership of the software is
|
|
35 |
hereby transferred.
|
|
36 |
"
|
|
37 |
!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
SmallBag behaves like Bag, but is tuned for a small number of distinct objects (in the order of 10),
|
|
42 |
by not using a dictionary, but a linear array holding count-value pairs in consecutive slots.
|
|
43 |
Only use it when many small bags are required, as this will be slower if too many (distinct) elements are added.
|
|
44 |
|
|
45 |
[Instance variables:]
|
|
46 |
contents <Array> holds count/value info in consecutive slots
|
|
47 |
|
|
48 |
|
|
49 |
[author:]
|
|
50 |
Claus Gittinger
|
|
51 |
|
|
52 |
[See also:]
|
|
53 |
Bag Set IdentitySet
|
|
54 |
Dictionary IdentityDictionary
|
|
55 |
OrderedCollection Array
|
|
56 |
"
|
|
57 |
! !
|
|
58 |
|
|
59 |
!SmallBag methodsFor:'accessing'!
|
|
60 |
|
|
61 |
contents
|
|
62 |
"return the dictionary which associates occurrence-counts
|
|
63 |
to the bags elements."
|
|
64 |
|
|
65 |
|d|
|
|
66 |
|
|
67 |
d := Dictionary new.
|
|
68 |
1 to:contents size-1 by:2 do:[:countIndex |
|
|
69 |
|count object|
|
|
70 |
|
|
71 |
(count := contents at:countIndex) > 0 ifTrue:[
|
|
72 |
object := (contents at:countIndex+1).
|
|
73 |
d at:object put:count
|
|
74 |
].
|
|
75 |
].
|
|
76 |
^ d
|
|
77 |
|
|
78 |
"
|
|
79 |
SmallBag new contents
|
|
80 |
SmallBag new size
|
|
81 |
"
|
|
82 |
! !
|
|
83 |
|
|
84 |
!SmallBag methodsFor:'adding & removing'!
|
|
85 |
|
|
86 |
add:newObject
|
|
87 |
"add the argument, anObject to the receiver.
|
|
88 |
Returns the object.
|
|
89 |
|
|
90 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
91 |
Iterate over a copy to do this."
|
|
92 |
|
|
93 |
^ self add:newObject withOccurrences:1
|
|
94 |
!
|
|
95 |
|
|
96 |
add:newObject withOccurrences:nMore
|
|
97 |
"add the argument, anObject anInteger times to the receiver.
|
|
98 |
Returns the object.
|
|
99 |
|
|
100 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
101 |
Iterate over a copy to do this."
|
|
102 |
|
|
103 |
|firstEmptySlot newContents|
|
|
104 |
|
|
105 |
1 to:contents size-1 by:2 do:[:countIndex |
|
|
106 |
|count|
|
|
107 |
|
|
108 |
count := contents at:countIndex.
|
|
109 |
(contents at:countIndex+1) = newObject ifTrue:[
|
|
110 |
contents at:countIndex put:count + nMore.
|
|
111 |
^ newObject.
|
|
112 |
].
|
|
113 |
count == 0 ifTrue:[ firstEmptySlot := firstEmptySlot ? countIndex ].
|
|
114 |
].
|
|
115 |
"/ not already there
|
|
116 |
|
|
117 |
firstEmptySlot isNil ifTrue:[
|
|
118 |
newContents := Array new:contents size * 2 withAll:0.
|
|
119 |
newContents replaceFrom:1 with:contents startingAt:1.
|
|
120 |
firstEmptySlot := contents size + 1.
|
|
121 |
contents := newContents.
|
|
122 |
].
|
|
123 |
contents at:firstEmptySlot put:nMore.
|
|
124 |
contents at:firstEmptySlot+1 put:newObject.
|
|
125 |
^ newObject
|
|
126 |
!
|
|
127 |
|
|
128 |
remove:oldObject ifAbsent:anExceptionBlock
|
|
129 |
"Remove oldObject from the collection.
|
|
130 |
If it was not present, return the value of the exceptionBlock;
|
|
131 |
otherwise return the removed object.
|
|
132 |
|
|
133 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
134 |
Iterate over a copy to do this."
|
|
135 |
|
|
136 |
1 to:contents size by:2 do:[:countIndex |
|
|
137 |
|count el|
|
|
138 |
|
|
139 |
count := contents at:countIndex.
|
|
140 |
(el := contents at:countIndex+1) = oldObject ifTrue:[
|
|
141 |
contents at:countIndex put:count - 1.
|
|
142 |
^ el.
|
|
143 |
].
|
|
144 |
].
|
|
145 |
|
|
146 |
"/ not found
|
|
147 |
^ anExceptionBlock value
|
|
148 |
!
|
|
149 |
|
|
150 |
removeAll
|
|
151 |
self initContents
|
|
152 |
!
|
|
153 |
|
|
154 |
removeAllOccurrencesOf:oldObject ifAbsent:anExceptionBlock
|
|
155 |
"Remove oldObject completely from the collection.
|
|
156 |
If it was not present, return the value of the exceptionBlock;
|
|
157 |
otherwise return the removed object.
|
|
158 |
|
|
159 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
160 |
Iterate over a copy to do this."
|
|
161 |
|
|
162 |
1 to:contents size by:2 do:[:countIndex |
|
|
163 |
|count el|
|
|
164 |
|
|
165 |
count := contents at:countIndex.
|
|
166 |
(el := contents at:countIndex+1) = oldObject ifTrue:[
|
|
167 |
contents at:countIndex put:0.
|
|
168 |
^ el.
|
|
169 |
].
|
|
170 |
].
|
|
171 |
|
|
172 |
"/ not found
|
|
173 |
^ anExceptionBlock value
|
|
174 |
! !
|
|
175 |
|
|
176 |
!SmallBag methodsFor:'converting'!
|
|
177 |
|
|
178 |
asSet
|
|
179 |
"return the receiver as a set"
|
|
180 |
|
|
181 |
|set|
|
|
182 |
|
|
183 |
set := Set new.
|
|
184 |
self valuesAndCountsDo:[:el :n | set add:el ].
|
|
185 |
^ set
|
|
186 |
|
|
187 |
"
|
|
188 |
|b|
|
|
189 |
|
|
190 |
b := Bag new.
|
|
191 |
b add:1; add:2; add:3; add:1; add:1.
|
|
192 |
b asSet.
|
|
193 |
"
|
|
194 |
! !
|
|
195 |
|
|
196 |
!SmallBag methodsFor:'enumerating'!
|
|
197 |
|
|
198 |
do:aBlock
|
|
199 |
"evaluate the block for all elements in the collection.
|
|
200 |
|
|
201 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
202 |
Iterate over a copy to do this."
|
|
203 |
|
|
204 |
1 to:contents size by:2 do:[:countIndex|
|
|
205 |
|count el|
|
|
206 |
|
|
207 |
(count := contents at:countIndex) ~~ 0 ifTrue:[
|
|
208 |
el:= contents at:countIndex+1.
|
|
209 |
count timesRepeat:[ aBlock value: el ]
|
|
210 |
]
|
|
211 |
]
|
|
212 |
|
|
213 |
"Modified: 1.3.1996 / 21:42:39 / cg"
|
|
214 |
!
|
|
215 |
|
|
216 |
valuesAndCountsDo:aBlock
|
|
217 |
"evaluate the block for all distinct elements in the collection,
|
|
218 |
passing both the element and the occurrence count as arguments.
|
|
219 |
|
|
220 |
WARNING: do not add/remove elements while iterating over the receiver.
|
|
221 |
Iterate over a copy to do this."
|
|
222 |
|
|
223 |
1 to:contents size by:2 do:[:countIndex|
|
|
224 |
|count el|
|
|
225 |
|
|
226 |
(count := contents at:countIndex) ~~ 0 ifTrue:[
|
|
227 |
el:= contents at:countIndex+1.
|
|
228 |
aBlock value: el value:count
|
|
229 |
]
|
|
230 |
]
|
|
231 |
|
|
232 |
"Modified: 1.3.1996 / 21:42:39 / cg"
|
|
233 |
! !
|
|
234 |
|
|
235 |
!SmallBag methodsFor:'private'!
|
|
236 |
|
|
237 |
initContents
|
|
238 |
"set the contents to be an empty contents array"
|
|
239 |
|
|
240 |
contents := Array new:10 withAll:0
|
|
241 |
!
|
|
242 |
|
|
243 |
initContents:size
|
|
244 |
"set the contents to be an empty contents array with initial size"
|
|
245 |
|
|
246 |
contents := Array new:size*2 withAll:0
|
|
247 |
! !
|
|
248 |
|
|
249 |
!SmallBag methodsFor:'queries'!
|
|
250 |
|
|
251 |
occurrencesOf:anObject
|
|
252 |
"return how often anObject is in the receiver"
|
|
253 |
|
|
254 |
1 to:contents size-1 by:2 do:[:countIndex |
|
|
255 |
|element|
|
|
256 |
|
|
257 |
(contents at:countIndex+1) = anObject ifTrue:[
|
|
258 |
^ contents at:countIndex.
|
|
259 |
].
|
|
260 |
].
|
|
261 |
|
|
262 |
^ 0
|
4802
|
263 |
!
|
|
264 |
|
|
265 |
size
|
|
266 |
"return the number of bag elements"
|
|
267 |
|
|
268 |
|count|
|
|
269 |
|
|
270 |
count := 0.
|
|
271 |
1 to:contents size-1 by:2 do:[:countIndex | count := count + (contents at:countIndex)].
|
|
272 |
^ count
|
2876
|
273 |
! !
|
|
274 |
|
|
275 |
!SmallBag class methodsFor:'documentation'!
|
|
276 |
|
|
277 |
version
|
4802
|
278 |
^ '$Header$'
|
2876
|
279 |
!
|
|
280 |
|
|
281 |
version_CVS
|
4802
|
282 |
^ '$Header$'
|
2876
|
283 |
! !
|
|
284 |
|