RunArray.st
author Claus Gittinger <cg@exept.de>
Sat, 11 May 1996 12:47:55 +0200
changeset 297 9526ea90d4f9
child 298 8a87aeffa1c0
permissions -rw-r--r--
intitial checkin
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
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
	instanceVariableNames:'runs'
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
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    of data that it holds. Basically it should be used if you know that an array
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    is going to contain a lot of runs of exactly the same data. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    The user should not be able to see the difference between an Array and a RunArray.  This has a
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    Notice:
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
        there is only a space saving if there are really runs (i.e. multiple
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
        elements which compare same).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
        Otherwise, an Array is more compact.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    [author:]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
        Claus Gittinger
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    [see also:]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
        Array OrderedCollection CompressedorderedCollection
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
examples
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
  this eats up a lot of memory ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
  ... but its relatively fast:
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
									[exBegin]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
    |coll|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    coll := OrderedCollection new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
    Transcript showCr:(    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
	Time millisecondsToRun:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
	    100000 timesRepeat:[coll add:'hello'].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
	    100000 timesRepeat:[coll add:'world'].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
	]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
    ).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
    coll inspect.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
									[exEnd]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
  this is very space efficient ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
  ... but much slower:
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
									[exBegin]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
    |coll|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
    coll := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
    Transcript showCr:(    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
	Time millisecondsToRun:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
	    100000 timesRepeat:[coll add:'hello'].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
	    100000 timesRepeat:[coll add:'world'].
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
    coll inspect.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
									[exEnd]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
  this is very space efficient ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
  ... AND much faster:
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
									[exBegin]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
    |coll|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
    coll := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
    Transcript showCr:(    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
	Time millisecondsToRun:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
	    coll add:'hello' withCount:100000.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
	    coll add:'world' withCount:100000.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
	]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    ).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
    coll inspect.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
									[exEnd]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
  this is very space INEFFICIENT (a real memory pig ;-) ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
  ... and much slower:
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
									[exBegin]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
    |coll|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
    coll := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
    Transcript showCr:(    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
	Time millisecondsToRun:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
	    1 to:1000 do:[:i | coll add:i].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
	]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
    ).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
    coll inspect.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
									[exEnd]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
  things like this are VERY slow:
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
									[exBegin]
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.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
    1 to:1000 do:[:i | coll add:i].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
    Transcript showCr:(    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
	Time millisecondsToRun:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
	    1000 to:1 by:-2 do:[:i | coll removeIndex:i].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
	]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
    )
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
									[exEnd]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
  much faster with OCs (although these are not optimal for this, too):
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
									[exBegin]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
    |coll|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
    coll := OrderedCollection new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
    1 to:1000 do:[:i | coll add:i].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
    Transcript showCr:(    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
	Time millisecondsToRun:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
	    1000 to:1 by:-2 do:[:i | coll removeIndex:i].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
	]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
    )
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
									[exEnd]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
  in general, such things are better done by constructing a new collection
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
  from scratch (use #select: or #collect:)
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
!RunArray class methodsFor:'instance creation'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
new:size
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
    "ignore the size argument - we dont know how many runs are
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
     needed - anyway"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
    ^ self new
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
!RunArray methodsFor:'accessing'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
at:anInteger 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
    "Answer the element at index anInteger. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
     at: is used by a knowledgeable client to access an existing element 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
     This is a pretty dumb thing to do a compressed collection and it is 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
     not at all efficient (think of that as a discouragement)."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
    |position "{ Class: SmallInteger }"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
     nRuns    "{ Class: SmallInteger }"|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
    (anInteger > 0) ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
        position := 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
        nRuns := runs size.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
        1 to:nRuns by:2 do:[:runIndex |
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
            |runLen|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
            runLen := runs at:runIndex.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
            anInteger >= position ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
                anInteger < (position + runLen) ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
                    ^ runs at:(runIndex + 1)
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
                ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
            ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
            position := position + runLen
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
        ]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
    ^ self errorInvalidKey:anInteger
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
     |c|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
     c := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
     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
   246
     c at:1. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
     c at:2. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
     c at:3. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
     c at:4.  
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
     c at:5. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
     c at:6. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
     c at:7. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
     c at:8.  
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
     c at:9.  
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
     c at:10.  
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
    "Modified: 10.5.1996 / 16:59:03 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
at:index put:anObject 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
    "Put anObject at element index anInteger.      
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
     at:put: can not be used to append, front or back, to an ordered      
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
     collection;  it is used by a knowledgeable client to replace an     
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
     element. It doesn't make a lot of sense for a compressed collection,    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
     and as you can see, the implementation is awful - still if you will    
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
     insist on using this what can you expect. Basically this just copies 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
     itself up to the start point then adds the required element then 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
     copies the rest of itself"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
    |runSz runIndex runOffset len l1 l2 prevIdx nextIdx
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
     val newRuns newValues prevLen prevVal nextLen nextVal idx|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
    runSz := runs size.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
    runIndex := nil.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
    (index > 0) ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
        runOffset := 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
        idx := 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
        [runIndex isNil and:[idx < runSz]] whileTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
            len := runs at:idx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
            nextIdx := runOffset + len.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
            index >= runOffset ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
                index < nextIdx ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
                    runIndex := idx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
                    nextIdx := runOffset. "/ dont advance
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
                ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
            ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
            runOffset := nextIdx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
            idx := idx + 2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
        ]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
    runIndex isNil ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
        ^ self errorInvalidKey:index
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
    val := runs at:(runIndex + 1).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
    "/ easiest: value there is the same ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
    val = anObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
        ^ anObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
    "/ if the length is 1, this is an island ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
    "/ ... which is either easy, or requires a merge.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
    len := runs at:runIndex.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
    len = 1 ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
        "/ check if it can be merged into the next or previous run
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
        runIndex > 1 ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
            prevIdx := runIndex - 2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
            prevVal := runs at:(prevIdx + 1).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
            prevVal = anObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
                "/ can merge it into previous
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
                prevLen := runs at:prevIdx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
                "/ check if merge into next is also possible (filling an island)
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
                
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
                runIndex < (runSz - 1) ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
                    nextIdx := runIndex + 2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
                    nextVal := runs at:(nextIdx + 1).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
                    nextVal = anObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
                        "/ can merge with both.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
                        
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
                        nextLen := runs at:nextIdx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
                        runs at:prevIdx put:prevLen+nextLen+1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
                        runSz := (runSz - 4).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
                        newRuns := Array new:runSz.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
                        newRuns replaceFrom:1 to:(prevIdx + 1) with:runs startingAt:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
                        newRuns replaceFrom:runIndex to:runSz with:runs startingAt:nextIdx+2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
                        runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
                        ^ anObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
                    ]
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
                runs at:prevIdx put:prevLen+1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
                runSz := (runSz - 2).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
                newRuns := Array new:runSz.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
                newRuns replaceFrom:1 to:(runIndex - 1) with:runs startingAt:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
                newRuns replaceFrom:runIndex to:runSz with:runs startingAt:runIndex+2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   346
                runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
                ^ anObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   349
            ].
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
        "/ check if merge into next is possible
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
        runIndex < runSz ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
            nextIdx := runIndex + 2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
            nextVal := runs at:nextIdx+1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
            nextVal = anObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
                nextLen := runs at:nextIdx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
                runs at:nextIdx put:nextLen + 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
                runSz := (runSz - 2).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
                newRuns := Array new:runSz.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
                newRuns replaceFrom:1 to:(runIndex - 1) with:runs startingAt:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
                newRuns replaceFrom:runIndex to:runSz with:runs startingAt:nextIdx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
                runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
                ^ anObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
            ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
        ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
        "/ no merge; island remains
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
        runs at:(runIndex+1) put:anObject.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
        ^ anObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
    runOffset == index ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
        "/ at the beginning of that run ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
        "/ check if its better added to the previous run ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
        runIndex > 1 ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
            prevIdx := runIndex - 2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
            prevVal := runs at:prevIdx+1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
            prevVal = anObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
                prevLen := runs at:prevIdx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
                runs at:prevIdx put:prevLen + 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
                runs at:runIndex put:len - 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
                ^ anObject.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
            ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
        ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
        "/ must cut off 1 & insert a new run before ..
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
        runs at:runIndex put:len - 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
        runSz := (runSz + 2).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
        newRuns := Array new:runSz.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
        newRuns replaceFrom:1 to:(runIndex - 1) with:runs startingAt:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
        newRuns replaceFrom:runIndex+2 to:runSz with:runs startingAt:runIndex.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
        runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
        runs at:runIndex   put:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
        runs at:runIndex+1 put:anObject.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
        ^ anObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
    (runOffset + len - 1) == index ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
        "/ at the end ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
        "/ check if its better added to the next run ...
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
        runIndex < runSz ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
            nextIdx := runIndex + 2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
            nextVal := runs at:nextIdx+1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
            nextVal = anObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
                nextLen := runs at:nextIdx.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
                runs at:nextIdx put:nextLen + 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
                runs at:runIndex put:len - 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
                ^ anObject.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
            ].
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
        "/ must cut off 1 & insert a new run after ..
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
        runs at:runIndex put:len - 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
        runSz := (runSz + 2).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
        newRuns := Array new:runSz.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
        newRuns replaceFrom:1 to:(runIndex + 1) with:runs startingAt:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
        newRuns replaceFrom:runIndex+4 to:runSz with:runs startingAt:runIndex+2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   431
        runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   432
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   433
        runs at:runIndex+2 put:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   434
        runs at:runIndex+2+1 put:anObject.
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
    "/ hardest - split run into two, insert new run in-between
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
    runSz := (runSz + 4).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
    newRuns := Array new:runSz.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
    runIndex > 1 ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   444
        newRuns replaceFrom:1 to:runIndex-1 with:runs.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
    newRuns replaceFrom:runIndex+6 to:(runSz+4) with:runs startingAt:runIndex+2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
    l2 := len - (index - runOffset).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
    l1 := len - l2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
    l2 := l2 - 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
    newRuns at:runIndex   put:l1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
    newRuns at:runIndex+1 put:val.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   454
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
    newRuns at:runIndex+4 put:l2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   456
    newRuns at:runIndex+5 put:val.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
    "/ insert
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
    newRuns at:runIndex+2 put:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
    newRuns at:runIndex+3 put:anObject.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
    runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   463
    ^ anObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   466
     |c|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   467
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
     Transcript cr.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
     c := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
     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
   472
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
     c at:1 put:$a.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
     c.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
     c at:3 put:$a.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
     c.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
     c at:4 put:$a.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
     c.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
     c at:5 put:$a.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
     c.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
     c at:2 put:$0.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   492
     c.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
     c at:2 put:$a.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
     c.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   498
     Transcript showCr:c.   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   499
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   500
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   501
    "Modified: 11.5.1996 / 08:41:49 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   502
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   503
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   504
size
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   505
    "Answer how many elements the receiver contains."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   506
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   507
    |n     "{ Class: SmallInteger }"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   508
     runSz "{ Class: SmallInteger }"|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   509
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   510
    n := 0.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   511
    runSz := runs size.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   512
    1 to:runSz by:2 do:[:i |
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   513
        n := n + (runs at:i)
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   514
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   515
    ^ n
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   516
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   517
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
!RunArray methodsFor:'adding'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
add:newObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
    "add newObject at the end; returns the object (sigh)"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
    ^ self add:newObject withOccurrences:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   524
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   525
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   526
     |c|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   527
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   528
     c := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   529
     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
   530
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   531
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   532
    "Modified: 10.5.1996 / 17:01:20 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   533
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   534
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   535
add:newObject withOccurrences:n
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   536
    "add newObject n times at the end; returns the object (sigh)"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
    |lastIdx runSz newRuns|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   539
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   540
    runs notNil ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   541
        "/ check for merge
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   542
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   543
        runSz := runs size.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   544
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   545
        (runs at:runSz) = newObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   546
            lastIdx := runSz - 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   547
            runs at:lastIdx put:(runs at:lastIdx) + n.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   548
            ^ newObject
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
        newRuns := Array new:(runSz + 2).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
        newRuns replaceFrom:1 to:runSz with:runs.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
        newRuns at:runSz+1 put:n.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
        newRuns at:runSz+2 put:newObject.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
        runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
    ] ifFalse:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   557
        runs := Array with:n with:newObject.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   558
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
    ^ newObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   561
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
     |c|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   564
     c := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   565
     c add:1 withOccurrences:1000; yourself.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
     c add:2 withOccurrences:1000; yourself.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
    "Modified: 10.5.1996 / 18:29:56 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
grow:howBig
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   573
    "grow or shrink the receiver to contain howBig elements.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
     If growing, new slots will be filled with nil."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   575
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
    |sz info runIndex runOffset runSz newRuns|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
    sz := self size.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
    howBig == sz ifTrue:[^ self].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
    howBig == 0 ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
        runs := nil.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
        ^ self.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
    runs isNil ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
        runs := Array with:howBig with:nil.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
        ^ self
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
    runSz := runs size.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
    howBig > sz ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
        newRuns := Array new:(runSz + 2).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
        newRuns replaceFrom:1 to:runSz with:runs startingAt:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
        newRuns at:(runSz+1) put:(howBig - sz).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
        runs := newRuns.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
        ^ self
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
    "/ shrinking; possibly cut of a run
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
    info := self runIndexAndStartIndexForIndex:howBig.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
    runIndex := info at:1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
    runOffset := info at:2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
    howBig == (runOffset - 1) ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
        "/ we are lucky - new size is up-to previous run
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
        runs := runs copyFrom:1 to:runIndex-1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
    ] ifFalse:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
        runs := runs copyFrom:1 to:(runIndex+1).
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
        runs at:runIndex put:(howBig - runOffset + 1)
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   618
     |c|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   619
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   620
     c := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   621
     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
   622
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   623
     c grow:50; yourself.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   624
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   625
     c grow:7; yourself.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   626
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   627
     c grow:6; yourself.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   628
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   629
     c grow:0; yourself.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   630
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   631
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   632
    "Modified: 10.5.1996 / 18:11:54 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   633
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   634
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   635
!RunArray methodsFor:'converting'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   636
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   637
asOrderedCollection
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   638
    "Uncompress this collection."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   639
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   640
    |newCollection|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   641
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   642
    newCollection := OrderedCollection new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   643
    newCollection addAll:self.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   644
    ^ newCollection
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   645
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   646
    "Modified: 10.5.1996 / 13:31:49 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   647
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   648
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   649
!RunArray methodsFor:'enumerating'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   650
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   651
do:aBlock 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   652
    "Evaluate aBlock with each of the receiver's elements as the 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   653
    argument. "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   654
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   655
    runs notNil ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   656
        runs pairWiseDo:[:len :val | 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   657
            len timesRepeat: [aBlock value:val]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   658
        ]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   659
    ]
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   660
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   661
    "Modified: 10.5.1996 / 16:56:01 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   662
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   663
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   664
!RunArray methodsFor:'printing'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   665
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   666
storeOn:aStream 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   667
    "Append to aStream an expression which, if evaluated, will generate   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   668
    an object similar to the receiver."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   669
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   670
    aStream nextPutAll: '(RunArray new'.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   671
    runs notNil ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   672
        runs pairWiseDo:[:len :val | 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   673
            aStream nextPutAll: ' add:'; nextPutAll:val storeString. 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   674
            len == 1 ifFalse:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   675
                aStream nextPutAll:' withCount:'; nextPutAll:len printString.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   676
            ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   677
            aStream nextPutAll:';'
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   678
        ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   679
        aStream nextPutAll:' 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
    aStream nextPutAll:')'
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   683
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   684
     (RunArray new 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
        add:1; 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
        add:1; 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
        add:2; 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
        add:3; 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
        add:4 withCount:100; 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   690
        add:5; 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
        yourself) storeString
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   692
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   693
     RunArray new storeString 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   695
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   697
!RunArray methodsFor:'private'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
find:oldObject 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   700
    "If I contain oldObject return its index, otherwise create an error   
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
    notifier. It will answer with the position of the very first element of  
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
    that value."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
    |position|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   705
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   706
    position := self find:oldObject ifAbsent:0.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   707
    position ~~ 0 ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   708
	^ position
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   710
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
    self errorValueNotFound:oldObject
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   712
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   714
     |c|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   715
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   716
     c := RunArray new.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   717
     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
   718
     c find:2.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   719
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   720
     c find:99
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   721
    "
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   722
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   723
    "Modified: 10.5.1996 / 13:39:58 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   724
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   725
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   726
find:oldObject ifAbsent:exceptionBlock 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   727
    "If I contain oldObject return its index, otherwise execute the 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   728
     exception block. It will answer with the position of the very first 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   729
     element of that value."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   730
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   731
    |position|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   732
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   733
    position := 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   734
    runs notNil ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   735
        runs pairWiseDo:[:len :val | 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   736
            val = oldObject ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   737
                ^ position
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   738
            ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   739
            position := position + len.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   740
        ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   741
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   742
    ^ exceptionBlock value
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   743
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   744
    "Modified: 10.5.1996 / 13:54:25 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   745
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   746
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   747
isEmpty
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   748
    "Am I empty or not. Returns a boolean"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   749
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   750
    ^ runs notNil
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   751
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   752
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   753
runIndexAndStartIndexForIndex:anIndex
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   754
    "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
   755
     of the first item in that run."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   756
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   757
    |position nRuns "{ Class: SmallInteger }"|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   758
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   759
    position := 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   760
    nRuns := runs size.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   761
    1 to:nRuns by:2 do:[:runIndex | 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   762
        |runLen runLast|
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
        runLen := runs at:runIndex.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   765
        anIndex >= position ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   766
            runLast := position + runLen - 1.
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
            anIndex <= runLast ifTrue:[
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
                ^ Array with:runIndex with:position 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   769
            ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   770
        ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   771
        position := position + runLen
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   772
    ].
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   773
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   774
    ^ #(0 0)
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   775
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
    "Created: 10.5.1996 / 17:12:28 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   777
    "Modified: 10.5.1996 / 17:13:42 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   778
!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   779
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   780
runIndexForIndex:anIndex
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   781
    "given a logical index, find the index of that run"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   782
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   783
    ^ (self runIndexAndStartIndexForIndex:anIndex) at:1
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   784
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   785
    "Modified: 10.5.1996 / 17:13:45 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   786
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   787
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   788
!RunArray methodsFor:'user interface'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   789
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   790
inspect
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   791
    "Reimplement so that they don't get an ordered collection inspector 
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   792
     which would get very confused."
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   793
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   794
    self basicInspect
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   795
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   796
    "Modified: 10.5.1996 / 18:24:43 / cg"
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   797
! !
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   798
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   799
!RunArray class methodsFor:'documentation'!
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   800
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   801
version
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   802
    ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.1 1996-05-11 10:47:55 cg Exp $'
9526ea90d4f9 intitial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   803
! !