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