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