297
|
1 |
"
|
|
2 |
This class is not covered by or part of the ST/X licence.
|
|
3 |
|
|
4 |
|
|
5 |
COPYRIGHT.
|
|
6 |
The above file is a Manchester Goodie protected by copyright.
|
|
7 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
8 |
part of it which is separately transmitted or stored:
|
|
9 |
* You must ensure that every copy includes this notice, and that
|
|
10 |
source and author(s) of the material are acknowledged.
|
|
11 |
* These conditions must be imposed on anyone who receives a copy.
|
|
12 |
* The material shall not be used for commercial gain without the prior
|
|
13 |
written consent of the author(s).
|
|
14 |
Further information on the copyright conditions may be obtained by
|
|
15 |
sending electronic mail:
|
|
16 |
To: goodies-lib@cs.man.ac.uk
|
|
17 |
Subject: copyright
|
|
18 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
19 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
20 |
|
|
21 |
(C) Copyright 1993 University of Manchester
|
|
22 |
For more information about the Manchester Goodies Library (from which
|
|
23 |
this file was distributed) send e-mail:
|
|
24 |
To: goodies-lib@cs.man.ac.uk
|
|
25 |
Subject: help
|
|
26 |
"
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
SequenceableCollection subclass:#RunArray
|
299
|
31 |
instanceVariableNames:'contentsArray'
|
297
|
32 |
classVariableNames:''
|
|
33 |
poolDictionaries:''
|
|
34 |
category:'Collections-Sequenceable'
|
|
35 |
!
|
|
36 |
|
|
37 |
RunArray comment:'This implements an ordered collection which uses runs to minimise the amount
|
|
38 |
of data that it holds. Basically it should be used if you know that an ordered
|
|
39 |
collections is giong to contain a lot of runs of eactly the same data. Implemented
|
|
40 |
to allow simultation playback, since the ordered collctions which that generates
|
|
41 |
are so big that the complier falls over, though most of it is extremely repetetive.
|
|
42 |
This should be totally abstracted. The user should not be a ble to see the difference
|
|
43 |
between an ordered collection and a ComrpessedOrderedCollection. This has a
|
|
44 |
lot in common with RunArray, and the two should probably share implementation.
|
|
45 |
but I could not do some of the things I wanted with the RunArray code, so this
|
|
46 |
is all done on its own.
|
|
47 |
Some of this could be made faster by adding a cache of the start and finish
|
|
48 |
indices of each run, but since I envisage that most additions etc. will be to
|
|
49 |
and from the end those are not included. In addition I have implemented the
|
|
50 |
bare essentials of this for what I need it for - i.e. add to the end and take
|
|
51 |
off the beginning.'!
|
|
52 |
|
|
53 |
!RunArray class methodsFor:'documentation'!
|
|
54 |
|
|
55 |
copyright
|
|
56 |
"
|
|
57 |
This class is not covered by or part of the ST/X licence.
|
|
58 |
|
|
59 |
|
|
60 |
COPYRIGHT.
|
|
61 |
The above file is a Manchester Goodie protected by copyright.
|
|
62 |
These conditions are imposed on the whole Goodie, and on any significant
|
|
63 |
part of it which is separately transmitted or stored:
|
|
64 |
* You must ensure that every copy includes this notice, and that
|
|
65 |
source and author(s) of the material are acknowledged.
|
|
66 |
* These conditions must be imposed on anyone who receives a copy.
|
|
67 |
* The material shall not be used for commercial gain without the prior
|
|
68 |
written consent of the author(s).
|
|
69 |
Further information on the copyright conditions may be obtained by
|
|
70 |
sending electronic mail:
|
|
71 |
To: goodies-lib@cs.man.ac.uk
|
|
72 |
Subject: copyright
|
|
73 |
or by writing to The Smalltalk Goodies Library Manager, Dept of
|
|
74 |
Computer Science, The University, Manchester M13 9PL, UK
|
|
75 |
|
|
76 |
(C) Copyright 1993 University of Manchester
|
|
77 |
For more information about the Manchester Goodies Library (from which
|
|
78 |
this file was distributed) send e-mail:
|
|
79 |
To: goodies-lib@cs.man.ac.uk
|
|
80 |
Subject: help
|
|
81 |
"
|
|
82 |
|
|
83 |
|
|
84 |
!
|
|
85 |
|
|
86 |
documentation
|
|
87 |
"
|
|
88 |
This implements an array which uses runs to minimise the amount
|
298
|
89 |
of data that it holds.
|
|
90 |
Basically it should be used if you know that an array is going to contain
|
|
91 |
a lot of runs of exactly the same data.
|
297
|
92 |
|
298
|
93 |
RunArrays are used by the Text class to keep character attributes
|
|
94 |
(which we expect to remain constant for longer character ranges).
|
297
|
95 |
|
|
96 |
Notice:
|
|
97 |
there is only a space saving if there are really runs (i.e. multiple
|
|
98 |
elements which compare same).
|
|
99 |
Otherwise, an Array is more compact.
|
|
100 |
|
298
|
101 |
[instance variables:]
|
|
102 |
runs <Array> contains the runs, consisting of
|
|
103 |
length/value entries.
|
|
104 |
|
|
105 |
[implementation note:]
|
|
106 |
structure-wise, it would have been more intuitive, to keep
|
|
107 |
instances of Run objects (as done in CompressedOrderedCollection)
|
|
108 |
instead of alternating length/values in the `runs' collection.
|
|
109 |
However, doing this means that lots of additional memory is required.
|
|
110 |
Here, we choose the more space efficient way.
|
297
|
111 |
|
|
112 |
[author:]
|
|
113 |
Claus Gittinger
|
|
114 |
|
|
115 |
[see also:]
|
298
|
116 |
Array OrderedCollection CompressedOrderedCollection
|
297
|
117 |
"
|
|
118 |
!
|
|
119 |
|
|
120 |
examples
|
|
121 |
"
|
|
122 |
this eats up a lot of memory ...
|
|
123 |
... but its relatively fast:
|
298
|
124 |
[exBegin]
|
297
|
125 |
|coll|
|
|
126 |
|
|
127 |
coll := OrderedCollection new.
|
|
128 |
Transcript showCr:(
|
298
|
129 |
Time millisecondsToRun:[
|
|
130 |
100000 timesRepeat:[coll add:'hello'].
|
|
131 |
100000 timesRepeat:[coll add:'world'].
|
|
132 |
]
|
297
|
133 |
).
|
|
134 |
coll inspect.
|
298
|
135 |
[exEnd]
|
297
|
136 |
|
|
137 |
|
|
138 |
this is very space efficient ...
|
298
|
139 |
... (and even slightly faster):
|
|
140 |
[exBegin]
|
297
|
141 |
|coll|
|
|
142 |
|
|
143 |
coll := RunArray new.
|
|
144 |
Transcript showCr:(
|
298
|
145 |
Time millisecondsToRun:[
|
|
146 |
100000 timesRepeat:[coll add:'hello'].
|
|
147 |
100000 timesRepeat:[coll add:'world'].
|
|
148 |
]
|
297
|
149 |
).
|
|
150 |
coll inspect.
|
298
|
151 |
[exEnd]
|
297
|
152 |
|
|
153 |
|
298
|
154 |
this is very space efficient ...
|
|
155 |
... AND much faster:
|
|
156 |
[exBegin]
|
297
|
157 |
|coll|
|
|
158 |
|
|
159 |
coll := RunArray new.
|
|
160 |
Transcript showCr:(
|
298
|
161 |
Time millisecondsToRun:[
|
|
162 |
coll add:'hello' withOccurrences:100000.
|
|
163 |
coll add:'world' withOccurrences:100000.
|
|
164 |
]
|
297
|
165 |
).
|
|
166 |
coll inspect.
|
298
|
167 |
[exEnd]
|
297
|
168 |
|
|
169 |
|
300
|
170 |
single-element runs;
|
|
171 |
this is very space INEFFICIENT (requires twice as much) ...
|
|
172 |
... and MUCH slower:
|
298
|
173 |
[exBegin]
|
297
|
174 |
|coll|
|
|
175 |
|
|
176 |
coll := RunArray new.
|
|
177 |
Transcript showCr:(
|
298
|
178 |
Time millisecondsToRun:[
|
|
179 |
1 to:1000 do:[:i | coll add:i].
|
|
180 |
]
|
|
181 |
).
|
|
182 |
[exEnd]
|
300
|
183 |
compare to:
|
|
184 |
[exBegin]
|
|
185 |
|coll|
|
|
186 |
|
|
187 |
coll := OrderedCollection new.
|
|
188 |
Transcript showCr:(
|
|
189 |
Time millisecondsToRun:[
|
|
190 |
1 to:1000 do:[:i | coll add:i].
|
|
191 |
]
|
|
192 |
).
|
|
193 |
[exEnd]
|
|
194 |
|
297
|
195 |
"
|
|
196 |
! !
|
|
197 |
|
|
198 |
!RunArray class methodsFor:'instance creation'!
|
|
199 |
|
|
200 |
new:size
|
|
201 |
"ignore the size argument - we dont know how many runs are
|
302
|
202 |
needed."
|
297
|
203 |
|
|
204 |
^ self new
|
302
|
205 |
|
|
206 |
"Modified: 11.5.1996 / 14:07:12 / cg"
|
|
207 |
!
|
|
208 |
|
|
209 |
new:size withAll:anObject
|
|
210 |
"create a new runArray, containing anObject size times"
|
|
211 |
|
|
212 |
^ self basicNew setElement:anObject occurrences:size
|
|
213 |
|
|
214 |
"
|
|
215 |
RunArray new:100 withAll:#hello
|
|
216 |
"
|
|
217 |
|
|
218 |
"Modified: 11.5.1996 / 14:06:48 / cg"
|
297
|
219 |
! !
|
|
220 |
|
|
221 |
!RunArray methodsFor:'accessing'!
|
|
222 |
|
|
223 |
at:anInteger
|
|
224 |
"Answer the element at index anInteger.
|
|
225 |
at: is used by a knowledgeable client to access an existing element
|
299
|
226 |
This is a pretty dumb thing to do to a runArray and it is
|
297
|
227 |
not at all efficient (think of that as a discouragement)."
|
|
228 |
|
|
229 |
|position "{ Class: SmallInteger }"
|
|
230 |
nRuns "{ Class: SmallInteger }"|
|
|
231 |
|
|
232 |
(anInteger > 0) ifTrue:[
|
|
233 |
position := 1.
|
299
|
234 |
nRuns := contentsArray size.
|
297
|
235 |
1 to:nRuns by:2 do:[:runIndex |
|
|
236 |
|runLen|
|
|
237 |
|
299
|
238 |
runLen := contentsArray at:runIndex.
|
297
|
239 |
anInteger >= position ifTrue:[
|
|
240 |
anInteger < (position + runLen) ifTrue:[
|
299
|
241 |
^ contentsArray at:(runIndex + 1)
|
297
|
242 |
].
|
|
243 |
].
|
|
244 |
position := position + runLen
|
|
245 |
]
|
|
246 |
].
|
|
247 |
^ self errorInvalidKey:anInteger
|
|
248 |
|
|
249 |
"
|
|
250 |
|c|
|
|
251 |
|
|
252 |
c := RunArray new.
|
|
253 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5.
|
|
254 |
c at:1.
|
|
255 |
c at:2.
|
|
256 |
c at:3.
|
|
257 |
c at:4.
|
|
258 |
c at:5.
|
|
259 |
c at:6.
|
|
260 |
c at:7.
|
|
261 |
c at:8.
|
|
262 |
c at:9.
|
|
263 |
c at:10.
|
|
264 |
"
|
|
265 |
|
299
|
266 |
"Modified: 11.5.1996 / 13:33:50 / cg"
|
297
|
267 |
!
|
|
268 |
|
|
269 |
at:index put:anObject
|
|
270 |
"Put anObject at element index anInteger.
|
|
271 |
at:put: can not be used to append, front or back, to an ordered
|
|
272 |
collection; it is used by a knowledgeable client to replace an
|
299
|
273 |
element.
|
|
274 |
This is a pretty dumb thing to do to a runArray and it is
|
|
275 |
very inefficient, since we have to check if runs are to be merged or
|
|
276 |
splitted."
|
297
|
277 |
|
|
278 |
|runSz runIndex runOffset len l1 l2 prevIdx nextIdx
|
|
279 |
val newRuns newValues prevLen prevVal nextLen nextVal idx|
|
|
280 |
|
299
|
281 |
runSz := contentsArray size.
|
297
|
282 |
|
|
283 |
runIndex := nil.
|
|
284 |
(index > 0) ifTrue:[
|
|
285 |
runOffset := 1.
|
|
286 |
idx := 1.
|
|
287 |
[runIndex isNil and:[idx < runSz]] whileTrue:[
|
299
|
288 |
len := contentsArray at:idx.
|
297
|
289 |
nextIdx := runOffset + len.
|
|
290 |
index >= runOffset ifTrue:[
|
|
291 |
index < nextIdx ifTrue:[
|
|
292 |
runIndex := idx.
|
|
293 |
nextIdx := runOffset. "/ dont advance
|
|
294 |
].
|
|
295 |
].
|
|
296 |
runOffset := nextIdx.
|
|
297 |
idx := idx + 2.
|
|
298 |
]
|
|
299 |
].
|
|
300 |
runIndex isNil ifTrue:[
|
|
301 |
^ self errorInvalidKey:index
|
|
302 |
].
|
|
303 |
|
299
|
304 |
val := contentsArray at:(runIndex + 1).
|
297
|
305 |
|
|
306 |
"/ easiest: value there is the same ...
|
|
307 |
|
|
308 |
val = anObject ifTrue:[
|
|
309 |
^ anObject
|
|
310 |
].
|
|
311 |
|
|
312 |
"/ if the length is 1, this is an island ...
|
|
313 |
"/ ... which is either easy, or requires a merge.
|
|
314 |
|
299
|
315 |
len := contentsArray at:runIndex.
|
297
|
316 |
len = 1 ifTrue:[
|
|
317 |
"/ check if it can be merged into the next or previous run
|
|
318 |
|
|
319 |
runIndex > 1 ifTrue:[
|
|
320 |
prevIdx := runIndex - 2.
|
299
|
321 |
prevVal := contentsArray at:(prevIdx + 1).
|
297
|
322 |
prevVal = anObject ifTrue:[
|
|
323 |
"/ can merge it into previous
|
|
324 |
|
299
|
325 |
prevLen := contentsArray at:prevIdx.
|
297
|
326 |
|
|
327 |
"/ check if merge into next is also possible (filling an island)
|
|
328 |
|
|
329 |
runIndex < (runSz - 1) ifTrue:[
|
|
330 |
nextIdx := runIndex + 2.
|
299
|
331 |
nextVal := contentsArray at:(nextIdx + 1).
|
297
|
332 |
nextVal = anObject ifTrue:[
|
|
333 |
"/ can merge with both.
|
|
334 |
|
299
|
335 |
nextLen := contentsArray at:nextIdx.
|
297
|
336 |
|
299
|
337 |
contentsArray at:prevIdx put:prevLen+nextLen+1.
|
297
|
338 |
runSz := (runSz - 4).
|
|
339 |
newRuns := Array new:runSz.
|
299
|
340 |
newRuns replaceFrom:1 to:(prevIdx + 1) with:contentsArray startingAt:1.
|
|
341 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx+2.
|
|
342 |
contentsArray := newRuns.
|
297
|
343 |
^ anObject
|
|
344 |
]
|
|
345 |
].
|
|
346 |
|
299
|
347 |
contentsArray at:prevIdx put:prevLen+1.
|
297
|
348 |
|
|
349 |
runSz := (runSz - 2).
|
|
350 |
newRuns := Array new:runSz.
|
299
|
351 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
|
|
352 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:runIndex+2.
|
|
353 |
contentsArray := newRuns.
|
297
|
354 |
|
|
355 |
^ anObject
|
|
356 |
].
|
|
357 |
].
|
|
358 |
|
|
359 |
"/ check if merge into next is possible
|
|
360 |
|
|
361 |
runIndex < runSz ifTrue:[
|
|
362 |
nextIdx := runIndex + 2.
|
299
|
363 |
nextVal := contentsArray at:nextIdx+1.
|
297
|
364 |
nextVal = anObject ifTrue:[
|
299
|
365 |
nextLen := contentsArray at:nextIdx.
|
|
366 |
contentsArray at:nextIdx put:nextLen + 1.
|
297
|
367 |
|
|
368 |
runSz := (runSz - 2).
|
|
369 |
newRuns := Array new:runSz.
|
299
|
370 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
|
|
371 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx.
|
|
372 |
contentsArray := newRuns.
|
297
|
373 |
^ anObject
|
|
374 |
].
|
|
375 |
].
|
|
376 |
|
|
377 |
"/ no merge; island remains
|
|
378 |
|
299
|
379 |
contentsArray at:(runIndex+1) put:anObject.
|
297
|
380 |
^ anObject
|
|
381 |
].
|
|
382 |
|
|
383 |
runOffset == index ifTrue:[
|
|
384 |
"/ at the beginning of that run ...
|
|
385 |
|
|
386 |
"/ check if its better added to the previous run ...
|
|
387 |
|
|
388 |
runIndex > 1 ifTrue:[
|
|
389 |
prevIdx := runIndex - 2.
|
299
|
390 |
prevVal := contentsArray at:prevIdx+1.
|
297
|
391 |
prevVal = anObject ifTrue:[
|
299
|
392 |
prevLen := contentsArray at:prevIdx.
|
|
393 |
contentsArray at:prevIdx put:prevLen + 1.
|
|
394 |
contentsArray at:runIndex put:len - 1.
|
297
|
395 |
^ anObject.
|
|
396 |
].
|
|
397 |
].
|
|
398 |
|
|
399 |
"/ must cut off 1 & insert a new run before ..
|
|
400 |
|
299
|
401 |
contentsArray at:runIndex put:len - 1.
|
297
|
402 |
|
|
403 |
runSz := (runSz + 2).
|
|
404 |
newRuns := Array new:runSz.
|
299
|
405 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
|
|
406 |
newRuns replaceFrom:runIndex+2 to:runSz with:contentsArray startingAt:runIndex.
|
|
407 |
contentsArray := newRuns.
|
297
|
408 |
|
299
|
409 |
contentsArray at:runIndex put:1.
|
|
410 |
contentsArray at:runIndex+1 put:anObject.
|
297
|
411 |
^ anObject
|
|
412 |
].
|
|
413 |
|
|
414 |
(runOffset + len - 1) == index ifTrue:[
|
|
415 |
"/ at the end ...
|
|
416 |
|
|
417 |
"/ check if its better added to the next run ...
|
|
418 |
|
|
419 |
runIndex < runSz ifTrue:[
|
|
420 |
nextIdx := runIndex + 2.
|
299
|
421 |
nextVal := contentsArray at:nextIdx+1.
|
297
|
422 |
nextVal = anObject ifTrue:[
|
299
|
423 |
nextLen := contentsArray at:nextIdx.
|
|
424 |
contentsArray at:nextIdx put:nextLen + 1.
|
|
425 |
contentsArray at:runIndex put:len - 1.
|
297
|
426 |
^ anObject.
|
|
427 |
].
|
|
428 |
].
|
|
429 |
|
|
430 |
"/ must cut off 1 & insert a new run after ..
|
|
431 |
|
299
|
432 |
contentsArray at:runIndex put:len - 1.
|
297
|
433 |
|
|
434 |
runSz := (runSz + 2).
|
|
435 |
newRuns := Array new:runSz.
|
299
|
436 |
newRuns replaceFrom:1 to:(runIndex + 1) with:contentsArray startingAt:1.
|
|
437 |
newRuns replaceFrom:runIndex+4 to:runSz with:contentsArray startingAt:runIndex+2.
|
|
438 |
contentsArray := newRuns.
|
297
|
439 |
|
299
|
440 |
contentsArray at:runIndex+2 put:1.
|
|
441 |
contentsArray at:runIndex+2+1 put:anObject.
|
297
|
442 |
^ anObject
|
|
443 |
].
|
|
444 |
|
|
445 |
"/ hardest - split run into two, insert new run in-between
|
|
446 |
|
|
447 |
runSz := (runSz + 4).
|
|
448 |
newRuns := Array new:runSz.
|
|
449 |
|
|
450 |
runIndex > 1 ifTrue:[
|
299
|
451 |
newRuns replaceFrom:1 to:runIndex-1 with:contentsArray.
|
297
|
452 |
].
|
299
|
453 |
newRuns replaceFrom:runIndex+6 to:(runSz+4) with:contentsArray startingAt:runIndex+2.
|
297
|
454 |
|
|
455 |
l2 := len - (index - runOffset).
|
|
456 |
l1 := len - l2.
|
|
457 |
l2 := l2 - 1.
|
|
458 |
|
|
459 |
newRuns at:runIndex put:l1.
|
|
460 |
newRuns at:runIndex+1 put:val.
|
|
461 |
|
|
462 |
newRuns at:runIndex+4 put:l2.
|
|
463 |
newRuns at:runIndex+5 put:val.
|
|
464 |
|
|
465 |
"/ insert
|
|
466 |
newRuns at:runIndex+2 put:1.
|
|
467 |
newRuns at:runIndex+3 put:anObject.
|
|
468 |
|
299
|
469 |
contentsArray := newRuns.
|
297
|
470 |
^ anObject
|
|
471 |
|
|
472 |
"
|
|
473 |
|c|
|
|
474 |
|
|
475 |
Transcript cr.
|
|
476 |
|
|
477 |
c := RunArray new.
|
|
478 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself.
|
|
479 |
Transcript showCr:c.
|
|
480 |
|
|
481 |
c at:1 put:$a.
|
|
482 |
Transcript showCr:c.
|
|
483 |
c.
|
|
484 |
|
|
485 |
c at:3 put:$a.
|
|
486 |
Transcript showCr:c.
|
|
487 |
c.
|
|
488 |
|
|
489 |
c at:4 put:$a.
|
|
490 |
Transcript showCr:c.
|
|
491 |
c.
|
|
492 |
|
|
493 |
c at:5 put:$a.
|
|
494 |
Transcript showCr:c.
|
|
495 |
c.
|
|
496 |
|
|
497 |
c at:2 put:$0.
|
|
498 |
Transcript showCr:c.
|
|
499 |
c.
|
|
500 |
|
|
501 |
c at:2 put:$a.
|
|
502 |
Transcript showCr:c.
|
|
503 |
c.
|
|
504 |
|
|
505 |
Transcript showCr:c.
|
|
506 |
"
|
|
507 |
|
299
|
508 |
"Modified: 11.5.1996 / 13:34:21 / cg"
|
297
|
509 |
!
|
|
510 |
|
|
511 |
size
|
299
|
512 |
"Answer how many elements the receiver contains.
|
|
513 |
Thsi is not the number of runs (but the simulated number of elements)."
|
297
|
514 |
|
|
515 |
|n "{ Class: SmallInteger }"
|
|
516 |
runSz "{ Class: SmallInteger }"|
|
|
517 |
|
|
518 |
n := 0.
|
299
|
519 |
runSz := contentsArray size.
|
297
|
520 |
1 to:runSz by:2 do:[:i |
|
299
|
521 |
n := n + (contentsArray at:i)
|
297
|
522 |
].
|
|
523 |
^ n
|
299
|
524 |
|
|
525 |
"Modified: 11.5.1996 / 13:34:27 / cg"
|
297
|
526 |
! !
|
|
527 |
|
|
528 |
!RunArray methodsFor:'adding'!
|
|
529 |
|
|
530 |
add:newObject
|
|
531 |
"add newObject at the end; returns the object (sigh)"
|
|
532 |
|
|
533 |
^ self add:newObject withOccurrences:1.
|
|
534 |
|
|
535 |
"
|
|
536 |
|c|
|
|
537 |
|
|
538 |
c := RunArray new.
|
|
539 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself.
|
|
540 |
"
|
|
541 |
|
|
542 |
"Modified: 10.5.1996 / 17:01:20 / cg"
|
|
543 |
!
|
|
544 |
|
|
545 |
add:newObject withOccurrences:n
|
|
546 |
"add newObject n times at the end; returns the object (sigh)"
|
|
547 |
|
|
548 |
|lastIdx runSz newRuns|
|
|
549 |
|
299
|
550 |
contentsArray notNil ifTrue:[
|
297
|
551 |
"/ check for merge
|
|
552 |
|
299
|
553 |
runSz := contentsArray size.
|
297
|
554 |
|
299
|
555 |
(contentsArray at:runSz) = newObject ifTrue:[
|
297
|
556 |
lastIdx := runSz - 1.
|
299
|
557 |
contentsArray at:lastIdx put:(contentsArray at:lastIdx) + n.
|
297
|
558 |
^ newObject
|
|
559 |
].
|
|
560 |
|
|
561 |
newRuns := Array new:(runSz + 2).
|
299
|
562 |
newRuns replaceFrom:1 to:runSz with:contentsArray.
|
297
|
563 |
newRuns at:runSz+1 put:n.
|
|
564 |
newRuns at:runSz+2 put:newObject.
|
299
|
565 |
contentsArray := newRuns.
|
297
|
566 |
] ifFalse:[
|
299
|
567 |
contentsArray := Array with:n with:newObject.
|
297
|
568 |
].
|
|
569 |
^ newObject
|
|
570 |
|
|
571 |
"
|
|
572 |
|c|
|
|
573 |
|
|
574 |
c := RunArray new.
|
|
575 |
c add:1 withOccurrences:1000; yourself.
|
|
576 |
c add:2 withOccurrences:1000; yourself.
|
|
577 |
"
|
|
578 |
|
299
|
579 |
"Modified: 11.5.1996 / 13:34:37 / cg"
|
297
|
580 |
!
|
|
581 |
|
|
582 |
grow:howBig
|
|
583 |
"grow or shrink the receiver to contain howBig elements.
|
299
|
584 |
If growing, new slots will be filled (logically) with nil."
|
297
|
585 |
|
|
586 |
|sz info runIndex runOffset runSz newRuns|
|
|
587 |
|
|
588 |
sz := self size.
|
|
589 |
|
|
590 |
howBig == sz ifTrue:[^ self].
|
|
591 |
|
|
592 |
howBig == 0 ifTrue:[
|
299
|
593 |
contentsArray := nil.
|
297
|
594 |
^ self.
|
|
595 |
].
|
|
596 |
|
299
|
597 |
contentsArray isNil ifTrue:[
|
|
598 |
contentsArray := Array with:howBig with:nil.
|
297
|
599 |
^ self
|
|
600 |
].
|
|
601 |
|
299
|
602 |
runSz := contentsArray size.
|
297
|
603 |
|
|
604 |
howBig > sz ifTrue:[
|
|
605 |
newRuns := Array new:(runSz + 2).
|
299
|
606 |
newRuns replaceFrom:1 to:runSz with:contentsArray startingAt:1.
|
297
|
607 |
newRuns at:(runSz+1) put:(howBig - sz).
|
299
|
608 |
contentsArray := newRuns.
|
297
|
609 |
^ self
|
|
610 |
].
|
|
611 |
|
|
612 |
"/ shrinking; possibly cut of a run
|
|
613 |
|
|
614 |
info := self runIndexAndStartIndexForIndex:howBig.
|
|
615 |
runIndex := info at:1.
|
|
616 |
runOffset := info at:2.
|
|
617 |
|
|
618 |
howBig == (runOffset - 1) ifTrue:[
|
|
619 |
"/ we are lucky - new size is up-to previous run
|
|
620 |
|
299
|
621 |
contentsArray := contentsArray copyFrom:1 to:runIndex-1.
|
297
|
622 |
] ifFalse:[
|
299
|
623 |
contentsArray := contentsArray copyFrom:1 to:(runIndex+1).
|
|
624 |
contentsArray at:runIndex put:(howBig - runOffset + 1)
|
297
|
625 |
].
|
|
626 |
|
|
627 |
"
|
|
628 |
|c|
|
|
629 |
|
|
630 |
c := RunArray new.
|
|
631 |
c addAll:#(1 2 3 4 4 4 4 5 5 5 1 2 3); yourself.
|
|
632 |
|
|
633 |
c grow:50; yourself.
|
|
634 |
|
|
635 |
c grow:7; yourself.
|
|
636 |
|
|
637 |
c grow:6; yourself.
|
|
638 |
|
|
639 |
c grow:0; yourself.
|
|
640 |
"
|
|
641 |
|
299
|
642 |
"Modified: 11.5.1996 / 13:34:53 / cg"
|
297
|
643 |
! !
|
|
644 |
|
|
645 |
!RunArray methodsFor:'converting'!
|
|
646 |
|
|
647 |
asOrderedCollection
|
299
|
648 |
"return a new orderedCollection, containing the receivers elements."
|
297
|
649 |
|
|
650 |
|newCollection|
|
|
651 |
|
299
|
652 |
contentsArray isNil ifTrue:[^ OrderedCollection new].
|
|
653 |
|
|
654 |
newCollection := OrderedCollection new:(self size).
|
|
655 |
contentsArray pairWiseDo:[:len :value |
|
|
656 |
newCollection add:value withOccurrences:len
|
|
657 |
].
|
297
|
658 |
^ newCollection
|
|
659 |
|
299
|
660 |
"
|
|
661 |
|r|
|
|
662 |
|
|
663 |
r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7).
|
|
664 |
Transcript showCr:r.
|
|
665 |
Transcript showCr:r asOrderedCollection
|
|
666 |
"
|
|
667 |
|
|
668 |
"Modified: 11.5.1996 / 13:34:59 / cg"
|
297
|
669 |
! !
|
|
670 |
|
|
671 |
!RunArray methodsFor:'enumerating'!
|
|
672 |
|
|
673 |
do:aBlock
|
|
674 |
"Evaluate aBlock with each of the receiver's elements as the
|
|
675 |
argument. "
|
|
676 |
|
299
|
677 |
contentsArray notNil ifTrue:[
|
|
678 |
contentsArray pairWiseDo:[:len :val |
|
297
|
679 |
len timesRepeat: [aBlock value:val]
|
|
680 |
]
|
|
681 |
]
|
|
682 |
|
299
|
683 |
"Modified: 11.5.1996 / 13:35:03 / cg"
|
297
|
684 |
! !
|
|
685 |
|
303
|
686 |
!RunArray methodsFor:'printing & storing'!
|
297
|
687 |
|
|
688 |
storeOn:aStream
|
|
689 |
"Append to aStream an expression which, if evaluated, will generate
|
|
690 |
an object similar to the receiver."
|
|
691 |
|
|
692 |
aStream nextPutAll: '(RunArray new'.
|
299
|
693 |
contentsArray notNil ifTrue:[
|
|
694 |
contentsArray pairWiseDo:[:len :val |
|
297
|
695 |
aStream nextPutAll: ' add:'; nextPutAll:val storeString.
|
|
696 |
len == 1 ifFalse:[
|
303
|
697 |
aStream nextPutAll:' withOccurrences:'; nextPutAll:len printString.
|
297
|
698 |
].
|
|
699 |
aStream nextPutAll:';'
|
|
700 |
].
|
|
701 |
aStream nextPutAll:' yourself'
|
|
702 |
].
|
|
703 |
aStream nextPutAll:')'
|
|
704 |
|
|
705 |
"
|
|
706 |
(RunArray new
|
|
707 |
add:1;
|
|
708 |
add:1;
|
|
709 |
add:2;
|
|
710 |
add:3;
|
303
|
711 |
add:4 withOccurrences:100;
|
297
|
712 |
add:5;
|
303
|
713 |
yourself) storeString
|
297
|
714 |
|
|
715 |
RunArray new storeString
|
|
716 |
"
|
299
|
717 |
|
303
|
718 |
"Modified: 11.5.1996 / 14:28:38 / cg"
|
297
|
719 |
! !
|
|
720 |
|
|
721 |
!RunArray methodsFor:'private'!
|
|
722 |
|
|
723 |
find:oldObject
|
|
724 |
"If I contain oldObject return its index, otherwise create an error
|
|
725 |
notifier. It will answer with the position of the very first element of
|
|
726 |
that value."
|
|
727 |
|
|
728 |
|position|
|
|
729 |
|
|
730 |
position := self find:oldObject ifAbsent:0.
|
|
731 |
position ~~ 0 ifTrue:[
|
|
732 |
^ position
|
|
733 |
].
|
|
734 |
|
|
735 |
self errorValueNotFound:oldObject
|
|
736 |
|
|
737 |
"
|
|
738 |
|c|
|
|
739 |
|
|
740 |
c := RunArray new.
|
|
741 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5.
|
|
742 |
c find:2.
|
|
743 |
|
|
744 |
c find:99
|
|
745 |
"
|
|
746 |
|
|
747 |
"Modified: 10.5.1996 / 13:39:58 / cg"
|
|
748 |
!
|
|
749 |
|
|
750 |
find:oldObject ifAbsent:exceptionBlock
|
|
751 |
"If I contain oldObject return its index, otherwise execute the
|
|
752 |
exception block. It will answer with the position of the very first
|
|
753 |
element of that value."
|
|
754 |
|
|
755 |
|position|
|
|
756 |
|
|
757 |
position := 1.
|
299
|
758 |
contentsArray notNil ifTrue:[
|
|
759 |
contentsArray pairWiseDo:[:len :val |
|
297
|
760 |
val = oldObject ifTrue:[
|
|
761 |
^ position
|
|
762 |
].
|
|
763 |
position := position + len.
|
|
764 |
].
|
|
765 |
].
|
|
766 |
^ exceptionBlock value
|
|
767 |
|
299
|
768 |
"Modified: 11.5.1996 / 13:35:14 / cg"
|
297
|
769 |
!
|
|
770 |
|
|
771 |
isEmpty
|
|
772 |
"Am I empty or not. Returns a boolean"
|
|
773 |
|
299
|
774 |
^ contentsArray notNil
|
|
775 |
|
|
776 |
"Modified: 11.5.1996 / 13:35:17 / cg"
|
297
|
777 |
!
|
|
778 |
|
|
779 |
runIndexAndStartIndexForIndex:anIndex
|
|
780 |
"given a logical index, find the index of that run and the logical index
|
|
781 |
of the first item in that run."
|
|
782 |
|
|
783 |
|position nRuns "{ Class: SmallInteger }"|
|
|
784 |
|
|
785 |
position := 1.
|
299
|
786 |
nRuns := contentsArray size.
|
297
|
787 |
1 to:nRuns by:2 do:[:runIndex |
|
|
788 |
|runLen runLast|
|
|
789 |
|
299
|
790 |
runLen := contentsArray at:runIndex.
|
297
|
791 |
anIndex >= position ifTrue:[
|
|
792 |
runLast := position + runLen - 1.
|
|
793 |
anIndex <= runLast ifTrue:[
|
|
794 |
^ Array with:runIndex with:position
|
|
795 |
].
|
|
796 |
].
|
|
797 |
position := position + runLen
|
|
798 |
].
|
|
799 |
|
|
800 |
^ #(0 0)
|
|
801 |
|
|
802 |
"Created: 10.5.1996 / 17:12:28 / cg"
|
299
|
803 |
"Modified: 11.5.1996 / 13:35:21 / cg"
|
302
|
804 |
!
|
|
805 |
|
|
806 |
setElement:newObject occurrences:n
|
|
807 |
"private instance setup"
|
|
808 |
|
|
809 |
contentsArray := Array with:n with:newObject.
|
|
810 |
|
|
811 |
"Created: 11.5.1996 / 14:05:58 / cg"
|
297
|
812 |
! !
|
|
813 |
|
|
814 |
!RunArray methodsFor:'user interface'!
|
|
815 |
|
|
816 |
inspect
|
|
817 |
"Reimplement so that they don't get an ordered collection inspector
|
|
818 |
which would get very confused."
|
|
819 |
|
|
820 |
self basicInspect
|
|
821 |
|
|
822 |
"Modified: 10.5.1996 / 18:24:43 / cg"
|
|
823 |
! !
|
|
824 |
|
|
825 |
!RunArray class methodsFor:'documentation'!
|
|
826 |
|
|
827 |
version
|
303
|
828 |
^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.6 1996-05-11 12:28:44 cg Exp $'
|
297
|
829 |
! !
|