SortedCollection.st
author Claus Gittinger <cg@exept.de>
Wed, 04 Sep 2002 12:49:07 +0200
changeset 6737 b60feb2c8026
parent 6411 199e6d88c562
child 7300 461bb09c5954
permissions -rw-r--r--
remove at:put: catcher
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 95
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
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
    13
"{ Package: 'stx:libbasic' }"
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
    14
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    15
OrderedCollection subclass:#SortedCollection
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    16
	instanceVariableNames:'sortBlock'
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    17
	classVariableNames:'DefaultSortBlock'
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    18
	poolDictionaries:''
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    19
	category:'Collections-Sequenceable'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    22
!SortedCollection class methodsFor:'documentation'!
1
a27a279701f8 Initial revision
claus
parents:
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) 1993 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 95
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
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    38
documentation
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    39
"
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    40
    I keep my elements sorted. The sort order is defined by a sortblock,
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    41
    a two-argument block which, when given two elements of the collection, 
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    42
    should return true if the element given as first arg has to come before the 
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    43
    element given as second arg.
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    44
1156
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    45
    Equal elements may occur multiple times.
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    46
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    47
    SortedCollection uses quickSort to resort and a binary search when adding/removing
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    48
    elements. 
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    49
    Because insertion/removal may require that remaining elements have to
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    50
    be shifted within the container, adding many individual elements may be done faster
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    51
    by creating a completely new collection from the unsorted elements.
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    52
    (see examples)
1156
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    53
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    54
    Thus a sortBlock of [:a :b | a < b] defines ascending sort-order,
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    55
    while [:a :b | a > b] defines descening order.
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    56
    The default sortBlock for SortedCollections is the first one.
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1175
diff changeset
    57
3251
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    58
    Compatibility Warning:
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    59
        VW seems to use a default sortBlock which compares a<=b,
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    60
        while ST/X uses a<b.
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    61
        That means, that elements which are compared MUST understand #< in ST/X
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    62
        while the minumum protocol is #<= in VW.
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    63
        This may be changed in a future release of ST/X 
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    64
        (it is not yet, to not confuse existing applications ...
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    65
         ... be aware, that the sortBlock has an effect on a few algorithms
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    66
         found here; especially #indexForInserting is critical.)
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    67
        
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    68
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1175
diff changeset
    69
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1175
diff changeset
    70
        Claus Gittinger
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    71
"
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    72
!
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    73
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    74
examples
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    75
"
1175
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
    76
    when many elements are to be added, it may be better to add them 
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
    77
    all en-bloeque instead of individually.
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    78
    The reason is that for each individual #add:, the contents has to be
1175
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
    79
    shifted, to create an empty slot for the new element.
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    80
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    81
    timing example:
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    82
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    83
        |o rnd|
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    84
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    85
        o := SortedCollection new.
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    86
        rnd := Random new.
1175
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
    87
        10000 timesRepeat:[
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    88
            o add:rnd next.
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
    89
        ]  
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    90
1175
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
    91
    takes 1365 ms on a P5 (admitted: this is a fast machine ;-)
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    92
    In contrast:
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    93
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    94
        |o rnd|
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    95
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    96
        o := OrderedCollection new.
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    97
        rnd := Random new.
1175
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
    98
        10000 timesRepeat:[
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    99
            o add:rnd next.
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
   100
        ].
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
   101
        o := o asSortedCollection
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
   102
1175
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
   103
    takes 383 ms on the same machine.
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   104
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   105
    If you already have a big sortedCollection at hand, adding multiple
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   106
    items may be better done with #addAll:, which resorts all elements, if
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   107
    the number of added items is more than some threshold number.
3251
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
   108
    However, the break-even point where bulk-adding is faster depends
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
   109
    on the machine ... (and ST/X version ;-).
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
   110
"
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   111
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   112
a27a279701f8 Initial revision
claus
parents:
diff changeset
   113
!SortedCollection class methodsFor:'initialization'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   114
a27a279701f8 Initial revision
claus
parents:
diff changeset
   115
initialize
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   116
    "setup the default sortBlock."
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   117
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   118
    "/ only do this once at early startup
913
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   119
    DefaultSortBlock isNil ifTrue:[
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   120
        DefaultSortBlock := [:a :b | a < b ]
913
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   121
    ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   122
913
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   123
    "
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   124
     SortedCollection initialize
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   125
    "
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   126
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   127
    "Modified: 12.4.1996 / 12:29:27 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   128
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   129
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
!SortedCollection class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
2929
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   132
forStrings
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   133
    "this is supposed to return a sortedCollection, which sorts using
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   134
     the current locales collating sequence. For now, simply use a
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   135
     normal string compare.
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   136
     This will change"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   137
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   138
    ^ self new
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   139
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   140
    "Modified: 13.9.1997 / 10:18:58 / cg"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   141
    "Created: 13.9.1997 / 10:41:54 / cg"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   142
!
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   143
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   144
forStrings:size
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   145
    "this is supposed to return a sortedCollection, which sorts using
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   146
     the current locales collating sequence. For now, simply use a
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   147
     normal string compare.
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   148
     This will change"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   149
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   150
    ^ self new:size
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   151
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   152
    "Modified: 13.9.1997 / 10:18:58 / cg"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   153
!
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   154
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   155
new
2
claus
parents: 1
diff changeset
   156
    "return a new sortedCollection, the sorting is done using
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   157
     a compare for a < b, in ascending order"
2
claus
parents: 1
diff changeset
   158
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   159
    ^ super new setSortBlock:DefaultSortBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   160
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   161
    "Modified: 12.4.1996 / 12:28:18 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   162
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
a27a279701f8 Initial revision
claus
parents:
diff changeset
   164
new:size
2
claus
parents: 1
diff changeset
   165
    "return a new sortedCollection with preallocated size.
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   166
     The sorting is done using a compare for a < b, in ascending order"
2
claus
parents: 1
diff changeset
   167
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   168
    ^ (super new:size) setSortBlock:DefaultSortBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   169
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   170
    "Modified: 12.4.1996 / 12:28:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   171
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   172
a27a279701f8 Initial revision
claus
parents:
diff changeset
   173
sortBlock:aBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   174
    "return a new sortedCollection, whe the sort order is defined
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   175
     by aBlock. 
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   176
     This must be a two-argument block which returns true if its arg1 has to come before
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   177
     its arg2 in the collection."
2
claus
parents: 1
diff changeset
   178
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   179
    ^ super new setSortBlock:aBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   180
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   181
    "default:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   182
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   183
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   184
     s := SortedCollection new.
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   185
     s add:15; add:99; add:3; add:-29; add:17; add:-6.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   186
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   187
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   188
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   189
    "sorting by absolute values:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   190
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   191
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   192
     s := SortedCollection sortBlock:[:a :b | a abs < b abs].
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   193
     s add:15; add:99; add:3; add:29; add:17; add:-6.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   194
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   195
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   196
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   197
    "default again:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   198
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   199
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   200
     s := SortedCollection new.
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   201
     s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   202
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   203
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   204
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   205
    "sorting strings caseless:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   206
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   207
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   208
     s := SortedCollection sortBlock:[:a :b | a asLowercase < b asLowercase].
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   209
     s add:'foo'; add:'Bar'; add:'baz'; add:'hello'; add:'world'; add:'Wow'.
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   210
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   211
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   212
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   213
    "Modified: 12.4.1996 / 12:26:28 / cg"
329
claus
parents: 216
diff changeset
   214
!
claus
parents: 216
diff changeset
   215
claus
parents: 216
diff changeset
   216
withAll:aCollection sortBlock:aBlock
claus
parents: 216
diff changeset
   217
    "initialize from aCollection and set the sort-block"
claus
parents: 216
diff changeset
   218
2248
b11a16f51048 withAll:aCollection sortBlock:aBlock
ca
parents: 1422
diff changeset
   219
    ^ (self sortBlock:aBlock) addAll:aCollection; yourself
329
claus
parents: 216
diff changeset
   220
claus
parents: 216
diff changeset
   221
    "
claus
parents: 216
diff changeset
   222
     SortedCollection withAll:#(1 2 3 4 5 6 7 8 9 0)
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   223
                      sortBlock:[:a :b | a > b] 
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   224
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   225
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   226
    "default:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   227
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   228
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   229
     s := SortedCollection withAll:#(15 99 3 29 17 -6).
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   230
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   231
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   232
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   233
    "sorting by absolute values:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   234
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   235
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   236
     s := SortedCollection withAll:#(15 99 3 29 17 -6) sortBlock:[:a :b | a abs < b abs].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   237
     Transcript showCR:s
329
claus
parents: 216
diff changeset
   238
    "
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   239
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   240
    "default again:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   241
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   242
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   243
     s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow').
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   244
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   245
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   246
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   247
    "sorting strings caseless:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   248
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   249
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   250
     s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow') sortBlock:[:a :b | a asLowercase < b asLowercase].
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   251
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   252
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   253
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   254
    "Modified: 12.4.1996 / 12:28:09 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   255
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   256
6411
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   257
!SortedCollection class methodsFor:'Compatibility - Dolphin'!
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   258
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   259
sortBlock:aBlock withAll:aCollection
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   260
    ^ self withAll:aCollection sortBlock:aBlock
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   261
! !
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   262
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   263
!SortedCollection methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   264
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   265
add:anObject
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   266
    "add the argument, anObject at the proper place in the
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   267
     receiver. Returns the argument, anObject."
2
claus
parents: 1
diff changeset
   268
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   269
    |index|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   270
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   271
    lastIndex < firstIndex "i.e. self size == 0" ifTrue:[
3340
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   272
        "/ empty - add at the end
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   273
        index := lastIndex.
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   274
        (index == contentsArray size) ifTrue:[
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   275
            self makeRoomAtLast.
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   276
            index := lastIndex.
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   277
        ].
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   278
        lastIndex := index := index + 1.
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   279
    ] ifFalse:[
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   280
        index := self indexForInserting:anObject. 
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   281
        index := self makeRoomAtIndex:index.
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   282
    ].
3340
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   283
    contentsArray basicAt:index put:anObject.
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   284
    ^ anObject
2
claus
parents: 1
diff changeset
   285
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   286
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   287
     |c| #(7 3 9 10 99) asSortedCollection add:5; yourself    
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   288
     #(7 3 9 10 99) asSortedCollection add:1; yourself        
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   289
     #(7 3 9 10 99) asSortedCollection add:1000; yourself     
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   290
    "
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   291
3340
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   292
    "Modified: / 13.3.1998 / 16:21:52 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   293
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   294
a27a279701f8 Initial revision
claus
parents:
diff changeset
   295
addAll:aCollection
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   296
    "add all elements of the argument, aCollection to the receiver. 
5028
1f7e943ce46e use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 4596
diff changeset
   297
     Returns the argument, aCollection (sigh)."
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   298
5816
ce15ce5294b4 Cleanup unused method vars
Stefan Vogel <sv@exept.de>
parents: 5668
diff changeset
   299
    |addedCollection|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   300
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   301
    (aCollection isSortedCollection
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   302
    and:[aCollection sortBlock == sortBlock]) ifTrue:[
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   303
        addedCollection := aCollection
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   304
    ] ifFalse:[
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   305
        addedCollection := Array withAll:(aCollection asArray).
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   306
        addedCollection == aCollection ifTrue:[
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   307
            addedCollection := addedCollection shallowCopy
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   308
        ].
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   309
        addedCollection sort:sortBlock.
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   310
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   311
        "/ special case: I am empty - add them en-bloque.
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   312
        "/ (but only if the argument is not a sortedCollection,
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   313
        "/  because quicksort degenerates to a veeery slow operation then)
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   314
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   315
        self size == 0 ifTrue:[
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   316
            contentsArray := addedCollection.
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   317
            firstIndex := 1.
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   318
            lastIndex := addedCollection size.
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   319
            ^ aCollection
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   320
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   321
    ].
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   322
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   323
    "/
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   324
    "/ do a mergeSort with the sortedContents
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   325
    "/
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   326
    self mergeSorted:addedCollection.
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   327
    ^ aCollection
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   328
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   329
    "
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   330
     #(7 3 9 10 99) asSortedCollection addAll:#(77 0 1 16 5); yourself  
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   331
5028
1f7e943ce46e use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 4596
diff changeset
   332
     ( #(7 3 9 10 99) asSortedCollection:[:a :b | a > b])
1f7e943ce46e use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 4596
diff changeset
   333
         addAll:#(77 0 1 16 5); yourself  
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   334
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   335
     #(7 3 9 10 99) asSortedCollection 
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   336
        addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a > b]); yourself  
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   337
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   338
     (#(7 3 9 10 99) asSortedCollection:[:a :b | a > b]) 
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   339
        addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a < b]); yourself  
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   340
    "
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   341
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   342
    "Modified: 13.4.1996 / 12:42:15 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   343
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   344
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   345
mergeSorted:aSortedCollection
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   346
    "add all elements of the argument, aSortedCollection to the
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   347
     receiver. 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   348
     This leads to an error, if the argument is not a sortedCollection.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   349
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   350
     This should be used when a number of elements is to be added. 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   351
     Notice, that quicksort degenerates to a veeery slow bubble-like
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   352
     sort when a collection of already sorted elements is given to it.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   353
     Therefore, adding chunks is better done by sorting the chunk and merging
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   354
     it in, instead of resorting the receiver."
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   355
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   356
    |newContentsArray 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   357
     contentsArray2
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   358
     destIndex "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   359
     n1        "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   360
     n2        "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   361
     srcIndex1 "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   362
     srcIndex2 "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   363
     last1     "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   364
     last2     "{ Class: SmallInteger }"  
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   365
     end1Reached end2Reached
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   366
     el1 el2 |
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   367
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   368
    n1 := self size.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   369
    n2 := aSortedCollection size.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   370
5381
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   371
    n1 == 0 ifTrue:[
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   372
        ^ aSortedCollection.
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   373
    ].
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   374
    n2 == 0 ifTrue:[
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   375
        ^ self.
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   376
    ].
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   377
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   378
    newContentsArray := Array new:(n1 + n2).
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   379
    destIndex := 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   380
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   381
    (aSortedCollection isSortedBy:sortBlock) ifTrue:[
5604
d6af3ba106f3 bug fix in merge, when arg is not a sortedCollection
Claus Gittinger <cg@exept.de>
parents: 5560
diff changeset
   382
        aSortedCollection isSortedCollection ifTrue:[
d6af3ba106f3 bug fix in merge, when arg is not a sortedCollection
Claus Gittinger <cg@exept.de>
parents: 5560
diff changeset
   383
            contentsArray2 := aSortedCollection contentsArray.
d6af3ba106f3 bug fix in merge, when arg is not a sortedCollection
Claus Gittinger <cg@exept.de>
parents: 5560
diff changeset
   384
            srcIndex2 := aSortedCollection firstIndex.
d6af3ba106f3 bug fix in merge, when arg is not a sortedCollection
Claus Gittinger <cg@exept.de>
parents: 5560
diff changeset
   385
        ] ifFalse:[
d6af3ba106f3 bug fix in merge, when arg is not a sortedCollection
Claus Gittinger <cg@exept.de>
parents: 5560
diff changeset
   386
            contentsArray2 := aSortedCollection.
d6af3ba106f3 bug fix in merge, when arg is not a sortedCollection
Claus Gittinger <cg@exept.de>
parents: 5560
diff changeset
   387
            srcIndex2 := 1.
d6af3ba106f3 bug fix in merge, when arg is not a sortedCollection
Claus Gittinger <cg@exept.de>
parents: 5560
diff changeset
   388
        ]
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   389
    ] ifFalse:[
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   390
        contentsArray2 := aSortedCollection.
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   391
        srcIndex2 := 1.
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   392
    ].
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   393
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   394
    last2 := srcIndex2 + n2 -1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   395
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   396
    srcIndex1 := firstIndex.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   397
    last1 := srcIndex1 + n1 -1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   398
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   399
    (srcIndex1 <= last1 and:[srcIndex2 <= last2]) ifTrue:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   400
        el1 := contentsArray at:srcIndex1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   401
        el2 := contentsArray2 at:srcIndex2.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   402
        end1Reached := end2Reached := false.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   403
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   404
        [end1Reached or:[end2Reached]] whileFalse:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   405
            (sortBlock value:el1 value:el2) ifTrue:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   406
                "/ el1 to come before el2
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   407
                newContentsArray at:destIndex put:el1. destIndex := destIndex + 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   408
                srcIndex1 := srcIndex1 + 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   409
                srcIndex1 <= last1 ifTrue:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   410
                    el1 := contentsArray at:srcIndex1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   411
                ] ifFalse:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   412
                    end1Reached := true
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   413
                ]
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   414
            ] ifFalse:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   415
                "/ el2 to come before el1
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   416
                newContentsArray at:destIndex put:el2. destIndex := destIndex + 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   417
                srcIndex2 := srcIndex2 + 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   418
                srcIndex2 <= last2 ifTrue:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   419
                    el2 := contentsArray2 at:srcIndex2.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   420
                ] ifFalse:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   421
                    end2Reached := true
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   422
                ]
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   423
            ]
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   424
        ]
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   425
    ].
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   426
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   427
    "/ fill up with remaining elements from source1
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   428
    (srcIndex1 <= last1) ifTrue:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   429
        newContentsArray 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   430
            replaceFrom:destIndex 
3250
c4b444774f1c oops - mergesort cannot simply copy remaining
Claus Gittinger <cg@exept.de>
parents: 2929
diff changeset
   431
            to:(destIndex+(last1-srcIndex1))
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   432
            with:contentsArray
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   433
            startingAt:srcIndex1
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   434
    ].
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   435
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   436
    "/ fill up with remaining elements from source2
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   437
    (srcIndex2 <= last2) ifTrue:[
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   438
        newContentsArray 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   439
            replaceFrom:destIndex 
3250
c4b444774f1c oops - mergesort cannot simply copy remaining
Claus Gittinger <cg@exept.de>
parents: 2929
diff changeset
   440
            to:(destIndex+(last2-srcIndex2))
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   441
            with:contentsArray2
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   442
            startingAt:srcIndex2
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   443
    ].
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   444
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   445
    firstIndex := 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   446
    lastIndex := n1 + n2.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   447
    contentsArray := newContentsArray
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   448
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   449
    "
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   450
     #(7 3 9 10 99) asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   451
        mergeSorted:#(77 0 1 16 5) asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   452
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   453
     #(7 3 9 10 99) asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   454
        mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   455
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   456
     #() asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   457
        mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   458
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   459
     #(7 3 9 10 99) asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   460
        mergeSorted:#() asSortedCollection 
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   461
    "
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   462
3250
c4b444774f1c oops - mergesort cannot simply copy remaining
Claus Gittinger <cg@exept.de>
parents: 2929
diff changeset
   463
    "Modified: / 30.1.1998 / 01:49:42 / cg"
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   464
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   465
5560
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   466
!SortedCollection methodsFor:'blocked'!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   467
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   468
add:newObject after:oldObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   469
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   470
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   471
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   472
!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   473
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   474
add:newObject before:oldObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   475
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   476
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   477
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   478
!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   479
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   480
addFirst:anObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   481
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   482
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   483
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   484
!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   485
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   486
addLast:anObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   487
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   488
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   489
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   490
! !
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   491
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   492
!SortedCollection methodsFor:'converting'!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   493
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   494
asSortedCollection
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   495
    "return the receiver as a sorted collection"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   496
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   497
    "could be an instance of a subclass..."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   498
    self class == SortedCollection ifTrue:[
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   499
        sortBlock == DefaultSortBlock ifTrue:[
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   500
            ^ self
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   501
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   502
    ].
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   503
    ^ super asSortedCollection
5029
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   504
!
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   505
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   506
asSortedCollection:aSortBlock
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   507
    "return the receiver as a sorted collection, using aSortBlock.
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   508
     Return the receiver, if its a sortedCollection and the sortBlock
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   509
     is the same as the argument-sortBlock"
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   510
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   511
    "could be an instance of a subclass..."
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   512
    self class == SortedCollection ifTrue:[
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   513
        "/ if the sortBlock is the same, return the receiver
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   514
        aSortBlock == sortBlock ifTrue:[
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   515
            ^ self
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   516
        ].
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   517
    ].
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   518
    ^ super asSortedCollection:aSortBlock
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   519
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   520
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   521
!SortedCollection methodsFor:'copying'!
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   522
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   523
copyEmpty:size
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   524
    "return a copy of the receiver with no elements, but
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   525
     the same size. This method has been be redefined to
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   526
     preserve the sortBlock."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   527
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   528
    ^ (self species new:size) sortBlock:sortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   529
!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   530
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   531
postCopyFrom:anOriginal
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   532
    "sent after a copy or when a new collection species has been created.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   533
     The new collection should have the same sortBlock as the original."
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   534
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   535
    sortBlock := anOriginal sortBlock
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   536
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   537
    "
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   538
     #(4 7 1 99 -1 17) asSortedCollection inspect
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   539
     #(4 7 1 99 -1 17) asSortedCollection copy inspect
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   540
     (#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) inspect
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   541
     (#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) copy inspect
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   542
     (#(4 7 1 99 -1 17) asSortedCollection select:[:e| e even]) inspect
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   543
    "
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   544
! !
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   545
216
a8abff749575 *** empty log message ***
claus
parents: 202
diff changeset
   546
!SortedCollection methodsFor:'enumerating'!
184
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   547
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   548
collect:aBlock
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   549
    "evaluate the argument, aBlock for every element in the collection
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   550
     and return a collection of the results. Redefined to return an OrderedCollection;
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   551
     see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)"
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   552
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   553
    |newCollection
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   554
     start  "{ Class:SmallInteger }"
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   555
     stop   "{ Class:SmallInteger }" |
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   556
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   557
    newCollection := OrderedCollection new:(self size).
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   558
    stop := lastIndex.
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   559
    start := firstIndex.
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   560
    start to:stop do:[:index |
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   561
	newCollection add:(aBlock value:(contentsArray at:index)).
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   562
    ].
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   563
    ^ newCollection
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   564
! !
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   565
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   566
!SortedCollection methodsFor:'instance protocol'!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   567
5668
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   568
sort:aSortBlock
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   569
    "redefined from superclass to change the sortBlock"
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   570
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   571
    sortBlock ~~ aSortBlock ifTrue:[
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   572
        self sortBlock:aSortBlock.
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   573
    ].
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   574
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   575
!
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   576
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   577
sortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   578
    "return the block used for sorting"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   579
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   580
    ^ sortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   581
!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   582
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   583
sortBlock:aSortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   584
    "change the sort criteria for a sorted collection, resort the elements of 
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   585
    the collection, and return the receiver. The argument, aSortBlock must
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   586
    be a two-argument block which returns true if its arg1 has to come before
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   587
    its arg2 in the collection."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   588
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   589
    sortBlock := aSortBlock.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   590
    lastIndex > firstIndex ifTrue:[
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   591
	contentsArray quickSortFrom:firstIndex to:lastIndex sortBlock:aSortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   592
    ]
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   593
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   594
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   595
     #(9 8 7 6 5 4 3) asSortedCollection
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   596
     #(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a < b]
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   597
     #(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a > b]
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   598
     #($f $G $z $Y $o $H) asSortedCollection
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   599
     #($f $G $z $Y $o $H) asSortedCollection sortBlock:[:a : b | a asUppercase < b asUppercase]
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   600
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   601
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   602
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   603
!SortedCollection methodsFor:'private'!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   604
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   605
indexForInserting:anObject
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   606
    "search the index at which to insert anObject. 
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   607
     Can also be used to search for an existing element
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   608
     by checking if the element at the returned index is the one we look for.
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   609
     Uses a binarySearch since we can depend on the elements being on sorted order.
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   610
     The returned index is a physical one, for accessing contentsArray."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   611
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   612
    |low    "{ Class: SmallInteger}"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   613
     high   "{ Class: SmallInteger}"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   614
     middle "{ Class: SmallInteger}"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   615
     element|
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   616
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   617
    "
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   618
     we can of course use a binary search - since the elements are sorted
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   619
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   620
    low := firstIndex.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   621
    high := lastIndex.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   622
    [low <= high] whileTrue:[
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   623
        middle := (low + high) // 2.
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   624
        element := contentsArray at:middle.
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   625
        (sortBlock value:element value:anObject) ifTrue:[
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   626
            "middleelement is smaller than object"
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   627
            low := middle + 1
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   628
        ] ifFalse:[
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   629
            high := middle - 1
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   630
        ]
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   631
    ].
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   632
    ^ low
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   633
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   634
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   635
     #(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50      
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   636
     #(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50 
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   637
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   638
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   639
    "Modified: 12.4.1996 / 13:22:03 / cg"
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   640
!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   641
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   642
setSortBlock: aSortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   643
    "set the sortblock without resorting - private only"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   644
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   645
    sortBlock := aSortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   646
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   647
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   648
!SortedCollection methodsFor:'queries'!
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   649
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   650
isSorted
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   651
    "return true. if my elements are sorted"
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   652
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   653
    ^ true
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   654
!
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   655
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   656
isSortedBy:aBlock
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   657
    "return true, if my elements are sorted (already) by the given criterion (sortBlock)."
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   658
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   659
    aBlock == sortBlock ifTrue:[^ true].
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   660
    ^ super isSortedBy:aBlock
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   661
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   662
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   663
!
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   664
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   665
isSortedCollection
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   666
    "return true. if I am a sorted collection"
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   667
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   668
    ^ true
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   669
! !
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   670
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   671
!SortedCollection methodsFor:'searching'!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   672
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   673
after:anObject ifAbsent:exceptionBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   674
    "return the element after the argument, anObject; or nil if there is none.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   675
     If anObject is contained multiple times in the collection, return the
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   676
     the first element which is non-equal to anObject.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   677
     If the receiver does not contain anObject, return the result from evaluating
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   678
     exceptionBlock."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   679
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   680
    |index      "{ Class: SmallInteger }"
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   681
     last       "{ Class: SmallInteger }"|
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   682
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   683
    index := self indexForInserting:anObject.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   684
    ((index < firstIndex) 
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   685
     or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value].
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   686
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   687
    "skip multiple occurences of the same ..."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   688
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   689
    last := lastIndex.
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   690
    [(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   691
        index := index + 1
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   692
    ].
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   693
    (index > last) ifTrue:[^ nil].
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   694
    ^ contentsArray at:index
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   695
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   696
    "Modified: 12.4.1996 / 13:20:33 / cg"
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   697
!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   698
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   699
before:anObject ifAbsent:exceptionBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   700
    "return the element before the argument, anObject; or nil if there is none.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   701
     If the receiver does not contain anObject, return the result from evaluating
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   702
     exceptionBlock."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   703
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   704
    |index      "{ Class: SmallInteger }"|
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   705
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   706
    index := self indexForInserting:anObject.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   707
    ((index <= firstIndex) 
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   708
     or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value].
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   709
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   710
    ^ contentsArray at:index - 1
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   711
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   712
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   713
     #(7 3 9 10 99) asSortedCollection before:50
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   714
     #(7 3 9 10 99) asSortedCollection before:1 
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   715
     #(7 3 9 10 99) asSortedCollection before:10  
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   716
     #(7 3 9 10 99) asSortedCollection before:7   
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   717
     #(7 3 9 10 99) asSortedCollection before:99   
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   718
     #(7 10 3 10 9 10 10 99) asSortedCollection before:9  
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   719
     #(7 10 3 10 9 10 10 99) asSortedCollection before:10   
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   720
    "
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   721
!
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   722
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   723
indexOf:anElement
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   724
    "Return the index of anElement within the receiver. 
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   725
     If the receiver does not contain anElement, return 0."
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   726
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   727
    ^ self indexOf:anElement ifAbsent:0
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   728
!
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   729
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   730
indexOf:anElement ifAbsent:exceptionBlock
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   731
    "Return the index of anElement within the receiver. 
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   732
     If the receiver does not contain anElement, 
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   733
     return the result of evaluating the argument, exceptionBlock."
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   734
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   735
    |insertionIndex index "{ Class: SmallInteger }" 
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   736
     obj|
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   737
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   738
    firstIndex > lastIndex ifTrue:[
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   739
        "/ empty
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   740
        ^ exceptionBlock value
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   741
    ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   742
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   743
    "/ if I am small, the inherited linear search is faster ...
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   744
    (lastIndex - firstIndex) < 20 ifTrue:[
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   745
        ^ super indexOf:anElement ifAbsent:exceptionBlock
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   746
    ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   747
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   748
    insertionIndex := self indexForInserting:anElement.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   749
    insertionIndex > lastIndex ifTrue:[
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   750
        insertionIndex := lastIndex
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   751
    ] ifFalse:[
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   752
        insertionIndex < firstIndex ifTrue:[
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   753
            insertionIndex := firstIndex
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   754
        ]
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   755
    ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   756
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   757
    index := insertionIndex.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   758
    [index >= firstIndex
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   759
    and:[obj := contentsArray basicAt:index.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   760
            anElement = obj ifTrue: [^ index - firstIndex + 1].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   761
            [sortBlock value:anElement value:obj]]]
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   762
                    whileTrue: [index := index - 1].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   763
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   764
    index := insertionIndex.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   765
    [index <= lastIndex
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   766
    and: [obj := contentsArray basicAt: index.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   767
            anElement = obj ifTrue: [^ index - firstIndex + 1].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   768
            [sortBlock value:obj value:anElement]]]
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   769
                    whileTrue: [index := index + 1].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   770
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   771
    ^exceptionBlock value
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   772
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   773
    "
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   774
     #('aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb'
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   775
     #('aa' 'bb' 'cc' 'dd' 'aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' 
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   776
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   777
     |allSyms indices|
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   778
     allSyms := Symbol allInstances asSortedCollection. 
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   779
     Time millisecondsToRun:[
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   780
         indices := allSyms collect:[:el | allSyms indexOf:el]. 
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   781
     ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   782
     indices = (1 to:allSyms size)
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   783
    "
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   784
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   785
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   786
!SortedCollection methodsFor:'testing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   787
a27a279701f8 Initial revision
claus
parents:
diff changeset
   788
includes:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   789
    "return true, if the argument, anObject is in the collection.
62
e1b4369c61fb *** empty log message ***
claus
parents: 44
diff changeset
   790
     Redefined, since due to being sorted, the inclusion check can
2
claus
parents: 1
diff changeset
   791
     be done with log-n compares i.e. much faster."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   792
32
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
   793
    |index "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   794
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   795
    index := self indexForInserting:anObject.
32
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
   796
    ((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false].
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
   797
    ^ (contentsArray at:index) = anObject
2
claus
parents: 1
diff changeset
   798
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   799
    "
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   800
     #(7 3 9 10 99) asSortedCollection includes:50
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   801
     #(7 3 9 10 99) asSortedCollection includes:10
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   802
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   803
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   804
a27a279701f8 Initial revision
claus
parents:
diff changeset
   805
occurrencesOf:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   806
    "return how many times the argument, anObject is in the collection.
62
e1b4369c61fb *** empty log message ***
claus
parents: 44
diff changeset
   807
     Redefined, since due to being sorted, the range of checked objects
5048
b1e10ce753fd comment
Claus Gittinger <cg@exept.de>
parents: 5030
diff changeset
   808
     can be limited i.e. it can be done much faster.
b1e10ce753fd comment
Claus Gittinger <cg@exept.de>
parents: 5030
diff changeset
   809
      Uses #= (i.e. equality) compare."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   810
a27a279701f8 Initial revision
claus
parents:
diff changeset
   811
    |index      "{ Class: SmallInteger }"
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   812
     tally      "{ Class: SmallInteger }"
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   813
     last       "{ Class: SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   814
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   815
    index := self indexForInserting:anObject.
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   816
    last := lastIndex.    
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   817
    ((index < firstIndex) or:[index > last]) ifTrue:[^ 0].
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   818
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   819
    "/ there may be multiple of them; count 'em
32
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
   820
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   821
    tally := 0.
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   822
    [(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   823
        tally := tally + 1.
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   824
        index := index + 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   825
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   826
    ^ tally
2
claus
parents: 1
diff changeset
   827
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   828
    "
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   829
     #(7 3 9 10 99) asSortedCollection occurrencesOf:50
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   830
     #(7 3 9 10 99) asSortedCollection occurrencesOf:10
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   831
     #(7 10 3 10 9 10 10 99) asSortedCollection occurrencesOf:10
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   832
    "
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   833
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   834
    "Modified: 12.4.1996 / 18:48:40 / cg"
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   835
! !
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   836
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
   837
!SortedCollection class methodsFor:'documentation'!
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
   838
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
   839
version
6737
b60feb2c8026 remove at:put: catcher
Claus Gittinger <cg@exept.de>
parents: 6411
diff changeset
   840
    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.51 2002-09-04 10:49:07 cg Exp $'
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
   841
! !
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   842
SortedCollection initialize!