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