SortedCollection.st
author Claus Gittinger <cg@exept.de>
Sat, 06 Jun 2015 14:42:18 +0200
changeset 18452 5724547da8de
parent 16002 5fd1486ee60a
child 18120 e3a375d5f6a8
child 18508 cd84f2138c53
permissions -rw-r--r--
class: UtcTimestamp comment/format in: #utcOffset
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
"
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
    12
"{ Package: 'stx:libbasic' }"
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
    13
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    14
OrderedCollection subclass:#SortedCollection
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    15
	instanceVariableNames:'sortBlock'
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    16
	classVariableNames:'DefaultSortBlock'
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    17
	poolDictionaries:''
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
    18
	category:'Collections-Sequenceable'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    21
!SortedCollection class methodsFor:'documentation'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    22
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    23
copyright
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    24
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    25
 COPYRIGHT (c) 1993 by Claus Gittinger
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
    26
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    27
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    28
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    29
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    31
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    32
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    33
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    34
"
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    35
!
81dacba7a63a *** empty log message ***
claus
parents: 77
diff changeset
    36
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    37
documentation
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    38
"
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    39
    I keep my elements sorted. The sort order is defined by a sortblock,
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
    40
    a two-argument block which, when given two elements of the collection,
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
    41
    should return true if the element given as first arg has to come before the
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    42
    element given as second arg.
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    43
1156
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    44
    Equal elements may occur multiple times.
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    45
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    46
    SortedCollection uses quickSort to resort and a binary search when adding/removing elements.
1156
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    47
    Because insertion/removal may require that remaining elements have to
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    48
    be shifted within the container, adding many individual elements may be done faster
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    49
    by creating a completely new collection from the unsorted elements.
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    50
    (see examples)
1156
7ce47f220573 commentary
Claus Gittinger <cg@exept.de>
parents: 1155
diff changeset
    51
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    52
    A sortBlock of [:a :b | a < b] defines ascending sort-order,
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    53
    while [:a :b | a > b] defines descening order.
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
    54
    The default sortBlock for SortedCollections is the first one.
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1175
diff changeset
    55
3251
87cac58a4e57 added VW compatibility warning
Claus Gittinger <cg@exept.de>
parents: 3250
diff changeset
    56
    Compatibility Warning:
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    57
        VW seems to use a default sortBlock which compares a<=b, wheras ST/X uses a<b.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    58
        That means, that elements which are compared MUST understand #< in ST/X
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    59
        while the minumum protocol is #<= in VW.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    60
        This may be changed in a future release of ST/X
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    61
        (it is not yet, to not confuse existing applications ...
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    62
         ... be aware, that the sortBlock has an effect on a few algorithms
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    63
         found here; especially #indexForInserting is critical.)
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
    64
14304
de0dd4c4c40a changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14116
diff changeset
    65
    [caveat:]
de0dd4c4c40a changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14116
diff changeset
    66
        a balanced tree may be a better choice, as inserting may be slow when
de0dd4c4c40a changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 14116
diff changeset
    67
        elements have to be shifted.
3251
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:]
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
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
"
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
    76
    when many elements are to be added, it may be better to add them
1175
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
9074
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
    83
        |o rnd|
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    84
9074
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
    85
        o := SortedCollection new.
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
    86
        rnd := Random new.
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
    87
        10000 timesRepeat:[
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
    88
            o add:rnd next.
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
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 ;-)
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    92
    Times are a'changing: 
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    93
        Just came around 2005: on my 1.5Ghz laptop, this now takes 260ms...
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
    94
        an another comment 2008: on a 1.8Ghz laptop, it takes now 105ms...
9074
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
    95
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    96
    In contrast:
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    97
9074
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
    98
        |o rnd|
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
    99
9074
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
   100
        o := OrderedCollection new.
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
   101
        rnd := Random new.
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
   102
        10000 timesRepeat:[
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
   103
            o add:rnd next.
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
   104
        ].
8358ce743c48 comment
Claus Gittinger <cg@exept.de>
parents: 9073
diff changeset
   105
        o := o asSortedCollection
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
   106
1175
7ca26065cf02 commentary
Claus Gittinger <cg@exept.de>
parents: 1173
diff changeset
   107
    takes 383 ms on the same machine.
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   108
        2005: on my 1.5Ghz laptop, this now takes 100ms
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   109
        2008: on a 1.8Ghz laptop, this now takes 47ms
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   110
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   111
    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
   112
    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
   113
    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
   114
    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
   115
    on the machine ... (and ST/X version ;-).
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   116
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   117
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   118
  adding elements in order:
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   119
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   120
    |c|
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   121
    Time millisecondsToRun:[
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   122
        10 timesRepeat:[
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   123
            |c|
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   124
            c := SortedCollection new.
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   125
            (1 to:100000) do:[:e | c add:e].
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   126
        ]
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   127
    ].    
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   128
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   129
    (5.4.1: 2031 2187)
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   130
    (5.4.3: 484 516)
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   131
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   132
    |c|
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   133
    c := SortedCollection new.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   134
    (1 to:100000) do:[:e | c add:e].
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   135
    self assert:(c asBag = (1 to:100000) asBag).
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   136
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   137
  adding elements in reverse order:
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   138
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   139
    Time millisecondsToRun:[
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   140
        10 timesRepeat:[
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   141
            |c|
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   142
            c := SortedCollection new.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   143
            (1 to:100000) reverseDo:[:e | c add:e].
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   144
        ]
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   145
    ].        
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   146
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   147
    (5.4.1: 201969)
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   148
    (5.4.3: 1609 1766)
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   149
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   150
    |c|
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   151
    c := SortedCollection new.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   152
    (1 to:100000) reverseDo:[:e | c add:e].
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   153
    self assert:(c asBag = (1 to:100000) asBag).
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   154
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   155
  adding elements in random order:
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   156
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   157
    |toAdd|
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   158
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   159
    toAdd := (1 to:100000) asOrderedCollection randomShuffle.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   160
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   161
    Time millisecondsToRun:[
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   162
        10 timesRepeat:[
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   163
            |c|
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   164
            c := SortedCollection new.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   165
            toAdd do:[:e | c add:e].
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   166
        ]
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   167
    ].
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   168
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   169
    (5.4.1: 108484)
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   170
    (5.4.3: 75734)
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   171
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   172
    |c|
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   173
    c := SortedCollection new.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   174
    (1 to:100000) asOrderedCollection randomShuffle do:[:e | c add:e].
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   175
    self assert:(c asBag = (1 to:100000) asBag).
1167
a24366edb65e commentary
Claus Gittinger <cg@exept.de>
parents: 1162
diff changeset
   176
"
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   177
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   178
a27a279701f8 Initial revision
claus
parents:
diff changeset
   179
!SortedCollection class methodsFor:'initialization'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   180
a27a279701f8 Initial revision
claus
parents:
diff changeset
   181
initialize
8180
7c346c8015f2 Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents: 7554
diff changeset
   182
    "setup the default sortBlock.
12034
0d6c9626eb0a comment
Claus Gittinger <cg@exept.de>
parents: 11478
diff changeset
   183
     Use #<, since this is the base method in Magnitude."
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   184
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   185
    "/ only do this once at early startup
913
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   186
    DefaultSortBlock isNil ifTrue:[
12034
0d6c9626eb0a comment
Claus Gittinger <cg@exept.de>
parents: 11478
diff changeset
   187
        DefaultSortBlock := [:a :b | a < b]
913
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   188
    ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   189
913
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   190
    "
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   191
     SortedCollection initialize
7d9881f9c908 only initialize once
Claus Gittinger <cg@exept.de>
parents: 629
diff changeset
   192
    "
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   193
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   194
    "Modified: 12.4.1996 / 12:29:27 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   195
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   196
a27a279701f8 Initial revision
claus
parents:
diff changeset
   197
!SortedCollection class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
2929
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   199
forStrings
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   200
    "this is supposed to return a sortedCollection, which sorts using
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   201
     the current locales collating sequence. For now, simply use a
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   202
     normal string compare.
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   203
     This will change"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   204
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   205
    ^ self new
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   206
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   207
    "Modified: 13.9.1997 / 10:18:58 / cg"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   208
    "Created: 13.9.1997 / 10:41:54 / cg"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   209
!
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   210
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   211
forStrings:size
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   212
    "this is supposed to return a sortedCollection, which sorts using
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   213
     the current locales collating sequence. For now, simply use a
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   214
     normal string compare.
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   215
     This will change"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   216
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   217
    ^ self new:size
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   218
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   219
    "Modified: 13.9.1997 / 10:18:58 / cg"
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   220
!
ca74fdc386cc *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2248
diff changeset
   221
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   222
new
2
claus
parents: 1
diff changeset
   223
    "return a new sortedCollection, the sorting is done using
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   224
     a compare for a < b, in ascending order"
2
claus
parents: 1
diff changeset
   225
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   226
    ^ super new setSortBlock:DefaultSortBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   227
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   228
    "Modified: 12.4.1996 / 12:28:18 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   229
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   230
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
new:size
2
claus
parents: 1
diff changeset
   232
    "return a new sortedCollection with preallocated size.
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   233
     The sorting is done using a compare for a < b, in ascending order"
2
claus
parents: 1
diff changeset
   234
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   235
    ^ (super new:size) setSortBlock:DefaultSortBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   236
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   237
    "Modified: 12.4.1996 / 12:28:22 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   238
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   239
a27a279701f8 Initial revision
claus
parents:
diff changeset
   240
sortBlock:aBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   241
    "return a new sortedCollection, whe the sort order is defined
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   242
     by aBlock.
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   243
     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
   244
     its arg2 in the collection."
2
claus
parents: 1
diff changeset
   245
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   246
    ^ super new setSortBlock:aBlock
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   247
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   248
    "default:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   249
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   250
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   251
     s := SortedCollection new.
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   252
     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
   253
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   254
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   255
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   256
    "sorting by absolute values:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   257
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   258
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   259
     s := SortedCollection sortBlock:[:a :b | a abs < b abs].
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   260
     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
   261
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   262
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   263
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   264
    "default again:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   265
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   266
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   267
     s := SortedCollection new.
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   268
     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
   269
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   270
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   271
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   272
    "sorting strings caseless:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   273
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   274
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   275
     s := SortedCollection sortBlock:[:a :b | a asLowercase < b asLowercase].
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   276
     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
   277
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   278
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   279
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   280
    "Modified: 12.4.1996 / 12:26:28 / cg"
329
claus
parents: 216
diff changeset
   281
!
claus
parents: 216
diff changeset
   282
claus
parents: 216
diff changeset
   283
withAll:aCollection sortBlock:aBlock
claus
parents: 216
diff changeset
   284
    "initialize from aCollection and set the sort-block"
claus
parents: 216
diff changeset
   285
2248
b11a16f51048 withAll:aCollection sortBlock:aBlock
ca
parents: 1422
diff changeset
   286
    ^ (self sortBlock:aBlock) addAll:aCollection; yourself
329
claus
parents: 216
diff changeset
   287
claus
parents: 216
diff changeset
   288
    "
claus
parents: 216
diff changeset
   289
     SortedCollection withAll:#(1 2 3 4 5 6 7 8 9 0)
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   290
		      sortBlock:[:a :b | a > b]
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   291
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   292
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   293
    "default:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   294
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   295
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   296
     s := SortedCollection withAll:#(15 99 3 29 17 -6).
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   297
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   298
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   299
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   300
    "sorting by absolute values:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   301
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   302
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   303
     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
   304
     Transcript showCR:s
329
claus
parents: 216
diff changeset
   305
    "
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   306
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   307
    "default again:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   308
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   309
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   310
     s := SortedCollection withAll:#('foo' 'Bar' 'baz' 'hello' 'world' 'Wow').
1422
9a0b792f2953 showCr: -> showCR:
Claus Gittinger <cg@exept.de>
parents: 1367
diff changeset
   311
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   312
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   313
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   314
    "sorting strings caseless:
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   315
     |s|
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   316
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   317
     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
   318
     Transcript showCR:s
1155
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   319
    "
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   320
4f5fe0bdfe54 commentary
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
   321
    "Modified: 12.4.1996 / 12:28:09 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   322
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   323
7300
461bb09c5954 category
Claus Gittinger <cg@exept.de>
parents: 6737
diff changeset
   324
!SortedCollection class methodsFor:'Compatibility-Dolphin'!
6411
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   325
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   326
sortBlock:aBlock withAll:aCollection
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   327
    ^ self withAll:aCollection sortBlock:aBlock
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   328
! !
199e6d88c562 dolphin compatibility
Claus Gittinger <cg@exept.de>
parents: 5816
diff changeset
   329
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   330
!SortedCollection methodsFor:'adding & removing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   331
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   332
add:anObject
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   333
    "add the argument, anObject at the proper place in the receiver. 
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   334
     Returns the argument, anObject (sigh)."
2
claus
parents: 1
diff changeset
   335
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   336
    |index lastElement|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   337
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   338
    "/
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   339
    "/ cg: the original code was simply:
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   340
    "/
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   341
    "/  index := self indexForInserting:anObject.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   342
    "/  index := self makeRoomAtIndex:index.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   343
    "/  contentsArray basicAt:index put:anObject.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   344
    "/
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   345
    "/  which was nice and simple to understand.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   346
    "/ However, the code below is 5 times faster, if elements are added in an already ordered fashion,
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   347
    "/ which often happens.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   348
    "/ so we allow the code to be a bit more complicated...
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   349
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   350
    (lastIndex < firstIndex "i.e. self size == 0"
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   351
    or:[ 
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   352
        lastElement := contentsArray at:lastIndex.
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   353
        (sortBlock value:lastElement value:anObject)    
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   354
    ]) ifTrue:[
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   355
        "/ empty or lastElement is smaller then newElement; add at the end
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   356
        index := lastIndex.
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   357
        (index == contentsArray size) ifTrue:[
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   358
            self makeRoomAtLast.
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   359
            index := lastIndex.
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   360
        ].
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   361
        lastIndex := index := index + 1.
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   362
    ] ifFalse:[
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   363
        index := self indexForInserting:anObject.
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   364
        (index == firstIndex) ifTrue:[
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   365
            (firstIndex == 1) ifTrue:[
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   366
                self makeRoomAtFront.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   367
            ].
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   368
            index := firstIndex := firstIndex - 1.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   369
        ] ifFalse:[
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   370
            index := self makeRoomAtIndex:index.
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   371
        ].
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   372
    ].
3340
300d88cf8b14 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 3251
diff changeset
   373
    contentsArray basicAt:index put:anObject.
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   374
    ^ anObject
2
claus
parents: 1
diff changeset
   375
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   376
    "
11268
7ce883e9a42e tuned add: if the elements are added in sorted order
Claus Gittinger <cg@exept.de>
parents: 9920
diff changeset
   377
     #(7 3 9 10 99) asSortedCollection add:5; yourself
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   378
     #(7 3 9 10 99) asSortedCollection add:1; yourself
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   379
     #(7 3 9 10 99) asSortedCollection add:1000; yourself
11274
a6f648b1d291 tune adding in order / in reverse order
Claus Gittinger <cg@exept.de>
parents: 11268
diff changeset
   380
     #(7 3 9 10 99) asSortedCollection add:1; yourself   
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   381
    "
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   382
11276
1d4450f49a14 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11274
diff changeset
   383
    "Modified: / 22-10-2008 / 17:09:39 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   384
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   385
a27a279701f8 Initial revision
claus
parents:
diff changeset
   386
addAll:aCollection
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   387
    "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
   388
     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
   389
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   390
    |addedCollection mySize numAdded|
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   391
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   392
    numAdded := aCollection size.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   393
    mySize := self size.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   394
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   395
    (aCollection isSortedCollection
7554
7ea961a68e34 Save copying in #addAll
Stefan Vogel <sv@exept.de>
parents: 7300
diff changeset
   396
     and:[aCollection sortBlock == sortBlock]) ifTrue:[
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   397
        addedCollection := aCollection.
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   398
    ] ifFalse:[
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   399
        addedCollection := Array withAll:aCollection.
14101
8c41a875b2cf changed: #addAll:
Stefan Vogel <sv@exept.de>
parents: 12038
diff changeset
   400
        addedCollection stableSort:sortBlock.
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   401
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   402
        mySize == 0 ifTrue:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   403
            "/ special case: I am empty - add them en-bloque.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   404
            contentsArray := addedCollection.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   405
            firstIndex := 1.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   406
            lastIndex := numAdded.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   407
            ^ aCollection
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   408
        ].
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   409
    ].
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   410
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   411
    "/
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   412
    "/ 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
   413
    "/
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   414
    self mergeSorted:addedCollection.
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   415
    ^ aCollection
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   416
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   417
    "
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   418
     #(7 3 9 10 99) asSortedCollection addAll:#(77 0 1 16 5); yourself
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   419
5028
1f7e943ce46e use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 4596
diff changeset
   420
     ( #(7 3 9 10 99) asSortedCollection:[:a :b | a > b])
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   421
         addAll:#(77 0 1 16 5); yourself
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   422
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   423
     #(7 3 9 10 99) asSortedCollection
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   424
        addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a > b]); yourself
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   425
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   426
     (#(7 3 9 10 99) asSortedCollection:[:a :b | a > b])
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   427
        addAll:( #(77 0 1 16 5) asSortedCollection:[:a :b | a < b]); yourself
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   428
    "
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   429
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   430
    "
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   431
     |x e|
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   432
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   433
     e := (1 to:100) asSortedCollection.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   434
     TimeDuration toRun:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   435
         10000 timesRepeat:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   436
            x := SortedCollection new:100.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   437
            x addAll:e. 
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   438
         ].
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   439
     ].      
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   440
    "
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   441
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   442
    "Modified: 13.4.1996 / 12:42:15 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   443
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   444
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   445
mergeSorted:aSortedCollection
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   446
    "add all elements of the argument, aSortedCollection to the receiver.
12038
1e214cdac722 comment/format in:
Claus Gittinger <cg@exept.de>
parents: 12034
diff changeset
   447
     This leads to an error, if the argument is not sorted.
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   448
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   449
     This should be used when a number of elements is to be added.
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   450
     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
   451
     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
   452
     Therefore, adding chunks is better done by sorting the chunk and merging
14310
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   453
     it in, instead of resorting the receiver.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   454
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   455
     aSortedCollection must be sorted by the same sortBlock as myself!!"
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   456
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   457
    |newContentsArray
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   458
     contentsArray2
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   459
     destIndex "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   460
     n1        "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   461
     n2        "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   462
     srcIndex1 "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   463
     srcIndex2 "{ Class: SmallInteger }"
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   464
     last1     "{ Class: SmallInteger }"
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   465
     last2     "{ Class: SmallInteger }"
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   466
     end1Reached end2Reached
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   467
     el1 el2 |
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   468
14310
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   469
    n2 := aSortedCollection size.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   470
    n2 == 0 ifTrue:[
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   471
        ^ self.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   472
    ].
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   473
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   474
    aSortedCollection isSortedCollection ifTrue:[
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   475
        contentsArray2 := aSortedCollection contentsArray.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   476
        srcIndex2 := aSortedCollection firstIndex.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   477
    ] ifFalse:[
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   478
        contentsArray2 := aSortedCollection.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   479
        srcIndex2 := 1.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   480
    ].
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   481
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   482
    n1 := self size.
5381
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   483
    n1 == 0 ifTrue:[
14310
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   484
        firstIndex := srcIndex2.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   485
        lastIndex := firstIndex + n2 - 1.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   486
        contentsArray := contentsArray2 copy.
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   487
        ^ self.
5381
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   488
    ].
914e310a69be Fix #mergeSorted if one of the collections is empty.
Stefan Vogel <sv@exept.de>
parents: 5359
diff changeset
   489
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   490
    newContentsArray := Array new:(n1 + n2).
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   491
    destIndex := 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   492
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   493
    last2 := srcIndex2 + n2 -1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   494
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   495
    srcIndex1 := firstIndex.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   496
    last1 := srcIndex1 + n1 -1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   497
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   498
    (srcIndex1 <= last1 and:[srcIndex2 <= last2]) ifTrue:[
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   499
        el1 := contentsArray at:srcIndex1.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   500
        el2 := contentsArray2 at:srcIndex2.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   501
        end1Reached := end2Reached := false.
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   502
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   503
        [end1Reached or:[end2Reached]] whileFalse:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   504
            (sortBlock value:el1 value:el2) ifTrue:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   505
                "/ el1 to come before el2
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   506
                newContentsArray at:destIndex put:el1. destIndex := destIndex + 1.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   507
                srcIndex1 := srcIndex1 + 1.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   508
                srcIndex1 <= last1 ifTrue:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   509
                    el1 := contentsArray at:srcIndex1.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   510
                ] ifFalse:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   511
                    end1Reached := true
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   512
                ]
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   513
            ] ifFalse:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   514
                "/ el2 to come before el1
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   515
                newContentsArray at:destIndex put:el2. destIndex := destIndex + 1.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   516
                srcIndex2 := srcIndex2 + 1.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   517
                srcIndex2 <= last2 ifTrue:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   518
                    el2 := contentsArray2 at:srcIndex2.
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   519
                ] ifFalse:[
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   520
                    end2Reached := true
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   521
                ]
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   522
            ]
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   523
        ]
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   524
    ].
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   525
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   526
    "/ 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
   527
    (srcIndex1 <= last1) ifTrue:[
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   528
        newContentsArray
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   529
            replaceFrom:destIndex
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   530
            to:(destIndex+(last1-srcIndex1))
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   531
            with:contentsArray
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   532
            startingAt:srcIndex1
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   533
    ].
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   534
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   535
    "/ 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
   536
    (srcIndex2 <= last2) ifTrue:[
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   537
        newContentsArray
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   538
            replaceFrom:destIndex
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   539
            to:(destIndex+(last2-srcIndex2))
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   540
            with:contentsArray2
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   541
            startingAt:srcIndex2
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   542
    ].
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   543
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   544
    firstIndex := 1.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   545
    lastIndex := n1 + n2.
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   546
    contentsArray := newContentsArray
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   547
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   548
    "
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   549
     #(7 3 9 10 99) asSortedCollection
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   550
        mergeSorted:#(77 0 1 16 5) asSortedCollection
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   551
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   552
     #(7 3 9 10 99) asSortedCollection
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   553
        mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   554
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   555
     #() asSortedCollection
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   556
        mergeSorted:#(77 0 3 1 98 99 16 5) asSortedCollection
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   557
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   558
     #(7 3 9 10 99) asSortedCollection
11286
76f3020ba113 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11276
diff changeset
   559
        mergeSorted:#() asSortedCollection
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   560
    "
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   561
3250
c4b444774f1c oops - mergesort cannot simply copy remaining
Claus Gittinger <cg@exept.de>
parents: 2929
diff changeset
   562
    "Modified: / 30.1.1998 / 01:49:42 / cg"
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   563
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   564
5560
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   565
!SortedCollection methodsFor:'blocked'!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   566
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   567
add:newObject after:oldObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   568
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   569
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   570
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   571
!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   572
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   573
add:newObject before:oldObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   574
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   575
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   576
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   577
!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   578
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   579
addFirst:anObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   580
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   581
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   582
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   583
!
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   584
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   585
addLast:anObject
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   586
    "catch this - its not allowed for sortedCollections"
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   587
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   588
    self shouldNotImplement
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   589
! !
89a83b354d27 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 5381
diff changeset
   590
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   591
!SortedCollection methodsFor:'converting'!
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
asSortedCollection
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   594
    "return the receiver as a sorted collection"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   595
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   596
    "could be an instance of a subclass..."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   597
    self class == SortedCollection ifTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   598
	sortBlock == DefaultSortBlock ifTrue:[
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   599
	    ^ self
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   600
	]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   601
    ].
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   602
    ^ super asSortedCollection
5029
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   603
!
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   604
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   605
asSortedCollection:aSortBlock
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   606
    "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
   607
     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
   608
     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
   609
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   610
    "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
   611
    self class == SortedCollection ifTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   612
	"/ if the sortBlock is the same, return the receiver
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   613
	aSortBlock == sortBlock ifTrue:[
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   614
	    ^ self
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   615
	].
5029
ae3312dda1dd avoid creation of a new sortedCollection in #asSortedCollection:
Claus Gittinger <cg@exept.de>
parents: 5028
diff changeset
   616
    ].
5030
655a0f0a60c3 use my sortBlock when merge-sorting
Claus Gittinger <cg@exept.de>
parents: 5029
diff changeset
   617
    ^ super asSortedCollection:aSortBlock
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   618
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   619
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   620
!SortedCollection methodsFor:'copying'!
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   621
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   622
copyEmpty:size
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   623
    "return a copy of the receiver with no elements, but
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   624
     the same size. This method has been be redefined to
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   625
     preserve the sortBlock."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   626
14110
86f18b7f3b54 changed: #copyEmpty:
Stefan Vogel <sv@exept.de>
parents: 14101
diff changeset
   627
    ^ (super copyEmpty:size) sortBlock:sortBlock
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   628
!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   629
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   630
postCopyFrom:anOriginal
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   631
    "sent after a copy or when a new collection species has been created.
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   632
     The new collection should have the same sortBlock as the original."
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   633
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   634
    sortBlock := anOriginal sortBlock
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   635
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   636
    "
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   637
     #(4 7 1 99 -1 17) asSortedCollection inspect
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   638
     #(4 7 1 99 -1 17) asSortedCollection copy inspect
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   639
     (#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) inspect
159
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   640
     (#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) copy inspect
514c749165c3 *** empty log message ***
claus
parents: 95
diff changeset
   641
     (#(4 7 1 99 -1 17) asSortedCollection select:[:e| e even]) inspect
95
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   642
    "
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   643
! !
d22739a0c6e9 *** empty log message ***
claus
parents: 88
diff changeset
   644
216
a8abff749575 *** empty log message ***
claus
parents: 202
diff changeset
   645
!SortedCollection methodsFor:'enumerating'!
184
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   646
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   647
collect:aBlock
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   648
    "evaluate the argument, aBlock for every element in the collection
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   649
     and return a collection of the results. Redefined to return an OrderedCollection;
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   650
     see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)"
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   651
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   652
    |newCollection
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   653
     start  "{ Class:SmallInteger }"
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   654
     stop   "{ Class:SmallInteger }" |
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   655
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   656
    newCollection := OrderedCollection new:(self size).
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   657
    stop := lastIndex.
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   658
    start := firstIndex.
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   659
    start to:stop do:[:index |
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   660
	newCollection add:(aBlock value:(contentsArray at:index)).
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   661
    ].
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   662
    ^ newCollection
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   663
! !
d795f0b1934a collect: fixed
claus
parents: 159
diff changeset
   664
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   665
!SortedCollection methodsFor:'instance protocol'!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   666
5668
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   667
sort:aSortBlock
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   668
    "redefined from superclass to change the sortBlock"
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   669
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   670
    sortBlock ~~ aSortBlock ifTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   671
	self sortBlock:aSortBlock.
5668
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   672
    ].
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   673
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   674
!
0cbaa16dc17c Inherited #sort: shows a debugger.
Stefan Vogel <sv@exept.de>
parents: 5604
diff changeset
   675
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   676
sortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   677
    "return the block used for sorting"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   678
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   679
    ^ sortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   680
!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   681
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   682
sortBlock:aSortBlock
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   683
    "change the sort criteria for a sorted collection, resort the elements of
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   684
    the collection, and return the receiver. The argument, aSortBlock must
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   685
    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
   686
    its arg2 in the collection."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   687
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   688
    sortBlock := aSortBlock.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   689
    lastIndex > firstIndex ifTrue:[
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   690
	contentsArray quickSortFrom:firstIndex to:lastIndex sortBlock:aSortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   691
    ]
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   692
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   693
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   694
     #(9 8 7 6 5 4 3) asSortedCollection
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   695
     #(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
   696
     #(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
   697
     #($f $G $z $Y $o $H) asSortedCollection
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   698
     #($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
   699
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   700
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   701
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   702
!SortedCollection methodsFor:'private'!
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
setSortBlock: aSortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   705
    "set the sortblock without resorting - private only"
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   706
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   707
    sortBlock := aSortBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   708
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   709
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   710
!SortedCollection methodsFor:'queries'!
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   711
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   712
isSorted
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   713
    "return true. if my elements are sorted"
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   714
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   715
    ^ true
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   716
!
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   717
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   718
isSortedBy:aBlock
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   719
    "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
   720
5359
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   721
    aBlock == sortBlock ifTrue:[^ true].
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   722
    ^ super isSortedBy:aBlock
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   723
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   724
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   725
!
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   726
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   727
isSortedCollection
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   728
    "return true. if I am a sorted collection"
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   729
396b77b6f1d2 added #isSortedCollection, #isSortedBy:
Claus Gittinger <cg@exept.de>
parents: 5048
diff changeset
   730
    ^ true
1173
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   731
! !
1d831f2c0d44 added #mergeSorted; changed #addAll: to make use of it
Claus Gittinger <cg@exept.de>
parents: 1167
diff changeset
   732
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   733
!SortedCollection methodsFor:'searching'!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   734
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   735
after:anObject ifAbsent:exceptionBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   736
    "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
   737
     If anObject is contained multiple times in the collection, return the
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   738
     the first element which is non-equal to anObject.
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   739
     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
   740
     exceptionBlock."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   741
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   742
    |index      "{ Class: SmallInteger }"
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   743
     last       "{ Class: SmallInteger }"|
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   744
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   745
    index := self indexForInserting:anObject.
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   746
    ((index < firstIndex)
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   747
     or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value].
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   748
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   749
    "skip multiple occurrences of the same ..."
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   750
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   751
    last := lastIndex.
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   752
    [(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   753
	index := index + 1
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   754
    ].
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   755
    (index > last) ifTrue:[^ nil].
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   756
    ^ contentsArray at:index
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   757
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
   758
    "Modified: 12.4.1996 / 13:20:33 / cg"
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   759
!
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   760
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   761
before:anObject ifAbsent:exceptionBlock
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   762
    "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
   763
     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
   764
     exceptionBlock."
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   765
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   766
    |index      "{ Class: SmallInteger }"|
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   767
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   768
    index := self indexForInserting:anObject.
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   769
    ((index <= firstIndex)
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   770
     or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value].
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   771
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   772
    ^ contentsArray at:index - 1
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   773
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   774
    "
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   775
     #(7 3 9 10 99) asSortedCollection before:50
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   776
     #(7 3 9 10 99) asSortedCollection before:1
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   777
     #(7 3 9 10 99) asSortedCollection before:10
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   778
     #(7 3 9 10 99) asSortedCollection before:7
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   779
     #(7 3 9 10 99) asSortedCollection before:99
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   780
     #(7 10 3 10 9 10 10 99) asSortedCollection before:9
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   781
     #(7 10 3 10 9 10 10 99) asSortedCollection before:10
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   782
    "
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   783
!
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   784
14310
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   785
indexForInserting:anObject
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   786
    "search the index at which to insert anObject.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   787
     Can also be used to search for an existing element
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   788
     by checking if the element at the returned index is the one we look for.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   789
     Uses a binarySearch since we can depend on the elements being in sorted order.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   790
     The returned index is a physical one, for accessing contentsArray."
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   791
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   792
    |low    "{ Class: SmallInteger}"
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   793
     high   "{ Class: SmallInteger}"
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   794
     middle "{ Class: SmallInteger}"
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   795
     element|
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   796
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   797
    "
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   798
     we can of course use a binary search - since the elements are sorted
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   799
    "
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   800
    low := firstIndex.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   801
    high := lastIndex.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   802
    [low > high] whileFalse:[
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   803
        middle := (low + high) // 2.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   804
        element := contentsArray at:middle.
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   805
        (sortBlock value:element value:anObject) ifTrue:[
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   806
            "middleelement is smaller than object"
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   807
            low := middle + 1
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   808
        ] ifFalse:[
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   809
            high := middle - 1
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   810
        ]
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   811
    ].
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   812
    ^ low
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   813
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   814
    "
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   815
     #(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   816
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   817
     #(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   818
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   819
    "
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   820
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   821
    "Modified: 12.4.1996 / 13:22:03 / cg"
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   822
!
b1787ec39fd8 changed: #mergeSorted:
Stefan Vogel <sv@exept.de>
parents: 14309
diff changeset
   823
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   824
indexOf:anElement
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   825
    "Return the index of anElement within the receiver.
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   826
     If the receiver does not contain anElement, return 0."
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   827
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   828
    ^ self indexOf:anElement ifAbsent:0
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   829
!
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   830
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   831
indexOf:anElement ifAbsent:exceptionBlock
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   832
    "Return the index of anElement within the receiver.
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   833
     If the receiver does not contain anElement,
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   834
     return the result of evaluating the argument, exceptionBlock."
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   835
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   836
    |insertionIndex index "{ Class: SmallInteger }"
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   837
     obj|
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   838
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   839
    firstIndex > lastIndex ifTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   840
	"/ empty
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   841
	^ exceptionBlock value
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   842
    ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   843
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   844
    "/ if I am small, the inherited linear search is faster ...
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   845
    (lastIndex - firstIndex) < 20 ifTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   846
	^ super indexOf:anElement ifAbsent:exceptionBlock
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   847
    ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   848
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   849
    insertionIndex := self indexForInserting:anElement.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   850
    insertionIndex > lastIndex ifTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   851
	insertionIndex := lastIndex
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   852
    ] ifFalse:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   853
	insertionIndex < firstIndex ifTrue:[
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   854
	    insertionIndex := firstIndex
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   855
	]
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   856
    ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   857
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   858
    index := insertionIndex.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   859
    [index >= firstIndex
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   860
    and:[obj := contentsArray basicAt:index.
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   861
	    anElement = obj ifTrue: [^ index - firstIndex + 1].
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   862
	    [sortBlock value:anElement value:obj]]]
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   863
		    whileTrue: [index := index - 1].
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   864
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   865
    index := insertionIndex.
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   866
    [index <= lastIndex
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   867
    and: [obj := contentsArray basicAt: index.
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   868
	    anElement = obj ifTrue: [^ index - firstIndex + 1].
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   869
	    [sortBlock value:obj value:anElement]]]
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   870
		    whileTrue: [index := index + 1].
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   871
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   872
    ^exceptionBlock value
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   873
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   874
    "
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   875
     #('aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb'
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   876
     #('aa' 'bb' 'cc' 'dd' 'aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb'
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   877
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   878
     |allSyms indices|
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   879
     allSyms := Symbol allInstances asSortedCollection.
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   880
     Time millisecondsToRun:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
   881
	 indices := allSyms collect:[:el | allSyms indexOf:el].
4596
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   882
     ].
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   883
     indices = (1 to:allSyms size)
66a082fbc5b3 tuned indexOf
Claus Gittinger <cg@exept.de>
parents: 3340
diff changeset
   884
    "
16002
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   885
!
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   886
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   887
largest:n
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   888
    "return a collection containing the n largest elements, largest last.
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   889
     Raises an exception, if the receiver does not contain at least n elements"
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   890
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   891
    |mySize|
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   892
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   893
    mySize := self size.
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   894
    n > mySize ifTrue:[
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   895
        self notEnoughElementsError
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   896
    ].
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   897
    sortBlock == DefaultSortBlock ifTrue:[
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   898
        ^ self copyFrom:(mySize-n+1)
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   899
    ].
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   900
    "/ do not trust the sortblock to sort small-to-large
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   901
    ^ super largest:n
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   902
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   903
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   904
     #(10 35 20 45 30 5) asSortedCollection largest:3  
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   905
     (#(10 35 20 45 30 5) asSortedCollection:[:a :b | a > b]) largest:3    
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   906
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   907
     #(10 35 20 45 30 5) asSortedCollection largest:6  
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   908
     #(10 35 20 45 30 5) asSortedCollection largest:7  
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   909
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   910
!
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   911
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   912
max
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   913
    "Return the largest element."
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   914
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   915
    sortBlock == DefaultSortBlock ifTrue:[
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   916
        ^ self last
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   917
    ].
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   918
    ^ super max
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   919
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   920
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   921
     #(10 35 20 45 30 5) asSortedCollection max 
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   922
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   923
!
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   924
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   925
min
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   926
    "Return the smallest element."
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   927
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   928
    sortBlock == DefaultSortBlock ifTrue:[
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   929
        ^ self first
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   930
    ].
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   931
    ^ super min
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   932
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   933
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   934
     #(10 35 20 45 30 5) asSortedCollection min
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   935
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   936
!
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   937
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   938
smallest:n
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   939
    "return a collection containing the n smallest elements, largest last.
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   940
     Raises an exception, if the receiver does not contain at least n elements"
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   941
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   942
    |mySize|
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   943
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   944
    mySize := self size.
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   945
    n > mySize ifTrue:[
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   946
        self notEnoughElementsError
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   947
    ].
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   948
    sortBlock == DefaultSortBlock ifTrue:[
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   949
        ^ self copyTo:n
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   950
    ].
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   951
    "/ do not trust the sortblock to sort small-to-large
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   952
    ^ super smallest:n
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   953
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   954
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   955
     #(10 35 20 45 30 5) asSortedCollection smallest:3  
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   956
     (#(10 35 20 45 30 5) asSortedCollection:[:a :b | a > b]) smallest:3    
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   957
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   958
     #(10 35 20 45 30 5) asSortedCollection smallest:6  
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   959
     #(10 35 20 45 30 5) asSortedCollection smallest:7  
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   960
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   961
! !
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   962
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   963
!SortedCollection methodsFor:'statistical functions'!
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   964
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   965
median
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   966
    "Return the middle element, or as close as we can get."
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   967
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   968
    ^ self at:(self size + 1 // 2)
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   969
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   970
    "
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   971
     #(10 35 20 45 30 5) asSortedCollection median
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   972
     #(10 35 20 45 30 5) median
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
   973
    "
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   974
! !
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   975
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   976
!SortedCollection methodsFor:'testing'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   977
a27a279701f8 Initial revision
claus
parents:
diff changeset
   978
includes:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   979
    "return true, if the argument, anObject is in the collection.
62
e1b4369c61fb *** empty log message ***
claus
parents: 44
diff changeset
   980
     Redefined, since due to being sorted, the inclusion check can
2
claus
parents: 1
diff changeset
   981
     be done with log-n compares i.e. much faster."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   982
32
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
   983
    |index "{ Class: SmallInteger }"|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   984
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   985
    index := self indexForInserting:anObject.
32
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
   986
    ((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false].
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
   987
    ^ (contentsArray at:index) = anObject
2
claus
parents: 1
diff changeset
   988
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   989
    "
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   990
     #(7 3 9 10 99) asSortedCollection includes:50
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   991
     #(7 3 9 10 99) asSortedCollection includes:10
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
   992
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   993
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   994
a27a279701f8 Initial revision
claus
parents:
diff changeset
   995
occurrencesOf:anObject
a27a279701f8 Initial revision
claus
parents:
diff changeset
   996
    "return how many times the argument, anObject is in the collection.
62
e1b4369c61fb *** empty log message ***
claus
parents: 44
diff changeset
   997
     Redefined, since due to being sorted, the range of checked objects
5048
b1e10ce753fd comment
Claus Gittinger <cg@exept.de>
parents: 5030
diff changeset
   998
     can be limited i.e. it can be done much faster.
b1e10ce753fd comment
Claus Gittinger <cg@exept.de>
parents: 5030
diff changeset
   999
      Uses #= (i.e. equality) compare."
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1000
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1001
    |index      "{ Class: SmallInteger }"
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1002
     tally      "{ Class: SmallInteger }"
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1003
     last       "{ Class: SmallInteger }" |
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1004
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1005
    index := self indexForInserting:anObject.
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
  1006
    last := lastIndex.
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1007
    ((index < firstIndex) or:[index > last]) ifTrue:[^ 0].
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1008
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1009
    "/ there may be multiple of them; count 'em
32
ee1a621c696c *** empty log message ***
claus
parents: 13
diff changeset
  1010
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1011
    tally := 0.
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1012
    [(index <= last) and:[(contentsArray at:index) = anObject]] whileTrue:[
8874
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
  1013
	tally := tally + 1.
f1fdda306d51 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8183
diff changeset
  1014
	index := index + 1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1015
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
  1016
    ^ tally
2
claus
parents: 1
diff changeset
  1017
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1018
    "
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1019
     #(7 3 9 10 99) asSortedCollection occurrencesOf:50
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1020
     #(7 3 9 10 99) asSortedCollection occurrencesOf:10
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1021
     #(7 10 3 10 9 10 10 99) asSortedCollection occurrencesOf:10
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1022
    "
1162
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1023
3995101dfa44 changed for new #makeRoomAtIndex:; added comments
Claus Gittinger <cg@exept.de>
parents: 1156
diff changeset
  1024
    "Modified: 12.4.1996 / 18:48:40 / cg"
77
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1025
! !
6c38ca59927f *** empty log message ***
claus
parents: 62
diff changeset
  1026
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
  1027
!SortedCollection class methodsFor:'documentation'!
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
  1028
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
  1029
version
16002
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
  1030
    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.78 2014-02-12 14:36:52 cg Exp $'
14309
602e61cb64a3 Remove now unused helper method
Stefan Vogel <sv@exept.de>
parents: 14308
diff changeset
  1031
!
602e61cb64a3 Remove now unused helper method
Stefan Vogel <sv@exept.de>
parents: 14308
diff changeset
  1032
602e61cb64a3 Remove now unused helper method
Stefan Vogel <sv@exept.de>
parents: 14308
diff changeset
  1033
version_CVS
16002
5fd1486ee60a class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 16000
diff changeset
  1034
    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.78 2014-02-12 14:36:52 cg Exp $'
629
2ceefe9b5a19 version at the end
Claus Gittinger <cg@exept.de>
parents: 607
diff changeset
  1035
! !
7300
461bb09c5954 category
Claus Gittinger <cg@exept.de>
parents: 6737
diff changeset
  1036
16000
74d0c38d1834 class: SortedCollection
Claus Gittinger <cg@exept.de>
parents: 14310
diff changeset
  1037
607
a9a526c51233 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
  1038
SortedCollection initialize!