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