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