author | Claus Gittinger <cg@exept.de> |
Tue, 07 Apr 1998 09:52:08 +0200 | |
changeset 630 | 55973d9fee59 |
parent 588 | 677527917896 |
child 871 | b44445dc4ac6 |
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" |
297 | 249 |
! ! |
250 |
||
251 |
!RunArray methodsFor:'accessing'! |
|
252 |
||
352 | 253 |
at:index |
254 |
"Answer the element at index. |
|
297 | 255 |
at: is used by a knowledgeable client to access an existing element |
299 | 256 |
This is a pretty dumb thing to do to a runArray and it is |
297 | 257 |
not at all efficient (think of that as a discouragement)." |
258 |
||
259 |
|position "{ Class: SmallInteger }" |
|
260 |
nRuns "{ Class: SmallInteger }"| |
|
261 |
||
352 | 262 |
(index > 0) ifTrue:[ |
297 | 263 |
position := 1. |
299 | 264 |
nRuns := contentsArray size. |
297 | 265 |
1 to:nRuns by:2 do:[:runIndex | |
266 |
|runLen| |
|
267 |
||
299 | 268 |
runLen := contentsArray at:runIndex. |
352 | 269 |
index >= position ifTrue:[ |
270 |
index < (position + runLen) ifTrue:[ |
|
299 | 271 |
^ contentsArray at:(runIndex + 1) |
297 | 272 |
]. |
273 |
]. |
|
274 |
position := position + runLen |
|
275 |
] |
|
276 |
]. |
|
352 | 277 |
^ self subscriptBoundsError:index |
297 | 278 |
|
279 |
" |
|
280 |
|c| |
|
281 |
||
282 |
c := RunArray new. |
|
283 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5. |
|
284 |
c at:1. |
|
285 |
c at:2. |
|
286 |
c at:3. |
|
287 |
c at:4. |
|
288 |
c at:5. |
|
289 |
c at:6. |
|
290 |
c at:7. |
|
291 |
c at:8. |
|
292 |
c at:9. |
|
293 |
c at:10. |
|
294 |
" |
|
295 |
||
352 | 296 |
"Modified: 18.5.1996 / 14:54:52 / cg" |
297 | 297 |
! |
298 |
||
299 |
at:index put:anObject |
|
300 |
"Put anObject at element index anInteger. |
|
588 | 301 |
at:put: can not be used to append, front or back, to a runArray. |
302 |
It is used by a knowledgeable client to replace an element. |
|
299 | 303 |
This is a pretty dumb thing to do to a runArray and it is |
304 |
very inefficient, since we have to check if runs are to be merged or |
|
305 |
splitted." |
|
297 | 306 |
|
307 |
|runSz runIndex runOffset len l1 l2 prevIdx nextIdx |
|
450 | 308 |
val newRuns prevLen prevVal nextLen nextVal idx| |
297 | 309 |
|
299 | 310 |
runSz := contentsArray size. |
297 | 311 |
|
312 |
runIndex := nil. |
|
313 |
(index > 0) ifTrue:[ |
|
314 |
runOffset := 1. |
|
315 |
idx := 1. |
|
316 |
[runIndex isNil and:[idx < runSz]] whileTrue:[ |
|
299 | 317 |
len := contentsArray at:idx. |
297 | 318 |
nextIdx := runOffset + len. |
319 |
index >= runOffset ifTrue:[ |
|
320 |
index < nextIdx ifTrue:[ |
|
321 |
runIndex := idx. |
|
322 |
nextIdx := runOffset. "/ dont advance |
|
323 |
]. |
|
324 |
]. |
|
325 |
runOffset := nextIdx. |
|
326 |
idx := idx + 2. |
|
327 |
] |
|
328 |
]. |
|
329 |
runIndex isNil ifTrue:[ |
|
352 | 330 |
^ self subscriptBoundsError:index |
297 | 331 |
]. |
332 |
||
299 | 333 |
val := contentsArray at:(runIndex + 1). |
297 | 334 |
|
335 |
"/ easiest: value there is the same ... |
|
336 |
||
337 |
val = anObject ifTrue:[ |
|
338 |
^ anObject |
|
339 |
]. |
|
340 |
||
341 |
"/ if the length is 1, this is an island ... |
|
342 |
"/ ... which is either easy, or requires a merge. |
|
343 |
||
299 | 344 |
len := contentsArray at:runIndex. |
297 | 345 |
len = 1 ifTrue:[ |
346 |
"/ check if it can be merged into the next or previous run |
|
347 |
||
348 |
runIndex > 1 ifTrue:[ |
|
349 |
prevIdx := runIndex - 2. |
|
299 | 350 |
prevVal := contentsArray at:(prevIdx + 1). |
297 | 351 |
prevVal = anObject ifTrue:[ |
352 |
"/ can merge it into previous |
|
353 |
||
299 | 354 |
prevLen := contentsArray at:prevIdx. |
297 | 355 |
|
356 |
"/ check if merge into next is also possible (filling an island) |
|
357 |
||
358 |
runIndex < (runSz - 1) ifTrue:[ |
|
359 |
nextIdx := runIndex + 2. |
|
299 | 360 |
nextVal := contentsArray at:(nextIdx + 1). |
297 | 361 |
nextVal = anObject ifTrue:[ |
362 |
"/ can merge with both. |
|
363 |
||
299 | 364 |
nextLen := contentsArray at:nextIdx. |
297 | 365 |
|
299 | 366 |
contentsArray at:prevIdx put:prevLen+nextLen+1. |
297 | 367 |
runSz := (runSz - 4). |
368 |
newRuns := Array new:runSz. |
|
299 | 369 |
newRuns replaceFrom:1 to:(prevIdx + 1) with:contentsArray startingAt:1. |
370 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx+2. |
|
371 |
contentsArray := newRuns. |
|
297 | 372 |
^ anObject |
373 |
] |
|
374 |
]. |
|
375 |
||
299 | 376 |
contentsArray at:prevIdx put:prevLen+1. |
297 | 377 |
|
378 |
runSz := (runSz - 2). |
|
379 |
newRuns := Array new:runSz. |
|
299 | 380 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1. |
381 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:runIndex+2. |
|
382 |
contentsArray := newRuns. |
|
297 | 383 |
|
384 |
^ anObject |
|
385 |
]. |
|
386 |
]. |
|
387 |
||
388 |
"/ check if merge into next is possible |
|
389 |
||
305 | 390 |
runIndex < (runSz-1) ifTrue:[ |
297 | 391 |
nextIdx := runIndex + 2. |
299 | 392 |
nextVal := contentsArray at:nextIdx+1. |
297 | 393 |
nextVal = anObject ifTrue:[ |
299 | 394 |
nextLen := contentsArray at:nextIdx. |
395 |
contentsArray at:nextIdx put:nextLen + 1. |
|
297 | 396 |
|
397 |
runSz := (runSz - 2). |
|
398 |
newRuns := Array new:runSz. |
|
299 | 399 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1. |
400 |
newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx. |
|
401 |
contentsArray := newRuns. |
|
297 | 402 |
^ anObject |
403 |
]. |
|
404 |
]. |
|
405 |
||
406 |
"/ no merge; island remains |
|
407 |
||
299 | 408 |
contentsArray at:(runIndex+1) put:anObject. |
297 | 409 |
^ anObject |
410 |
]. |
|
411 |
||
412 |
runOffset == index ifTrue:[ |
|
413 |
"/ at the beginning of that run ... |
|
414 |
||
415 |
"/ check if its better added to the previous run ... |
|
416 |
||
417 |
runIndex > 1 ifTrue:[ |
|
418 |
prevIdx := runIndex - 2. |
|
299 | 419 |
prevVal := contentsArray at:prevIdx+1. |
297 | 420 |
prevVal = anObject ifTrue:[ |
299 | 421 |
prevLen := contentsArray at:prevIdx. |
422 |
contentsArray at:prevIdx put:prevLen + 1. |
|
423 |
contentsArray at:runIndex put:len - 1. |
|
297 | 424 |
^ anObject. |
425 |
]. |
|
426 |
]. |
|
427 |
||
428 |
"/ must cut off 1 & insert a new run before .. |
|
429 |
||
299 | 430 |
contentsArray at:runIndex put:len - 1. |
297 | 431 |
|
432 |
runSz := (runSz + 2). |
|
433 |
newRuns := Array new:runSz. |
|
299 | 434 |
newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1. |
435 |
newRuns replaceFrom:runIndex+2 to:runSz with:contentsArray startingAt:runIndex. |
|
436 |
contentsArray := newRuns. |
|
297 | 437 |
|
299 | 438 |
contentsArray at:runIndex put:1. |
439 |
contentsArray at:runIndex+1 put:anObject. |
|
297 | 440 |
^ anObject |
441 |
]. |
|
442 |
||
443 |
(runOffset + len - 1) == index ifTrue:[ |
|
444 |
"/ at the end ... |
|
445 |
||
446 |
"/ check if its better added to the next run ... |
|
447 |
||
306 | 448 |
runIndex < (runSz-1) ifTrue:[ |
297 | 449 |
nextIdx := runIndex + 2. |
299 | 450 |
nextVal := contentsArray at:nextIdx+1. |
297 | 451 |
nextVal = anObject ifTrue:[ |
299 | 452 |
nextLen := contentsArray at:nextIdx. |
453 |
contentsArray at:nextIdx put:nextLen + 1. |
|
454 |
contentsArray at:runIndex put:len - 1. |
|
297 | 455 |
^ anObject. |
456 |
]. |
|
457 |
]. |
|
458 |
||
459 |
"/ must cut off 1 & insert a new run after .. |
|
460 |
||
299 | 461 |
contentsArray at:runIndex put:len - 1. |
297 | 462 |
|
463 |
runSz := (runSz + 2). |
|
464 |
newRuns := Array new:runSz. |
|
299 | 465 |
newRuns replaceFrom:1 to:(runIndex + 1) with:contentsArray startingAt:1. |
466 |
newRuns replaceFrom:runIndex+4 to:runSz with:contentsArray startingAt:runIndex+2. |
|
467 |
contentsArray := newRuns. |
|
297 | 468 |
|
299 | 469 |
contentsArray at:runIndex+2 put:1. |
470 |
contentsArray at:runIndex+2+1 put:anObject. |
|
297 | 471 |
^ anObject |
472 |
]. |
|
473 |
||
474 |
"/ hardest - split run into two, insert new run in-between |
|
475 |
||
476 |
runSz := (runSz + 4). |
|
477 |
newRuns := Array new:runSz. |
|
478 |
||
479 |
runIndex > 1 ifTrue:[ |
|
299 | 480 |
newRuns replaceFrom:1 to:runIndex-1 with:contentsArray. |
297 | 481 |
]. |
305 | 482 |
newRuns replaceFrom:runIndex+6 to:runSz with:contentsArray startingAt:runIndex+2. |
297 | 483 |
|
484 |
l2 := len - (index - runOffset). |
|
485 |
l1 := len - l2. |
|
486 |
l2 := l2 - 1. |
|
487 |
||
488 |
newRuns at:runIndex put:l1. |
|
489 |
newRuns at:runIndex+1 put:val. |
|
490 |
||
491 |
newRuns at:runIndex+4 put:l2. |
|
492 |
newRuns at:runIndex+5 put:val. |
|
493 |
||
494 |
"/ insert |
|
495 |
newRuns at:runIndex+2 put:1. |
|
496 |
newRuns at:runIndex+3 put:anObject. |
|
497 |
||
299 | 498 |
contentsArray := newRuns. |
297 | 499 |
^ anObject |
500 |
||
501 |
" |
|
502 |
|c| |
|
503 |
||
504 |
Transcript cr. |
|
505 |
||
506 |
c := RunArray new. |
|
507 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself. |
|
350 | 508 |
Transcript showCR:c. |
297 | 509 |
|
510 |
c at:1 put:$a. |
|
350 | 511 |
Transcript showCR:c. |
297 | 512 |
c. |
513 |
||
514 |
c at:3 put:$a. |
|
350 | 515 |
Transcript showCR:c. |
297 | 516 |
c. |
517 |
||
518 |
c at:4 put:$a. |
|
350 | 519 |
Transcript showCR:c. |
297 | 520 |
c. |
521 |
||
522 |
c at:5 put:$a. |
|
350 | 523 |
Transcript showCR:c. |
297 | 524 |
c. |
525 |
||
526 |
c at:2 put:$0. |
|
350 | 527 |
Transcript showCR:c. |
297 | 528 |
c. |
529 |
||
530 |
c at:2 put:$a. |
|
350 | 531 |
Transcript showCR:c. |
297 | 532 |
c. |
533 |
||
350 | 534 |
Transcript showCR:c. |
297 | 535 |
" |
536 |
||
588 | 537 |
"Modified: / 30.10.1997 / 14:38:45 / cg" |
297 | 538 |
! |
539 |
||
540 |
size |
|
299 | 541 |
"Answer how many elements the receiver contains. |
542 |
Thsi is not the number of runs (but the simulated number of elements)." |
|
297 | 543 |
|
544 |
|n "{ Class: SmallInteger }" |
|
545 |
runSz "{ Class: SmallInteger }"| |
|
546 |
||
547 |
n := 0. |
|
299 | 548 |
runSz := contentsArray size. |
297 | 549 |
1 to:runSz by:2 do:[:i | |
299 | 550 |
n := n + (contentsArray at:i) |
297 | 551 |
]. |
552 |
^ n |
|
299 | 553 |
|
554 |
"Modified: 11.5.1996 / 13:34:27 / cg" |
|
297 | 555 |
! ! |
556 |
||
557 |
!RunArray methodsFor:'adding'! |
|
558 |
||
559 |
add:newObject |
|
560 |
"add newObject at the end; returns the object (sigh)" |
|
561 |
||
562 |
^ self add:newObject withOccurrences:1. |
|
563 |
||
564 |
" |
|
565 |
|c| |
|
566 |
||
567 |
c := RunArray new. |
|
568 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself. |
|
569 |
" |
|
570 |
||
571 |
"Modified: 10.5.1996 / 17:01:20 / cg" |
|
572 |
! |
|
573 |
||
574 |
add:newObject withOccurrences:n |
|
575 |
"add newObject n times at the end; returns the object (sigh)" |
|
576 |
||
577 |
|lastIdx runSz newRuns| |
|
578 |
||
299 | 579 |
contentsArray notNil ifTrue:[ |
297 | 580 |
"/ check for merge |
581 |
||
299 | 582 |
runSz := contentsArray size. |
297 | 583 |
|
299 | 584 |
(contentsArray at:runSz) = newObject ifTrue:[ |
297 | 585 |
lastIdx := runSz - 1. |
299 | 586 |
contentsArray at:lastIdx put:(contentsArray at:lastIdx) + n. |
297 | 587 |
^ newObject |
588 |
]. |
|
589 |
||
590 |
newRuns := Array new:(runSz + 2). |
|
299 | 591 |
newRuns replaceFrom:1 to:runSz with:contentsArray. |
297 | 592 |
newRuns at:runSz+1 put:n. |
593 |
newRuns at:runSz+2 put:newObject. |
|
299 | 594 |
contentsArray := newRuns. |
297 | 595 |
] ifFalse:[ |
299 | 596 |
contentsArray := Array with:n with:newObject. |
297 | 597 |
]. |
598 |
^ newObject |
|
599 |
||
600 |
" |
|
601 |
|c| |
|
602 |
||
603 |
c := RunArray new. |
|
604 |
c add:1 withOccurrences:1000; yourself. |
|
605 |
c add:2 withOccurrences:1000; yourself. |
|
606 |
" |
|
607 |
||
299 | 608 |
"Modified: 11.5.1996 / 13:34:37 / cg" |
297 | 609 |
! |
610 |
||
611 |
grow:howBig |
|
612 |
"grow or shrink the receiver to contain howBig elements. |
|
299 | 613 |
If growing, new slots will be filled (logically) with nil." |
297 | 614 |
|
615 |
|sz info runIndex runOffset runSz newRuns| |
|
616 |
||
617 |
sz := self size. |
|
618 |
||
619 |
howBig == sz ifTrue:[^ self]. |
|
620 |
||
621 |
howBig == 0 ifTrue:[ |
|
299 | 622 |
contentsArray := nil. |
297 | 623 |
^ self. |
624 |
]. |
|
625 |
||
299 | 626 |
contentsArray isNil ifTrue:[ |
627 |
contentsArray := Array with:howBig with:nil. |
|
297 | 628 |
^ self |
629 |
]. |
|
630 |
||
299 | 631 |
runSz := contentsArray size. |
297 | 632 |
|
633 |
howBig > sz ifTrue:[ |
|
634 |
newRuns := Array new:(runSz + 2). |
|
299 | 635 |
newRuns replaceFrom:1 to:runSz with:contentsArray startingAt:1. |
297 | 636 |
newRuns at:(runSz+1) put:(howBig - sz). |
299 | 637 |
contentsArray := newRuns. |
297 | 638 |
^ self |
639 |
]. |
|
640 |
||
641 |
"/ shrinking; possibly cut of a run |
|
642 |
||
643 |
info := self runIndexAndStartIndexForIndex:howBig. |
|
644 |
runIndex := info at:1. |
|
645 |
runOffset := info at:2. |
|
646 |
||
647 |
howBig == (runOffset - 1) ifTrue:[ |
|
648 |
"/ we are lucky - new size is up-to previous run |
|
649 |
||
299 | 650 |
contentsArray := contentsArray copyFrom:1 to:runIndex-1. |
297 | 651 |
] ifFalse:[ |
299 | 652 |
contentsArray := contentsArray copyFrom:1 to:(runIndex+1). |
653 |
contentsArray at:runIndex put:(howBig - runOffset + 1) |
|
297 | 654 |
]. |
655 |
||
656 |
" |
|
657 |
|c| |
|
658 |
||
659 |
c := RunArray new. |
|
660 |
c addAll:#(1 2 3 4 4 4 4 5 5 5 1 2 3); yourself. |
|
661 |
||
662 |
c grow:50; yourself. |
|
663 |
||
664 |
c grow:7; yourself. |
|
665 |
||
666 |
c grow:6; yourself. |
|
667 |
||
668 |
c grow:0; yourself. |
|
669 |
" |
|
670 |
||
299 | 671 |
"Modified: 11.5.1996 / 13:34:53 / cg" |
297 | 672 |
! ! |
673 |
||
674 |
!RunArray methodsFor:'converting'! |
|
675 |
||
676 |
asOrderedCollection |
|
299 | 677 |
"return a new orderedCollection, containing the receivers elements." |
297 | 678 |
|
679 |
|newCollection| |
|
680 |
||
299 | 681 |
contentsArray isNil ifTrue:[^ OrderedCollection new]. |
682 |
||
683 |
newCollection := OrderedCollection new:(self size). |
|
684 |
contentsArray pairWiseDo:[:len :value | |
|
685 |
newCollection add:value withOccurrences:len |
|
686 |
]. |
|
297 | 687 |
^ newCollection |
688 |
||
299 | 689 |
" |
690 |
|r| |
|
691 |
||
692 |
r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7). |
|
350 | 693 |
Transcript showCR:r. |
694 |
Transcript showCR:r asOrderedCollection |
|
299 | 695 |
" |
696 |
||
697 |
"Modified: 11.5.1996 / 13:34:59 / cg" |
|
334 | 698 |
! |
699 |
||
700 |
asRunArray |
|
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
701 |
"return the receiver itself" |
334 | 702 |
|
703 |
^ self |
|
704 |
||
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
705 |
"Modified: / 7.4.1998 / 09:50:29 / cg" |
297 | 706 |
! ! |
707 |
||
352 | 708 |
!RunArray methodsFor:'copying'! |
709 |
||
710 |
copyFrom:start to:stop |
|
711 |
"return a new collection, containing the elements from start to stop" |
|
712 |
||
713 |
|runStart runNext newRuns idx copying| |
|
714 |
||
715 |
(contentsArray notNil |
|
716 |
and:[stop >= start]) ifTrue:[ |
|
717 |
newRuns := self species new. |
|
718 |
runStart := 1. |
|
719 |
idx := start. |
|
720 |
copying := false. |
|
721 |
contentsArray pairWiseDo:[:len :val | |
|
722 |
runNext := runStart + len. |
|
723 |
||
724 |
copying ifFalse:[ |
|
725 |
idx >= runStart ifTrue:[ |
|
726 |
copying := true |
|
727 |
] |
|
728 |
]. |
|
729 |
copying ifTrue:[ |
|
730 |
idx < runNext ifTrue:[ |
|
731 |
"/ found the first run |
|
732 |
||
733 |
stop < runNext ifTrue:[ |
|
734 |
newRuns add:val withOccurrences:(stop-idx+1). |
|
735 |
^ newRuns |
|
736 |
]. |
|
737 |
||
738 |
newRuns add:val withOccurrences:(runNext-idx). |
|
739 |
idx := runNext. |
|
740 |
]. |
|
741 |
]. |
|
742 |
runStart := runNext. |
|
743 |
] |
|
744 |
]. |
|
745 |
||
746 |
^ super copyFrom:start to:stop "/ for the error report |
|
747 |
||
748 |
" |
|
749 |
|r| |
|
750 |
r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7). |
|
751 |
r copyFrom:1 to:10 |
|
752 |
||
753 |
|r| |
|
754 |
r := RunArray withAll:#(1 2 3 3 3 3 3 3 4 5 6 7 7 7 7 7 7 7). |
|
755 |
r copyFrom:4 to:10 |
|
756 |
||
757 |
|r| |
|
758 |
r := RunArray withAll:#(1 2 3 3 3 3 3 3 4 5 6 7 7 7 7 7 7 7). |
|
759 |
r copyFrom:1 to:20 |
|
760 |
" |
|
761 |
||
762 |
"Modified: 18.5.1996 / 19:28:47 / cg" |
|
763 |
! ! |
|
764 |
||
297 | 765 |
!RunArray methodsFor:'enumerating'! |
766 |
||
767 |
do:aBlock |
|
768 |
"Evaluate aBlock with each of the receiver's elements as the |
|
769 |
argument. " |
|
770 |
||
299 | 771 |
contentsArray notNil ifTrue:[ |
772 |
contentsArray pairWiseDo:[:len :val | |
|
588 | 773 |
len timesRepeat:[aBlock value:val] |
297 | 774 |
] |
775 |
] |
|
776 |
||
588 | 777 |
"Modified: / 30.10.1997 / 15:53:37 / cg" |
305 | 778 |
! |
779 |
||
780 |
runsDo:aBlock |
|
781 |
"Evaluate aBlock with each of the receiver's runs, passing |
|
782 |
length and value as arguments." |
|
783 |
||
784 |
contentsArray notNil ifTrue:[ |
|
785 |
contentsArray pairWiseDo:aBlock |
|
786 |
] |
|
787 |
||
788 |
"Modified: 11.5.1996 / 13:35:03 / cg" |
|
789 |
"Created: 12.5.1996 / 11:06:51 / cg" |
|
297 | 790 |
! ! |
791 |
||
303 | 792 |
!RunArray methodsFor:'printing & storing'! |
297 | 793 |
|
794 |
storeOn:aStream |
|
795 |
"Append to aStream an expression which, if evaluated, will generate |
|
796 |
an object similar to the receiver." |
|
797 |
||
798 |
aStream nextPutAll: '(RunArray new'. |
|
299 | 799 |
contentsArray notNil ifTrue:[ |
800 |
contentsArray pairWiseDo:[:len :val | |
|
297 | 801 |
aStream nextPutAll: ' add:'; nextPutAll:val storeString. |
802 |
len == 1 ifFalse:[ |
|
303 | 803 |
aStream nextPutAll:' withOccurrences:'; nextPutAll:len printString. |
297 | 804 |
]. |
805 |
aStream nextPutAll:';' |
|
806 |
]. |
|
807 |
aStream nextPutAll:' yourself' |
|
808 |
]. |
|
809 |
aStream nextPutAll:')' |
|
810 |
||
811 |
" |
|
812 |
(RunArray new |
|
813 |
add:1; |
|
814 |
add:1; |
|
815 |
add:2; |
|
816 |
add:3; |
|
303 | 817 |
add:4 withOccurrences:100; |
297 | 818 |
add:5; |
303 | 819 |
yourself) storeString |
297 | 820 |
|
821 |
RunArray new storeString |
|
822 |
" |
|
299 | 823 |
|
303 | 824 |
"Modified: 11.5.1996 / 14:28:38 / cg" |
297 | 825 |
! ! |
826 |
||
827 |
!RunArray methodsFor:'private'! |
|
828 |
||
829 |
find:oldObject |
|
830 |
"If I contain oldObject return its index, otherwise create an error |
|
588 | 831 |
notifier. It will answer with the position of the very first element of |
832 |
that value. |
|
833 |
OBSOLETE: this is a leftover from earlier times; use #indexOf:ifAbsent:" |
|
297 | 834 |
|
588 | 835 |
|idx| |
297 | 836 |
|
588 | 837 |
self obsoleteMethodWarning:'use #indexOf:ifAbsent:'. |
838 |
idx := self indexOf:oldObject ifAbsent:0. |
|
839 |
idx ~~ 0 ifTrue:[^ idx]. |
|
840 |
^ self errorValueNotFound:oldObject |
|
297 | 841 |
|
842 |
" |
|
843 |
|c| |
|
844 |
||
845 |
c := RunArray new. |
|
846 |
c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5. |
|
847 |
c find:2. |
|
848 |
||
849 |
c find:99 |
|
850 |
" |
|
851 |
||
588 | 852 |
"Modified: / 30.10.1997 / 15:51:38 / cg" |
297 | 853 |
! |
854 |
||
855 |
find:oldObject ifAbsent:exceptionBlock |
|
856 |
"If I contain oldObject return its index, otherwise execute the |
|
857 |
exception block. It will answer with the position of the very first |
|
588 | 858 |
element of that value. |
859 |
OBSOLETE: this is a leftover from earlier times; use #indexOf:ifAbsent:" |
|
297 | 860 |
|
588 | 861 |
self obsoleteMethodWarning:'use #indexOf:'. |
862 |
^ self indexOf:oldObject ifAbsent:exceptionBlock |
|
297 | 863 |
|
588 | 864 |
"Modified: / 30.10.1997 / 15:49:54 / cg" |
297 | 865 |
! |
866 |
||
867 |
isEmpty |
|
868 |
"Am I empty or not. Returns a boolean" |
|
869 |
||
299 | 870 |
^ contentsArray notNil |
871 |
||
872 |
"Modified: 11.5.1996 / 13:35:17 / cg" |
|
297 | 873 |
! |
874 |
||
875 |
runIndexAndStartIndexForIndex:anIndex |
|
876 |
"given a logical index, find the index of that run and the logical index |
|
877 |
of the first item in that run." |
|
878 |
||
879 |
|position nRuns "{ Class: SmallInteger }"| |
|
880 |
||
881 |
position := 1. |
|
299 | 882 |
nRuns := contentsArray size. |
297 | 883 |
1 to:nRuns by:2 do:[:runIndex | |
884 |
|runLen runLast| |
|
885 |
||
299 | 886 |
runLen := contentsArray at:runIndex. |
297 | 887 |
anIndex >= position ifTrue:[ |
888 |
runLast := position + runLen - 1. |
|
889 |
anIndex <= runLast ifTrue:[ |
|
890 |
^ Array with:runIndex with:position |
|
891 |
]. |
|
892 |
]. |
|
893 |
position := position + runLen |
|
894 |
]. |
|
895 |
||
896 |
^ #(0 0) |
|
897 |
||
898 |
"Created: 10.5.1996 / 17:12:28 / cg" |
|
299 | 899 |
"Modified: 11.5.1996 / 13:35:21 / cg" |
302 | 900 |
! |
901 |
||
902 |
setElement:newObject occurrences:n |
|
903 |
"private instance setup" |
|
904 |
||
905 |
contentsArray := Array with:n with:newObject. |
|
906 |
||
907 |
"Created: 11.5.1996 / 14:05:58 / cg" |
|
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
908 |
! |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
909 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
910 |
setElementsFrom:aCollection |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
911 |
"set my elements from aCollection" |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
912 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
913 |
|nRuns last runLen first dIdx| |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
914 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
915 |
aCollection size == 0 ifTrue:[^ self]. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
916 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
917 |
"/ to avoid the reallocations when using |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
918 |
"/ multiple #add:withOccurrences: calls, |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
919 |
"/ generate the runs collction as an orderedCollection |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
920 |
"/ and convert to an array at the end. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
921 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
922 |
nRuns := 0. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
923 |
first := true. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
924 |
aCollection do:[:element | |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
925 |
first ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
926 |
nRuns := nRuns + 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
927 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
928 |
first := false. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
929 |
] ifFalse:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
930 |
element ~~ last ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
931 |
nRuns := nRuns + 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
932 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
933 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
934 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
935 |
]. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
936 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
937 |
"/ now, we know how many runs there are ... |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
938 |
contentsArray := Array new:(nRuns*2). |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
939 |
|
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
940 |
nRuns := 0. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
941 |
runLen := 0. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
942 |
dIdx := 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
943 |
aCollection do:[:element | |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
944 |
runLen == 0 ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
945 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
946 |
runLen := 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
947 |
] ifFalse:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
948 |
element == last ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
949 |
runLen := runLen + 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
950 |
] ifFalse:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
951 |
contentsArray at:dIdx put:runLen. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
952 |
contentsArray at:dIdx+1 put:last. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
953 |
dIdx := dIdx + 2. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
954 |
runLen := 1. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
955 |
last := element. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
956 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
957 |
] |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
958 |
]. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
959 |
runLen ~~ 0 ifTrue:[ |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
960 |
contentsArray at:dIdx put:runLen. |
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
961 |
contentsArray at:dIdx+1 put:last. |
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 |
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
|
966 |
" |
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 |
"Modified: / 7.4.1998 / 09:33:57 / cg" |
297 | 969 |
! ! |
970 |
||
334 | 971 |
!RunArray methodsFor:'searching'! |
972 |
||
973 |
findFirst:aBlock |
|
974 |
"find the first element, for which evaluation of the argument, aBlock |
|
975 |
returns true; return its index or 0 if none detected. |
|
976 |
Notice, that not all elements are processed here |
|
977 |
(the block is only evaluated for each runs first element)." |
|
978 |
||
979 |
|idx| |
|
980 |
||
981 |
idx := 1. |
|
982 |
contentsArray notNil ifTrue:[ |
|
983 |
contentsArray pairWiseDo:[:len :val | |
|
984 |
(aBlock value:val) ifTrue:[^ idx]. |
|
985 |
idx := idx + len. |
|
986 |
] |
|
987 |
]. |
|
988 |
^ 0 |
|
989 |
||
990 |
" |
|
991 |
(RunArray new |
|
992 |
add:1 withOccurrences:100; |
|
993 |
add:2 withOccurrences:100; |
|
994 |
add:3 withOccurrences:100; |
|
995 |
add:4 withOccurrences:100; |
|
996 |
yourself) findFirst:[:el | el == 3] |
|
997 |
" |
|
998 |
||
999 |
"Modified: 14.5.1996 / 15:54:45 / cg" |
|
588 | 1000 |
! |
1001 |
||
1002 |
identityIndexOf:anElement startingAt:start |
|
1003 |
"search for identical anElement, return the index or 0 if not found. |
|
1004 |
Redefined to prevent a loop over individual accesses via #at:, |
|
1005 |
which would be very inefficient." |
|
1006 |
||
1007 |
|idx "{ Class: SmallInteger }" | |
|
1008 |
||
1009 |
contentsArray notNil ifTrue:[ |
|
1010 |
||
1011 |
idx := 1. |
|
1012 |
contentsArray pairWiseDo:[:len :val | |
|
1013 |
|nextRunIdx "{ Class: SmallInteger }"| |
|
1014 |
||
1015 |
nextRunIdx := idx + len. |
|
1016 |
nextRunIdx > start ifTrue:[ |
|
1017 |
(anElement == val) ifTrue:[ |
|
1018 |
^ idx max:start |
|
1019 |
] |
|
1020 |
]. |
|
1021 |
idx := nextRunIdx. |
|
1022 |
]. |
|
1023 |
]. |
|
1024 |
^ 0 |
|
1025 |
||
1026 |
"Modified: / 14.5.1996 / 15:54:45 / cg" |
|
1027 |
"Created: / 30.10.1997 / 15:33:18 / cg" |
|
1028 |
! |
|
1029 |
||
1030 |
indexOf:anElement startingAt:start |
|
1031 |
"search for equal anElement, return the index or 0 if not found. |
|
1032 |
Redefined to prevent a loop over individual accesses via #at:, |
|
1033 |
which would be very inefficient." |
|
1034 |
||
1035 |
|idx "{ Class: SmallInteger }" | |
|
1036 |
||
1037 |
contentsArray notNil ifTrue:[ |
|
1038 |
||
1039 |
idx := 1. |
|
1040 |
contentsArray pairWiseDo:[:len :val | |
|
1041 |
|nextRunIdx "{ Class: SmallInteger }"| |
|
1042 |
||
1043 |
nextRunIdx := idx + len. |
|
1044 |
nextRunIdx > start ifTrue:[ |
|
1045 |
(anElement = val) ifTrue:[ |
|
1046 |
^ idx max:start |
|
1047 |
] |
|
1048 |
]. |
|
1049 |
idx := nextRunIdx. |
|
1050 |
]. |
|
1051 |
]. |
|
1052 |
^ 0 |
|
1053 |
||
1054 |
" |
|
1055 |
|r| |
|
1056 |
r := RunArray new. |
|
1057 |
r add:1 withOccurrences:100. |
|
1058 |
r add:2 withOccurrences:100. |
|
1059 |
r add:3 withOccurrences:100. |
|
1060 |
r add:4 withOccurrences:100. |
|
1061 |
r indexOf:2 startingAt:50. |
|
1062 |
r indexOf:2 startingAt:100. |
|
1063 |
r indexOf:2 startingAt:101. |
|
1064 |
r indexOf:2 startingAt:150. |
|
1065 |
r indexOf:2 startingAt:200. |
|
1066 |
r indexOf:2 startingAt:201. |
|
1067 |
r indexOf:2 startingAt:202. |
|
1068 |
" |
|
1069 |
||
1070 |
"Modified: / 30.10.1997 / 15:33:29 / cg" |
|
1071 |
! ! |
|
1072 |
||
1073 |
!RunArray methodsFor:'testing'! |
|
1074 |
||
1075 |
includes:anElement |
|
1076 |
"return true, if the receiver includes an element which is equal |
|
1077 |
to the argument, anElement; false otherwise. |
|
1078 |
Redefined to prevent the inherited loop over individual accesses via #at:, |
|
1079 |
which would be very inefficient." |
|
1080 |
||
1081 |
contentsArray notNil ifTrue:[ |
|
1082 |
contentsArray pairWiseDo:[:len :val | |
|
1083 |
(anElement = val) ifTrue:[ |
|
1084 |
^ true |
|
1085 |
]. |
|
1086 |
]. |
|
1087 |
]. |
|
1088 |
^ false |
|
1089 |
||
1090 |
"Modified: / 14.5.1996 / 15:54:45 / cg" |
|
1091 |
"Created: / 30.10.1997 / 15:38:02 / cg" |
|
1092 |
! |
|
1093 |
||
1094 |
includesIdentical:anElement |
|
1095 |
"return true, if the receiver includes the argument, anElement; false otherwise. |
|
1096 |
Redefined to prevent the inherited loop over individual accesses via #at:, |
|
1097 |
which would be very inefficient." |
|
1098 |
||
1099 |
contentsArray notNil ifTrue:[ |
|
1100 |
contentsArray pairWiseDo:[:len :val | |
|
1101 |
(anElement == val) ifTrue:[ |
|
1102 |
^ true |
|
1103 |
]. |
|
1104 |
]. |
|
1105 |
]. |
|
1106 |
^ false |
|
1107 |
||
1108 |
"Created: / 30.10.1997 / 15:38:30 / cg" |
|
334 | 1109 |
! ! |
1110 |
||
297 | 1111 |
!RunArray methodsFor:'user interface'! |
1112 |
||
588 | 1113 |
inspectorClass |
1114 |
"Re-reimplemented so that we don't get an ordered collection inspector |
|
1115 |
which would get very confused when confronted with a runArray." |
|
297 | 1116 |
|
588 | 1117 |
^ InspectorView |
297 | 1118 |
|
588 | 1119 |
"Modified: / 30.10.1997 / 14:28:20 / cg" |
297 | 1120 |
! ! |
1121 |
||
1122 |
!RunArray class methodsFor:'documentation'! |
|
1123 |
||
1124 |
version |
|
630
55973d9fee59
added #from: instance creation method.
Claus Gittinger <cg@exept.de>
parents:
588
diff
changeset
|
1125 |
^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.14 1998-04-07 07:52:08 cg Exp $' |
297 | 1126 |
! ! |