OrderedCollection.st
author Claus Gittinger <cg@exept.de>
Wed, 02 Apr 2003 19:03:50 +0200
changeset 7166 9b10ca27bc32
parent 7165 ab7667df05f6
child 7260 edfa8d6a6046
permissions -rw-r--r--
tuning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
5401
81ea19e6843c test for class
ps
parents: 4862
diff changeset
    13
"{ Package: 'stx:libbasic' }"
81ea19e6843c test for class
ps
parents: 4862
diff changeset
    14
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    15
SequenceableCollection subclass:#OrderedCollection
1071
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
    16
	instanceVariableNames:'contentsArray firstIndex lastIndex'
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
    17
	classVariableNames:''
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
    18
	poolDictionaries:''
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
    19
	category:'Collections-Sequenceable'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    22
!OrderedCollection class methodsFor:'documentation'!
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    23
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    26
 COPYRIGHT (c) 1989 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
    27
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    37
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    38
documentation
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    39
"
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    40
    OrderedCollections (OCs) have their elements ordered as they were
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    41
    added. In addition, they provide all indexing access protocol
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    42
    and bulk copying (much like Arrays).
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    43
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
    44
    Insertion and removal at both ends is possible and reasonably 
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
    45
    fast - therefore they can be used for queues and stacks.
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
    46
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    47
    [Instance variables:]
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    48
        contentsArray   <Array>         the actual contents
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    49
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    50
        firstIndex      <SmallInteger>  index of first valid element
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    51
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    52
        lastIndex       <SmallInteger>  index of last valid element
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    53
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    54
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    55
    [performance hint:]
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
    56
      Although insertion and removal of inner elements is possible and supported,
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
    57
      it may be slow, because remaining elements have to be copied.
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    58
      If many elements have to be removed out of the middle of a big OC,
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    59
      it is often faster to create a new OC from scratch.
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
    60
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    61
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    62
    [beginners bug hint:]
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    63
        notice that:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    64
            Array new:n
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    65
        is quite different from:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    66
            OrderedCollection new:n
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
    67
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    68
        The later creates an OC which is prepared to hold <n> elements,
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    69
        but has a logical size of 0 (zero). To get an OC containing <n> nils,
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    70
        use:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    71
             (OrderedCollection new) grow:n
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    72
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
4064
fd3d2922fb8b documentation
Claus Gittinger <cg@exept.de>
parents: 3968
diff changeset
    74
    [memory requirements:]
fd3d2922fb8b documentation
Claus Gittinger <cg@exept.de>
parents: 3968
diff changeset
    75
        OBJ-HEADER + (3 * ptr-size)
fd3d2922fb8b documentation
Claus Gittinger <cg@exept.de>
parents: 3968
diff changeset
    76
                   + (size-roundedUpToNextPowerOf2 * ptr-size)
fd3d2922fb8b documentation
Claus Gittinger <cg@exept.de>
parents: 3968
diff changeset
    77
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
    78
    [see also:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
    79
        Array
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
    80
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
    81
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1221
diff changeset
    82
        Claus Gittinger
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
    83
"
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    84
!
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    85
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    86
examples
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    87
"
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    88
  using OC as a stack:
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    89
                                                                        [exBegin]
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    90
    |stack top|
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    91
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    92
    stack := OrderedCollection new.
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    93
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    94
    1 to:10 do:[:i |
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    95
        stack add:i
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    96
    ].
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
    97
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    98
    10 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
    99
        top := stack removeLast.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
   100
        Transcript showCR:top
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   101
    ]
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   102
                                                                        [exEnd]
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   103
  using OC as a queue (you should use Queue right away ..):
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   104
                                                                        [exBegin]
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   105
    |queue dequeued|
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   106
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   107
    queue := OrderedCollection new.
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   108
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   109
    1 to:10 do:[:i |
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   110
        queue addLast:i
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   111
    ].
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   112
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   113
    10 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   114
        dequeued := queue removeFirst.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
   115
        Transcript showCR:dequeued
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   116
    ]
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   117
                                                                        [exEnd]
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   118
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   119
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   120
  examples to support the performance hint (see documentation)
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   121
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   122
    timing removal of all odd elements in a collection of 10000:
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   123
    (940 ms on P5/133)
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   124
                                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   125
        |coll time|
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   126
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   127
        coll := (1 to:10000) asOrderedCollection.
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   128
        time := Time millisecondsToRun:[
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   129
            coll removeAllSuchThat:[:el | el even]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   130
        ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
   131
        Transcript show:'time is '; show:time; showCR:' ms'.
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   132
                                                                        [exEnd]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   133
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   134
    tuning the removal by doing it reverse
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   135
    (less copying in #removeAtIndex:) speeds it up by a factor of 2:
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   136
                                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   137
        |coll time|
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   138
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   139
        coll := (1 to:10000) asOrderedCollection.
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   140
        time := Time millisecondsToRun:[
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   141
            coll size to:1 by:-1 do:[:index |
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   142
                (coll at:index) even ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   143
                    coll removeAtIndex:index
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   144
                ]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   145
            ]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   146
        ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
   147
        Transcript show:'time is '; show:time; showCR:' ms'.
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   148
                                                                        [exEnd]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   149
1387
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   150
    rebuilding a new collection:
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   151
    (64 ms on P5/133)
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   152
                                                                        [exBegin]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   153
        |coll time|
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   154
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   155
        coll := (1 to:10000) asOrderedCollection.
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   156
        time := Time millisecondsToRun:[
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   157
            coll := coll select:[:el | el odd]
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   158
        ].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
   159
        Transcript show:'time is '; show:time; showCR:' ms'.
1324
Claus Gittinger <cg@exept.de>
parents: 1321
diff changeset
   160
                                                                        [exEnd]
1321
52e043fb7eaf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   161
"
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
   162
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
a27a279701f8 Initial revision
claus
parents:
diff changeset
   164
!OrderedCollection class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   165
a27a279701f8 Initial revision
claus
parents:
diff changeset
   166
new
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   167
    "create a new, empty OrderedCollection"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   168
a27a279701f8 Initial revision
claus
parents:
diff changeset
   169
    ^ (self basicNew) initContents:10
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   170
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   171
    "Modified: 19.3.1996 / 17:53:12 / cg"
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   172
!
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   173
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   174
new:size
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   175
    "create a new, empty OrderedCollection with a preallocated physical
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   176
     size. 
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   177
     NOTICE: 
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   178
        the logical size of the returned collection is 0 (i.e. it is empty).
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   179
        This may be confusing, in that it is different from what Array>>new:
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   180
        returns.
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   181
     "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   182
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   183
    ^ (self basicNew) initContents:(size max:3)
1106
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   184
a101113cc0a8 commentary
Claus Gittinger <cg@exept.de>
parents: 1090
diff changeset
   185
    "Modified: 19.3.1996 / 17:53:47 / cg"
4427
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   186
!
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   187
6700
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   188
new:size withAll:element
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   189
    "return a new collection of size, where all elements are
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   190
     initialized to element."
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   191
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   192
    |newCollection|
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   193
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   194
    newCollection := (self new:size) grow:size.
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   195
    newCollection atAllPut:element.
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   196
    ^ newCollection
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   197
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   198
    "
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   199
     OrderedCollection new:10 withAll:1234  
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   200
    "
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   201
!
e4254778c239 new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents: 6088
diff changeset
   202
4427
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   203
newFrom:aCollection
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   204
    "return a new OrderedCollection filled with all elements from the argument,
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   205
     aCollection"
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   206
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   207
    |newColl|
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   208
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   209
    newColl := self new:(aCollection size).
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   210
    newColl addAll:aCollection.
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   211
    ^ newColl
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   212
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   213
    "
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   214
     OrderedCollection newFrom:#(1 2 3 4)
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   215
     OrderedCollection newFrom:(Set with:1 with:2 with:3)
8833e118ff25 added #newFrom:
Claus Gittinger <cg@exept.de>
parents: 4153
diff changeset
   216
    "
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   217
! !
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
   218
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   219
!OrderedCollection methodsFor:'accessing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   220
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   221
at:anInteger
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   222
    "return the element at index, anInteger"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   223
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   224
    |idx "{ Class: SmallInteger }"|
387
claus
parents: 384
diff changeset
   225
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   226
    idx := anInteger + firstIndex - 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   227
    ((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   228
        ^ self subscriptBoundsError:anInteger
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   229
    ].
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   230
    ^ contentsArray basicAt:idx
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   231
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   232
    "Modified: / 12.11.1997 / 17:56:05 / cg"
360
claus
parents: 349
diff changeset
   233
!
claus
parents: 349
diff changeset
   234
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   235
at:anInteger put:anObject
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1170
diff changeset
   236
    "set the element at index, to be anInteger.
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1170
diff changeset
   237
     Return anObject (sigh)."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   238
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   239
    |idx "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   240
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   241
    idx := anInteger + firstIndex - 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   242
    ((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1170
diff changeset
   243
        ^ self subscriptBoundsError:anInteger
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   244
    ].
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   245
    ^ contentsArray basicAt:idx put:anObject
1221
46d72af387e9 commentary
Claus Gittinger <cg@exept.de>
parents: 1170
diff changeset
   246
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   247
    "Modified: / 12.11.1997 / 17:56:32 / cg"
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
   248
!
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
   249
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   250
first
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   251
    "return the first element"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   252
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   253
    firstIndex <= lastIndex ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   254
	^ contentsArray at:firstIndex
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   255
    ].
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   256
    "error if collection is empty"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   257
    ^ self emptyCollectionError
2
claus
parents: 1
diff changeset
   258
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   259
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   260
     (OrderedCollection withAll:#(1 2 3 4 5)) first
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   261
     (SortedCollection withAll:#(5 4 3 2 1)) first
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   262
    "
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   263
!
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   264
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   265
last
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   266
    "return the last element"
68
59faa75185ba *** empty log message ***
claus
parents: 56
diff changeset
   267
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   268
    firstIndex <= lastIndex ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   269
	^ contentsArray at:lastIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   270
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   271
    "error if collection is empty"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   272
    ^ self emptyCollectionError
68
59faa75185ba *** empty log message ***
claus
parents: 56
diff changeset
   273
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   274
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   275
     (OrderedCollection withAll:#(1 2 3 4 5)) last
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   276
     (SortedCollection withAll:#(5 4 3 2 1)) last
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   277
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   278
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   279
a27a279701f8 Initial revision
claus
parents:
diff changeset
   280
!OrderedCollection methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   281
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   282
add:anObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   283
    "add the argument, anObject to the end of the collection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   284
     Return the argument, anObject."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   285
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   286
    |idx "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   287
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   288
    idx := lastIndex.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   289
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   290
    (idx == contentsArray size) ifTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   291
        self makeRoomAtLast.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   292
        idx := lastIndex.
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   293
    ].
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   294
    lastIndex := idx := idx + 1.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   295
    contentsArray basicAt:idx put:anObject.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   296
    ^ anObject
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   297
348
claus
parents: 345
diff changeset
   298
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   299
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   300
     c := #(4 3 2 1) asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   301
     c add:'here'
348
claus
parents: 345
diff changeset
   302
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   303
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   304
    "Modified: / 12.11.1997 / 17:49:47 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   305
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   306
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   307
add:newObject after:oldObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   308
    "insert the argument, newObject after oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   309
     If oldObject is not in the receiver, report an error,
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   310
     otherwise return the argument, anObject."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   311
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   312
    |idx|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   313
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   314
    idx := self indexOf:oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   315
    idx ~~ 0 ifTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   316
        self add:newObject beforeIndex:(idx + 1).
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   317
        ^ newObject
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   318
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   319
    ^ self errorValueNotFound:oldObject
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
   320
348
claus
parents: 345
diff changeset
   321
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   322
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   323
     c := #(4 3 2 1) asOrderedCollection.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   324
     c add:'here' after:3; yourself.   
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   325
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   326
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   327
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   328
     c := #(4 3 2 1) asOrderedCollection.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   329
     c add:'here' after:1; yourself.   
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   330
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   331
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   332
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   333
     c := #(4 3 2 1) asOrderedCollection.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   334
     c add:'here' after:5; yourself 
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   335
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   336
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   337
    "Modified: 12.4.1996 / 13:51:56 / cg"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   338
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   339
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   340
add:anObject afterIndex:index
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   341
    "insert the argument, anObject to become located at index.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   342
     Return the receiver (sigh - ST-80 compatibility)."
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   343
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   344
    ^ self add:anObject beforeIndex:(index + 1)
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   345
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   346
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   347
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   348
     c := #(4 3 2 1) asOrderedCollection.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   349
     c add:'here' afterIndex:2           
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   350
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   351
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   352
     |c|
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   353
     c := #(4 3 2 1) asOrderedCollection.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   354
     c add:'here' afterIndex:4           
348
claus
parents: 345
diff changeset
   355
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   356
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   357
     |c|
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   358
     c := #(4 3 2 1) asOrderedCollection.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   359
     c add:'here' afterIndex:0           
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   360
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   361
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   362
     |c|
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   363
     c := #(4 3 2 1) asOrderedCollection.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   364
     c add:'here' afterIndex:5           
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   365
    "
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   366
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   367
    "Modified: 12.4.1996 / 13:52:41 / cg"
348
claus
parents: 345
diff changeset
   368
!
claus
parents: 345
diff changeset
   369
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   370
add:newObject before:oldObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   371
    "insert the argument, newObject before oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   372
     If oldObject is not in the receiver, report an error,
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   373
     otherwise return the argument, anObject."
348
claus
parents: 345
diff changeset
   374
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   375
    |idx|
348
claus
parents: 345
diff changeset
   376
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   377
    idx := self indexOf:oldObject.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   378
    idx ~~ 0 ifTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   379
        self add:newObject beforeIndex:idx.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   380
        ^ newObject
348
claus
parents: 345
diff changeset
   381
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   382
    ^ self errorValueNotFound:oldObject
348
claus
parents: 345
diff changeset
   383
claus
parents: 345
diff changeset
   384
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   385
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   386
     c := #(4 3 2 1) asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   387
     c add:'here' before:3.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   388
     c add:'here' before:5 
348
claus
parents: 345
diff changeset
   389
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   390
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   391
    "Modified: 12.4.1996 / 13:25:47 / cg"
348
claus
parents: 345
diff changeset
   392
!
claus
parents: 345
diff changeset
   393
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   394
add:anObject beforeIndex:index
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   395
    "insert the argument, anObject to become located at index.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   396
     Return the receiver (sigh - ST-80 compatibility)."
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   397
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   398
    |idx physicalIndex|
348
claus
parents: 345
diff changeset
   399
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   400
    physicalIndex := index + firstIndex - 1.
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   401
    physicalIndex > lastIndex ifTrue:[
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   402
        physicalIndex == (lastIndex+1) ifTrue:[
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   403
            self addLast:anObject.
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   404
            ^ self
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   405
        ].
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   406
        ^ self subscriptBoundsError:index
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   407
    ].
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   408
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   409
    idx := self makeRoomAtIndex:physicalIndex.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   410
    "/ notice: the above may change firstIndex
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   411
    contentsArray basicAt:idx put:anObject.
348
claus
parents: 345
diff changeset
   412
claus
parents: 345
diff changeset
   413
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   414
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   415
     c := #(4 3 2 1) asOrderedCollection.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   416
     c add:'here' beforeIndex:3   
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   417
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   418
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   419
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   420
     c := #(4 3 2 1) asOrderedCollection.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   421
     c add:'here' beforeIndex:1    
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   422
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   423
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   424
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   425
     c := #(4 3 2 1) asOrderedCollection.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   426
     c add:'here' beforeIndex:5     
348
claus
parents: 345
diff changeset
   427
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   428
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
   429
    "Modified: / 28.1.1998 / 17:17:48 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   430
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   431
3109
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   432
addAll:aCollection
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   433
    "add all elements of the argument, aCollection to the
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   434
     receiver. Returns the argument, aCollection."
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   435
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   436
    self addAll:aCollection beforeIndex:(1 + self size).
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   437
    ^ aCollection
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   438
!
2ac640e610aa reimplement #addAll:aCollection
ca
parents: 3107
diff changeset
   439
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   440
addAll:aCollection beforeIndex:index
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   441
    "insert all elements of the argument, anObject to become located at index.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   442
     The collection may be unordered, but then order of the sliced-in elements
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   443
     is undefined.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   444
     Return the receiver."
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   445
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   446
    |idx count|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   447
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   448
    aCollection isSequenceable ifTrue:[
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   449
        "/ we are lucky - that thing can count & do bulk copies
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   450
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   451
        count := aCollection size.
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   452
        idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count.
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   453
        "/ notice: the above may change firstIndex
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   454
        contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   455
        ^ self
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   456
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   457
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   458
    idx := index.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   459
    aCollection do:[:element |
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   460
        self add:element beforeIndex:idx.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   461
        idx := idx + 1.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   462
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   463
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   464
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   465
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   466
     c := #(1 2 3 4) asOrderedCollection.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   467
     c addAll:'here' beforeIndex:3   
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   468
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   469
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   470
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   471
     c := #(1 2 3 4) asOrderedCollection.
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   472
     c removeFirst.
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   473
     c addAll:'here' beforeIndex:3   
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   474
    "
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   475
    "
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   476
     |c|
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   477
     c := #(1 2 3 4) asOrderedCollection.
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   478
     c addAll:'here' beforeIndex:1    
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   479
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   480
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   481
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   482
     c := #(1 2 3 4) asOrderedCollection.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   483
     c addAll:'here' beforeIndex:5     
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   484
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   485
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   486
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   487
     |c|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   488
     c := #(1 2 3 4) asOrderedCollection.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   489
     c addAll:('hello' asSet) beforeIndex:3
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   490
    "
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   491
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   492
    "Modified: 15.4.1997 / 12:43:59 / cg"
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   493
!
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
   494
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   495
addFirst:anObject
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   496
    "add the argument, anObject to the beginning of the collection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   497
     Return the argument, anObject."
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   498
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   499
    |idx "{ Class:SmallInteger }"|
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   500
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   501
    (idx := firstIndex) == 1 ifTrue:[
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   502
        self makeRoomAtFront.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   503
        idx := firstIndex.
348
claus
parents: 345
diff changeset
   504
    ].
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   505
    firstIndex := idx := idx - 1.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   506
    contentsArray basicAt:idx put:anObject.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   507
    ^ anObject
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   508
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   509
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   510
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   511
     c := #(1 2 3 4) asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   512
     c addFirst:'here'.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   513
     c
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   514
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   515
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   516
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   517
     |c|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   518
     c := #() asOrderedCollection.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   519
     c addFirst:'here'.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   520
     c
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   521
    "
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   522
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   523
    "Modified: / 12.11.1997 / 17:58:05 / cg"
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   524
!
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   525
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   526
addLast:anObject
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   527
    "add the argument, anObject to the end of the collection
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   528
     Return the argument, anObject."
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   529
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   530
    |idx "{ Class:SmallInteger }"|
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   531
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   532
    idx := lastIndex.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   533
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   534
    (idx == contentsArray size) ifTrue:[
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   535
        self makeRoomAtLast.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   536
        idx := lastIndex.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   537
    ].
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   538
    lastIndex := idx := idx + 1.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   539
    contentsArray basicAt:idx put:anObject.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   540
    ^ anObject
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   541
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   542
    "
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   543
     |c|
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   544
     c := #(4 3 2 1) asOrderedCollection.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   545
     c add:'here'
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   546
    "
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   547
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   548
    "Modified: / 12.4.1996 / 13:26:57 / cg"
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   549
    "Created: / 12.11.1997 / 17:46:29 / cg"
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   550
!
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
   551
3368
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   552
clearContents
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   553
    "remove all elements from the collection but keep the contentsArray.
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   554
     Useful for huge lists, if the contents will be rebuild soon (using #add:) 
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   555
     to a size which is similar to the lists current size.
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   556
     Returns the receiver."
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   557
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   558
    "/ clear those references, to give the garbage collector
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   559
    "/ a chance ...
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   560
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   561
    firstIndex >= lastIndex ifTrue:[
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   562
        contentsArray from:firstIndex to:lastIndex put:nil.
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   563
    ].
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   564
    firstIndex := 1.
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   565
    lastIndex := 0.
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   566
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   567
    "/ this is the same as:
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   568
    "/ self initContents:(self size)
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   569
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   570
    "Modified: 12.4.1996 / 13:34:19 / cg"
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   571
!
5b84482d26c9 added #clearContents:
ca
parents: 3236
diff changeset
   572
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   573
remove:anObject ifAbsent:exceptionBlock
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   574
    "remove the first element which is equal to anObject;
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   575
     if found, remove and return it; 
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   576
     if not, return the value from evaluating exceptionBlock.
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   577
     Uses equality compare (=) to search for the element."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   578
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   579
    |index retVal|
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   580
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   581
    index := contentsArray indexOf:anObject startingAt:firstIndex endingAt:lastIndex.
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   582
    index ~~ 0 ifTrue:[
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   583
        retVal := contentsArray at:index.
2361
dd95b53674e9 oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents: 2355
diff changeset
   584
        index := index - firstIndex + 1.
dd95b53674e9 oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents: 2355
diff changeset
   585
        self removeFromIndex:index toIndex:index.
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   586
        ^ retVal
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   587
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   588
    ^ exceptionBlock value
348
claus
parents: 345
diff changeset
   589
claus
parents: 345
diff changeset
   590
    "
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   591
     #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt]
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   592
     #(1 2 3 4 5) asOrderedCollection remove:3 ifAbsent:[self halt]
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   593
348
claus
parents: 345
diff changeset
   594
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops' 
claus
parents: 345
diff changeset
   595
     #(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' 
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   596
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   597
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
348
claus
parents: 345
diff changeset
   598
    "
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   599
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   600
    "Modified: 8.2.1997 / 19:17:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   601
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   602
114
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   603
removeAll
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   604
    "remove all elements from the collection.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   605
     Returns the receiver."
114
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   606
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   607
    self initContents:10
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   608
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   609
    "Modified: 12.4.1996 / 13:34:19 / cg"
114
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   610
!
0353ec6749c8 added faster removeAll
claus
parents: 93
diff changeset
   611
348
claus
parents: 345
diff changeset
   612
removeAllSuchThat:aBlock
claus
parents: 345
diff changeset
   613
    "remove all elements that meet a test criteria as specified in aBlock.
claus
parents: 345
diff changeset
   614
     The argument, aBlock is evaluated for successive elements and all those,
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   615
     for which it returns true, are removed.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   616
     Return a collection containing the removed elements.
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   617
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   618
     Performance hint:
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   619
        if a large number of objects is to be removed this way,
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   620
        it may be better to rebuild a new collection via #select:,
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   621
        since removing elements out of the middle is somewhat slow
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   622
        due to the need to copy over remaining elements within the collection."
348
claus
parents: 345
diff changeset
   623
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   624
    "/ this is a q&d implementation (possibly slow).
348
claus
parents: 345
diff changeset
   625
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   626
    |runIndex removed element sz|
348
claus
parents: 345
diff changeset
   627
claus
parents: 345
diff changeset
   628
    removed := self species new.
claus
parents: 345
diff changeset
   629
claus
parents: 345
diff changeset
   630
    runIndex := 1.
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   631
    sz := self size.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   632
    [runIndex <= sz] whileTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   633
        element := self at:runIndex.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   634
        (aBlock value:element) ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   635
            removed add:element.
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   636
            self removeAtIndex:runIndex.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   637
            sz := sz - 1.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   638
        ] ifFalse:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   639
            runIndex := runIndex + 1
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   640
        ]
348
claus
parents: 345
diff changeset
   641
    ].
claus
parents: 345
diff changeset
   642
    ^ removed
claus
parents: 345
diff changeset
   643
claus
parents: 345
diff changeset
   644
    "
claus
parents: 345
diff changeset
   645
     |coll|
claus
parents: 345
diff changeset
   646
claus
parents: 345
diff changeset
   647
     coll := OrderedCollection withAll:(1 to:10).
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
   648
     Transcript showCR:(coll removeAllSuchThat:[:el | el even]).
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
   649
     Transcript showCR:coll
348
claus
parents: 345
diff changeset
   650
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   651
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   652
    "Modified: 8.2.1997 / 19:19:00 / cg"
348
claus
parents: 345
diff changeset
   653
!
claus
parents: 345
diff changeset
   654
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   655
removeFirst
3695
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   656
    "remove the first element from the collection; return the element.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   657
     If there is no element in the receiver collection, raise an error."
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 159
diff changeset
   658
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   659
    |anObject 
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   660
     fI "{ Class: SmallInteger }" |
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   661
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   662
    fI := firstIndex.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   663
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   664
    fI > lastIndex ifTrue:[
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   665
        "error if collection is empty"
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   666
        ^ self emptyCollectionError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   667
    ].
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   668
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   669
    anObject := contentsArray basicAt:fI.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   670
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   671
    "/ nil it out, to allow GC to reclaim it.
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   672
    contentsArray basicAt:fI put:nil.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   673
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   674
    fI := fI + 1.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   675
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   676
    fI > lastIndex ifTrue:[
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   677
        "reset to avoid ever growing"
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   678
        fI := 1.
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   679
        lastIndex := 0 
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   680
    ].
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   681
    firstIndex := fI.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   682
    ^ anObject
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   683
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   684
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   685
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   686
     OrderedCollection new removeFirst
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   687
     (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   688
    "
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   689
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   690
    "Modified: / 5.2.1999 / 23:22:58 / cg"
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   691
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   692
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   693
removeFirst:n
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   694
    "remove the first n elements from the collection; 
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   695
     Return a collection containing the removed elements."
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   696
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   697
    |mySize ret newFirstIndex|
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   698
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   699
    mySize := self size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   700
    mySize < n ifTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   701
        "error if collection has not enough elements"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   702
        ^ self notEnoughElementsError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   703
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   704
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   705
    ret := Array new:n.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   706
    ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   707
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   708
    "/
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   709
    "/ nil-out contents array, to not keep elements from being GC'd
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   710
    "/
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   711
    newFirstIndex := firstIndex + n.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   712
    contentsArray from:firstIndex to:newFirstIndex - 1 put:nil.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   713
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   714
    newFirstIndex > lastIndex ifTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   715
        "reset to avoid ever growing"
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   716
        newFirstIndex := 1.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   717
        lastIndex := 0 
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   718
    ].
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   719
    firstIndex := newFirstIndex.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   720
    ^ ret
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   721
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
   722
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   723
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   724
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   725
     OrderedCollection new removeFirst:2 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   726
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   727
     (SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself  
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   728
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   729
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   730
    "Modified: 8.2.1997 / 19:20:18 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   731
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   732
3695
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   733
removeFirstIfAbsent:exceptionBlock
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   734
    "remove the first element from the collection; return the element.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   735
     If there is no element in the receiver collection, return the value from
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   736
     exceptionBlock."
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   737
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   738
    |anObject fI "{ Class: SmallInteger }" |
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   739
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   740
    fI := firstIndex.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   741
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   742
    fI > lastIndex ifTrue:[
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   743
        "error if collection is empty"
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   744
        ^ exceptionBlock value.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   745
    ].
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   746
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   747
    anObject := contentsArray basicAt:fI.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   748
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   749
    "/ nil it out, to allow GC to reclaim it.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   750
    contentsArray basicAt:fI put:nil.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   751
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   752
    fI := fI + 1.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   753
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   754
    fI > lastIndex ifTrue:[
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   755
        "reset to avoid ever growing"
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   756
        fI := 1.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   757
        lastIndex := 0 
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   758
    ].
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   759
    firstIndex := fI.
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   760
    ^ anObject
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   761
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   762
    "
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   763
     (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   764
     OrderedCollection new removeFirst
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   765
     (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   766
    "
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   767
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   768
    "Modified: / 12.11.1997 / 17:58:43 / cg"
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   769
    "Created: / 30.7.1998 / 13:19:42 / cg"
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   770
!
9ebf6de8a764 added #removeAllIfAbsent:
Claus Gittinger <cg@exept.de>
parents: 3368
diff changeset
   771
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   772
removeFrom:startIndex to:stopIndex
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   773
    "added for ST-80 compatibility.
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   774
     Same as removeFromIndex:toIndex:."
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   775
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   776
    ^ self removeFromIndex:startIndex toIndex:stopIndex
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   777
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   778
    "Created: 15.4.1997 / 12:39:00 / cg"
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   779
!
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
   780
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   781
removeFromIndex:startIndex toIndex:stopIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   782
    "remove the elements stored under startIndex up to and including
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   783
     the elements under stopIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   784
     Return the receiver.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   785
     Returning the receiver here is a historic leftover - it may change.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   786
     Please use yourself in a cascade, if you need the receivers value
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   787
     when using this method."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   788
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   789
    |nDeleted "{ Class: SmallInteger }"
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   790
     fI "{ Class: SmallInteger }" 
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   791
     lI "{ Class: SmallInteger }" 
2364
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
   792
     newLastIndex sz|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   793
2364
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
   794
    sz := self size.
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
   795
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
   796
    (startIndex < 1 or:[stopIndex > sz]) ifTrue:[
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   797
        ^ self notEnoughElementsError
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   798
    ].
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   799
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   800
    nDeleted := stopIndex - startIndex + 1.
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   801
    nDeleted < 0 ifTrue:[
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   802
        "/ mhmh - what should be done here ?
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   803
        ^ self error:'bad index range'
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   804
    ].
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   805
    nDeleted == 0 ifTrue:[^ self].
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   806
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   807
    fI := firstIndex.
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   808
    lI := lastIndex.
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   809
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   810
    "/
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   811
    "/ can be done faster, when removing the first elements
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   812
    "/
2364
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
   813
    startIndex == 1 ifTrue:[
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   814
        "/ nil out (helps GC)
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   815
        contentsArray
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   816
            from:fI
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   817
            to:fI + nDeleted - 1
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   818
            put:nil.
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   819
        firstIndex := fI := fI + nDeleted
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   820
    ] ifFalse:[
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   821
        "/
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   822
        "/ can be done faster, when removing the last elements
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   823
        "/
2364
788423a8d67f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2362
diff changeset
   824
        stopIndex == sz ifTrue:[
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   825
            "/ nil out (helps GC)
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   826
            contentsArray
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   827
                from:lI - nDeleted + 1
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   828
                to:lI
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   829
                put:nil.
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   830
            lastIndex := lI := lI - nDeleted
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   831
        ] ifFalse:[
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   832
            "/
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   833
            "/ must shuffle
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   834
            "/ TODO:
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   835
            "/    for big collections, try to copy the smallest
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   836
            "/    possible number of elements
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   837
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   838
            newLastIndex := lI - nDeleted.
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   839
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   840
            contentsArray 
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   841
                replaceFrom:(fI + startIndex - 1)
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   842
                to:newLastIndex
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   843
                with:contentsArray 
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   844
                startingAt:(fI + stopIndex).
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   845
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   846
            "/ nil out rest (helps GC)
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   847
            contentsArray
2362
23574f29c8af checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2361
diff changeset
   848
                from:(newLastIndex + 1)
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   849
                to:lI
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   850
                put:nil.
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   851
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   852
            lastIndex := lI := newLastIndex.
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   853
        ]
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   854
    ].
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   855
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   856
    fI > lI ifTrue:[
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   857
        "reset to avoid ever growing"
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   858
        firstIndex := 1.
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   859
        lastIndex := 0 
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   860
    ]
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   861
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   862
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   863
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   864
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:8
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   865
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   866
     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   867
     #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
   868
    "
2355
9476ba4d810d faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents: 2354
diff changeset
   869
3968
a526029723ac checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3695
diff changeset
   870
    "Modified: / 5.2.1999 / 23:22:07 / cg"
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
   871
!
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
   872
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   873
removeIdentical:anObject ifAbsent:exceptionBlock
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   874
    "remove the first element which is identical to anObject;
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   875
     if found, remove and return it; 
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   876
     if not, return the value from evaluating exceptionBlock.
2361
dd95b53674e9 oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents: 2355
diff changeset
   877
     Uses identity compare (==) to search for the element."
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   878
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   879
    |index|
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   880
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   881
    index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex.
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   882
    index ~~ 0 ifTrue:[
6755
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   883
        index == firstIndex ifTrue:[
6756
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   884
            contentsArray basicAt:firstIndex put:nil.
6755
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   885
            firstIndex := firstIndex + 1.
6756
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   886
            firstIndex > lastIndex ifTrue:[
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   887
                "reset to avoid ever growing"
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   888
                firstIndex := 1.
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   889
                lastIndex := 0 
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   890
            ].
6755
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   891
        ] ifFalse:[
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   892
            index == lastIndex ifTrue:[
6756
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   893
                contentsArray basicAt:lastIndex put:nil.
6755
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   894
                lastIndex := lastIndex - 1.
6756
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   895
                firstIndex > lastIndex ifTrue:[
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   896
                    "reset to avoid ever growing"
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   897
                    firstIndex := 1.
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   898
                    lastIndex := 0 
e3ad68595086 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 6755
diff changeset
   899
                ].
6755
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   900
            ] ifFalse:[
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   901
                index := index - firstIndex + 1.
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   902
                self removeFromIndex:index toIndex:index.
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   903
            ]
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   904
        ].
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   905
        ^ anObject
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   906
    ].
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   907
    ^ exceptionBlock value
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   908
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   909
    "
2354
652d17daeeb5 faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents: 2348
diff changeset
   910
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' 
2361
dd95b53674e9 oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents: 2355
diff changeset
   911
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'; yourself 
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   912
     #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
6755
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   913
     #(fee foo bar baz) asOrderedCollection removeIdentical:#fee; yourself 
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   914
     #(fee foo bar baz) asOrderedCollection removeIdentical:#foo; yourself 
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   915
     #(fee foo bar baz) asOrderedCollection removeIdentical:#baz; yourself 
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   916
     #(fee) asOrderedCollection removeIdentical:#fee; yourself             
a5704d23e08f slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents: 6700
diff changeset
   917
     #(fee) asOrderedCollection removeIdentical:#foo; yourself             
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   918
    "
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   919
2361
dd95b53674e9 oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents: 2355
diff changeset
   920
    "Modified: 8.2.1997 / 18:57:43 / cg"
2348
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   921
!
523e9d6e0049 added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents: 2222
diff changeset
   922
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   923
removeLast
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   924
    "remove the last element from the collection; return the element"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   925
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   926
    |anObject
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   927
     idx "{ Class: SmallInteger }" |
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
   928
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   929
    idx := lastIndex.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   930
    firstIndex > idx ifTrue:[
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   931
        "error if collection is empty"
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   932
        ^ self emptyCollectionError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   933
    ].
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   934
    anObject := contentsArray basicAt:idx.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   935
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   936
    "/ nil it (helps GC)
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   937
    contentsArray basicAt:idx put:nil.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   938
    lastIndex := idx := idx - 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   939
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   940
    firstIndex > idx ifTrue:[
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   941
        "reset to avoid ever growing"
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   942
        firstIndex := 1.
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   943
        lastIndex := 0 
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   944
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   945
    ^ anObject
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
   946
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
   947
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   948
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   949
     OrderedCollection new removeLast
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   950
     (SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself
217
a0400fdbc933 *** empty log message ***
claus
parents: 202
diff changeset
   951
    "
3107
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   952
ce4b0a4b3b3a checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3095
diff changeset
   953
    "Modified: / 12.11.1997 / 17:58:57 / cg"
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   954
!
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   955
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   956
removeLast:n
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   957
    "remove the last n elements from the collection; 
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   958
     Return a collection of removed  elements."
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   959
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   960
    |mySize ret|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   961
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   962
    mySize := self size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   963
    mySize < n ifTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   964
        "error if collection has not enough elements"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   965
        ^ self notEnoughElementsError.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   966
    ].
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   967
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   968
    ret := Array new:n.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   969
    ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1.
1496
bea70dc24829 commentary
Claus Gittinger <cg@exept.de>
parents: 1422
diff changeset
   970
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   971
    "/
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   972
    "/ nil-out contents array, to not keep elements from being GC'd
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   973
    "/
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   974
    contentsArray from:lastIndex - n + 1 to:lastIndex put:nil.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   975
    lastIndex := lastIndex - n.
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   976
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   977
    firstIndex > lastIndex ifTrue:[
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   978
        "reset to avoid ever growing"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   979
        firstIndex := 1.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   980
        lastIndex := 0 
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   981
    ].
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   982
    ^ ret
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   983
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   984
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   985
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   986
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   987
     (OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   988
     (SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself  
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   989
    "
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   990
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
   991
    "Modified: 12.4.1996 / 13:39:12 / cg"
4862
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   992
!
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   993
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   994
reset
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   995
    "logically remove all elements from the collection.
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   996
     Thats almost the same as #removeAll, but keeps the contentsArray.
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   997
     Returns the receiver."
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   998
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
   999
    firstIndex := contentsArray size // 3 max: 1.
5df99a53c364 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 4427
diff changeset
  1000
    lastIndex := firstIndex - 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1001
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1002
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1003
!OrderedCollection methodsFor:'converting'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1004
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1005
asArray 
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1006
    "return the receiver as an array."
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1007
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1008
    ^ contentsArray copyFrom:firstIndex to:lastIndex
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1009
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1010
"/    |newArray sz|
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1011
"/
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1012
"/    sz := self size.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1013
"/    newArray := Array new:sz.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1014
"/    newArray replaceFrom:1 to:sz with:contentsArray startingAt:firstIndex.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1015
"/    ^ newArray
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1016
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1017
    "
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1018
     OrderedCollection new asArray
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1019
     OrderedCollection new add:1;add:2;asArray 
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1020
     OrderedCollection new add:1;removeFirst;asArray 
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1021
    "
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1022
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1023
    "
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1024
     |o rnd|
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1025
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1026
     o := OrderedCollection new.
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1027
     rnd := Random new.
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1028
     10000 timesRepeat:[
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1029
         o add:rnd next.
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1030
     ].
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1031
     Time millisecondsToRun:[o asArray]  
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1032
    "
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1033
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1034
    "Modified: 13.4.1996 / 12:10:56 / cg"
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1035
!
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1036
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1037
asOrderedCollection 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1038
    "return the receiver as an ordered collection"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1039
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1040
    "could be an instance of a subclass..."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1041
    self class == OrderedCollection ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1042
	^ self
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1043
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1044
    ^ super asOrderedCollection
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1045
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1046
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1047
!OrderedCollection methodsFor:'copying'!
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1048
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1049
, aCollection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1050
    "return a new collection formed from concatenating the receiver with
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1051
     the argument"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1052
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1053
    |newCollection|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1054
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1055
    newCollection := self copyEmpty:(self size + aCollection size).
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1056
    self do:[:element |
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1057
	newCollection add:element
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1058
    ].
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1059
    aCollection do:[:element |
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1060
	newCollection add:element
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1061
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1062
    ^ newCollection
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1063
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1064
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1065
     #(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1066
     #(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1067
    "
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1068
!
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1069
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1070
copy
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1071
    "return a new OrderedCollection containing the elements of the receiver."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1072
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1073
    "redefinition is a consequence of the implementation with a
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1074
     separate array - otherwise we get a shallow copy of the
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1075
     contents array, which is not what we want here"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1076
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1077
    ^ self copyFrom:1 to:self size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1078
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1079
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1080
copyEmpty
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1081
    "return a copy of the receiver without any elements."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1082
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1083
    ^ self copyEmpty:10
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1084
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1085
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1086
copyFrom:start to:stop
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1087
    "return a new OrderedCollection containing the elements
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1088
     from start to stop."
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1089
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1090
    |newCollection sz|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1091
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1092
    sz := stop - start + 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1093
    newCollection := self copyEmptyAndGrow:sz.   "must grow, otherwise replace fails"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1094
    newCollection replaceFrom:1 to:sz with:self startingAt:start.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1095
    ^ newCollection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1096
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1097
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1098
copyWith:newElement
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1099
    "return a new collection containing the receivers elements
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1100
     and the single new element, newElement. 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1101
     This is different from concatentation, which expects another collection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1102
     as argument, but equivalent to copy-and-addLast."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1103
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1104
    |newCollection mySize newSize|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1105
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1106
    mySize := self size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1107
    newSize := mySize + 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1108
    newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1109
    newCollection replaceFrom:1 to:mySize with:self startingAt:1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1110
    newCollection at:newSize put:newElement.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1111
    ^newCollection
42
e33491f6f260 *** empty log message ***
claus
parents: 40
diff changeset
  1112
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1113
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1114
     #(1 2 3 4 5) copyWith:$a
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1115
     'abcdefg' copyWith:$h
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1116
     'abcdefg' copyWith:'123'    -- will fail: string cannot be stored into string
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1117
     'abcdefg' copyWith:1        -- will fail: integer cannot be stored into string
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1118
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1119
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1120
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1121
postCopy
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1122
    "have to copy the contentsArray too"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1123
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1124
    contentsArray := contentsArray shallowCopy
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1125
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1126
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1127
!OrderedCollection methodsFor:'enumerating'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1128
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1129
collect:aBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1130
    "evaluate the argument, aBlock for every element in the collection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1131
     and return a collection of the results"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1132
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1133
    |newCollection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1134
     start  "{ Class:SmallInteger }"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1135
     stop   "{ Class:SmallInteger }" |
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1136
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1137
    newCollection := self copyEmpty:(self size).
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1138
    stop := lastIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1139
    start := firstIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1140
    start to:stop do:[:index |
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1141
	newCollection add:(aBlock value:(contentsArray at:index)).
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1142
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1143
    ^ newCollection
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1144
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1145
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1146
     #(1 2 3 4) asOrderedCollection collect:[:i | i * i] 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1147
     #(1 2 3 4) asOrderedCollection collect:[:i | i even] 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1148
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1149
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1150
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1151
collect:collectBlock thenSelect:selectBlock
5874
bc4d80e3700f category change
Claus Gittinger <cg@exept.de>
parents: 5556
diff changeset
  1152
    "combination of collect followed by select; 
7164
5cdefa506c0e comment
Claus Gittinger <cg@exept.de>
parents: 6756
diff changeset
  1153
     redefined to avoid the creation of an intermediate (garbage) collection."
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1154
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1155
    |newCollection newElement
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1156
     start  "{ Class:SmallInteger }"
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1157
     stop   "{ Class:SmallInteger }" |
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1158
7166
Claus Gittinger <cg@exept.de>
parents: 7165
diff changeset
  1159
    newCollection := self copyEmpty.
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1160
    stop := lastIndex.
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1161
    start := firstIndex.
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1162
    start to:stop do:[:index |
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1163
        newElement := collectBlock value:(contentsArray at:index).
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1164
        (selectBlock value:newElement) ifTrue:[
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1165
            newCollection add:newElement.
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1166
        ]
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1167
    ].
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1168
    ^ newCollection
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1169
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1170
    "
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1171
     #(1 2 3 4) asOrderedCollection collect:[:i | i * i] thenSelect:[:each | each > 5]
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1172
     ( #(1 2 3 4) asOrderedCollection collect:[:i | i * i]) select:[:each | each > 5]  
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1173
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1174
     |coll|
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1175
     coll := #(1 2 3 4) asOrderedCollection.   
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1176
     Time millisecondsToRun:[
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1177
        100000 timesRepeat:[
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1178
            coll collect:[:i | i * i] thenSelect:[:each | each > 5]
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1179
        ]
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1180
     ]   
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1181
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1182
     |coll|
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1183
     coll := #(1 2 3 4) asOrderedCollection.   
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1184
     Time millisecondsToRun:[
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1185
        100000 timesRepeat:[
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1186
            ( coll collect:[:i | i * i]) select:[:each | each > 5]
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1187
        ]
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1188
     ]   
5508
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1189
    "
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1190
!
caabcccaf926 added #collect:thenSelect:
Claus Gittinger <cg@exept.de>
parents: 5502
diff changeset
  1191
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1192
do:aBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1193
    "evaluate the argument, aBlock for every element in the collection."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1194
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1195
    contentsArray from:firstIndex to:lastIndex do:aBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1196
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1197
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1198
keysAndValuesDo:aTwoArgBlock
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1199
    "evaluate the argument, aBlock for every element in the collection,
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1200
     passing both index and element as arguments."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1201
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1202
    |start  "{ Class:SmallInteger }"
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1203
     stop   "{ Class:SmallInteger }" 
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1204
     idx    "{ Class:SmallInteger }" |
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1205
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1206
    stop := lastIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1207
    start := firstIndex.
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1208
    idx := 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1209
    start to:stop do:[:index |
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1210
        aTwoArgBlock value:idx value:(contentsArray at:index).
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1211
        idx := idx + 1.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1212
    ]
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1213
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1214
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1215
     #(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value |
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
  1216
        Transcript show:index; show:' '; showCR:value
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1217
     ]  
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1218
    "
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1219
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1220
     |oc|
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1221
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1222
     oc := #(10 20 30 40 50 60 70 80) asOrderedCollection.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1223
     oc removeFirst; removeFirst.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1224
     oc keysAndValuesDo:[:index :value |
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
  1225
        Transcript show:index; show:' '; showCR:value
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1226
     ]  
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1227
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1228
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1229
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1230
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1231
keysAndValuesReverseDo:aTwoArgBlock
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1232
    "evaluate the argument, aBlock for every element in the collection,
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1233
     passing both index and element as arguments."
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1234
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1235
    |start  "{ Class:SmallInteger }"
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1236
     stop   "{ Class:SmallInteger }" 
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1237
     idx    "{ Class:SmallInteger }"|
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1238
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1239
    stop := lastIndex.
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1240
    start := firstIndex.
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1241
    idx := (stop - start + 1).
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1242
    stop to:start by: -1 do:[:index |
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1243
        aTwoArgBlock value:idx value:(contentsArray at:index).
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1244
        idx := idx - 1.
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1245
    ]
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1246
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1247
    "
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1248
     #(10 20 30 40) asOrderedCollection keysAndValuesReverseDo:[:index :value |
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
  1249
        Transcript show:index; show:' '; showCR:value
1090
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1250
     ]  
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1251
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1252
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1253
    "
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1254
     |oc|
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1255
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1256
     oc := #(10 20 30 40 50 60 70 80) asOrderedCollection.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1257
     oc removeFirst; removeFirst.
def6e50a2d13 oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents: 1071
diff changeset
  1258
     oc keysAndValuesReverseDo:[:index :value |
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1387
diff changeset
  1259
        Transcript show:index; show:' '; showCR:value
679
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1260
     ]  
d3165edbb098 *** empty log message ***
chrisy
parents: 629
diff changeset
  1261
    "
1071
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1262
!
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1263
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1264
reverseDo:aBlock
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1265
    "evaluate the argument, aBlock for every element in the collection
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1266
     procesing elements in reverse direction (i.e. starting with the last)"
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1267
5875a6eedd28 added #includesIdentical:
Claus Gittinger <cg@exept.de>
parents: 679
diff changeset
  1268
    contentsArray from:firstIndex to:lastIndex reverseDo:aBlock
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1269
!
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1270
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1271
select:selectBlock thenCollect:collectBlock
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1272
    "combination of select followed by collect; 
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1273
     redefined to avoid the creation of an intermediate (garbage) collection."
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1274
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1275
    |newCollection element
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1276
     start  "{ Class:SmallInteger }"
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1277
     stop   "{ Class:SmallInteger }" |
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1278
7166
Claus Gittinger <cg@exept.de>
parents: 7165
diff changeset
  1279
    newCollection := self copyEmpty.
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1280
    stop := lastIndex.
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1281
    start := firstIndex.
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1282
    start to:stop do:[:index |
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1283
        element := contentsArray at:index.
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1284
        (selectBlock value:element) ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1285
            newCollection add:(collectBlock value:element).
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1286
        ]
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1287
    ].
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1288
    ^ newCollection
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1289
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1290
    "
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1291
     #(1 2 3 4 5 6 7 8) asOrderedCollection 
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1292
        select:[:each | each > 5] thenCollect:[:i | i * i]  
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1293
     ( #(1 2 3 4 5 6 7 8) asOrderedCollection 
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1294
        select:[:each | each > 5]) collect:[:i | i * i]   
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1295
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1296
     |coll|
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1297
     coll := #(1 2 3 4 5 6 7 8) asOrderedCollection. 
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1298
     Time millisecondsToRun:[
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1299
        100000 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1300
            coll select:[:each | each > 5] thenCollect:[:i | i * i]
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1301
        ]
7166
Claus Gittinger <cg@exept.de>
parents: 7165
diff changeset
  1302
     ]        
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1303
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1304
     |coll|
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1305
     coll := #(1 2 3 4 5 6 7 8) asOrderedCollection. 
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1306
     Time millisecondsToRun:[
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1307
        100000 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1308
            ( coll select:[:each | each > 5]) collect:[:i | i * i]
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1309
        ]
7166
Claus Gittinger <cg@exept.de>
parents: 7165
diff changeset
  1310
     ]        
7165
Claus Gittinger <cg@exept.de>
parents: 7164
diff changeset
  1311
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1312
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1313
56
be0ed17e6f85 *** empty log message ***
claus
parents: 42
diff changeset
  1314
!OrderedCollection methodsFor:'filling & replacing'!
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1315
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1316
replaceFrom:start to:stop with:aCollection startingAt:repStart
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1317
    "replace elements in the receiver between index start and stop,
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1318
     with elements  taken from replacementCollection starting at repStart.
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
  1319
     Return the receiver.
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
  1320
     Redefined here; can be done faster as the inherited operation."
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1321
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1322
    |end|
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1323
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1324
    end := stop + firstIndex - 1.
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1325
    ((start >= 1) and:[end <= lastIndex]) ifTrue:[
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1326
        aCollection class == self class ifTrue:[
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1327
            contentsArray
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1328
                replaceFrom:(start + firstIndex - 1)
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1329
                to:end
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1330
                with:aCollection contentsArray
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1331
                startingAt:(repStart + aCollection firstIndex - 1).
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1332
        ] ifFalse:[
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1333
            contentsArray
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1334
                replaceFrom:(start + firstIndex - 1)
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1335
                to:end
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1336
                with:aCollection
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1337
                startingAt:repStart.
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1338
        ].
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1339
        ^ self
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1340
    ].
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1341
    ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
293
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1342
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1343
    "
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1344
     |c|
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1345
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1346
     c := #(1 2 3 4 5 6) asOrderedCollection.
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1347
     c replaceFrom:3 to:6 with:c startingAt:2.
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1348
     c  
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1349
    "
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1350
    "
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1351
     |c|
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1352
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1353
     c := #(1 2 3 4 5 6) asOrderedCollection.
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1354
     c replaceFrom:3 to:5 with:c startingAt:4.
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1355
     c     
31df3850e98c *** empty log message ***
claus
parents: 282
diff changeset
  1356
    "
1170
4250fe63479b added #asArray; commentary
Claus Gittinger <cg@exept.de>
parents: 1161
diff changeset
  1357
3236
b5f80b416cff check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 3109
diff changeset
  1358
    "Modified: / 28.1.1998 / 16:49:31 / cg"
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1359
! !
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
  1360
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1361
!OrderedCollection methodsFor:'grow & shrink'!
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1362
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1363
grow:newSize
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1364
    "grow the receiver to newSize"
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1365
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1366
    |oldSize newContents oldLast newLast|
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1367
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1368
    oldSize := lastIndex - firstIndex + 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1369
    newSize ~~ oldSize ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1370
	newLast := firstIndex + newSize - 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1371
	newSize < oldSize ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1372
	    oldLast := lastIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1373
	    lastIndex := newLast.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1374
	    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1375
	     nil out rest, to give GC a chance to reclaim things
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1376
	    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1377
	    contentsArray from:lastIndex + 1 to:oldLast put:nil. 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1378
	] ifFalse:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1379
	    newLast <= contentsArray size ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1380
		lastIndex := newLast.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1381
		^ self
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1382
	    ].
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1383
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1384
	    newContents := Array basicNew:newSize.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1385
	    newContents replaceFrom:1 to:oldSize with:contentsArray startingAt:firstIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1386
	    contentsArray := newContents.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1387
	    firstIndex := 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1388
	    lastIndex := newSize
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1389
	]
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1390
    ]
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1391
! !
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1392
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1393
!OrderedCollection methodsFor:'inspecting'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1394
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1395
inspectorClass
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1396
    "redefined to launch an OrderedCollectionInspector
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1397
     (instead of the default InspectorView)."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1398
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1399
    ^ OrderedCollectionInspectorView
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1400
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1401
    "
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1402
     (OrderedCollection withAll:#(3 2 1)) inspect
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1403
     (OrderedCollection withAll:#(3 2 1)) removeFirst; yourself; inspect
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1404
     #(0 8 15 3 99 2) asSortedCollection inspect
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1405
    "
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1406
! !
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1407
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1408
!OrderedCollection methodsFor:'private'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1409
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1410
initContents:size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1411
    "setup the receiver-collection to hold size entries"
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1412
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1413
    contentsArray := Array basicNew:size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1414
    firstIndex := 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1415
    lastIndex := 0 
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1416
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1417
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1418
makeRoomAtFront
282
94f5c3a6230d *** empty log message ***
claus
parents: 253
diff changeset
  1419
    "grow/shift the contents for more room at the beginning.
94f5c3a6230d *** empty log message ***
claus
parents: 253
diff changeset
  1420
     Does not change the logical size.
94f5c3a6230d *** empty log message ***
claus
parents: 253
diff changeset
  1421
     i.e.
94f5c3a6230d *** empty log message ***
claus
parents: 253
diff changeset
  1422
     #(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6)"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1423
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1424
    |newContents
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1425
     oldSize    "{ Class:SmallInteger }"
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1426
     newSize    "{ Class:SmallInteger }" 
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1427
     startIndex "{ Class:SmallInteger }"
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1428
     sz         "{ Class:SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1429
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1430
    oldSize := contentsArray size.
506
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1431
    sz := lastIndex - firstIndex + 1.
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1432
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1433
    ((oldSize == 0) or:[sz == 0]) ifTrue:[ 
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1434
	contentsArray := Array basicNew:3.
159
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
  1435
	firstIndex := 2. lastIndex := 1.
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
  1436
	^ self
93
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1437
    ].
e31220cb391f *** empty log message ***
claus
parents: 88
diff changeset
  1438
329
claus
parents: 293
diff changeset
  1439
    "
claus
parents: 293
diff changeset
  1440
     if there is lots of room at the end (> 50%), 
claus
parents: 293
diff changeset
  1441
     shift instead of growing. This helps collections
claus
parents: 293
diff changeset
  1442
     which get elements removed at the end and added at front.
claus
parents: 293
diff changeset
  1443
    "
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1444
    oldSize > (sz * 2) ifTrue:[
508
cba92a0c84ac oops - last fix wasn't
Claus Gittinger <cg@exept.de>
parents: 506
diff changeset
  1445
	startIndex := oldSize // 4.
cba92a0c84ac oops - last fix wasn't
Claus Gittinger <cg@exept.de>
parents: 506
diff changeset
  1446
	startIndex > 1 ifTrue:[
506
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1447
	    contentsArray
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1448
		replaceFrom:startIndex
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1449
		to:startIndex + sz - 1
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1450
		with:contentsArray
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1451
		startingAt:1.
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1452
	    contentsArray from:1 to:(startIndex - 1) put:nil.
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1453
	    firstIndex := startIndex.
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1454
	    lastIndex := startIndex + sz - 1.
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1455
	    ^ self
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1456
	]
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1457
    ].
2
claus
parents: 1
diff changeset
  1458
    newSize := oldSize * 2.
329
claus
parents: 293
diff changeset
  1459
    newSize == 0 ifTrue:[ newSize := 3].
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1460
    newContents := Array basicNew:newSize.
77
6c38ca59927f *** empty log message ***
claus
parents: 68
diff changeset
  1461
    newContents
159
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
  1462
	replaceFrom:(oldSize + 1)
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
  1463
	to:newSize
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
  1464
	with:contentsArray
514c749165c3 *** empty log message ***
claus
parents: 114
diff changeset
  1465
	startingAt:1.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1466
    contentsArray := newContents.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1467
    firstIndex := firstIndex + oldSize.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1468
    lastIndex := lastIndex + oldSize
506
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1469
984b32fe635a fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents: 407
diff changeset
  1470
    "Created: 8.11.1995 / 12:47:49 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1471
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1472
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1473
makeRoomAtIndex:whereToMakeEmptySlot
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1474
    "grow the contents for inserting at whereToMakeEmptySlot. 
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1475
     The whereToMakeEmptySlot argument must be a physical index within the contentsArray.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1476
     If there is (plenty of) room at either end, elements are shifted inplace to create
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1477
     an empty slot; otherwise, a new contentsArray is allocated.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1478
     Since this changes the logical size, the modified index is returned.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1479
     i.e.
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1480
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1481
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6)
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1482
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)"
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1483
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1484
    |newContents
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1485
     newSize         "{ Class:SmallInteger }" 
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1486
     oldSize         "{ Class:SmallInteger }" 
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1487
     oneFourthOfSize "{ Class:SmallInteger }"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1488
     first           "{ Class:SmallInteger }"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1489
     last            "{ Class:SmallInteger }"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1490
     index           "{ Class:SmallInteger }"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1491
     shiftLeft shiftRight|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1492
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1493
    oldSize := contentsArray size.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1494
    oneFourthOfSize := oldSize // 4.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1495
    index := whereToMakeEmptySlot.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1496
    first := firstIndex.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1497
    last := lastIndex.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1498
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1499
    ((first > 1) and:[first > oneFourthOfSize]) ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1500
        "there is room (>25%) at the beginning"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1501
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1502
        shiftLeft := true.
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1503
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1504
        index == first ifFalse:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1505
            "/ so, we'd have to copy all elements before that index
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1506
            "/ one slot towards the containers beginning ...
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1507
            "/ if there is also space at the end, AND the number of
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1508
            "/ elements after the index is smaller than the number before,
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1509
            "/ copy the remaining elements. To copy the least possible number.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1510
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1511
            (last - index) < (index - first) ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1512
                last < (oneFourthOfSize * 3) ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1513
                    shiftLeft := false.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1514
                    shiftRight := true.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1515
                ]
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1516
            ]
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1517
        ]
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1518
    ] ifFalse:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1519
        last < (oneFourthOfSize * 3) ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1520
            shiftRight := true
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1521
        ]
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1522
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1523
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1524
    shiftLeft == true ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1525
        "there is room (>25%) at the beginning"
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1526
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1527
        index == first ifFalse:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1528
            contentsArray
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1529
                replaceFrom:(first - 1)
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1530
                to:(index - 2)
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1531
                with:contentsArray
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1532
                startingAt:first.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1533
            contentsArray at:index-1 put:nil.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1534
        ].
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1535
        firstIndex := first - 1.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1536
        ^ index - 1
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1537
    ].
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1538
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1539
    shiftRight == true ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1540
        "there is room (>25%) at the end"
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1541
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1542
        last := last + 1.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1543
        index == last ifFalse:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1544
            contentsArray
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1545
                replaceFrom:(index + 1)
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1546
                to:last 
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1547
                with:contentsArray
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1548
                startingAt:index.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1549
            contentsArray at:index put:nil
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1550
        ].
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1551
        lastIndex := last.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1552
        ^ index
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1553
    ].
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1554
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1555
    newSize := oldSize * 2.
253
30daee717a53 *** empty log message ***
claus
parents: 229
diff changeset
  1556
    newContents := Array basicNew:newSize.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1557
    index ~~ first ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1558
        newContents 
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1559
            replaceFrom:1 
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1560
            to:index - first
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1561
            with:contentsArray
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1562
            startingAt:first.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1563
    ].
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1564
    index <= last ifTrue:[
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1565
        newContents
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1566
            replaceFrom:index - first + 2
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1567
            to:(last - first + 2) 
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1568
            with:contentsArray
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1569
            startingAt:index.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1570
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1571
    contentsArray := newContents.
1161
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1572
    firstIndex := 1.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1573
    lastIndex := last - first + 2.
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1574
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1575
    "/ return the modified index
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1576
    ^ index - (first - firstIndex).
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1577
aad722dea51a return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents: 1157
diff changeset
  1578
    "Modified: 12.4.1996 / 17:18:58 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1579
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1580
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1581
makeRoomAtIndex:whereToMakeEmptySlots for:howMany
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1582
    "grow the contents for inserting at whereToMakeEmptySlot. 
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1583
     The whereToMakeEmptySlot argument must be a physical index within the contentsArray.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1584
     If there is (plenty of) room at either end, elements are shifted inplace to create
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1585
     an empty slot; otherwise, a new contentsArray is allocated.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1586
     Since this changes the logical size, the modified index is returned.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1587
     i.e.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1588
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 for:2 -> #(1 2 nil nil 3 4 5 6)
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1589
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 for:2 -> #(nil nil 1 2 3 4 5 6)
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1590
     #(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 for:2 -> #(1 2 3 4 5 6 nil nil)"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1591
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1592
    |newContents
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1593
     newSize         "{ Class:SmallInteger }" 
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1594
     oldSize         "{ Class:SmallInteger }" 
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1595
     oneFourthOfSize "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1596
     first           "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1597
     last            "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1598
     index           "{ Class:SmallInteger }"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1599
     shiftLeft shiftRight|
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1600
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1601
    oldSize := contentsArray size.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1602
    oneFourthOfSize := oldSize // 4.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1603
    index := whereToMakeEmptySlots.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1604
    first := firstIndex.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1605
    last := lastIndex.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1606
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1607
    shiftLeft := shiftRight := false.
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1608
    ((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1609
        "there is room (>25%) at the beginning"
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1610
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1611
        shiftLeft := true.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1612
    ] ifFalse:[
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1613
        ((last + howMany) <= oldSize
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1614
        and:[last < (oneFourthOfSize * 3)]) ifTrue:[
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1615
            shiftRight := true
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1616
        ]
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1617
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1618
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1619
    shiftLeft == true ifTrue:[
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1620
        "there is room at the beginning"
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1621
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1622
        index == first ifFalse:[
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1623
            contentsArray
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1624
                replaceFrom:(first - howMany)
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1625
                to:(index - howMany - 1)
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1626
                with:contentsArray
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1627
                startingAt:first.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1628
            contentsArray from:index-howMany to:index-1 put:nil.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1629
        ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1630
        firstIndex := first - howMany.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1631
        ^ index - howMany
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1632
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1633
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1634
    shiftRight == true ifTrue:[
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1635
        "there is room at the end"
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1636
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1637
        last := last + howMany.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1638
        index == last ifFalse:[
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1639
            contentsArray
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1640
                replaceFrom:(index + howMany)
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1641
                to:last 
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1642
                with:contentsArray
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1643
                startingAt:index.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1644
            contentsArray from:index to:index+howMany-1 put:nil
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1645
        ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1646
        lastIndex := last.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1647
        ^ index
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1648
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1649
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1650
    newSize := oldSize * 2.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1651
    [newSize < (oldSize+howMany)] whileTrue:[
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1652
        newSize := newSize * 2
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1653
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1654
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1655
    newContents := Array basicNew:newSize.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1656
    index > first ifTrue:[
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1657
        newContents 
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1658
            replaceFrom:1 
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1659
            to:index - first
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1660
            with:contentsArray
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1661
            startingAt:first.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1662
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1663
    index <= last ifTrue:[
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1664
        newContents
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1665
            replaceFrom:index - first + howMany + 1
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1666
            to:(last - first + howMany + 1) 
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1667
            with:contentsArray
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1668
            startingAt:(index).
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1669
    ].
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1670
    contentsArray := newContents.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1671
    firstIndex := 1.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1672
    lastIndex := last - first + howMany + 1.
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1673
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1674
    "/ return the modified index
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1675
    ^ index - (first - firstIndex).
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1676
2549
9ca8a3fcdaa7 fixed #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2364
diff changeset
  1677
    "Modified: 15.4.1997 / 12:34:16 / cg"
2222
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1678
!
e6fa084818ec added #addAll:beforeIndex:
Claus Gittinger <cg@exept.de>
parents: 2166
diff changeset
  1679
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1680
makeRoomAtLast
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1681
    "grow/shift the contents for more room at the end.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1682
     Does not change the logical size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1683
     i.e.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1684
     #(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1685
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1686
    |newContents 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1687
     oldSize    "{ Class:SmallInteger }"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1688
     newSize    "{ Class:SmallInteger }" 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1689
     startIndex "{ Class:SmallInteger }"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1690
     sz         "{ Class:SmallInteger }"|
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1691
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1692
    oldSize := contentsArray size.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1693
    sz := lastIndex - firstIndex + 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1694
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1695
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1696
     if there is lots of room at the beginning (> 50%), 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1697
     shift instead of growing. This helps collections which get
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1698
     elements removed at front and added at the end.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1699
    "
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1700
    oldSize > (sz * 2) ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1701
	startIndex := firstIndex // 4.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1702
	startIndex == 0 ifTrue:[
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1703
	    startIndex := 1
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1704
	].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1705
	contentsArray 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1706
	    replaceFrom:startIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1707
	    to:startIndex + sz - 1
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1708
	    with:contentsArray
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1709
	    startingAt:firstIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1710
	contentsArray from:startIndex + sz to:lastIndex put:nil.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1711
	firstIndex := startIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1712
	lastIndex := startIndex + sz - 1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1713
	^ self
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1714
    ].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1715
    newSize := oldSize * 2.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1716
    newSize == 0 ifTrue:[newSize := 3].
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1717
    newContents := Array basicNew:newSize.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1718
    newContents 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1719
	replaceFrom:1 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1720
	to:oldSize 
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1721
	with:contentsArray
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1722
	startingAt:1.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1723
    contentsArray := newContents
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1724
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1725
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1726
setFirstIndex:newFirstIndex lastIndex:newLastIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1727
    "set first and last index"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1728
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1729
    firstIndex := newFirstIndex.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1730
    lastIndex := newLastIndex.
6088
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  1731
!
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  1732
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  1733
setIndices
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  1734
    "added for VW compatibility: set the indices for an empty collection"
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  1735
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  1736
    firstIndex := 1.
db3fc1ec9c1a added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents: 5874
diff changeset
  1737
    lastIndex := 0.
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1738
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1739
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1740
!OrderedCollection methodsFor:'private accessing'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1741
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1742
contentsArray
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1743
    "return the orderedCollections underlying contentsArray.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1744
     The actual elements are found here starting at firstIndex,
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1745
     and ending at lastIndex."
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1746
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1747
    ^ contentsArray
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1748
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1749
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1750
firstIndex
4153
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1751
    "return the index of my first element in my underlying contentsArray.
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1752
     The actual elements are found starting this index,
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1753
     and ending at lastIndex."
3481f2a3429c much better #asArray
Claus Gittinger <cg@exept.de>
parents: 4064
diff changeset
  1754
606
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1755
    ^ firstIndex
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1756
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1757
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1758
!OrderedCollection methodsFor:'queries'!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1759
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1760
capacity
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1761
    "return the number of elements, that the receiver is
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1762
     prepared to take.
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1763
     Not used by the system; added for ST-80 compatibility."
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1764
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1765
    ^ contentsArray size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1766
!
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1767
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1768
size
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1769
    "return the number of elements in the collection"
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1770
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1771
    ^ lastIndex - firstIndex + 1
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1772
! !
7a9ab63a6757 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1773
3095
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1774
!OrderedCollection methodsFor:'searching'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1775
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1776
identityIndexOf:anObject
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1777
    "return the index of anObject or 0 if not found. Compare using =="
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1778
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1779
    |index|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1780
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1781
    index := contentsArray 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1782
                identityIndexOf:anObject 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1783
                startingAt:firstIndex
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1784
                endingAt:lastIndex.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1785
    index == 0 ifTrue:[^ 0].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1786
    ^ index - firstIndex + 1
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1787
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1788
    "Modified: 12.4.1996 / 17:58:25 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1789
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1790
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1791
identityIndexOf:anObject startingAt:startIndex
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1792
    "return the index of anObject, starting search at startIndex.
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1793
     Compare using ==; return 0 if not found in the collection"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1794
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1795
    |index|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1796
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1797
    index := contentsArray 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1798
                identityIndexOf:anObject 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1799
                startingAt:(startIndex + firstIndex - 1)
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1800
                endingAt:lastIndex.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1801
    index == 0 ifTrue:[^ 0].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1802
    ^ index - firstIndex + 1
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1803
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1804
    "Modified: 12.4.1996 / 17:58:19 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1805
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1806
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1807
indexOf:anObject
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1808
    "return the index of anObject or 0 if not found in the collection.
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1809
     Compare using ="
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1810
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1811
    |index|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1812
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1813
    index := contentsArray 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1814
                indexOf:anObject 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1815
                startingAt:firstIndex
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1816
                endingAt:lastIndex.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1817
    index == 0 ifTrue:[^ 0].
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1818
    ^ index - firstIndex + 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1819
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1820
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1821
     |c|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1822
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1823
     c := OrderedCollection new:10000.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1824
     c add:1; add:2; add:3.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1825
     c indexOf:99  
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1826
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1827
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1828
    "Modified: 12.4.1996 / 17:57:54 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1829
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1830
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1831
indexOf:anObject startingAt:startIndex
33
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1832
    "return the index of anObject, starting search at startIndex.
50cf0f6bc0ad *** empty log message ***
claus
parents: 13
diff changeset
  1833
     Compare using =; return 0 if not found in the collection"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1834
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1835
    |index|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1836
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1837
    index := contentsArray 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1838
                indexOf:anObject 
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1839
                startingAt:(startIndex + firstIndex - 1)
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1840
                endingAt:lastIndex.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1841
    index == 0 ifTrue:[^ 0].
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1842
    ^ index - firstIndex + 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1843
1157
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1844
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1845
     |c|
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1846
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1847
     c := OrderedCollection new:10000.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1848
     c add:1; add:2; add:3.
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1849
     c indexOf:4 startingAt:5    
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1850
    "
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1851
c52d548036e2 faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents: 1106
diff changeset
  1852
    "Modified: 12.4.1996 / 17:58:53 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1853
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1854
3095
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1855
!OrderedCollection methodsFor:'testing'!
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1856
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1857
includes:anObject
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1858
    "return true if anObject is in the collection. Compare using ="
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1859
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1860
    ^ (contentsArray 
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1861
                indexOf:anObject 
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1862
                startingAt:firstIndex
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1863
                endingAt:lastIndex) ~~ 0
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1864
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1865
    "Modified: 12.4.1996 / 17:57:27 / cg"
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1866
!
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1867
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1868
includesIdentical:anObject
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1869
    "return true if anObject is in the collection. Compare using =="
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1870
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1871
    ^ (contentsArray 
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1872
                identityIndexOf:anObject 
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1873
                startingAt:firstIndex
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1874
                endingAt:lastIndex) ~~ 0
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1875
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1876
    "Modified: 12.4.1996 / 17:57:09 / cg"
5401
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1877
!
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1878
5556
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1879
isEmpty
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1880
    "return true, if the receiver has no elements"
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1881
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1882
    ^ lastIndex < firstIndex
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1883
!
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1884
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1885
isFixedSize
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1886
    "return true if the receiver cannot grow - this will vanish once
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1887
     Arrays and Strings learn how to grow ..."
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1888
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1889
    ^ false
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1890
!
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1891
5401
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1892
isOrderedCollection
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1893
    "return true, if the receiver is some kind of ordered collection (or list etc);
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1894
     true is returned here - the method is only redefined in Object."
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1895
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1896
    ^ true
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1897
81ea19e6843c test for class
ps
parents: 4862
diff changeset
  1898
5556
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1899
!
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1900
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1901
notEmpty
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1902
    "return true, if the receiver has any elements"
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1903
1056cc5d6ce0 category change
Claus Gittinger <cg@exept.de>
parents: 5508
diff changeset
  1904
    ^ lastIndex >= firstIndex
3095
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1905
! !
cdb753308f6e checkin from browser
Claus Gittinger <cg@exept.de>
parents: 2549
diff changeset
  1906
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  1907
!OrderedCollection class methodsFor:'documentation'!
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  1908
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  1909
version
7166
Claus Gittinger <cg@exept.de>
parents: 7165
diff changeset
  1910
    ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.85 2003-04-02 17:03:50 cg Exp $'
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 606
diff changeset
  1911
! !