5148
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
5099
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1996 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 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
ArrayedCollection subclass:#CollectionBuilder
|
|
19 |
instanceVariableNames:'collections'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Sequenceable'
|
|
23 |
!
|
|
24 |
|
|
25 |
!CollectionBuilder class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1996 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 |
When building large collections by concatenation, this helper can speedup this operation.
|
|
44 |
It non-destructively creates a list of the slices added during the concatenation process,
|
|
45 |
thus (usually) reducing the amount of storage and time required to construct the collection.
|
|
46 |
Especially, if the slices are constants (strings) and even reused (same string occurring multiple times),
|
|
47 |
the saving in memory mey be significant.
|
|
48 |
|
|
49 |
However, there is a tradeof: the time to access, to enumerate and to query the size of
|
|
50 |
the resulting collection is slower.
|
|
51 |
Therefore, this is (usually) only useful if a throw-away collection is
|
|
52 |
constructed to be used only a small number of times for enumeration (or writing to a file).
|
|
53 |
|
|
54 |
It is very similar to Java's StringBuffer, but can handle any kind of collection.
|
|
55 |
(however, please see also Stream / #streamContents:, for yet another (better?) way of building collections ...)
|
|
56 |
|
|
57 |
[see also:]
|
|
58 |
WriteStream
|
|
59 |
|
|
60 |
[author:]
|
|
61 |
Claus Gittinger
|
|
62 |
|
|
63 |
[example:]
|
|
64 |
|coll|
|
|
65 |
|
|
66 |
coll := CollectionBuilder new.
|
|
67 |
1 to:100000 do:[:i |
|
|
68 |
coll addAll:#(1 2 3 4 5 6 7 8 9 0)
|
|
69 |
].
|
|
70 |
coll
|
|
71 |
"
|
|
72 |
!
|
|
73 |
|
|
74 |
examples
|
|
75 |
"
|
|
76 |
when building large collections by concatenation,
|
|
77 |
the following standard creation is time consuming:
|
|
78 |
[exBegin]
|
|
79 |
Time millisecondsToRun:[
|
|
80 |
|coll|
|
|
81 |
|
|
82 |
coll := OrderedCollection new.
|
|
83 |
1 to:100000 do:[:i |
|
|
84 |
coll addAll:i printString
|
|
85 |
]
|
|
86 |
]
|
|
87 |
[exEnd]
|
|
88 |
|
|
89 |
the following is faster, and does not actually create a new collection:
|
|
90 |
[exBegin]
|
|
91 |
Time millisecondsToRun:[
|
|
92 |
|coll|
|
|
93 |
|
|
94 |
coll := CollectionBuilder new.
|
|
95 |
1 to:100000 do:[:i |
|
|
96 |
coll addAll:i printString
|
|
97 |
]
|
|
98 |
]
|
|
99 |
[exEnd]
|
|
100 |
Time to enumerate:
|
|
101 |
[exBegin]
|
|
102 |
|coll|
|
|
103 |
|
|
104 |
coll := OrderedCollection new.
|
|
105 |
1 to:100000 do:[:i |
|
|
106 |
coll addAll:i printString
|
|
107 |
].
|
|
108 |
Time millisecondsToRun:[
|
|
109 |
coll do:[:each | each ].
|
|
110 |
]
|
|
111 |
[exEnd]
|
|
112 |
[exBegin]
|
|
113 |
|coll|
|
|
114 |
|
|
115 |
coll := CollectionBuilder new.
|
|
116 |
1 to:100000 do:[:i |
|
|
117 |
coll addAll:i printString
|
|
118 |
].
|
|
119 |
Time millisecondsToRun:[
|
|
120 |
coll do:[:each | each ].
|
|
121 |
]
|
|
122 |
[exEnd]
|
|
123 |
|
|
124 |
"
|
|
125 |
! !
|
|
126 |
|
|
127 |
!CollectionBuilder class methodsFor:'instance creation'!
|
|
128 |
|
|
129 |
withAll:collections and:aCollection
|
|
130 |
^ self new withAll:collections and:aCollection
|
|
131 |
|
|
132 |
|
|
133 |
! !
|
|
134 |
|
|
135 |
!CollectionBuilder methodsFor:'accessing'!
|
|
136 |
|
|
137 |
at:index
|
|
138 |
|collFirst collLast|
|
|
139 |
|
|
140 |
index < 1 ifTrue:[^ self subscriptBoundsError:index].
|
|
141 |
|
|
142 |
collFirst := 0.
|
|
143 |
collections do:[:eachSlice |
|
|
144 |
collLast := collFirst + eachSlice size.
|
|
145 |
index <= collLast ifTrue:[
|
|
146 |
^ eachSlice at:(index - collFirst)
|
|
147 |
].
|
|
148 |
collFirst := collLast.
|
|
149 |
].
|
|
150 |
^ self indexNotIntegerOrOutOfBounds:index
|
|
151 |
|
|
152 |
"
|
|
153 |
|c|
|
|
154 |
|
|
155 |
c := CollectionBuilder new.
|
|
156 |
c := c , #(1 2 3 4).
|
|
157 |
c := c , #(5 6 7 8).
|
|
158 |
c := c , #(9 10 11 12).
|
|
159 |
c inspect.
|
|
160 |
c at:13
|
|
161 |
"
|
|
162 |
|
|
163 |
"Modified: 1.2.1996 / 17:01:16 / cg"
|
|
164 |
! !
|
|
165 |
|
|
166 |
!CollectionBuilder methodsFor:'adding & removing'!
|
|
167 |
|
|
168 |
add:anObject
|
5148
|
169 |
collections := collections copyWith:(Array with:anObject).
|
|
170 |
^ anObject
|
5099
|
171 |
|
|
172 |
"Created: 1.2.1996 / 17:09:15 / cg"
|
|
173 |
!
|
|
174 |
|
|
175 |
addAll:aCollection
|
5148
|
176 |
"add all elements of the argument, aCollection to the receiver.
|
|
177 |
Returns the argument, aCollection."
|
|
178 |
|
5099
|
179 |
collections isNil ifTrue:[
|
|
180 |
collections := OrderedCollection with:aCollection
|
|
181 |
] ifFalse:[
|
|
182 |
collections add:aCollection
|
5148
|
183 |
].
|
|
184 |
^ aCollection
|
5099
|
185 |
|
|
186 |
"Created: 1.2.1996 / 17:09:29 / cg"
|
|
187 |
"Modified: 1.2.1996 / 17:14:54 / cg"
|
|
188 |
! !
|
|
189 |
|
|
190 |
!CollectionBuilder methodsFor:'copying'!
|
|
191 |
|
|
192 |
, aCollection
|
|
193 |
^ CollectionBuilder withAll:collections and:aCollection
|
|
194 |
|
|
195 |
"Created: 1.2.1996 / 16:43:18 / cg"
|
|
196 |
! !
|
|
197 |
|
|
198 |
!CollectionBuilder methodsFor:'enumerating'!
|
|
199 |
|
|
200 |
do:aBlock
|
|
201 |
collections do:[:coll |
|
|
202 |
coll do:aBlock
|
|
203 |
]
|
|
204 |
|
|
205 |
"Created: 1.2.1996 / 17:05:36 / cg"
|
|
206 |
!
|
|
207 |
|
|
208 |
keysAndValuesDo:aBlock
|
|
209 |
|idx|
|
|
210 |
|
|
211 |
idx := 1.
|
|
212 |
collections do:[:coll |
|
|
213 |
coll do:[:element |
|
|
214 |
aBlock value:idx value:element.
|
|
215 |
idx := idx + 1
|
|
216 |
]
|
|
217 |
]
|
|
218 |
|
|
219 |
"Created: 1.2.1996 / 17:06:13 / cg"
|
|
220 |
! !
|
|
221 |
|
|
222 |
!CollectionBuilder methodsFor:'inspecting'!
|
|
223 |
|
|
224 |
inspectorClass
|
|
225 |
"redefined to launch an OrderedCollectionInspector
|
|
226 |
(instead of the default InspectorView)."
|
|
227 |
|
|
228 |
^ OrderedCollectionInspectorView
|
|
229 |
|
|
230 |
"Created: 1.2.1996 / 17:07:53 / cg"
|
|
231 |
! !
|
|
232 |
|
|
233 |
!CollectionBuilder methodsFor:'private'!
|
|
234 |
|
|
235 |
withAll:collections1 and:collections2
|
|
236 |
collections1 isEmptyOrNil ifTrue:[
|
|
237 |
collections := OrderedCollection with:collections2
|
|
238 |
] ifFalse:[
|
|
239 |
collections := OrderedCollection withAll:collections1.
|
|
240 |
collections2 notEmptyOrNil ifTrue:[
|
|
241 |
collections add:collections2
|
|
242 |
].
|
|
243 |
].
|
|
244 |
|
|
245 |
"Created: 1.2.1996 / 16:44:15 / cg"
|
|
246 |
"Modified: 1.2.1996 / 17:00:17 / cg"
|
|
247 |
! !
|
|
248 |
|
|
249 |
!CollectionBuilder methodsFor:'queries'!
|
|
250 |
|
|
251 |
size
|
|
252 |
^ collections inject:0 into:[:sumSoFar :subColl | sumSoFar + subColl size]
|
|
253 |
|
|
254 |
"Created: 1.2.1996 / 16:42:31 / cg"
|
|
255 |
! !
|
|
256 |
|
|
257 |
!CollectionBuilder class methodsFor:'documentation'!
|
|
258 |
|
|
259 |
version_CVS
|
|
260 |
^ '$Header$'
|
|
261 |
! !
|
|
262 |
|