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