SegmentedOrderedCollection.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 4282 241919ca22f1
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4282
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     1
"
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     2
 COPYRIGHT (c) 2013 by Claus Gittinger
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     3
              All Rights Reserved
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     4
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     5
 This software is furnished under a license and may be used
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     6
 only in accordance with the terms of that license and with the
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     8
 be provided or otherwise made available to, or used by, any
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
     9
 other person.  No title to or ownership of the software is
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    10
 hereby transferred.
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    11
"
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
    14
"{ NameSpace: Smalltalk }"
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
    15
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
SequenceableCollection subclass:#SegmentedOrderedCollection
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'segments maxSegmentSize tally'
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Sequenceable'
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!SegmentedOrderedCollection class methodsFor:'documentation'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
4282
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    25
copyright
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    26
"
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    27
 COPYRIGHT (c) 2013 by Claus Gittinger
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    28
              All Rights Reserved
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    29
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    30
 This software is furnished under a license and may be used
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    31
 only in accordance with the terms of that license and with the
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    33
 be provided or otherwise made available to, or used by, any
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    34
 other person.  No title to or ownership of the software is
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    35
 hereby transferred.
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    36
"
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    37
!
241919ca22f1 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4241
diff changeset
    38
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
documentation
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
"
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
    41
    SegmentedOrderedCollections are intended as a replacement for huge OrderedCollections or Lists.
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    42
    They keep their elements in chunks (segments), allowing for fast 
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    43
    adding/removing at either end AND relatively fast add/remove inside the collection.
4146
8603b2e0a79e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4004
diff changeset
    44
    Compared to regular orderedColletions, there is not much of a difference if
8603b2e0a79e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4004
diff changeset
    45
    elements are added at either end.
4241
c1649b2bb8f7 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4146
diff changeset
    46
    However, when adding/removing inner elements, the performance of SegmentedOrderedCollections
4146
8603b2e0a79e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4004
diff changeset
    47
    is much better above a certain number of elements (actually quite big).
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
    48
4146
8603b2e0a79e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4004
diff changeset
    49
    However, notice again: 
8603b2e0a79e #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4004
diff changeset
    50
        when only removing at either end only, an OrderedCollection is faster.
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    52
    The break-even in performance depends on the number of elements and the usage pattern.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    53
    Consider it with (say) > 10000 elements and many adds/removes from the inside.
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
    55
    This class was added to support huge selection-in-lists (>100k elements), which are
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
    56
    constantly changing by adding/removing elements at arbitrary positions.
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
    57
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    58
    Possibly unfinished (may need optimized search and replace).
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    [author:]
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
        Claus Gittinger
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
    [see also:]
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
        Array OrderedCollection BTree 
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
"
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
!SegmentedOrderedCollection class methodsFor:'instance creation'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
new
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    "return an initialized instance"
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
    ^ self basicNew initialize.
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    74
!
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    75
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    76
new:size
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    77
    "return an initialized instance with space for size elements.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    78
     However, be aware that the logical size is 0"
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    79
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
    80
    ^ self basicNew initialize.
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
!SegmentedOrderedCollection methodsFor:'accessing'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
at:index
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
    86
    "return the element at index, anInteger"
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
    87
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
    |segStart segEnd|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    (index between:1 and:tally) ifFalse:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
        ^ self indexNotIntegerOrOutOfBounds:index
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    segStart := 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    segments do:[:seg |
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
        segEnd := segStart + (seg size - 1).
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
        (index between:segStart and:segEnd) ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
            ^ seg at:(index - segStart + 1).
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
        ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
        segStart := segEnd + 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
    self error:'oops - should not be here'
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
at:index put:newElement
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   105
    "set the element at index, to be anInteger.
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   106
     Return anObject (sigh)."
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   107
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
    |segStart segEnd|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
    (index between:1 and:tally) ifFalse:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
        ^ self indexNotIntegerOrOutOfBounds:index
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
    segStart := 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    segments do:[:seg |
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
        segEnd := segStart + (seg size - 1).
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
        (index between:segStart and:segEnd) ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
            ^ seg at:(index - segStart + 1) put:newElement.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
        ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
        segStart := segEnd + 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
    self error:'oops - should not be here'
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
first
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   126
    "return the first element"
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   127
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
    tally == 0 ifTrue:[^ self emptyCollectionError].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
    ^ segments first first
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
last
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   133
    "return the last element"
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   134
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
    tally == 0 ifTrue:[^ self emptyCollectionError].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
    ^ segments last last
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
!SegmentedOrderedCollection methodsFor:'adding & removing'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
add:anObject
2935
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   142
    "add anObject to the end.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   143
     Returns the object (sigh)"
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
    |seg|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
    (seg := segments last) size >= maxSegmentSize ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
        self splitSegmentAt:(segments size).
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
        seg := segments last.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
    seg add:anObject.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
    tally := tally + 1.
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   153
    self changed:#insert: with:tally.
2935
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   154
    ^ anObject.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   155
!
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   156
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   157
add:anObject beforeIndex:index
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   158
    "insert the first argument, anObject into the collection before slot index.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   159
     Return the receiver (sigh - ST-80 compatibility).
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   160
4004
1c39c504fb49 #OTHER by mawalch
mawalch
parents: 3838
diff changeset
   161
     Notice that this modifies the receiver, NOT a copy."
2935
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   162
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   163
    |prevSeg segStart|
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   164
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   165
    index == (tally+1) ifTrue:[
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   166
        ^ self add:anObject
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   167
    ].
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   168
    segStart := 1.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   169
    segments do:[:seg |
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   170
        |segEnd|
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   171
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   172
        segEnd := segStart + (seg size - 1).
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   173
        (index between:segStart and:segEnd) ifTrue:[
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   174
            index == segStart ifTrue:[
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   175
                prevSeg notNil ifTrue:[
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   176
                    prevSeg size < seg size ifTrue:[
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   177
                        prevSeg add:anObject.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   178
                        tally := tally + 1.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   179
                        ^ anObject
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   180
                    ].
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   181
                ].
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   182
            ].
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   183
            seg add:anObject beforeIndex:(index - segStart + 1).
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   184
            tally := tally + 1.
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   185
            self changed:#insert: with:index.
2935
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   186
            ^ anObject
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   187
        ].
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   188
        segStart := segEnd + 1.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   189
        prevSeg := seg.
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   190
    ].
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   191
    self error:'oops - should not be here'
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
addFirst:anObject
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    "add anObject to the beginning (insert as new first element)."
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
    |seg|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
    (seg := segments first) size >= maxSegmentSize ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
        self splitSegmentAt:1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
        seg := segments first.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
    seg addFirst:anObject.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
    tally := tally + 1.
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   205
    self changed:#insert: with:1
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
3056
fef823bb10e4 class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3014
diff changeset
   208
removeAll
fef823bb10e4 class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3014
diff changeset
   209
    "remove all elements from the receiver. Returns the receiver."
fef823bb10e4 class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3014
diff changeset
   210
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   211
    |prevSize|
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   212
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   213
    prevSize := tally.
3066
5ec162bc812d class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3056
diff changeset
   214
    segments := OrderedCollection with:(OrderedCollection new).
3056
fef823bb10e4 class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3014
diff changeset
   215
    tally := 0.
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   216
    self changed:#removeFrom: with:(Array with:1 with:prevSize)
3066
5ec162bc812d class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3056
diff changeset
   217
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   218
    
3066
5ec162bc812d class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3056
diff changeset
   219
    "Modified: / 30-07-2013 / 19:31:05 / cg"
3056
fef823bb10e4 class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3014
diff changeset
   220
!
fef823bb10e4 class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3014
diff changeset
   221
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
removeFirst
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   223
    "remove the first element from the collection; return the element.
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   224
     If there is no element in the receiver collection, raise an error."
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   225
3267
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   226
    ^ self removeFirstIfAbsent:[self emptyCollectionError]
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   227
!
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   228
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   229
removeFirstIfAbsent:exceptionBlock
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   230
    "remove the first element from the collection; return the element.
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   231
     If there is no element in the receiver collection, return the value from
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   232
     exceptionBlock.
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   233
     Destructive: modifies the receiver"
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   234
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
    |seg el|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
    tally == 0 ifTrue:[
3267
4cd1614af5ca class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   238
        ^ exceptionBlock value
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
    seg := segments first.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
    el := seg removeFirst.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
    seg isEmpty ifTrue:[
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   243
        tally == 1 ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   244
            segments := OrderedCollection with:(OrderedCollection new).
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   245
        ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   246
            segments removeFirst.
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   247
        ]    
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
    tally := tally - 1.
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   250
    self changed:#remove: with:1.
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
    ^ el
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
removeFromIndex:startIndex toIndex:endIndex
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   255
    "remove the elements stored under startIndex up to and including
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   256
     the elements under stopIndex.
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   257
     Return the receiver.
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   258
     Returning the receiver here is a historic leftover - it may change.
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   259
     Please use yourself in a cascade, if you need the receiver's value
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   260
     when using this method."
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   261
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
    |segStart segEnd removing segIndex nextSegIndex seg|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
    (startIndex between:1 and:tally) ifFalse:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
        ^ self indexNotIntegerOrOutOfBounds:startIndex
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
    (endIndex between:1 and:tally) ifFalse:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
        ^ self indexNotIntegerOrOutOfBounds:endIndex
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
    segStart := 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
    segIndex := 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
    removing := false.
3211
af5cc3c54fa8 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 3066
diff changeset
   274
    [
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
        seg := segments at:segIndex.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
        nextSegIndex := segIndex + 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
        segEnd := segStart + (seg size - 1).
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
        removing ifFalse:[
2935
d15b179aabfb class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2916
diff changeset
   281
            "still searching for the segment"
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
            (startIndex between:segStart and:segEnd) ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
                (endIndex between:segStart and:segEnd) ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
                    seg removeFromIndex:(startIndex - segStart + 1) toIndex:(endIndex - segStart + 1).
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
                    seg isEmpty ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
                        segments removeAtIndex:segIndex.
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   287
                        segments isEmpty ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   288
                            segments := OrderedCollection with:(OrderedCollection new).
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   289
                        ]    
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
                    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
                    tally := tally - (endIndex - startIndex + 1).
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   292
                    self changed:#removeFrom: with:(Array with:startIndex with:endIndex).
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
                    ^ self.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
                ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
                seg removeFromIndex:(startIndex - segStart + 1).        
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
                seg isEmpty ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
                    segments removeAtIndex:segIndex.
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   298
                    segments isEmpty ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   299
                        segments := OrderedCollection with:(OrderedCollection new).
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   300
                    ].    
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
                    nextSegIndex := segIndex.    
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
                ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
                removing := true.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
            ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
        ] ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
            (endIndex between:segStart and:segEnd) ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
                seg removeFromIndex:1 toIndex:(endIndex - segStart + 1).
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
                seg isEmpty ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
                    segments removeAtIndex:segIndex.
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   310
                    segments isEmpty ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   311
                        segments := OrderedCollection with:(OrderedCollection new).
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   312
                    ]    
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
                ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
                tally := tally - (endIndex - startIndex + 1).
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   315
                self changed:#removeFrom: with:(Array with:startIndex with:endIndex).
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
                ^ self.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
            ] ifFalse:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
                "/ remove the whole segment
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
                segments removeAtIndex:segIndex.
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   320
                segments isEmpty ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   321
                    segments := OrderedCollection with:(OrderedCollection new).
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   322
                ].    
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
                nextSegIndex := segIndex.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
            ]
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
        ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
        segStart := segEnd + 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
        segIndex := nextSegIndex.
3211
af5cc3c54fa8 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 3066
diff changeset
   328
    ] loop.
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
removeLast
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   332
    "remove the last element from the collection; return the element"
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   333
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
    |seg el|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
    tally == 0 ifTrue:[
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
        ^ self emptyCollectionError
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
    seg := segments last.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
    el := seg removeLast.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
    seg isEmpty ifTrue:[
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   342
        tally == 1 ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   343
            segments := OrderedCollection with:(OrderedCollection new).
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   344
        ] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   345
            segments removeLast.
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   346
        ]    
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
    tally := tally - 1.
3328
deaef41ffd6f class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3327
diff changeset
   349
    self changed:#remove: with:(1 + tally).
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
    ^ el
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
!SegmentedOrderedCollection methodsFor:'enumerating'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
do:aBlock
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   356
    "evaluate the argument, aBlock for every element in the collection."
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   357
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
    segments do:[:seg |
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
        seg do:aBlock
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
keysAndValuesDo:aBlock
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   364
    "evaluate the argument, aBlock for every element in the collection,
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   365
     passing both index and element as arguments."
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   366
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
    |idx|
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
    idx := 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
    segments do:[:seg |
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
        seg do:[:each |
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
            aBlock value:idx value:each.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
            idx := idx + 1.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
        ]
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
    ].
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   378
!SegmentedOrderedCollection methodsFor:'grow & shrink'!
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   379
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   380
grow:newSize
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   381
    "adjust the logical size to newSize"
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   382
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   383
    |numExtraSegments segStart segEnd segIndex seg|
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   384
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   385
    newSize == 0 ifTrue:[
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   386
        segments := OrderedCollection with:(OrderedCollection new).
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   387
        tally := 0.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   388
        ^ self
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   389
    ].
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   390
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   391
    newSize < tally ifTrue:[
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   392
        "/ shrinking
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   393
        segStart := 1.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   394
        segIndex := 1.
3211
af5cc3c54fa8 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 3066
diff changeset
   395
        [
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   396
            seg := segments at:segIndex.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   397
            segEnd := segStart + (seg size - 1).
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   398
            (newSize between:segStart and:segEnd) ifTrue:[
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   399
                (segments at:segIndex) removeFromIndex:(newSize - segStart + 1 + 1).
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   400
                segments removeFromIndex:segIndex+1.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   401
                tally := newSize.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   402
                ^ self
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   403
            ].
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   404
            segStart := segEnd + 1.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   405
            segIndex := segIndex + 1.
3211
af5cc3c54fa8 [true] whileTrue: -> #loop
Stefan Vogel <sv@exept.de>
parents: 3066
diff changeset
   406
        ] loop.
2948
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   407
        "/ not reached
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   408
    ].
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   409
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   410
    "/ growing
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   411
    numExtraSegments := (newSize - tally) // maxSegmentSize + 1.
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   412
    numExtraSegments timesRepeat:[
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   413
        segments add:((OrderedCollection new:maxSegmentSize) grow:maxSegmentSize)
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   414
    ].
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   415
    tally := newSize
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   416
! !
dc6efd25377b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2935
diff changeset
   417
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
!SegmentedOrderedCollection methodsFor:'initialization'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
initialize
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
    "Invoked when a new instance is created."
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
3066
5ec162bc812d class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3056
diff changeset
   423
    segments := OrderedCollection with:(OrderedCollection new).
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
    maxSegmentSize := 200.
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
    tally := 0.
3066
5ec162bc812d class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3056
diff changeset
   426
5ec162bc812d class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 3056
diff changeset
   427
    "Modified: / 30-07-2013 / 19:31:12 / cg"
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   430
!SegmentedOrderedCollection methodsFor:'private'!
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   431
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   432
splitSegmentAt:segmentIndex
3838
c2875c56b614 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3671
diff changeset
   433
    |seg segSize rightPart|
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   434
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   435
    seg := segments at:segmentIndex.
3838
c2875c56b614 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3671
diff changeset
   436
    segSize := seg size.
c2875c56b614 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3671
diff changeset
   437
    
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   438
    rightPart := OrderedCollection new:20.
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   439
    rightPart grow:10.
3838
c2875c56b614 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3671
diff changeset
   440
    rightPart replaceFrom:1 to:10 with:seg startingAt:(segSize - 9).
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   441
3838
c2875c56b614 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 3671
diff changeset
   442
    seg removeFromIndex:(segSize - 9) toIndex:segSize.
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   443
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   444
    segments add:rightPart afterIndex:segmentIndex.
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   445
! !
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   446
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
!SegmentedOrderedCollection methodsFor:'queries'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
3014
d9e664b6d2c7 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2949
diff changeset
   449
isFixedSize
d9e664b6d2c7 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2949
diff changeset
   450
    "return true if the receiver cannot grow"
d9e664b6d2c7 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2949
diff changeset
   451
d9e664b6d2c7 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2949
diff changeset
   452
    ^ false
d9e664b6d2c7 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2949
diff changeset
   453
!
d9e664b6d2c7 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2949
diff changeset
   454
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
size
2949
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   456
    "return the number of elements in the collection"
bc40924d516b class: SegmentedOrderedCollection
Claus Gittinger <cg@exept.de>
parents: 2948
diff changeset
   457
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
    ^ tally
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
!SegmentedOrderedCollection class methodsFor:'documentation'!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   463
version
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   464
    ^ '$Header$'
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
!
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   466
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   467
version_CVS
3671
Claus Gittinger <cg@exept.de>
parents: 3328
diff changeset
   468
    ^ '$Header$'
2916
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
! !
984324441864 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470