eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'RunArray':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: RunArray


Inheritance:

   Object
   |
   +--Collection
      |
      +--SequenceableCollection
         |
         +--RunArray

Package:
stx:libbasic2
Category:
Collections-Sequenceable
Version:
rev: 1.47 date: 2018/01/25 19:43:24
user: mawalch
file: RunArray.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger

Description:


This implements an array which uses runs to minimise the amount
of data that it physically holds. 
A run consists of a count/object pair, which (when the collection
is accessed), is treated as if the object was contained count-times 
in the runArray.

Basically it can be used if you know that your collection 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).
    The break-even is when runs have an average size of 2 elements,
    if average runs are shorter, other collections (i.e. Array or
    OrderedCollection) are more compact.

    Also note that indexed access (i.e. #at: / #at:put:) may be
    very inefficient (as opposed to enumeration, which is reasonably
    fast).
    The reason is that for indexed access the runs have to be walked.
    When storing into a runArray, runs may have to be splitted or merged,
    depending on where the store is done (within a run).
    This may lead to a resize operation.
    You have been warned.

[instance variables:]
    contentsArray    <Array>         contains the runs, consisting of
                                     alternating 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,
    since for every run, another object (consisting of 12bytes header)
    would be needed.
    Here, we choose the more space efficient way - after all, space saving
    is the only reason this class was built for.


Related information:

    Array
    OrderedCollection
    CompressedOrderedCollection

Class protocol:

instance creation
o  from: aSequencableCollection
return a new runArray, containing the elements from aSequencableCollection
usage example(s):
     RunArray from:#(1 2 3 3 3 4 5 5 6 6 6 6 6 6)

o  new: count
return a new runArray, containing count elements - all nil

o  new: count withAll: anObject
create a new runArray with count elements, all being anObject
usage example(s):
     RunArray new:100 withAll:#hello

o  runs: runs values: values
return a new runArray, containing elements defined by pairs from
runs and values
usage example(s):
     RunArray runs:#(2 3 4) values:#($a $b $c)


Instance protocol:

Compatibility-ST80
o  runs
|c|

c := RunArray new.
c add:1 withOccurrences:1000.
c add:2 withOccurrences:1000.
c runs.

o  values
|c|

c := RunArray new.
c add:1 withOccurrences:1000.
c add:2 withOccurrences:1000.
c values.

accessing
o  at: index
Answer the element at index.
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).

o  at: index put: anObject
Put anObject at element index anInteger.
at:put: can not be used to append, front or back, to a runArray.
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.

o  atAllPut: anObject
replace all elements of the collection by the argument, anObject.
Notice: This operation modifies the receiver, NOT a copy;
therefore the change may affect all others referencing the receiver.

adding & removing
o  add: newObject
add newObject at the end.
Returns the object (sigh)

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

comparing
o  = aCollection
return true, if the argument contains the same elements as the receiver.
Optimized, especially for the common case, that collection is another runArray
usage example(s):
     'hello' asText sameStringAndEmphasisAs: 'hello' asText
     'hello' asText sameStringAndEmphasisAs: 'hello' asText allBold
     'hello' asText allBold sameStringAndEmphasisAs: 'hello' asText allBold
     'hello1' asText allBold sameStringAndEmphasisAs: 'hello' asText allBold  
     'hello' asText allBold sameStringAndEmphasisAs: 'hello1' asText allBold  
     ('hello' asText allBold , ' ') sameStringAndEmphasisAs: 'hello ' asText allBold  
     ('hello ' asText allBold) sameStringAndEmphasisAs: ('hello' asText allBold , ' ')  
     ('hello' asText allBold , ' ') sameStringAndEmphasisAs: ('hello' asText allBold , ' ')  
     'hello' asRunArray = 'hello' asRunArray 
     'hello1' asRunArray = 'hello' asRunArray 
     'hello' asRunArray = 'hello1' asRunArray 
     'hello' asRunArray = 'hello' asArray      
     'hello1' asRunArray = 'hello' asArray     
     'hello' asRunArray = 'hello1' asArray     

converting
o  asArray
return a new array, containing the receiver's elements.

o  asOrderedCollection
return a new orderedCollection, containing the receiver's elements.

o  asRunArray
return the receiver itself

copying
o  copyFrom: start to: stop
return a new collection, containing the elements from start to stop

o  postCopy

enumerating
o  conform: aBlock
return true, if every element conforms to some condition.
I.e. return false, if aBlock returns false for any element;
true otherwise. Returns true for empty receivers.

o  contains: aBlock
Return true, if aBlock returns true for any of the receiver's elements

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

o  runsDo: aBlock
Evaluate aBlock with each of the receiver's runs,
passing length and value as arguments.

o  withStartStopAndValueDo: aThreeArgBlock
Evaluate aBlock with each of the receiver's runs, passing
start, end and attributes as arguments.

misc ui support
o  inspectorClass
( an extension from the stx:libtool package )
Re-reimplemented so that we don't get an ordered collection inspector
which would get very confused when confronted with a runArray.

printing & storing
o  displayOn: aGCOrStream
Append to aStream an expression which, if evaluated, will generate
an object similar to the receiver.

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

private
o  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.
OBSOLETE: this is a leftover from earlier times; use #indexOf:ifAbsent:

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  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.
OBSOLETE: this is a leftover from earlier times; use #indexOf:ifAbsent:

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  getContentsArray

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

o  setElement: newObject occurrences: n
private instance setup

o  setElementsFrom: aCollection
set my elements from aCollection

o  setElementsFromRuns: runs values: values

o  setRuns: runs setValues: values

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

queries
o  size
Answer how many elements the receiver contains.
This is not the number of runs (but the simulated number of elements).

searching
o  findFirst: aBlock ifNone: exceptionalValue
find the index of the first element, for which evaluation of the argument, aBlock
returns true; return its index or 0 if none detected.
Notice, that not all elements are processed here
(the block is only evaluated for each runs first element).
usage example(s):
     (RunArray new 
        add:1 withOccurrences:100;
        add:2 withOccurrences:100;
        add:3 withOccurrences:100;
        add:4 withOccurrences:100;
        yourself) findFirst:[:el | el == 3] 

o  identityIndexOf: anElement startingAt: start
search for identical anElement, return the index or 0 if not found.
Redefined to prevent a loop over individual accesses via #at:,
which would be very inefficient.

o  indexOf: anElement startingAt: start
search for equal anElement, return the index or 0 if not found.
Redefined to prevent a loop over individual accesses via #at:,
which would be very inefficient.

testing
o  includes: anElement
return true, if the receiver includes an element which is equal
to the argument, anElement; false otherwise.
Redefined to prevent the inherited loop over individual accesses via #at:,
which would be very inefficient.

o  includesIdentical: anElement
return true, if the receiver includes the argument, anElement; false otherwise.
Redefined to prevent the inherited loop over individual accesses via #at:,
which would be very inefficient.

o  isEmpty
Am I empty or not. Returns a boolean


Examples:


this eats up a lot of memory ... ... but its relatively fast:
  |coll|

  coll := OrderedCollection new.
  Transcript showCR:(    
      Time millisecondsToRun:[
          100000 timesRepeat:[coll add:'hello'].
          100000 timesRepeat:[coll add:'world'].
      ]
  ).
  coll inspect.
this is very space efficient ... ... (and even slightly faster):
  |coll|

  coll := RunArray new.
  Transcript showCR:(    
      Time millisecondsToRun:[
          100000 timesRepeat:[coll add:'hello'].
          100000 timesRepeat:[coll add:'world'].
      ]
  ).
  coll inspect.
this is very space efficient ... ... AND much faster:
  |coll|

  coll := RunArray new.
  Transcript showCR:(    
      Time millisecondsToRun:[
          coll add:'hello' withOccurrences:100000.
          coll add:'world' withOccurrences:100000.
      ]
  ).
  coll inspect.
single-element runs; this is very space INEFFICIENT (requires twice as much) ... ... and MUCH slower:
  |coll|

  coll := RunArray new.
  Transcript showCR:(    
      Time millisecondsToRun:[
          1 to:1000 do:[:i | coll add:i].
      ]
  ).
compare to:
  |coll|

  coll := OrderedCollection new.
  Transcript showCR:(    
      Time millisecondsToRun:[
          1 to:1000 do:[:i | coll add:i].
      ]
  ).


ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Wed, 19 Sep 2018 08:52:20 GMT