RunArray.st
author Claus Gittinger <cg@exept.de>
Sat, 11 May 1996 13:35:58 +0200
changeset 299 2b891872f0af
parent 298 8a87aeffa1c0
child 300 4855b70c63b6
permissions -rw-r--r--
comments

"
 This class is not covered by or part of the ST/X licence.


 COPYRIGHT.
 The above file is a Manchester Goodie protected by copyright.
 These conditions are imposed on the whole Goodie, and on any significant
 part of it which is separately transmitted or stored:
	* You must ensure that every copy includes this notice, and that
	  source and author(s) of the material are acknowledged.
	* These conditions must be imposed on anyone who receives a copy.
	* The material shall not be used for commercial gain without the prior
	  written consent of the author(s).
 Further information on the copyright conditions may be obtained by
 sending electronic mail:
	To: goodies-lib@cs.man.ac.uk
	Subject: copyright
 or by writing to The Smalltalk Goodies Library Manager, Dept of
 Computer Science, The University, Manchester M13 9PL, UK

 (C) Copyright 1993 University of Manchester
 For more information about the Manchester Goodies Library (from which 
 this file was distributed) send e-mail:
	To: goodies-lib@cs.man.ac.uk
	Subject: help 
"



SequenceableCollection subclass:#RunArray
	instanceVariableNames:'contentsArray'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Sequenceable'
!

RunArray comment:'This implements an ordered collection which uses runs to minimise the amount
 of data that it holds. Basically it should be used if you know that an ordered
 collections is giong to contain a lot of runs of eactly the same data. Implemented
 to allow simultation playback, since the ordered collctions which that generates
 are so big that the complier falls over, though most of it is extremely repetetive.
 This should be totally abstracted. The user should not be a ble to see the difference
 between an ordered collection and a ComrpessedOrderedCollection.  This has a
 lot in common with RunArray, and the two should probably share implementation.
 but I could not do some of the things I wanted with the RunArray code, so this
 is all done on its own.
	Some of this could be made faster by adding a cache of the start and finish
 indices of each run, but since I envisage that most additions etc. will be to
 and from the end those are not included. In addition I have implemented the
 bare essentials of this for what I need it for - i.e. add to the end and take
 off the beginning.'!

!RunArray class methodsFor:'documentation'!

copyright
"
 This class is not covered by or part of the ST/X licence.


 COPYRIGHT.
 The above file is a Manchester Goodie protected by copyright.
 These conditions are imposed on the whole Goodie, and on any significant
 part of it which is separately transmitted or stored:
	* You must ensure that every copy includes this notice, and that
	  source and author(s) of the material are acknowledged.
	* These conditions must be imposed on anyone who receives a copy.
	* The material shall not be used for commercial gain without the prior
	  written consent of the author(s).
 Further information on the copyright conditions may be obtained by
 sending electronic mail:
	To: goodies-lib@cs.man.ac.uk
	Subject: copyright
 or by writing to The Smalltalk Goodies Library Manager, Dept of
 Computer Science, The University, Manchester M13 9PL, UK

 (C) Copyright 1993 University of Manchester
 For more information about the Manchester Goodies Library (from which 
 this file was distributed) send e-mail:
	To: goodies-lib@cs.man.ac.uk
	Subject: help 
"


!

documentation
"
    This implements an array which uses runs to minimise the amount
    of data that it holds. 
    Basically it should be used if you know that an array is going to contain 
    a lot of runs of exactly the same data. 

    RunArrays are used by the Text class to keep character attributes
    (which we expect to remain constant for longer character ranges).

    Notice:
        there is only a space saving if there are really runs (i.e. multiple
        elements which compare same).
        Otherwise, an Array is more compact.

    [instance variables:]
        runs            <Array>         contains the runs, consisting of
                                        length/value entries.

    [implementation note:]
        structure-wise, it would have been more intuitive, to keep
        instances of Run objects (as done in CompressedOrderedCollection)
        instead of alternating length/values in the `runs' collection.
        However, doing this means that lots of additional memory is required.
        Here, we choose the more space efficient way.

    [author:]
        Claus Gittinger

    [see also:]
        Array OrderedCollection CompressedOrderedCollection
"
!

examples
"
  this eats up a lot of memory ...
  ... but its relatively fast:
                                                                        [exBegin]
    |coll|

    coll := OrderedCollection new.
    Transcript showCr:(    
        Time millisecondsToRun:[
            100000 timesRepeat:[coll add:'hello'].
            100000 timesRepeat:[coll add:'world'].
        ]
    ).
    coll inspect.
                                                                        [exEnd]


  this is very space efficient ...
  ... (and even slightly faster):
                                                                        [exBegin]
    |coll|

    coll := RunArray new.
    Transcript showCr:(    
        Time millisecondsToRun:[
            100000 timesRepeat:[coll add:'hello'].
            100000 timesRepeat:[coll add:'world'].
        ]
    ).
    coll inspect.
                                                                        [exEnd]


  this is very space efficient ...
  ... AND much faster:
                                                                        [exBegin]
    |coll|

    coll := RunArray new.
    Transcript showCr:(    
        Time millisecondsToRun:[
            coll add:'hello' withOccurrences:100000.
            coll add:'world' withOccurrences:100000.
        ]
    ).
    coll inspect.
                                                                        [exEnd]


  this is very space INEFFICIENT (a real memory pig ;-) ...
  ... and much slower:
                                                                        [exBegin]
    |coll|

    coll := RunArray new.
    Transcript showCr:(    
        Time millisecondsToRun:[
            1 to:1000 do:[:i | coll add:i].
        ]
    ).
    coll inspect.
                                                                        [exEnd]
"
! !

!RunArray class methodsFor:'instance creation'!

new:size
    "ignore the size argument - we dont know how many runs are
     needed - anyway"

    ^ self new
! !

!RunArray methodsFor:'accessing'!

at:anInteger 
    "Answer the element at index anInteger. 
     at: is used by a knowledgeable client to access an existing element 
     This is a pretty dumb thing to do to a runArray and it is 
     not at all efficient (think of that as a discouragement)."

    |position "{ Class: SmallInteger }"
     nRuns    "{ Class: SmallInteger }"|

    (anInteger > 0) ifTrue:[
        position := 1.
        nRuns := contentsArray size.
        1 to:nRuns by:2 do:[:runIndex |
            |runLen|

            runLen := contentsArray at:runIndex.
            anInteger >= position ifTrue:[
                anInteger < (position + runLen) ifTrue:[
                    ^ contentsArray at:(runIndex + 1)
                ].
            ].
            position := position + runLen
        ]
    ].
    ^ self errorInvalidKey:anInteger

    "
     |c|

     c := RunArray new.
     c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5.
     c at:1. 
     c at:2. 
     c at:3. 
     c at:4.  
     c at:5. 
     c at:6. 
     c at:7. 
     c at:8.  
     c at:9.  
     c at:10.  
    "

    "Modified: 11.5.1996 / 13:33:50 / cg"
!

at:index put:anObject 
    "Put anObject at element index anInteger.      
     at:put: can not be used to append, front or back, to an ordered      
     collection;  it is used by a knowledgeable client to replace an     
     element. 
     This is a pretty dumb thing to do to a runArray and it is 
     very inefficient, since we have to check if runs are to be merged or
     splitted."

    |runSz runIndex runOffset len l1 l2 prevIdx nextIdx
     val newRuns newValues prevLen prevVal nextLen nextVal idx|

    runSz := contentsArray size.

    runIndex := nil.
    (index > 0) ifTrue:[
        runOffset := 1.
        idx := 1.
        [runIndex isNil and:[idx < runSz]] whileTrue:[
            len := contentsArray at:idx.
            nextIdx := runOffset + len.
            index >= runOffset ifTrue:[
                index < nextIdx ifTrue:[
                    runIndex := idx.
                    nextIdx := runOffset. "/ dont advance
                ].
            ].
            runOffset := nextIdx.
            idx := idx + 2.
        ]
    ].
    runIndex isNil ifTrue:[
        ^ self errorInvalidKey:index
    ].

    val := contentsArray at:(runIndex + 1).

    "/ easiest: value there is the same ...

    val = anObject ifTrue:[
        ^ anObject
    ].

    "/ if the length is 1, this is an island ...
    "/ ... which is either easy, or requires a merge.

    len := contentsArray at:runIndex.
    len = 1 ifTrue:[
        "/ check if it can be merged into the next or previous run

        runIndex > 1 ifTrue:[
            prevIdx := runIndex - 2.
            prevVal := contentsArray at:(prevIdx + 1).
            prevVal = anObject ifTrue:[
                "/ can merge it into previous

                prevLen := contentsArray at:prevIdx.

                "/ check if merge into next is also possible (filling an island)
                
                runIndex < (runSz - 1) ifTrue:[
                    nextIdx := runIndex + 2.
                    nextVal := contentsArray at:(nextIdx + 1).
                    nextVal = anObject ifTrue:[
                        "/ can merge with both.
                        
                        nextLen := contentsArray at:nextIdx.

                        contentsArray at:prevIdx put:prevLen+nextLen+1.
                        runSz := (runSz - 4).
                        newRuns := Array new:runSz.
                        newRuns replaceFrom:1 to:(prevIdx + 1) with:contentsArray startingAt:1.
                        newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx+2.
                        contentsArray := newRuns.
                        ^ anObject
                    ]
                ].

                contentsArray at:prevIdx put:prevLen+1.

                runSz := (runSz - 2).
                newRuns := Array new:runSz.
                newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
                newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:runIndex+2.
                contentsArray := newRuns.

                ^ anObject
            ].
        ].

        "/ check if merge into next is possible

        runIndex < runSz ifTrue:[
            nextIdx := runIndex + 2.
            nextVal := contentsArray at:nextIdx+1.
            nextVal = anObject ifTrue:[
                nextLen := contentsArray at:nextIdx.
                contentsArray at:nextIdx put:nextLen + 1.

                runSz := (runSz - 2).
                newRuns := Array new:runSz.
                newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
                newRuns replaceFrom:runIndex to:runSz with:contentsArray startingAt:nextIdx.
                contentsArray := newRuns.
                ^ anObject
            ].
        ].

        "/ no merge; island remains

        contentsArray at:(runIndex+1) put:anObject.
        ^ anObject
    ].

    runOffset == index ifTrue:[
        "/ at the beginning of that run ...

        "/ check if its better added to the previous run ...

        runIndex > 1 ifTrue:[
            prevIdx := runIndex - 2.
            prevVal := contentsArray at:prevIdx+1.
            prevVal = anObject ifTrue:[
                prevLen := contentsArray at:prevIdx.
                contentsArray at:prevIdx put:prevLen + 1.
                contentsArray at:runIndex put:len - 1.
                ^ anObject.
            ].
        ].

        "/ must cut off 1 & insert a new run before ..

        contentsArray at:runIndex put:len - 1.

        runSz := (runSz + 2).
        newRuns := Array new:runSz.
        newRuns replaceFrom:1 to:(runIndex - 1) with:contentsArray startingAt:1.
        newRuns replaceFrom:runIndex+2 to:runSz with:contentsArray startingAt:runIndex.
        contentsArray := newRuns.

        contentsArray at:runIndex   put:1.
        contentsArray at:runIndex+1 put:anObject.
        ^ anObject
    ].

    (runOffset + len - 1) == index ifTrue:[
        "/ at the end ...

        "/ check if its better added to the next run ...

        runIndex < runSz ifTrue:[
            nextIdx := runIndex + 2.
            nextVal := contentsArray at:nextIdx+1.
            nextVal = anObject ifTrue:[
                nextLen := contentsArray at:nextIdx.
                contentsArray at:nextIdx put:nextLen + 1.
                contentsArray at:runIndex put:len - 1.
                ^ anObject.
            ].
        ].

        "/ must cut off 1 & insert a new run after ..

        contentsArray at:runIndex put:len - 1.

        runSz := (runSz + 2).
        newRuns := Array new:runSz.
        newRuns replaceFrom:1 to:(runIndex + 1) with:contentsArray startingAt:1.
        newRuns replaceFrom:runIndex+4 to:runSz with:contentsArray startingAt:runIndex+2.
        contentsArray := newRuns.

        contentsArray at:runIndex+2 put:1.
        contentsArray at:runIndex+2+1 put:anObject.
        ^ anObject
    ].

    "/ hardest - split run into two, insert new run in-between

    runSz := (runSz + 4).
    newRuns := Array new:runSz.

    runIndex > 1 ifTrue:[
        newRuns replaceFrom:1 to:runIndex-1 with:contentsArray.
    ].
    newRuns replaceFrom:runIndex+6 to:(runSz+4) with:contentsArray startingAt:runIndex+2.

    l2 := len - (index - runOffset).
    l1 := len - l2.
    l2 := l2 - 1.

    newRuns at:runIndex   put:l1.
    newRuns at:runIndex+1 put:val.

    newRuns at:runIndex+4 put:l2.
    newRuns at:runIndex+5 put:val.

    "/ insert
    newRuns at:runIndex+2 put:1.
    newRuns at:runIndex+3 put:anObject.

    contentsArray := newRuns.
    ^ anObject

    "
     |c|

     Transcript cr.

     c := RunArray new.
     c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself.
     Transcript showCr:c.   

     c at:1 put:$a.
     Transcript showCr:c.   
     c.

     c at:3 put:$a.
     Transcript showCr:c.   
     c.

     c at:4 put:$a.   
     Transcript showCr:c.   
     c.

     c at:5 put:$a.   
     Transcript showCr:c.   
     c.

     c at:2 put:$0.   
     Transcript showCr:c.   
     c.

     c at:2 put:$a.   
     Transcript showCr:c.   
     c.

     Transcript showCr:c.   
    "

    "Modified: 11.5.1996 / 13:34:21 / cg"
!

size
    "Answer how many elements the receiver contains.
     Thsi is not the number of runs (but the simulated number of elements)."

    |n     "{ Class: SmallInteger }"
     runSz "{ Class: SmallInteger }"|

    n := 0.
    runSz := contentsArray size.
    1 to:runSz by:2 do:[:i |
        n := n + (contentsArray at:i)
    ].
    ^ n

    "Modified: 11.5.1996 / 13:34:27 / cg"
! !

!RunArray methodsFor:'adding'!

add:newObject
    "add newObject at the end; returns the object (sigh)"

    ^ self add:newObject withOccurrences:1.

    "
     |c|

     c := RunArray new.
     c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5; yourself.
    "

    "Modified: 10.5.1996 / 17:01:20 / cg"
!

add:newObject withOccurrences:n
    "add newObject n times at the end; returns the object (sigh)"

    |lastIdx runSz newRuns|

    contentsArray notNil ifTrue:[
        "/ check for merge

        runSz := contentsArray size.

        (contentsArray at:runSz) = newObject ifTrue:[
            lastIdx := runSz - 1.
            contentsArray at:lastIdx put:(contentsArray at:lastIdx) + n.
            ^ newObject
        ].

        newRuns := Array new:(runSz + 2).
        newRuns replaceFrom:1 to:runSz with:contentsArray.
        newRuns at:runSz+1 put:n.
        newRuns at:runSz+2 put:newObject.
        contentsArray := newRuns.
    ] ifFalse:[
        contentsArray := Array with:n with:newObject.
    ].
    ^ newObject

    "
     |c|

     c := RunArray new.
     c add:1 withOccurrences:1000; yourself.
     c add:2 withOccurrences:1000; yourself.
    "

    "Modified: 11.5.1996 / 13:34:37 / cg"
!

grow:howBig
    "grow or shrink the receiver to contain howBig elements.
     If growing, new slots will be filled (logically) with nil."

    |sz info runIndex runOffset runSz newRuns|

    sz := self size.

    howBig == sz ifTrue:[^ self].

    howBig == 0 ifTrue:[
        contentsArray := nil.
        ^ self.
    ].

    contentsArray isNil ifTrue:[
        contentsArray := Array with:howBig with:nil.
        ^ self
    ].

    runSz := contentsArray size.

    howBig > sz ifTrue:[
        newRuns := Array new:(runSz + 2).
        newRuns replaceFrom:1 to:runSz with:contentsArray startingAt:1.
        newRuns at:(runSz+1) put:(howBig - sz).
        contentsArray := newRuns.
        ^ self
    ].

    "/ shrinking; possibly cut of a run

    info := self runIndexAndStartIndexForIndex:howBig.
    runIndex := info at:1.
    runOffset := info at:2.

    howBig == (runOffset - 1) ifTrue:[
        "/ we are lucky - new size is up-to previous run

        contentsArray := contentsArray copyFrom:1 to:runIndex-1.
    ] ifFalse:[
        contentsArray := contentsArray copyFrom:1 to:(runIndex+1).
        contentsArray at:runIndex put:(howBig - runOffset + 1)
    ].

    "
     |c|

     c := RunArray new.
     c addAll:#(1 2 3 4 4 4 4 5 5 5 1 2 3); yourself.

     c grow:50; yourself.

     c grow:7; yourself.

     c grow:6; yourself.

     c grow:0; yourself.
    "

    "Modified: 11.5.1996 / 13:34:53 / cg"
! !

!RunArray methodsFor:'converting'!

asOrderedCollection
    "return a new orderedCollection, containing the receivers elements."

    |newCollection|

    contentsArray isNil ifTrue:[^ OrderedCollection new].

    newCollection := OrderedCollection new:(self size).
    contentsArray pairWiseDo:[:len :value |
        newCollection add:value withOccurrences:len
    ].
    ^ newCollection

    "
     |r|

     r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7).
     Transcript showCr:r.
     Transcript showCr:r asOrderedCollection
    "

    "Modified: 11.5.1996 / 13:34:59 / cg"
! !

!RunArray methodsFor:'enumerating'!

do:aBlock 
    "Evaluate aBlock with each of the receiver's elements as the 
    argument. "

    contentsArray notNil ifTrue:[
        contentsArray pairWiseDo:[:len :val | 
            len timesRepeat: [aBlock value:val]
        ]
    ]

    "Modified: 11.5.1996 / 13:35:03 / cg"
! !

!RunArray methodsFor:'printing'!

storeOn:aStream 
    "Append to aStream an expression which, if evaluated, will generate   
    an object similar to the receiver."

    aStream nextPutAll: '(RunArray new'.
    contentsArray notNil ifTrue:[
        contentsArray pairWiseDo:[:len :val | 
            aStream nextPutAll: ' add:'; nextPutAll:val storeString. 
            len == 1 ifFalse:[
                aStream nextPutAll:' withCount:'; nextPutAll:len printString.
            ].
            aStream nextPutAll:';'
        ].
        aStream nextPutAll:' yourself'
    ].
    aStream nextPutAll:')'

    "
     (RunArray new 
        add:1; 
        add:1; 
        add:2; 
        add:3; 
        add:4 withCount:100; 
        add:5; 
        yourself) storeString

     RunArray new storeString 
    "

    "Modified: 11.5.1996 / 13:35:08 / cg"
! !

!RunArray methodsFor:'private'!

find:oldObject 
    "If I contain oldObject return its index, otherwise create an error   
    notifier. It will answer with the position of the very first element of  
    that value."

    |position|

    position := self find:oldObject ifAbsent:0.
    position ~~ 0 ifTrue:[
	^ position
    ].

    self errorValueNotFound:oldObject

    "
     |c|

     c := RunArray new.
     c add:1; add:1; add:1; add:2; add:2; add:3; add:3; add:4; add:5.
     c find:2.

     c find:99
    "

    "Modified: 10.5.1996 / 13:39:58 / cg"
!

find:oldObject ifAbsent:exceptionBlock 
    "If I contain oldObject return its index, otherwise execute the 
     exception block. It will answer with the position of the very first 
     element of that value."

    |position|

    position := 1.
    contentsArray notNil ifTrue:[
        contentsArray pairWiseDo:[:len :val | 
            val = oldObject ifTrue:[
                ^ position
            ].
            position := position + len.
        ].
    ].
    ^ exceptionBlock value

    "Modified: 11.5.1996 / 13:35:14 / cg"
!

isEmpty
    "Am I empty or not. Returns a boolean"

    ^ contentsArray notNil

    "Modified: 11.5.1996 / 13:35:17 / cg"
!

runIndexAndStartIndexForIndex:anIndex
    "given a logical index, find the index of that run and the logical index
     of the first item in that run."

    |position nRuns "{ Class: SmallInteger }"|

    position := 1.
    nRuns := contentsArray size.
    1 to:nRuns by:2 do:[:runIndex | 
        |runLen runLast|

        runLen := contentsArray at:runIndex.
        anIndex >= position ifTrue:[
            runLast := position + runLen - 1.
            anIndex <= runLast ifTrue:[
                ^ Array with:runIndex with:position 
            ].
        ].
        position := position + runLen
    ].

    ^ #(0 0)

    "Created: 10.5.1996 / 17:12:28 / cg"
    "Modified: 11.5.1996 / 13:35:21 / cg"
! !

!RunArray methodsFor:'user interface'!

inspect
    "Reimplement so that they don't get an ordered collection inspector 
     which would get very confused."

    self basicInspect

    "Modified: 10.5.1996 / 18:24:43 / cg"
! !

!RunArray class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.3 1996-05-11 11:35:58 cg Exp $'
! !