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