author | Claus Gittinger <cg@exept.de> |
Tue, 22 Aug 2000 15:49:22 +0200 | |
changeset 904 | f4791de2295b |
parent 895 | 799219e60ad6 |
child 906 | 30c1ab2aee8d |
permissions | -rw-r--r-- |
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 |
||
892 | 30 |
"{ Package: 'stx:libbasic2' }" |
31 |
||
297 | 32 |
SequenceableCollection subclass:#RunArray |
299 | 33 |
instanceVariableNames:'contentsArray' |
297 | 34 |
classVariableNames:'' |
35 |
poolDictionaries:'' |
|
36 |
category:'Collections-Sequenceable' |
|
37 |
! |
|
38 |
||
39 |
RunArray comment:'This implements an ordered collection which uses runs to minimise the amount |
|
40 |
of data that it holds. Basically it should be used if you know that an ordered |
|
41 |
collections is giong to contain a lot of runs of eactly the same data. Implemented |
|
42 |
to allow simultation playback, since the ordered collctions which that generates |
|
43 |
are so big that the complier falls over, though most of it is extremely repetetive. |
|
44 |
This should be totally abstracted. The user should not be a ble to see the difference |
|
45 |
between an ordered collection and a ComrpessedOrderedCollection. This has a |
|
46 |
lot in common with RunArray, and the two should probably share implementation. |
|
47 |
but I could not do some of the things I wanted with the RunArray code, so this |
|
48 |
is all done on its own. |
|
49 |
Some of this could be made faster by adding a cache of the start and finish |
|
50 |
indices of each run, but since I envisage that most additions etc. will be to |
|
51 |
and from the end those are not included. In addition I have implemented the |
|
52 |
bare essentials of this for what I need it for - i.e. add to the end and take |
|
53 |
off the beginning.'! |
|
54 |
||
55 |
!RunArray class methodsFor:'documentation'! |
|
56 |
||
57 |
copyright |
|
58 |
" |
|
59 |
This class is not covered by or part of the ST/X licence. |
|
60 |
||
61 |
||
62 |
COPYRIGHT. |
|
63 |
The above file is a Manchester Goodie protected by copyright. |
|
64 |
These conditions are imposed on the whole Goodie, and on any significant |
|
65 |
part of it which is separately transmitted or stored: |
|
66 |
* You must ensure that every copy includes this notice, and that |
|
67 |
source and author(s) of the material are acknowledged. |
|
68 |
* These conditions must be imposed on anyone who receives a copy. |
|
69 |
* The material shall not be used for commercial gain without the prior |
|
70 |
written consent of the author(s). |
|
71 |
Further information on the copyright conditions may be obtained by |
|
72 |
sending electronic mail: |
|
73 |
To: goodies-lib@cs.man.ac.uk |
|
74 |
Subject: copyright |
|
75 |
or by writing to The Smalltalk Goodies Library Manager, Dept of |
|
76 |
Computer Science, The University, Manchester M13 9PL, UK |
|
77 |
||
78 |
(C) Copyright 1993 University of Manchester |
|
79 |
For more information about the Manchester Goodies Library (from which |
|
80 |
this file was distributed) send e-mail: |
|
81 |
To: goodies-lib@cs.man.ac.uk |
|
82 |
Subject: help |
|
83 |
" |
|
84 |
||
85 |
||
86 |
! |
|
87 |
||
88 |
documentation |
|
89 |
" |
|
90 |
This implements an array which uses runs to minimise the amount |
|
588 | 91 |
of data that it physically holds. |
92 |
A run consists of a count/object pair, which (when the collection |
|
93 |
is accessed), is treated as if the object was contained count-times |
|
94 |
in the runArray. |
|
95 |
||
96 |
Basically it can be used if you know that your collection is |
|
97 |
going to contain a lot of runs of exactly the same data. |
|
297 | 98 |
|
298 | 99 |
RunArrays are used by the Text class to keep character attributes |
100 |
(which we expect to remain constant for longer character ranges). |
|
297 | 101 |
|
588 | 102 |
[notice:] |
103 |
there is ONLY a space saving if there are really runs |
|
104 |
(i.e. multiple elements which compare same). |
|
105 |
The break-even is when runs have an average size of 2 elements, |
|
106 |
if average runs are shorter, other collections (i.e. Array or |
|
107 |
OrderedCollection) are more compact. |
|
108 |
||
109 |
Also note, that indexed access (i.e. #at: / #at:put:) may be |
|
110 |
very inefficient (as opposed to enumeration, which is reasonably |
|
111 |
fast). |
|
112 |
The reason is that for indexed access the runs have to be walked. |
|
113 |
When storing into a runArray, runs may have to be splitted or merged, |
|
114 |
depending on where the store is done (within a run). |
|
115 |
This may lead to a resize operation. |
|
116 |
You have been warned. |
|
297 | 117 |
|
298 | 118 |
[instance variables:] |
588 | 119 |
contentsArray <Array> contains the runs, consisting of |
120 |
alternating length/value entries. |
|
298 | 121 |
|
122 |
[implementation note:] |
|
123 |
structure-wise, it would have been more intuitive, to keep |
|
124 |
instances of Run objects (as done in CompressedOrderedCollection) |
|
125 |
instead of alternating length/values in the `runs' collection. |
|
588 | 126 |
However, doing this means that lots of additional memory is required, |
127 |
since for every run, another object (consisting of 12bytes header) |
|
128 |
would be needed. |
|
129 |
Here, we choose the more space efficient way - after all, space saving |
|
130 |
is the only reason this class was built for. |
|
297 | 131 |
|
132 |
[author:] |
|
133 |
Claus Gittinger |
|
134 |
||
135 |
[see also:] |
|
298 | 136 |
Array OrderedCollection CompressedOrderedCollection |
297 | 137 |
" |
138 |
! |
|
139 |
||
140 |
examples |
|
141 |
" |
|
142 |
this eats up a lot of memory ... |
|
143 |
... but its relatively fast: |
|
298 | 144 |
[exBegin] |
297 | 145 |
|coll| |
146 |
||
147 |
coll := OrderedCollection new. |
|
350 | 148 |
Transcript showCR:( |
298 | 149 |
Time millisecondsToRun:[ |
150 |
100000 timesRepeat:[coll add:'hello']. |
|
151 |
100000 timesRepeat:[coll add:'world']. |
|
152 |
] |
|
297 | 153 |
). |
154 |
coll inspect. |
|
298 | 155 |
[exEnd] |
297 | 156 |
|
157 |
||
158 |
this is very space efficient ... |
|
298 | 159 |
... (and even slightly faster): |
160 |
[exBegin] |
|
297 | 161 |
|coll| |
162 |
||
163 |
coll := RunArray new. |
|
350 | 164 |
Transcript showCR:( |
298 | 165 |
Time millisecondsToRun:[ |
166 |
100000 timesRepeat:[coll add:'hello']. |
|
167 |
100000 timesRepeat:[coll add:'world']. |
|
168 |
] |
|
297 | 169 |
). |
170 |
coll inspect. |
|
298 | 171 |
[exEnd] |
297 | 172 |
|
173 |
||
298 | 174 |
this is very space efficient ... |
175 |
... AND much faster: |
|
176 |
[exBegin] |
|
297 | 177 |
|coll| |
178 |
||
179 |
coll := RunArray new. |
|
350 | 180 |
Transcript showCR:( |
298 | 181 |
Time millisecondsToRun:[ |
182 |
coll add:'hello' withOccurrences:100000. |
|
183 |
coll add:'world' withOccurrences:100000. |
|
184 |
] |
|
297 | 185 |
). |
186 |
coll inspect. |
|
298 | 187 |
[exEnd] |
297 | 188 |
|
189 |
||
300 | 190 |
single-element runs; |
191 |
this is very space INEFFICIENT (requires twice as much) ... |
|
192 |
... and MUCH slower: |
|
298 | 193 |
[exBegin] |
297 | 194 |
|coll| |
195 |
||
196 |
coll := RunArray new. |
|
350 | 197 |
Transcript showCR:( |
298 | 198 |
Time millisecondsToRun:[ |
199 |
1 to:1000 do:[:i | coll add:i]. |
|
200 |
] |
|
201 |
). |
|
202 |
[exEnd] |
|
300 | 203 |
compare to: |
204 |
[exBegin] |
|
205 |
|coll| |
|
206 |
||
207 |
coll := OrderedCollection new. |
|
350 | 208 |
Transcript showCR:( |
300 | 209 |
Time millisecondsToRun:[ |
210 |
1 to:1000 do:[:i | coll add:i]. |
|
211 |
] |
|
212 |
). |
|
213 |
[exEnd] |
|
214 |
||
297 | 215 |
" |
216 |
! ! |
|
217 |
||
218 |
!RunArray class methodsFor:'instance creation'! |
|
219 |
||
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
220 |
from:aSequencableCollection |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
221 |
"return a new runArray, containing the elements from aSequencableCollection" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
222 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
223 |
^ self basicNew setElementsFrom:aSequencableCollection |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
224 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
225 |
" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
226 |
RunArray from:#(1 2 3 3 3 4 5 5 6 6 6 6 6 6) |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
227 |
" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
228 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
229 |
"Created: / 7.4.1998 / 09:12:45 / cg" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
230 |
"Modified: / 7.4.1998 / 09:15:53 / cg" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
231 |
! |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
232 |
|
588 | 233 |
new:count |
234 |
"return a new runArray, containing count elements - all nil" |
|
297 | 235 |
|
588 | 236 |
^ self basicNew setElement:nil occurrences:count |
302 | 237 |
|
588 | 238 |
"Modified: / 30.10.1997 / 14:36:05 / cg" |
302 | 239 |
! |
240 |
||
588 | 241 |
new:count withAll:anObject |
242 |
"create a new runArray with count elements, all being anObject" |
|
302 | 243 |
|
588 | 244 |
^ self basicNew setElement:anObject occurrences:count |
302 | 245 |
|
246 |
" |
|
247 |
RunArray new:100 withAll:#hello |
|
248 |
" |
|
249 |
||
588 | 250 |
"Modified: / 30.10.1997 / 14:36:38 / cg" |
871 | 251 |
! |
252 |
||
253 |
runs:runs values:values |
|
254 |
"return a new runArray, containing elements defined by pairs from |
|
255 |
runs and values" |
|
256 |
||
257 |
^ self basicNew setElementsFromRuns:runs values:values |
|
258 |
||
259 |
" |
|
260 |
RunArray runs:#(2 3 4) values:#($a $b $c) |
|
261 |
" |
|
297 | 262 |
! ! |
263 |
||
264 |
!RunArray methodsFor:'accessing'! |
|
265 |
||
352 | 266 |
at:index |
267 |
"Answer the element at index. |
|
297 | 268 |
at: is used by a knowledgeable client to access an existing element |
299 | 269 |
This is a pretty dumb thing to do to a runArray and it is |
297 | 270 |
not at all efficient (think of that as a discouragement)." |
271 |
||
272 |
|position "{ Class: SmallInteger }" |
|
273 |
nRuns "{ Class: SmallInteger }"| |
|
274 |
||
352 | 275 |
(index > 0) ifTrue:[ |
297 | 276 |
position := 1. |
299 | 277 |
nRuns := contentsArray size. |
297 | 278 |
1 to:nRuns by:2 do:[:runIndex | |
279 |
|runLen| |
|
280 |
||
299 | 281 |
runLen := contentsArray at:runIndex. |
352 | 282 |
index >= position ifTrue:[ |
283 |
index < (position + runLen) ifTrue:[ |
|
299 | 284 |
^ contentsArray at:(runIndex + 1) |
297 | 285 |
]. |
286 |
]. |
|
287 |
position := position + runLen |
|
288 |
] |
|
289 |
]. |
|
352 | 290 |
^ self subscriptBoundsError:index |
297 | 291 |
|
292 |
" |
|
293 |
|c| |
|
294 |
||
295 |
c := RunArray new. |
|
296 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5. |
|
297 |
c at:1. |
|
298 |
c at:2. |
|
299 |
c at:3. |
|
300 |
c at:4. |
|
301 |
c at:5. |
|
302 |
c at:6. |
|
303 |
c at:7. |
|
304 |
c at:8. |
|
305 |
c at:9. |
|
306 |
c at:10. |
|
307 |
" |
|
308 |
||
352 | 309 |
"Modified: 18.5.1996 / 14:54:52 / cg" |
297 | 310 |
! |
311 |
||
312 |
at:index put:anObject |
|
313 |
"Put anObject at element index anInteger. |
|
588 | 314 |
at:put: can not be used to append, front or back, to a runArray. |
315 |
It is used by a knowledgeable client to replace an element. |
|
299 | 316 |
This is a pretty dumb thing to do to a runArray and it is |
317 |
very inefficient, since we have to check if runs are to be merged or |
|
318 |
splitted." |
|
297 | 319 |
|
320 |
|runSz runIndex runOffset len l1 l2 prevIdx nextIdx |
|
450 | 321 |
val newRuns prevLen prevVal nextLen nextVal idx| |
297 | 322 |
|
299 | 323 |
runSz := contentsArray size. |
297 | 324 |
|
325 |
runIndex := nil. |
|
326 |
(index > 0) ifTrue:[ |
|
327 |
runOffset := 1. |
|
328 |
idx := 1. |
|
329 |
[runIndex isNil and:[idx < runSz]] whileTrue:[ |
|
299 | 330 |
len := contentsArray at:idx. |
297 | 331 |
nextIdx := runOffset + len. |
332 |
index >= runOffset ifTrue:[ |
|
333 |
index < nextIdx ifTrue:[ |
|
334 |
runIndex := idx. |
|
335 |
nextIdx := runOffset. "/ dont advance |
|
336 |
]. |
|
337 |
]. |
|
338 |
runOffset := nextIdx. |
|
339 |
idx := idx + 2. |
|
340 |
] |
|
341 |
]. |
|
342 |
runIndex isNil ifTrue:[ |
|
352 | 343 |
^ self subscriptBoundsError:index |
297 | 344 |
]. |
345 |
||
299 | 346 |
val := contentsArray at:(runIndex + 1). |
297 | 347 |
|
348 |
"/ easiest: value there is the same ... |
|
349 |
||
350 |
val = anObject ifTrue:[ |
|
351 |
^ anObject |
|
352 |
]. |
|
353 |
||
354 |
"/ if the length is 1, this is an island ... |
|
355 |
"/ ... which is either easy, or requires a merge. |
|
356 |
||
299 | 357 |
len := contentsArray at:runIndex. |
297 | 358 |
len = 1 ifTrue:[ |
359 |
"/ check if it can be merged into the next or previous run |
|
360 |
||
361 |
runIndex > 1 ifTrue:[ |
|
362 |
prevIdx := runIndex - 2. |
|
299 | 363 |
prevVal := contentsArray at:(prevIdx + 1). |
297 | 364 |
prevVal = anObject ifTrue:[ |
365 |
"/ can merge it into previous |
|
366 |
||
299 | 367 |
prevLen := contentsArray at:prevIdx. |
297 | 368 |
|
369 |
"/ check if merge into next is also possible (filling an island) |
|
370 |
||
371 |
runIndex < (runSz - 1) ifTrue:[ |
|
372 |
nextIdx := runIndex + 2. |
|
299 | 373 |
nextVal := contentsArray at:(nextIdx + 1). |
297 | 374 |
nextVal = anObject ifTrue:[ |
375 |
"/ can merge with both. |
|
376 |
||
299 | 377 |
nextLen := contentsArray at:nextIdx. |
297 | 378 |
|
299 | 379 |
contentsArray at:prevIdx put:prevLen+nextLen+1. |
297 | 380 |
runSz := (runSz - 4). |
381 |
newRuns := Array new:runSz. |
|
299 | 382 |
newRuns replaceFrom:1 to:(prevIdx + 1) with:contentsArray startingAt:1. |
383 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx+2. |
|
384 |
contentsArray := newRuns. |
|
297 | 385 |
^ anObject |
386 |
] |
|
387 |
]. |
|
388 |
||
299 | 389 |
contentsArray at:prevIdx put:prevLen+1. |
297 | 390 |
|
391 |
runSz := (runSz - 2). |
|
392 |
newRuns := Array new:runSz. |
|
299 | 393 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1. |
394 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:runIndex+2. |
|
395 |
contentsArray := newRuns. |
|
297 | 396 |
|
397 |
^ anObject |
|
398 |
]. |
|
399 |
]. |
|
400 |
||
401 |
"/ check if merge into next is possible |
|
402 |
||
305 | 403 |
runIndex < (runSz-1) ifTrue:[ |
297 | 404 |
nextIdx := runIndex + 2. |
299 | 405 |
nextVal := contentsArray at:nextIdx+1. |
297 | 406 |
nextVal = anObject ifTrue:[ |
299 | 407 |
nextLen := contentsArray at:nextIdx. |
408 |
contentsArray at:nextIdx put:nextLen + 1. |
|
297 | 409 |
|
410 |
runSz := (runSz - 2). |
|
411 |
newRuns := Array new:runSz. |
|
299 | 412 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1. |
413 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx. |
|
414 |
contentsArray := newRuns. |
|
297 | 415 |
^ anObject |
416 |
]. |
|
417 |
]. |
|
418 |
||
419 |
"/ no merge; island remains |
|
420 |
||
299 | 421 |
contentsArray at:(runIndex+1) put:anObject. |
297 | 422 |
^ anObject |
423 |
]. |
|
424 |
||
425 |
runOffset == index ifTrue:[ |
|
426 |
"/ at the beginning of that run ... |
|
427 |
||
428 |
"/ check if its better added to the previous run ... |
|
429 |
||
430 |
runIndex > 1 ifTrue:[ |
|
431 |
prevIdx := runIndex - 2. |
|
299 | 432 |
prevVal := contentsArray at:prevIdx+1. |
297 | 433 |
prevVal = anObject ifTrue:[ |
299 | 434 |
prevLen := contentsArray at:prevIdx. |
435 |
contentsArray at:prevIdx put:prevLen + 1. |
|
436 |
contentsArray at:runIndex put:len - 1. |
|
297 | 437 |
^ anObject. |
438 |
]. |
|
439 |
]. |
|
440 |
||
441 |
"/ must cut off 1 & insert a new run before .. |
|
442 |
||
299 | 443 |
contentsArray at:runIndex put:len - 1. |
297 | 444 |
|
445 |
runSz := (runSz + 2). |
|
446 |
newRuns := Array new:runSz. |
|
299 | 447 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1. |
448 |
newRuns replaceFrom:runIndex+2 to:runSz with:contentsArray startingAt:runIndex. |
|
449 |
contentsArray := newRuns. |
|
297 | 450 |
|
299 | 451 |
contentsArray at:runIndex put:1. |
452 |
contentsArray at:runIndex+1 put:anObject. |
|
297 | 453 |
^ anObject |
454 |
]. |
|
455 |
||
456 |
(runOffset + len - 1) == index ifTrue:[ |
|
457 |
"/ at the end ... |
|
458 |
||
459 |
"/ check if its better added to the next run ... |
|
460 |
||
306 | 461 |
runIndex < (runSz-1) ifTrue:[ |
297 | 462 |
nextIdx := runIndex + 2. |
299 | 463 |
nextVal := contentsArray at:nextIdx+1. |
297 | 464 |
nextVal = anObject ifTrue:[ |
299 | 465 |
nextLen := contentsArray at:nextIdx. |
466 |
contentsArray at:nextIdx put:nextLen + 1. |
|
467 |
contentsArray at:runIndex put:len - 1. |
|
297 | 468 |
^ anObject. |
469 |
]. |
|
470 |
]. |
|
471 |
||
472 |
"/ must cut off 1 & insert a new run after .. |
|
473 |
||
299 | 474 |
contentsArray at:runIndex put:len - 1. |
297 | 475 |
|
476 |
runSz := (runSz + 2). |
|
477 |
newRuns := Array new:runSz. |
|
299 | 478 |
newRuns replaceFrom:1 to:(runIndex + 1) with:contentsArray startingAt:1. |
479 |
newRuns replaceFrom:runIndex+4 to:runSz with:contentsArray startingAt:runIndex+2. |
|
480 |
contentsArray := newRuns. |
|
297 | 481 |
|
299 | 482 |
contentsArray at:runIndex+2 put:1. |
483 |
contentsArray at:runIndex+2+1 put:anObject. |
|
297 | 484 |
^ anObject |
485 |
]. |
|
486 |
||
487 |
"/ hardest - split run into two, insert new run in-between |
|
488 |
||
489 |
runSz := (runSz + 4). |
|
490 |
newRuns := Array new:runSz. |
|
491 |
||
492 |
runIndex > 1 ifTrue:[ |
|
299 | 493 |
newRuns replaceFrom:1 to:runIndex-1 with:contentsArray. |
297 | 494 |
]. |
305 | 495 |
newRuns replaceFrom:runIndex+6 to:runSz with:contentsArray startingAt:runIndex+2. |
297 | 496 |
|
497 |
l2 := len - (index - runOffset). |
|
498 |
l1 := len - l2. |
|
499 |
l2 := l2 - 1. |
|
500 |
||
501 |
newRuns at:runIndex put:l1. |
|
502 |
newRuns at:runIndex+1 put:val. |
|
503 |
||
504 |
newRuns at:runIndex+4 put:l2. |
|
505 |
newRuns at:runIndex+5 put:val. |
|
506 |
||
507 |
"/ insert |
|
508 |
newRuns at:runIndex+2 put:1. |
|
509 |
newRuns at:runIndex+3 put:anObject. |
|
510 |
||
299 | 511 |
contentsArray := newRuns. |
297 | 512 |
^ anObject |
513 |
||
514 |
" |
|
515 |
|c| |
|
516 |
||
517 |
Transcript cr. |
|
518 |
||
519 |
c := RunArray new. |
|
520 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself. |
|
350 | 521 |
Transcript showCR:c. |
297 | 522 |
|
523 |
c at:1 put:$a. |
|
350 | 524 |
Transcript showCR:c. |
297 | 525 |
c. |
526 |
||
527 |
c at:3 put:$a. |
|
350 | 528 |
Transcript showCR:c. |
297 | 529 |
c. |
530 |
||
531 |
c at:4 put:$a. |
|
350 | 532 |
Transcript showCR:c. |
297 | 533 |
c. |
534 |
||
535 |
c at:5 put:$a. |
|
350 | 536 |
Transcript showCR:c. |
297 | 537 |
c. |
538 |
||
539 |
c at:2 put:$0. |
|
350 | 540 |
Transcript showCR:c. |
297 | 541 |
c. |
542 |
||
543 |
c at:2 put:$a. |
|
350 | 544 |
Transcript showCR:c. |
297 | 545 |
c. |
546 |
||
350 | 547 |
Transcript showCR:c. |
297 | 548 |
" |
549 |
||
588 | 550 |
"Modified: / 30.10.1997 / 14:38:45 / cg" |
297 | 551 |
! |
552 |
||
553 |
size |
|
299 | 554 |
"Answer how many elements the receiver contains. |
555 |
Thsi is not the number of runs (but the simulated number of elements)." |
|
297 | 556 |
|
557 |
|n "{ Class: SmallInteger }" |
|
558 |
runSz "{ Class: SmallInteger }"| |
|
559 |
||
560 |
n := 0. |
|
299 | 561 |
runSz := contentsArray size. |
297 | 562 |
1 to:runSz by:2 do:[:i | |
299 | 563 |
n := n + (contentsArray at:i) |
297 | 564 |
]. |
565 |
^ n |
|
299 | 566 |
|
567 |
"Modified: 11.5.1996 / 13:34:27 / cg" |
|
297 | 568 |
! ! |
569 |
||
904 | 570 |
!RunArray methodsFor:'adding & removing'! |
297 | 571 |
|
572 |
add:newObject |
|
573 |
"add newObject at the end; returns the object (sigh)" |
|
574 |
||
575 |
^ self add:newObject withOccurrences:1. |
|
576 |
||
577 |
" |
|
578 |
|c| |
|
579 |
||
580 |
c := RunArray new. |
|
581 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself. |
|
582 |
" |
|
583 |
||
584 |
"Modified: 10.5.1996 / 17:01:20 / cg" |
|
585 |
! |
|
586 |
||
587 |
add:newObject withOccurrences:n |
|
588 |
"add newObject n times at the end; returns the object (sigh)" |
|
589 |
||
590 |
|lastIdx runSz newRuns| |
|
591 |
||
299 | 592 |
contentsArray notNil ifTrue:[ |
297 | 593 |
"/ check for merge |
594 |
||
299 | 595 |
runSz := contentsArray size. |
297 | 596 |
|
299 | 597 |
(contentsArray at:runSz) = newObject ifTrue:[ |
297 | 598 |
lastIdx := runSz - 1. |
299 | 599 |
contentsArray at:lastIdx put:(contentsArray at:lastIdx) + n. |
297 | 600 |
^ newObject |
601 |
]. |
|
602 |
||
603 |
newRuns := Array new:(runSz + 2). |
|
299 | 604 |
newRuns replaceFrom:1 to:runSz with:contentsArray. |
297 | 605 |
newRuns at:runSz+1 put:n. |
606 |
newRuns at:runSz+2 put:newObject. |
|
299 | 607 |
contentsArray := newRuns. |
297 | 608 |
] ifFalse:[ |
299 | 609 |
contentsArray := Array with:n with:newObject. |
297 | 610 |
]. |
611 |
^ newObject |
|
612 |
||
613 |
" |
|
614 |
|c| |
|
615 |
||
616 |
c := RunArray new. |
|
617 |
c add:1 withOccurrences:1000; yourself. |
|
618 |
c add:2 withOccurrences:1000; yourself. |
|
619 |
" |
|
620 |
||
299 | 621 |
"Modified: 11.5.1996 / 13:34:37 / cg" |
297 | 622 |
! ! |
623 |
||
892 | 624 |
!RunArray methodsFor:'comparing'! |
625 |
||
626 |
= aCollection |
|
627 |
"return true, if the argument contains the same elements as the receiver. |
|
628 |
Optimized, especially for the common case, that collection is another runArray" |
|
629 |
||
630 |
|otherContents idx1 idx2 runCount1 runValue1 runCount2 runValue2| |
|
631 |
||
632 |
aCollection class == self class ifFalse:[ |
|
633 |
aCollection isSequenceable ifTrue:[ |
|
634 |
idx1 := 1. |
|
635 |
self do:[:element | |
|
636 |
idx1 > aCollection size ifTrue:[^ false]. |
|
637 |
element = (aCollection at:idx1) ifFalse:[^ false]. |
|
638 |
idx1 := idx1+1. |
|
639 |
]. |
|
640 |
idx1 > aCollection size ifFalse:[^ false]. |
|
641 |
^ true. |
|
642 |
]. |
|
643 |
||
644 |
idx1 := 1. |
|
645 |
runCount1 := 0. |
|
646 |
||
647 |
aCollection do:[:element | |
|
648 |
runCount1 == 0 ifTrue:[ |
|
649 |
idx1+1 > contentsArray size ifTrue:[^ false]. |
|
650 |
||
651 |
runCount1 := contentsArray at:idx1. |
|
652 |
runValue1 := contentsArray at:idx1+1. |
|
653 |
idx1 := idx1+2. |
|
654 |
]. |
|
655 |
runValue1 = element ifFalse:[^ false]. |
|
656 |
runCount1 := runCount1 - 1. |
|
657 |
]. |
|
658 |
runCount1 ~~ 0 ifTrue:[^ false]. |
|
659 |
idx1+1 > contentsArray size ifFalse:[^ false]. |
|
660 |
^ true |
|
661 |
]. |
|
662 |
otherContents := aCollection getContentsArray. |
|
663 |
otherContents = contentsArray ifTrue:[^ true]. |
|
664 |
||
665 |
idx1 := 1. |
|
895 | 666 |
runCount1 := 0. |
892 | 667 |
|
668 |
idx2 := 1. |
|
895 | 669 |
runCount2 := 0. |
892 | 670 |
|
671 |
[true] whileTrue:[ |
|
895 | 672 |
runCount1 == 0 ifTrue:[ |
673 |
idx1+1 > contentsArray size ifTrue:[ |
|
674 |
idx2+1 <= otherContents size ifTrue:[^ false]. |
|
675 |
runCount2 ~~ 0 ifTrue:[^ false]. |
|
676 |
^ true. |
|
677 |
]. |
|
678 |
runCount1 := contentsArray at:idx1. |
|
679 |
runValue1 := contentsArray at:idx1+1. |
|
680 |
idx1 := idx1+2. |
|
681 |
]. |
|
682 |
runCount2 == 0 ifTrue:[ |
|
683 |
idx2+1 > otherContents size ifTrue:[ |
|
684 |
^ false |
|
685 |
]. |
|
686 |
runCount2 := otherContents at:idx2. |
|
687 |
runValue2 := otherContents at:idx2+1. |
|
688 |
idx2 := idx2+2. |
|
689 |
]. |
|
690 |
||
892 | 691 |
runValue1 = runValue2 ifFalse:[^ false]. |
895 | 692 |
runCount1 > runCount2 ifTrue:[ |
693 |
runCount1 := runCount1 - runCount2. |
|
694 |
runCount2 := 0. |
|
892 | 695 |
] ifFalse:[ |
895 | 696 |
runCount2 := runCount2 - runCount1. |
697 |
runCount1 := 0. |
|
698 |
]. |
|
892 | 699 |
]. |
700 |
||
701 |
" |
|
702 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
703 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText allBold |
|
704 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' asText allBold |
|
705 |
'hello1' asText allBold sameStringAndEmphasisAs: 'hello' asText allBold |
|
706 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello1' asText allBold |
|
707 |
('hello' asText allBold , ' ') sameStringAndEmphasisAs: 'hello ' asText allBold |
|
708 |
('hello ' asText allBold) sameStringAndEmphasisAs: ('hello' asText allBold , ' ') |
|
709 |
('hello' asText allBold , ' ') sameStringAndEmphasisAs: ('hello' asText allBold , ' ') |
|
710 |
'hello' asRunArray = 'hello' asRunArray |
|
711 |
'hello1' asRunArray = 'hello' asRunArray |
|
712 |
'hello' asRunArray = 'hello1' asRunArray |
|
713 |
'hello' asRunArray = 'hello' asArray |
|
714 |
'hello1' asRunArray = 'hello' asArray |
|
715 |
'hello' asRunArray = 'hello1' asArray |
|
716 |
" |
|
717 |
! ! |
|
718 |
||
297 | 719 |
!RunArray methodsFor:'converting'! |
720 |
||
721 |
asOrderedCollection |
|
299 | 722 |
"return a new orderedCollection, containing the receivers elements." |
297 | 723 |
|
724 |
|newCollection| |
|
725 |
||
299 | 726 |
contentsArray isNil ifTrue:[^ OrderedCollection new]. |
727 |
||
728 |
newCollection := OrderedCollection new:(self size). |
|
729 |
contentsArray pairWiseDo:[:len :value | |
|
730 |
newCollection add:value withOccurrences:len |
|
731 |
]. |
|
297 | 732 |
^ newCollection |
733 |
||
299 | 734 |
" |
735 |
|r| |
|
736 |
||
737 |
r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7). |
|
350 | 738 |
Transcript showCR:r. |
739 |
Transcript showCR:r asOrderedCollection |
|
299 | 740 |
" |
741 |
||
742 |
"Modified: 11.5.1996 / 13:34:59 / cg" |
|
334 | 743 |
! |
744 |
||
745 |
asRunArray |
|
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
746 |
"return the receiver itself" |
334 | 747 |
|
748 |
^ self |
|
749 |
||
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
750 |
"Modified: / 7.4.1998 / 09:50:29 / cg" |
297 | 751 |
! ! |
752 |
||
352 | 753 |
!RunArray methodsFor:'copying'! |
754 |
||
755 |
copyFrom:start to:stop |
|
756 |
"return a new collection, containing the elements from start to stop" |
|
757 |
||
758 |
|runStart runNext newRuns idx copying| |
|
759 |
||
760 |
(contentsArray notNil |
|
761 |
and:[stop >= start]) ifTrue:[ |
|
762 |
newRuns := self species new. |
|
763 |
runStart := 1. |
|
764 |
idx := start. |
|
765 |
copying := false. |
|
766 |
contentsArray pairWiseDo:[:len :val | |
|
767 |
runNext := runStart + len. |
|
768 |
||
769 |
copying ifFalse:[ |
|
770 |
idx >= runStart ifTrue:[ |
|
771 |
copying := true |
|
772 |
] |
|
773 |
]. |
|
774 |
copying ifTrue:[ |
|
775 |
idx < runNext ifTrue:[ |
|
776 |
"/ found the first run |
|
777 |
||
778 |
stop < runNext ifTrue:[ |
|
779 |
newRuns add:val withOccurrences:(stop-idx+1). |
|
780 |
^ newRuns |
|
781 |
]. |
|
782 |
||
783 |
newRuns add:val withOccurrences:(runNext-idx). |
|
784 |
idx := runNext. |
|
785 |
]. |
|
786 |
]. |
|
787 |
runStart := runNext. |
|
788 |
] |
|
789 |
]. |
|
790 |
||
791 |
^ super copyFrom:start to:stop "/ for the error report |
|
792 |
||
793 |
" |
|
794 |
|r| |
|
795 |
r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7). |
|
796 |
r copyFrom:1 to:10 |
|
797 |
||
798 |
|r| |
|
799 |
r := RunArray withAll:#(1 2 3 3 3 3 3 3 4 5 6 7 7 7 7 7 7 7). |
|
800 |
r copyFrom:4 to:10 |
|
801 |
||
802 |
|r| |
|
803 |
r := RunArray withAll:#(1 2 3 3 3 3 3 3 4 5 6 7 7 7 7 7 7 7). |
|
804 |
r copyFrom:1 to:20 |
|
805 |
" |
|
806 |
||
807 |
"Modified: 18.5.1996 / 19:28:47 / cg" |
|
808 |
! ! |
|
809 |
||
297 | 810 |
!RunArray methodsFor:'enumerating'! |
811 |
||
812 |
do:aBlock |
|
813 |
"Evaluate aBlock with each of the receiver's elements as the |
|
814 |
argument. " |
|
815 |
||
299 | 816 |
contentsArray notNil ifTrue:[ |
817 |
contentsArray pairWiseDo:[:len :val | |
|
588 | 818 |
len timesRepeat:[aBlock value:val] |
297 | 819 |
] |
820 |
] |
|
821 |
||
588 | 822 |
"Modified: / 30.10.1997 / 15:53:37 / cg" |
305 | 823 |
! |
824 |
||
825 |
runsDo:aBlock |
|
826 |
"Evaluate aBlock with each of the receiver's runs, passing |
|
827 |
length and value as arguments." |
|
828 |
||
829 |
contentsArray notNil ifTrue:[ |
|
830 |
contentsArray pairWiseDo:aBlock |
|
831 |
] |
|
832 |
||
833 |
"Modified: 11.5.1996 / 13:35:03 / cg" |
|
834 |
"Created: 12.5.1996 / 11:06:51 / cg" |
|
297 | 835 |
! ! |
836 |
||
303 | 837 |
!RunArray methodsFor:'printing & storing'! |
297 | 838 |
|
839 |
storeOn:aStream |
|
840 |
"Append to aStream an expression which, if evaluated, will generate |
|
841 |
an object similar to the receiver." |
|
842 |
||
843 |
aStream nextPutAll: '(RunArray new'. |
|
299 | 844 |
contentsArray notNil ifTrue:[ |
845 |
contentsArray pairWiseDo:[:len :val | |
|
297 | 846 |
aStream nextPutAll: ' add:'; nextPutAll:val storeString. |
847 |
len == 1 ifFalse:[ |
|
303 | 848 |
aStream nextPutAll:' withOccurrences:'; nextPutAll:len printString. |
297 | 849 |
]. |
850 |
aStream nextPutAll:';' |
|
851 |
]. |
|
852 |
aStream nextPutAll:' yourself' |
|
853 |
]. |
|
854 |
aStream nextPutAll:')' |
|
855 |
||
856 |
" |
|
857 |
(RunArray new |
|
858 |
add:1; |
|
859 |
add:1; |
|
860 |
add:2; |
|
861 |
add:3; |
|
303 | 862 |
add:4 withOccurrences:100; |
297 | 863 |
add:5; |
303 | 864 |
yourself) storeString |
297 | 865 |
|
866 |
RunArray new storeString |
|
867 |
" |
|
299 | 868 |
|
303 | 869 |
"Modified: 11.5.1996 / 14:28:38 / cg" |
297 | 870 |
! ! |
871 |
||
872 |
!RunArray methodsFor:'private'! |
|
873 |
||
874 |
find:oldObject |
|
875 |
"If I contain oldObject return its index, otherwise create an error |
|
588 | 876 |
notifier. It will answer with the position of the very first element of |
877 |
that value. |
|
878 |
OBSOLETE: this is a leftover from earlier times; use #indexOf:ifAbsent:" |
|
297 | 879 |
|
588 | 880 |
|idx| |
297 | 881 |
|
588 | 882 |
self obsoleteMethodWarning:'use #indexOf:ifAbsent:'. |
883 |
idx := self indexOf:oldObject ifAbsent:0. |
|
884 |
idx ~~ 0 ifTrue:[^ idx]. |
|
885 |
^ self errorValueNotFound:oldObject |
|
297 | 886 |
|
887 |
" |
|
888 |
|c| |
|
889 |
||
890 |
c := RunArray new. |
|
891 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5. |
|
892 |
c find:2. |
|
893 |
||
894 |
c find:99 |
|
895 |
" |
|
896 |
||
588 | 897 |
"Modified: / 30.10.1997 / 15:51:38 / cg" |
297 | 898 |
! |
899 |
||
900 |
find:oldObject ifAbsent:exceptionBlock |
|
901 |
"If I contain oldObject return its index, otherwise execute the |
|
902 |
exception block. It will answer with the position of the very first |
|
588 | 903 |
element of that value. |
904 |
OBSOLETE: this is a leftover from earlier times; use #indexOf:ifAbsent:" |
|
297 | 905 |
|
588 | 906 |
self obsoleteMethodWarning:'use #indexOf:'. |
907 |
^ self indexOf:oldObject ifAbsent:exceptionBlock |
|
297 | 908 |
|
588 | 909 |
"Modified: / 30.10.1997 / 15:49:54 / cg" |
297 | 910 |
! |
911 |
||
892 | 912 |
getContentsArray |
913 |
^ contentsArray |
|
914 |
! |
|
915 |
||
297 | 916 |
runIndexAndStartIndexForIndex:anIndex |
917 |
"given a logical index, find the index of that run and the logical index |
|
918 |
of the first item in that run." |
|
919 |
||
920 |
|position nRuns "{ Class: SmallInteger }"| |
|
921 |
||
922 |
position := 1. |
|
299 | 923 |
nRuns := contentsArray size. |
297 | 924 |
1 to:nRuns by:2 do:[:runIndex | |
925 |
|runLen runLast| |
|
926 |
||
299 | 927 |
runLen := contentsArray at:runIndex. |
297 | 928 |
anIndex >= position ifTrue:[ |
929 |
runLast := position + runLen - 1. |
|
930 |
anIndex <= runLast ifTrue:[ |
|
931 |
^ Array with:runIndex with:position |
|
932 |
]. |
|
933 |
]. |
|
934 |
position := position + runLen |
|
935 |
]. |
|
936 |
||
937 |
^ #(0 0) |
|
938 |
||
939 |
"Created: 10.5.1996 / 17:12:28 / cg" |
|
299 | 940 |
"Modified: 11.5.1996 / 13:35:21 / cg" |
302 | 941 |
! |
942 |
||
943 |
setElement:newObject occurrences:n |
|
944 |
"private instance setup" |
|
945 |
||
946 |
contentsArray := Array with:n with:newObject. |
|
947 |
||
948 |
"Created: 11.5.1996 / 14:05:58 / cg" |
|
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
949 |
! |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
950 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
951 |
setElementsFrom:aCollection |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
952 |
"set my elements from aCollection" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
953 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
954 |
|nRuns last runLen first dIdx| |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
955 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
956 |
aCollection size == 0 ifTrue:[^ self]. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
957 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
958 |
"/ to avoid the reallocations when using |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
959 |
"/ multiple #add:withOccurrences: calls, |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
960 |
"/ generate the runs collction as an orderedCollection |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
961 |
"/ and convert to an array at the end. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
962 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
963 |
nRuns := 0. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
964 |
first := true. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
965 |
aCollection do:[:element | |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
966 |
first ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
967 |
nRuns := nRuns + 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
968 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
969 |
first := false. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
970 |
] ifFalse:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
971 |
element ~~ last ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
972 |
nRuns := nRuns + 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
973 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
974 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
975 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
976 |
]. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
977 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
978 |
"/ now, we know how many runs there are ... |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
979 |
contentsArray := Array new:(nRuns*2). |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
980 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
981 |
nRuns := 0. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
982 |
runLen := 0. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
983 |
dIdx := 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
984 |
aCollection do:[:element | |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
985 |
runLen == 0 ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
986 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
987 |
runLen := 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
988 |
] ifFalse:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
989 |
element == last ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
990 |
runLen := runLen + 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
991 |
] ifFalse:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
992 |
contentsArray at:dIdx put:runLen. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
993 |
contentsArray at:dIdx+1 put:last. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
994 |
dIdx := dIdx + 2. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
995 |
runLen := 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
996 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
997 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
998 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
999 |
]. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1000 |
runLen ~~ 0 ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1001 |
contentsArray at:dIdx put:runLen. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1002 |
contentsArray at:dIdx+1 put:last. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1003 |
]. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1004 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1005 |
" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1006 |
RunArray from:#(1 2 3 3 3 4 5 5 6 6 6 6 6 6) |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1007 |
" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1008 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1009 |
"Modified: / 7.4.1998 / 09:33:57 / cg" |
871 | 1010 |
! |
1011 |
||
1012 |
setElementsFromRuns:runs values:values |
|
1013 |
|idx| |
|
1014 |
||
1015 |
contentsArray := Array new:(runs size * 2). |
|
1016 |
idx := 1. |
|
1017 |
runs with:values do:[:length :value | |
|
1018 |
contentsArray at:idx put:length. |
|
1019 |
contentsArray at:idx+1 put:value. |
|
1020 |
idx := idx + 2. |
|
1021 |
]. |
|
297 | 1022 |
! ! |
1023 |
||
904 | 1024 |
!RunArray methodsFor:'private-growing'! |
1025 |
||
1026 |
grow:howBig |
|
1027 |
"grow or shrink the receiver to contain howBig elements. |
|
1028 |
If growing, new slots will be filled (logically) with nil." |
|
1029 |
||
1030 |
|sz info runIndex runOffset runSz newRuns| |
|
1031 |
||
1032 |
sz := self size. |
|
1033 |
||
1034 |
howBig == sz ifTrue:[^ self]. |
|
1035 |
||
1036 |
howBig == 0 ifTrue:[ |
|
1037 |
contentsArray := nil. |
|
1038 |
^ self. |
|
1039 |
]. |
|
1040 |
||
1041 |
contentsArray isNil ifTrue:[ |
|
1042 |
contentsArray := Array with:howBig with:nil. |
|
1043 |
^ self |
|
1044 |
]. |
|
1045 |
||
1046 |
runSz := contentsArray size. |
|
1047 |
||
1048 |
howBig > sz ifTrue:[ |
|
1049 |
newRuns := Array new:(runSz + 2). |
|
1050 |
newRuns replaceFrom:1 to:runSz with:contentsArray startingAt:1. |
|
1051 |
newRuns at:(runSz+1) put:(howBig - sz). |
|
1052 |
contentsArray := newRuns. |
|
1053 |
^ self |
|
1054 |
]. |
|
1055 |
||
1056 |
"/ shrinking; possibly cut of a run |
|
1057 |
||
1058 |
info := self runIndexAndStartIndexForIndex:howBig. |
|
1059 |
runIndex := info at:1. |
|
1060 |
runOffset := info at:2. |
|
1061 |
||
1062 |
howBig == (runOffset - 1) ifTrue:[ |
|
1063 |
"/ we are lucky - new size is up-to previous run |
|
1064 |
||
1065 |
contentsArray := contentsArray copyFrom:1 to:runIndex-1. |
|
1066 |
] ifFalse:[ |
|
1067 |
contentsArray := contentsArray copyFrom:1 to:(runIndex+1). |
|
1068 |
contentsArray at:runIndex put:(howBig - runOffset + 1) |
|
1069 |
]. |
|
1070 |
||
1071 |
" |
|
1072 |
|c| |
|
1073 |
||
1074 |
c := RunArray new. |
|
1075 |
c addAll:#(1 2 3 4 4 4 4 5 5 5 1 2 3); yourself. |
|
1076 |
||
1077 |
c grow:50; yourself. |
|
1078 |
||
1079 |
c grow:7; yourself. |
|
1080 |
||
1081 |
c grow:6; yourself. |
|
1082 |
||
1083 |
c grow:0; yourself. |
|
1084 |
" |
|
1085 |
||
1086 |
"Modified: 11.5.1996 / 13:34:53 / cg" |
|
1087 |
! ! |
|
1088 |
||
334 | 1089 |
!RunArray methodsFor:'searching'! |
1090 |
||
1091 |
findFirst:aBlock |
|
1092 |
"find the first element, for which evaluation of the argument, aBlock |
|
1093 |
returns true; return its index or 0 if none detected. |
|
1094 |
Notice, that not all elements are processed here |
|
1095 |
(the block is only evaluated for each runs first element)." |
|
1096 |
||
1097 |
|idx| |
|
1098 |
||
1099 |
idx := 1. |
|
1100 |
contentsArray notNil ifTrue:[ |
|
1101 |
contentsArray pairWiseDo:[:len :val | |
|
1102 |
(aBlock value:val) ifTrue:[^ idx]. |
|
1103 |
idx := idx + len. |
|
1104 |
] |
|
1105 |
]. |
|
1106 |
^ 0 |
|
1107 |
||
1108 |
" |
|
1109 |
(RunArray new |
|
1110 |
add:1 withOccurrences:100; |
|
1111 |
add:2 withOccurrences:100; |
|
1112 |
add:3 withOccurrences:100; |
|
1113 |
add:4 withOccurrences:100; |
|
1114 |
yourself) findFirst:[:el | el == 3] |
|
1115 |
" |
|
1116 |
||
1117 |
"Modified: 14.5.1996 / 15:54:45 / cg" |
|
588 | 1118 |
! |
1119 |
||
1120 |
identityIndexOf:anElement startingAt:start |
|
1121 |
"search for identical anElement, return the index or 0 if not found. |
|
1122 |
Redefined to prevent a loop over individual accesses via #at:, |
|
1123 |
which would be very inefficient." |
|
1124 |
||
1125 |
|idx "{ Class: SmallInteger }" | |
|
1126 |
||
1127 |
contentsArray notNil ifTrue:[ |
|
1128 |
||
1129 |
idx := 1. |
|
1130 |
contentsArray pairWiseDo:[:len :val | |
|
1131 |
|nextRunIdx "{ Class: SmallInteger }"| |
|
1132 |
||
1133 |
nextRunIdx := idx + len. |
|
1134 |
nextRunIdx > start ifTrue:[ |
|
1135 |
(anElement == val) ifTrue:[ |
|
1136 |
^ idx max:start |
|
1137 |
] |
|
1138 |
]. |
|
1139 |
idx := nextRunIdx. |
|
1140 |
]. |
|
1141 |
]. |
|
1142 |
^ 0 |
|
1143 |
||
1144 |
"Modified: / 14.5.1996 / 15:54:45 / cg" |
|
1145 |
"Created: / 30.10.1997 / 15:33:18 / cg" |
|
1146 |
! |
|
1147 |
||
1148 |
indexOf:anElement startingAt:start |
|
1149 |
"search for equal anElement, return the index or 0 if not found. |
|
1150 |
Redefined to prevent a loop over individual accesses via #at:, |
|
1151 |
which would be very inefficient." |
|
1152 |
||
1153 |
|idx "{ Class: SmallInteger }" | |
|
1154 |
||
1155 |
contentsArray notNil ifTrue:[ |
|
1156 |
||
1157 |
idx := 1. |
|
1158 |
contentsArray pairWiseDo:[:len :val | |
|
1159 |
|nextRunIdx "{ Class: SmallInteger }"| |
|
1160 |
||
1161 |
nextRunIdx := idx + len. |
|
1162 |
nextRunIdx > start ifTrue:[ |
|
1163 |
(anElement = val) ifTrue:[ |
|
1164 |
^ idx max:start |
|
1165 |
] |
|
1166 |
]. |
|
1167 |
idx := nextRunIdx. |
|
1168 |
]. |
|
1169 |
]. |
|
1170 |
^ 0 |
|
1171 |
||
1172 |
" |
|
1173 |
|r| |
|
1174 |
r := RunArray new. |
|
1175 |
r add:1 withOccurrences:100. |
|
1176 |
r add:2 withOccurrences:100. |
|
1177 |
r add:3 withOccurrences:100. |
|
1178 |
r add:4 withOccurrences:100. |
|
1179 |
r indexOf:2 startingAt:50. |
|
1180 |
r indexOf:2 startingAt:100. |
|
1181 |
r indexOf:2 startingAt:101. |
|
1182 |
r indexOf:2 startingAt:150. |
|
1183 |
r indexOf:2 startingAt:200. |
|
1184 |
r indexOf:2 startingAt:201. |
|
1185 |
r indexOf:2 startingAt:202. |
|
1186 |
" |
|
1187 |
||
1188 |
"Modified: / 30.10.1997 / 15:33:29 / cg" |
|
1189 |
! ! |
|
1190 |
||
1191 |
!RunArray methodsFor:'testing'! |
|
1192 |
||
1193 |
includes:anElement |
|
1194 |
"return true, if the receiver includes an element which is equal |
|
1195 |
to the argument, anElement; false otherwise. |
|
1196 |
Redefined to prevent the inherited loop over individual accesses via #at:, |
|
1197 |
which would be very inefficient." |
|
1198 |
||
1199 |
contentsArray notNil ifTrue:[ |
|
1200 |
contentsArray pairWiseDo:[:len :val | |
|
1201 |
(anElement = val) ifTrue:[ |
|
1202 |
^ true |
|
1203 |
]. |
|
1204 |
]. |
|
1205 |
]. |
|
1206 |
^ false |
|
1207 |
||
1208 |
"Modified: / 14.5.1996 / 15:54:45 / cg" |
|
1209 |
"Created: / 30.10.1997 / 15:38:02 / cg" |
|
1210 |
! |
|
1211 |
||
1212 |
includesIdentical:anElement |
|
1213 |
"return true, if the receiver includes the argument, anElement; false otherwise. |
|
1214 |
Redefined to prevent the inherited loop over individual accesses via #at:, |
|
1215 |
which would be very inefficient." |
|
1216 |
||
1217 |
contentsArray notNil ifTrue:[ |
|
1218 |
contentsArray pairWiseDo:[:len :val | |
|
1219 |
(anElement == val) ifTrue:[ |
|
1220 |
^ true |
|
1221 |
]. |
|
1222 |
]. |
|
1223 |
]. |
|
1224 |
^ false |
|
1225 |
||
1226 |
"Created: / 30.10.1997 / 15:38:30 / cg" |
|
904 | 1227 |
! |
1228 |
||
1229 |
isEmpty |
|
1230 |
"Am I empty or not. Returns a boolean" |
|
1231 |
||
1232 |
^ contentsArray notNil |
|
1233 |
||
1234 |
"Modified: 11.5.1996 / 13:35:17 / cg" |
|
334 | 1235 |
! ! |
1236 |
||
297 | 1237 |
!RunArray methodsFor:'user interface'! |
1238 |
||
588 | 1239 |
inspectorClass |
1240 |
"Re-reimplemented so that we don't get an ordered collection inspector |
|
1241 |
which would get very confused when confronted with a runArray." |
|
297 | 1242 |
|
588 | 1243 |
^ InspectorView |
297 | 1244 |
|
588 | 1245 |
"Modified: / 30.10.1997 / 14:28:20 / cg" |
297 | 1246 |
! ! |
1247 |
||
1248 |
!RunArray class methodsFor:'documentation'! |
|
1249 |
||
1250 |
version |
|
904 | 1251 |
^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.18 2000-08-22 13:49:22 cg Exp $' |
297 | 1252 |
! ! |