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.
|
350
|
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.
|
350
|
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.
|
350
|
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.
|
350
|
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.
|
350
|
188 |
Transcript showCR:(
|
300
|
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
|
334
|
201 |
"return a new runArray, containing size nils"
|
297
|
202 |
|
334
|
203 |
^ self basicNew setElement:nil occurrences:size
|
302
|
204 |
|
334
|
205 |
"Modified: 14.5.1996 / 14:04:37 / cg"
|
302
|
206 |
!
|
|
207 |
|
|
208 |
new:size withAll:anObject
|
|
209 |
"create a new runArray, containing anObject size times"
|
|
210 |
|
|
211 |
^ self basicNew setElement:anObject occurrences:size
|
|
212 |
|
|
213 |
"
|
|
214 |
RunArray new:100 withAll:#hello
|
|
215 |
"
|
|
216 |
|
|
217 |
"Modified: 11.5.1996 / 14:06:48 / cg"
|
297
|
218 |
! !
|
|
219 |
|
|
220 |
!RunArray methodsFor:'accessing'!
|
|
221 |
|
|
222 |
at:anInteger
|
|
223 |
"Answer the element at index anInteger.
|
|
224 |
at: is used by a knowledgeable client to access an existing element
|
299
|
225 |
This is a pretty dumb thing to do to a runArray and it is
|
297
|
226 |
not at all efficient (think of that as a discouragement)."
|
|
227 |
|
|
228 |
|position "{ Class: SmallInteger }"
|
|
229 |
nRuns "{ Class: SmallInteger }"|
|
|
230 |
|
|
231 |
(anInteger > 0) ifTrue:[
|
|
232 |
position := 1.
|
299
|
233 |
nRuns := contentsArray size.
|
297
|
234 |
1 to:nRuns by:2 do:[:runIndex |
|
|
235 |
|runLen|
|
|
236 |
|
299
|
237 |
runLen := contentsArray at:runIndex.
|
297
|
238 |
anInteger >= position ifTrue:[
|
|
239 |
anInteger < (position + runLen) ifTrue:[
|
299
|
240 |
^ contentsArray at:(runIndex + 1)
|
297
|
241 |
].
|
|
242 |
].
|
|
243 |
position := position + runLen
|
|
244 |
]
|
|
245 |
].
|
|
246 |
^ self errorInvalidKey:anInteger
|
|
247 |
|
|
248 |
"
|
|
249 |
|c|
|
|
250 |
|
|
251 |
c := RunArray new.
|
|
252 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5.
|
|
253 |
c at:1.
|
|
254 |
c at:2.
|
|
255 |
c at:3.
|
|
256 |
c at:4.
|
|
257 |
c at:5.
|
|
258 |
c at:6.
|
|
259 |
c at:7.
|
|
260 |
c at:8.
|
|
261 |
c at:9.
|
|
262 |
c at:10.
|
|
263 |
"
|
|
264 |
|
299
|
265 |
"Modified: 11.5.1996 / 13:33:50 / cg"
|
297
|
266 |
!
|
|
267 |
|
|
268 |
at:index put:anObject
|
|
269 |
"Put anObject at element index anInteger.
|
|
270 |
at:put: can not be used to append, front or back, to an ordered
|
|
271 |
collection; it is used by a knowledgeable client to replace an
|
299
|
272 |
element.
|
|
273 |
This is a pretty dumb thing to do to a runArray and it is
|
|
274 |
very inefficient, since we have to check if runs are to be merged or
|
|
275 |
splitted."
|
297
|
276 |
|
|
277 |
|runSz runIndex runOffset len l1 l2 prevIdx nextIdx
|
|
278 |
val newRuns newValues prevLen prevVal nextLen nextVal idx|
|
|
279 |
|
299
|
280 |
runSz := contentsArray size.
|
297
|
281 |
|
|
282 |
runIndex := nil.
|
|
283 |
(index > 0) ifTrue:[
|
|
284 |
runOffset := 1.
|
|
285 |
idx := 1.
|
|
286 |
[runIndex isNil and:[idx < runSz]] whileTrue:[
|
299
|
287 |
len := contentsArray at:idx.
|
297
|
288 |
nextIdx := runOffset + len.
|
|
289 |
index >= runOffset ifTrue:[
|
|
290 |
index < nextIdx ifTrue:[
|
|
291 |
runIndex := idx.
|
|
292 |
nextIdx := runOffset. "/ dont advance
|
|
293 |
].
|
|
294 |
].
|
|
295 |
runOffset := nextIdx.
|
|
296 |
idx := idx + 2.
|
|
297 |
]
|
|
298 |
].
|
|
299 |
runIndex isNil ifTrue:[
|
|
300 |
^ self errorInvalidKey:index
|
|
301 |
].
|
|
302 |
|
299
|
303 |
val := contentsArray at:(runIndex + 1).
|
297
|
304 |
|
|
305 |
"/ easiest: value there is the same ...
|
|
306 |
|
|
307 |
val = anObject ifTrue:[
|
|
308 |
^ anObject
|
|
309 |
].
|
|
310 |
|
|
311 |
"/ if the length is 1, this is an island ...
|
|
312 |
"/ ... which is either easy, or requires a merge.
|
|
313 |
|
299
|
314 |
len := contentsArray at:runIndex.
|
297
|
315 |
len = 1 ifTrue:[
|
|
316 |
"/ check if it can be merged into the next or previous run
|
|
317 |
|
|
318 |
runIndex > 1 ifTrue:[
|
|
319 |
prevIdx := runIndex - 2.
|
299
|
320 |
prevVal := contentsArray at:(prevIdx + 1).
|
297
|
321 |
prevVal = anObject ifTrue:[
|
|
322 |
"/ can merge it into previous
|
|
323 |
|
299
|
324 |
prevLen := contentsArray at:prevIdx.
|
297
|
325 |
|
|
326 |
"/ check if merge into next is also possible (filling an island)
|
|
327 |
|
|
328 |
runIndex < (runSz - 1) ifTrue:[
|
|
329 |
nextIdx := runIndex + 2.
|
299
|
330 |
nextVal := contentsArray at:(nextIdx + 1).
|
297
|
331 |
nextVal = anObject ifTrue:[
|
|
332 |
"/ can merge with both.
|
|
333 |
|
299
|
334 |
nextLen := contentsArray at:nextIdx.
|
297
|
335 |
|
299
|
336 |
contentsArray at:prevIdx put:prevLen+nextLen+1.
|
297
|
337 |
runSz := (runSz - 4).
|
|
338 |
newRuns := Array new:runSz.
|
299
|
339 |
newRuns replaceFrom:1 to:(prevIdx + 1) with:contentsArray startingAt:1.
|
|
340 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx+2.
|
|
341 |
contentsArray := newRuns.
|
297
|
342 |
^ anObject
|
|
343 |
]
|
|
344 |
].
|
|
345 |
|
299
|
346 |
contentsArray at:prevIdx put:prevLen+1.
|
297
|
347 |
|
|
348 |
runSz := (runSz - 2).
|
|
349 |
newRuns := Array new:runSz.
|
299
|
350 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
|
|
351 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:runIndex+2.
|
|
352 |
contentsArray := newRuns.
|
297
|
353 |
|
|
354 |
^ anObject
|
|
355 |
].
|
|
356 |
].
|
|
357 |
|
|
358 |
"/ check if merge into next is possible
|
|
359 |
|
305
|
360 |
runIndex < (runSz-1) ifTrue:[
|
297
|
361 |
nextIdx := runIndex + 2.
|
299
|
362 |
nextVal := contentsArray at:nextIdx+1.
|
297
|
363 |
nextVal = anObject ifTrue:[
|
299
|
364 |
nextLen := contentsArray at:nextIdx.
|
|
365 |
contentsArray at:nextIdx put:nextLen + 1.
|
297
|
366 |
|
|
367 |
runSz := (runSz - 2).
|
|
368 |
newRuns := Array new:runSz.
|
299
|
369 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
|
|
370 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx.
|
|
371 |
contentsArray := newRuns.
|
297
|
372 |
^ anObject
|
|
373 |
].
|
|
374 |
].
|
|
375 |
|
|
376 |
"/ no merge; island remains
|
|
377 |
|
299
|
378 |
contentsArray at:(runIndex+1) put:anObject.
|
297
|
379 |
^ anObject
|
|
380 |
].
|
|
381 |
|
|
382 |
runOffset == index ifTrue:[
|
|
383 |
"/ at the beginning of that run ...
|
|
384 |
|
|
385 |
"/ check if its better added to the previous run ...
|
|
386 |
|
|
387 |
runIndex > 1 ifTrue:[
|
|
388 |
prevIdx := runIndex - 2.
|
299
|
389 |
prevVal := contentsArray at:prevIdx+1.
|
297
|
390 |
prevVal = anObject ifTrue:[
|
299
|
391 |
prevLen := contentsArray at:prevIdx.
|
|
392 |
contentsArray at:prevIdx put:prevLen + 1.
|
|
393 |
contentsArray at:runIndex put:len - 1.
|
297
|
394 |
^ anObject.
|
|
395 |
].
|
|
396 |
].
|
|
397 |
|
|
398 |
"/ must cut off 1 & insert a new run before ..
|
|
399 |
|
299
|
400 |
contentsArray at:runIndex put:len - 1.
|
297
|
401 |
|
|
402 |
runSz := (runSz + 2).
|
|
403 |
newRuns := Array new:runSz.
|
299
|
404 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
|
|
405 |
newRuns replaceFrom:runIndex+2 to:runSz with:contentsArray startingAt:runIndex.
|
|
406 |
contentsArray := newRuns.
|
297
|
407 |
|
299
|
408 |
contentsArray at:runIndex put:1.
|
|
409 |
contentsArray at:runIndex+1 put:anObject.
|
297
|
410 |
^ anObject
|
|
411 |
].
|
|
412 |
|
|
413 |
(runOffset + len - 1) == index ifTrue:[
|
|
414 |
"/ at the end ...
|
|
415 |
|
|
416 |
"/ check if its better added to the next run ...
|
|
417 |
|
306
|
418 |
runIndex < (runSz-1) ifTrue:[
|
297
|
419 |
nextIdx := runIndex + 2.
|
299
|
420 |
nextVal := contentsArray at:nextIdx+1.
|
297
|
421 |
nextVal = anObject ifTrue:[
|
299
|
422 |
nextLen := contentsArray at:nextIdx.
|
|
423 |
contentsArray at:nextIdx put:nextLen + 1.
|
|
424 |
contentsArray at:runIndex put:len - 1.
|
297
|
425 |
^ anObject.
|
|
426 |
].
|
|
427 |
].
|
|
428 |
|
|
429 |
"/ must cut off 1 & insert a new run after ..
|
|
430 |
|
299
|
431 |
contentsArray at:runIndex put:len - 1.
|
297
|
432 |
|
|
433 |
runSz := (runSz + 2).
|
|
434 |
newRuns := Array new:runSz.
|
299
|
435 |
newRuns replaceFrom:1 to:(runIndex + 1) with:contentsArray startingAt:1.
|
|
436 |
newRuns replaceFrom:runIndex+4 to:runSz with:contentsArray startingAt:runIndex+2.
|
|
437 |
contentsArray := newRuns.
|
297
|
438 |
|
299
|
439 |
contentsArray at:runIndex+2 put:1.
|
|
440 |
contentsArray at:runIndex+2+1 put:anObject.
|
297
|
441 |
^ anObject
|
|
442 |
].
|
|
443 |
|
|
444 |
"/ hardest - split run into two, insert new run in-between
|
|
445 |
|
|
446 |
runSz := (runSz + 4).
|
|
447 |
newRuns := Array new:runSz.
|
|
448 |
|
|
449 |
runIndex > 1 ifTrue:[
|
299
|
450 |
newRuns replaceFrom:1 to:runIndex-1 with:contentsArray.
|
297
|
451 |
].
|
305
|
452 |
newRuns replaceFrom:runIndex+6 to:runSz with:contentsArray startingAt:runIndex+2.
|
297
|
453 |
|
|
454 |
l2 := len - (index - runOffset).
|
|
455 |
l1 := len - l2.
|
|
456 |
l2 := l2 - 1.
|
|
457 |
|
|
458 |
newRuns at:runIndex put:l1.
|
|
459 |
newRuns at:runIndex+1 put:val.
|
|
460 |
|
|
461 |
newRuns at:runIndex+4 put:l2.
|
|
462 |
newRuns at:runIndex+5 put:val.
|
|
463 |
|
|
464 |
"/ insert
|
|
465 |
newRuns at:runIndex+2 put:1.
|
|
466 |
newRuns at:runIndex+3 put:anObject.
|
|
467 |
|
299
|
468 |
contentsArray := newRuns.
|
297
|
469 |
^ anObject
|
|
470 |
|
|
471 |
"
|
|
472 |
|c|
|
|
473 |
|
|
474 |
Transcript cr.
|
|
475 |
|
|
476 |
c := RunArray new.
|
|
477 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself.
|
350
|
478 |
Transcript showCR:c.
|
297
|
479 |
|
|
480 |
c at:1 put:$a.
|
350
|
481 |
Transcript showCR:c.
|
297
|
482 |
c.
|
|
483 |
|
|
484 |
c at:3 put:$a.
|
350
|
485 |
Transcript showCR:c.
|
297
|
486 |
c.
|
|
487 |
|
|
488 |
c at:4 put:$a.
|
350
|
489 |
Transcript showCR:c.
|
297
|
490 |
c.
|
|
491 |
|
|
492 |
c at:5 put:$a.
|
350
|
493 |
Transcript showCR:c.
|
297
|
494 |
c.
|
|
495 |
|
|
496 |
c at:2 put:$0.
|
350
|
497 |
Transcript showCR:c.
|
297
|
498 |
c.
|
|
499 |
|
|
500 |
c at:2 put:$a.
|
350
|
501 |
Transcript showCR:c.
|
297
|
502 |
c.
|
|
503 |
|
350
|
504 |
Transcript showCR:c.
|
297
|
505 |
"
|
|
506 |
|
306
|
507 |
"Modified: 12.5.1996 / 15:55:32 / cg"
|
297
|
508 |
!
|
|
509 |
|
|
510 |
size
|
299
|
511 |
"Answer how many elements the receiver contains.
|
|
512 |
Thsi is not the number of runs (but the simulated number of elements)."
|
297
|
513 |
|
|
514 |
|n "{ Class: SmallInteger }"
|
|
515 |
runSz "{ Class: SmallInteger }"|
|
|
516 |
|
|
517 |
n := 0.
|
299
|
518 |
runSz := contentsArray size.
|
297
|
519 |
1 to:runSz by:2 do:[:i |
|
299
|
520 |
n := n + (contentsArray at:i)
|
297
|
521 |
].
|
|
522 |
^ n
|
299
|
523 |
|
|
524 |
"Modified: 11.5.1996 / 13:34:27 / cg"
|
297
|
525 |
! !
|
|
526 |
|
|
527 |
!RunArray methodsFor:'adding'!
|
|
528 |
|
|
529 |
add:newObject
|
|
530 |
"add newObject at the end; returns the object (sigh)"
|
|
531 |
|
|
532 |
^ self add:newObject withOccurrences:1.
|
|
533 |
|
|
534 |
"
|
|
535 |
|c|
|
|
536 |
|
|
537 |
c := RunArray new.
|
|
538 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself.
|
|
539 |
"
|
|
540 |
|
|
541 |
"Modified: 10.5.1996 / 17:01:20 / cg"
|
|
542 |
!
|
|
543 |
|
|
544 |
add:newObject withOccurrences:n
|
|
545 |
"add newObject n times at the end; returns the object (sigh)"
|
|
546 |
|
|
547 |
|lastIdx runSz newRuns|
|
|
548 |
|
299
|
549 |
contentsArray notNil ifTrue:[
|
297
|
550 |
"/ check for merge
|
|
551 |
|
299
|
552 |
runSz := contentsArray size.
|
297
|
553 |
|
299
|
554 |
(contentsArray at:runSz) = newObject ifTrue:[
|
297
|
555 |
lastIdx := runSz - 1.
|
299
|
556 |
contentsArray at:lastIdx put:(contentsArray at:lastIdx) + n.
|
297
|
557 |
^ newObject
|
|
558 |
].
|
|
559 |
|
|
560 |
newRuns := Array new:(runSz + 2).
|
299
|
561 |
newRuns replaceFrom:1 to:runSz with:contentsArray.
|
297
|
562 |
newRuns at:runSz+1 put:n.
|
|
563 |
newRuns at:runSz+2 put:newObject.
|
299
|
564 |
contentsArray := newRuns.
|
297
|
565 |
] ifFalse:[
|
299
|
566 |
contentsArray := Array with:n with:newObject.
|
297
|
567 |
].
|
|
568 |
^ newObject
|
|
569 |
|
|
570 |
"
|
|
571 |
|c|
|
|
572 |
|
|
573 |
c := RunArray new.
|
|
574 |
c add:1 withOccurrences:1000; yourself.
|
|
575 |
c add:2 withOccurrences:1000; yourself.
|
|
576 |
"
|
|
577 |
|
299
|
578 |
"Modified: 11.5.1996 / 13:34:37 / cg"
|
297
|
579 |
!
|
|
580 |
|
|
581 |
grow:howBig
|
|
582 |
"grow or shrink the receiver to contain howBig elements.
|
299
|
583 |
If growing, new slots will be filled (logically) with nil."
|
297
|
584 |
|
|
585 |
|sz info runIndex runOffset runSz newRuns|
|
|
586 |
|
|
587 |
sz := self size.
|
|
588 |
|
|
589 |
howBig == sz ifTrue:[^ self].
|
|
590 |
|
|
591 |
howBig == 0 ifTrue:[
|
299
|
592 |
contentsArray := nil.
|
297
|
593 |
^ self.
|
|
594 |
].
|
|
595 |
|
299
|
596 |
contentsArray isNil ifTrue:[
|
|
597 |
contentsArray := Array with:howBig with:nil.
|
297
|
598 |
^ self
|
|
599 |
].
|
|
600 |
|
299
|
601 |
runSz := contentsArray size.
|
297
|
602 |
|
|
603 |
howBig > sz ifTrue:[
|
|
604 |
newRuns := Array new:(runSz + 2).
|
299
|
605 |
newRuns replaceFrom:1 to:runSz with:contentsArray startingAt:1.
|
297
|
606 |
newRuns at:(runSz+1) put:(howBig - sz).
|
299
|
607 |
contentsArray := newRuns.
|
297
|
608 |
^ self
|
|
609 |
].
|
|
610 |
|
|
611 |
"/ shrinking; possibly cut of a run
|
|
612 |
|
|
613 |
info := self runIndexAndStartIndexForIndex:howBig.
|
|
614 |
runIndex := info at:1.
|
|
615 |
runOffset := info at:2.
|
|
616 |
|
|
617 |
howBig == (runOffset - 1) ifTrue:[
|
|
618 |
"/ we are lucky - new size is up-to previous run
|
|
619 |
|
299
|
620 |
contentsArray := contentsArray copyFrom:1 to:runIndex-1.
|
297
|
621 |
] ifFalse:[
|
299
|
622 |
contentsArray := contentsArray copyFrom:1 to:(runIndex+1).
|
|
623 |
contentsArray at:runIndex put:(howBig - runOffset + 1)
|
297
|
624 |
].
|
|
625 |
|
|
626 |
"
|
|
627 |
|c|
|
|
628 |
|
|
629 |
c := RunArray new.
|
|
630 |
c addAll:#(1 2 3 4 4 4 4 5 5 5 1 2 3); yourself.
|
|
631 |
|
|
632 |
c grow:50; yourself.
|
|
633 |
|
|
634 |
c grow:7; yourself.
|
|
635 |
|
|
636 |
c grow:6; yourself.
|
|
637 |
|
|
638 |
c grow:0; yourself.
|
|
639 |
"
|
|
640 |
|
299
|
641 |
"Modified: 11.5.1996 / 13:34:53 / cg"
|
297
|
642 |
! !
|
|
643 |
|
|
644 |
!RunArray methodsFor:'converting'!
|
|
645 |
|
|
646 |
asOrderedCollection
|
299
|
647 |
"return a new orderedCollection, containing the receivers elements."
|
297
|
648 |
|
|
649 |
|newCollection|
|
|
650 |
|
299
|
651 |
contentsArray isNil ifTrue:[^ OrderedCollection new].
|
|
652 |
|
|
653 |
newCollection := OrderedCollection new:(self size).
|
|
654 |
contentsArray pairWiseDo:[:len :value |
|
|
655 |
newCollection add:value withOccurrences:len
|
|
656 |
].
|
297
|
657 |
^ newCollection
|
|
658 |
|
299
|
659 |
"
|
|
660 |
|r|
|
|
661 |
|
|
662 |
r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7).
|
350
|
663 |
Transcript showCR:r.
|
|
664 |
Transcript showCR:r asOrderedCollection
|
299
|
665 |
"
|
|
666 |
|
|
667 |
"Modified: 11.5.1996 / 13:34:59 / cg"
|
334
|
668 |
!
|
|
669 |
|
|
670 |
asRunArray
|
|
671 |
"return a receiver itself"
|
|
672 |
|
|
673 |
^ self
|
|
674 |
|
|
675 |
"Modified: 14.5.1996 / 14:59:12 / cg"
|
297
|
676 |
! !
|
|
677 |
|
|
678 |
!RunArray methodsFor:'enumerating'!
|
|
679 |
|
|
680 |
do:aBlock
|
|
681 |
"Evaluate aBlock with each of the receiver's elements as the
|
|
682 |
argument. "
|
|
683 |
|
299
|
684 |
contentsArray notNil ifTrue:[
|
|
685 |
contentsArray pairWiseDo:[:len :val |
|
297
|
686 |
len timesRepeat: [aBlock value:val]
|
|
687 |
]
|
|
688 |
]
|
|
689 |
|
299
|
690 |
"Modified: 11.5.1996 / 13:35:03 / cg"
|
305
|
691 |
!
|
|
692 |
|
|
693 |
runsDo:aBlock
|
|
694 |
"Evaluate aBlock with each of the receiver's runs, passing
|
|
695 |
length and value as arguments."
|
|
696 |
|
|
697 |
contentsArray notNil ifTrue:[
|
|
698 |
contentsArray pairWiseDo:aBlock
|
|
699 |
]
|
|
700 |
|
|
701 |
"Modified: 11.5.1996 / 13:35:03 / cg"
|
|
702 |
"Created: 12.5.1996 / 11:06:51 / cg"
|
297
|
703 |
! !
|
|
704 |
|
303
|
705 |
!RunArray methodsFor:'printing & storing'!
|
297
|
706 |
|
|
707 |
storeOn:aStream
|
|
708 |
"Append to aStream an expression which, if evaluated, will generate
|
|
709 |
an object similar to the receiver."
|
|
710 |
|
|
711 |
aStream nextPutAll: '(RunArray new'.
|
299
|
712 |
contentsArray notNil ifTrue:[
|
|
713 |
contentsArray pairWiseDo:[:len :val |
|
297
|
714 |
aStream nextPutAll: ' add:'; nextPutAll:val storeString.
|
|
715 |
len == 1 ifFalse:[
|
303
|
716 |
aStream nextPutAll:' withOccurrences:'; nextPutAll:len printString.
|
297
|
717 |
].
|
|
718 |
aStream nextPutAll:';'
|
|
719 |
].
|
|
720 |
aStream nextPutAll:' yourself'
|
|
721 |
].
|
|
722 |
aStream nextPutAll:')'
|
|
723 |
|
|
724 |
"
|
|
725 |
(RunArray new
|
|
726 |
add:1;
|
|
727 |
add:1;
|
|
728 |
add:2;
|
|
729 |
add:3;
|
303
|
730 |
add:4 withOccurrences:100;
|
297
|
731 |
add:5;
|
303
|
732 |
yourself) storeString
|
297
|
733 |
|
|
734 |
RunArray new storeString
|
|
735 |
"
|
299
|
736 |
|
303
|
737 |
"Modified: 11.5.1996 / 14:28:38 / cg"
|
297
|
738 |
! !
|
|
739 |
|
|
740 |
!RunArray methodsFor:'private'!
|
|
741 |
|
|
742 |
find:oldObject
|
|
743 |
"If I contain oldObject return its index, otherwise create an error
|
|
744 |
notifier. It will answer with the position of the very first element of
|
|
745 |
that value."
|
|
746 |
|
|
747 |
|position|
|
|
748 |
|
|
749 |
position := self find:oldObject ifAbsent:0.
|
|
750 |
position ~~ 0 ifTrue:[
|
|
751 |
^ position
|
|
752 |
].
|
|
753 |
|
|
754 |
self errorValueNotFound:oldObject
|
|
755 |
|
|
756 |
"
|
|
757 |
|c|
|
|
758 |
|
|
759 |
c := RunArray new.
|
|
760 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5.
|
|
761 |
c find:2.
|
|
762 |
|
|
763 |
c find:99
|
|
764 |
"
|
|
765 |
|
|
766 |
"Modified: 10.5.1996 / 13:39:58 / cg"
|
|
767 |
!
|
|
768 |
|
|
769 |
find:oldObject ifAbsent:exceptionBlock
|
|
770 |
"If I contain oldObject return its index, otherwise execute the
|
|
771 |
exception block. It will answer with the position of the very first
|
|
772 |
element of that value."
|
|
773 |
|
|
774 |
|position|
|
|
775 |
|
|
776 |
position := 1.
|
299
|
777 |
contentsArray notNil ifTrue:[
|
|
778 |
contentsArray pairWiseDo:[:len :val |
|
297
|
779 |
val = oldObject ifTrue:[
|
|
780 |
^ position
|
|
781 |
].
|
|
782 |
position := position + len.
|
|
783 |
].
|
|
784 |
].
|
|
785 |
^ exceptionBlock value
|
|
786 |
|
299
|
787 |
"Modified: 11.5.1996 / 13:35:14 / cg"
|
297
|
788 |
!
|
|
789 |
|
|
790 |
isEmpty
|
|
791 |
"Am I empty or not. Returns a boolean"
|
|
792 |
|
299
|
793 |
^ contentsArray notNil
|
|
794 |
|
|
795 |
"Modified: 11.5.1996 / 13:35:17 / cg"
|
297
|
796 |
!
|
|
797 |
|
|
798 |
runIndexAndStartIndexForIndex:anIndex
|
|
799 |
"given a logical index, find the index of that run and the logical index
|
|
800 |
of the first item in that run."
|
|
801 |
|
|
802 |
|position nRuns "{ Class: SmallInteger }"|
|
|
803 |
|
|
804 |
position := 1.
|
299
|
805 |
nRuns := contentsArray size.
|
297
|
806 |
1 to:nRuns by:2 do:[:runIndex |
|
|
807 |
|runLen runLast|
|
|
808 |
|
299
|
809 |
runLen := contentsArray at:runIndex.
|
297
|
810 |
anIndex >= position ifTrue:[
|
|
811 |
runLast := position + runLen - 1.
|
|
812 |
anIndex <= runLast ifTrue:[
|
|
813 |
^ Array with:runIndex with:position
|
|
814 |
].
|
|
815 |
].
|
|
816 |
position := position + runLen
|
|
817 |
].
|
|
818 |
|
|
819 |
^ #(0 0)
|
|
820 |
|
|
821 |
"Created: 10.5.1996 / 17:12:28 / cg"
|
299
|
822 |
"Modified: 11.5.1996 / 13:35:21 / cg"
|
302
|
823 |
!
|
|
824 |
|
|
825 |
setElement:newObject occurrences:n
|
|
826 |
"private instance setup"
|
|
827 |
|
|
828 |
contentsArray := Array with:n with:newObject.
|
|
829 |
|
|
830 |
"Created: 11.5.1996 / 14:05:58 / cg"
|
297
|
831 |
! !
|
|
832 |
|
334
|
833 |
!RunArray methodsFor:'searching'!
|
|
834 |
|
|
835 |
findFirst:aBlock
|
|
836 |
"find the first element, for which evaluation of the argument, aBlock
|
|
837 |
returns true; return its index or 0 if none detected.
|
|
838 |
Notice, that not all elements are processed here
|
|
839 |
(the block is only evaluated for each runs first element)."
|
|
840 |
|
|
841 |
|idx|
|
|
842 |
|
|
843 |
idx := 1.
|
|
844 |
contentsArray notNil ifTrue:[
|
|
845 |
contentsArray pairWiseDo:[:len :val |
|
|
846 |
(aBlock value:val) ifTrue:[^ idx].
|
|
847 |
idx := idx + len.
|
|
848 |
]
|
|
849 |
].
|
|
850 |
^ 0
|
|
851 |
|
|
852 |
"
|
|
853 |
(RunArray new
|
|
854 |
add:1 withOccurrences:100;
|
|
855 |
add:2 withOccurrences:100;
|
|
856 |
add:3 withOccurrences:100;
|
|
857 |
add:4 withOccurrences:100;
|
|
858 |
yourself) findFirst:[:el | el == 3]
|
|
859 |
"
|
|
860 |
|
|
861 |
"Modified: 14.5.1996 / 15:54:45 / cg"
|
|
862 |
! !
|
|
863 |
|
297
|
864 |
!RunArray methodsFor:'user interface'!
|
|
865 |
|
|
866 |
inspect
|
|
867 |
"Reimplement so that they don't get an ordered collection inspector
|
|
868 |
which would get very confused."
|
|
869 |
|
|
870 |
self basicInspect
|
|
871 |
|
|
872 |
"Modified: 10.5.1996 / 18:24:43 / cg"
|
|
873 |
! !
|
|
874 |
|
|
875 |
!RunArray class methodsFor:'documentation'!
|
|
876 |
|
|
877 |
version
|
350
|
878 |
^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.10 1996-05-18 15:32:47 cg Exp $'
|
297
|
879 |
! !
|