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