ReindexedCollection.st
author Claus Gittinger <cg@exept.de>
Fri, 25 Mar 2016 17:09:17 +0100
changeset 3783 fcc30a3af3df
parent 3687 2c6d35ee488d
child 3861 694e93011ca8
permissions -rw-r--r--
#DOCUMENTATION class: ReindexedCollection comment/format in: #from: #from:to: #to:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1924
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     1
"
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     2
 COPYRIGHT (c) 2006 by eXept Software AG
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     3
              All Rights Reserved
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     4
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     5
 This software is furnished under a license and may be used
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     6
 only in accordance with the terms of that license and with the
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     8
 be provided or otherwise made available to, or used by, any
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
     9
 other person.  No title to or ownership of the software is
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    10
 hereby transferred.
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    11
"
1753
f9be6a841a22 ReindexedCollectionCVS: ----------------------------------------------------------------------
Claus Gittinger <cg@exept.de>
parents: 1618
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
3687
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
    14
"{ NameSpace: Smalltalk }"
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
    15
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
SequenceableCollection subclass:#ReindexedCollection
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:'sequence interval'
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	classVariableNames:''
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	category:'Collections-Sequenceable'
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!ReindexedCollection class methodsFor:'documentation'!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
1924
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    25
copyright
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    26
"
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    27
 COPYRIGHT (c) 2006 by eXept Software AG
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    28
              All Rights Reserved
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    29
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    30
 This software is furnished under a license and may be used
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    31
 only in accordance with the terms of that license and with the
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    33
 be provided or otherwise made available to, or used by, any
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    34
 other person.  No title to or ownership of the software is
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    35
 hereby transferred.
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    36
"
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    37
!
4f84cb037a21 copyright
Claus Gittinger <cg@exept.de>
parents: 1753
diff changeset
    38
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
documentation
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    ReindexedCollection is a wrapper around a sequenceable collection that remaps the indices 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    with in linear algorithm.  
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
    The elements in the ReindexedCollection are elements in the sequenceable collection at 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
    some start to some stop at some step.  
1453
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
    45
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
    46
    ReindexedCollection allows for efficient use of first/rest-like algorithms (i.e. aka Lisp)
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
    47
    applied to Sequenceable collections, as they avoid element-copying.
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
    48
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
    See class side examples.
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
    [Instance Variables:]
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
        sequence        <SequenceableCollection>        the sequence that will be reindexed.
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
        interval        <Interval>      the object that describes indicies of interest in the sequence.
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
1191
f527b4561175 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1190
diff changeset
    55
    [Origin:]
f527b4561175 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1190
diff changeset
    56
        Part of the Engineering Math Goodies package from Travis.
f527b4561175 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1190
diff changeset
    57
    
f527b4561175 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1190
diff changeset
    58
    [Author:]
f527b4561175 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1190
diff changeset
    59
        Travis Griggs (tgriggs@keyww.com or tkc@bmi.net)
f527b4561175 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1190
diff changeset
    60
        Ported from Squeak by Claus Gittinger (not much of a port, though)
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
examples
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    |coll|
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
1618
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    68
    coll := #(1 2 3 4 5 6 7 8 9 10) from:8.
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    69
    Transcript show:'from 8: '; showCR:coll.     
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    70
    Transcript show:'size: '; showCR:(coll size).  
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    71
    Transcript show:'at 1: '; showCR:(coll at:1).   
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    72
    Transcript show:'first: '; showCR:(coll first).   
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    73
    Transcript show:'last: '; showCR:(coll last).   
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    74
    coll do:[:each | Transcript show:'do: '; showCR:each].
63a351ca7d76 comments
Claus Gittinger <cg@exept.de>
parents: 1453
diff changeset
    75
    coll reverseDo:[:each | Transcript show:'reverseDo: '; showCR:each].
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    |coll|
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    coll := (1 to:10) asOrderedCollection from:3 to:8.
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
    coll.     
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    coll size.  
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
    coll at:1.   
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
    coll do:[:each | Transcript showCR:each].
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
    |coll|
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
    coll := (1 to:10) asOrderedCollection to:4.
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    coll.     
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    coll size.  
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
    coll at:1.   
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    coll last.   
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
    coll do:[:each | Transcript showCR:each].
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
! !
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
!ReindexedCollection class methodsFor:'instance creation'!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
on: aSequence from: start 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
    "Create a reindexedCollection on aSequence from start to the end of aSequence"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    ^ self
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
        on: aSequence
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
        from: start
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
        to: aSequence size
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
        by: 1
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
on: aSequence from: start by: step 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
    "Create a reindexedCollection on aSequence start to the end of aSequence 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
     if step is positive, else  
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
     from start to the beginning of the sequence if step is negative."
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
    ^ self
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
        on: aSequence
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
        from: start
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
        to: (step > 0
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
                ifTrue: [aSequence size]
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
                ifFalse: [1])
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
        by: step
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
on: aSequence from: start to: stop 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
    "Create a reindexedCollection on aSequence from start to stop by 1 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
     (or -1 if start is greater than stop)"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
    ^ self
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
        on: aSequence
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
        from: start
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
        to: stop
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
        by: (start <= stop
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
                ifTrue: [1]
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
                ifFalse: [-1])
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
on: aSequence from: start to: stop by: step 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
    "Create a reindexedCollection on aSequence from start to stop by step"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
    ^ self new
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
            initialize: aSequence
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
            from: start
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
            to: stop
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
            by: step
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
on: aSequence to: stop 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
    "Create a reindexedCollection on aSequence from 1 to stop by 1"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
    ^ self
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
        on: aSequence
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
        from: 1
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
        to: stop
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
        by: 1
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
on: aSequence to: stop by: step 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
    "Create a reindexedCollection on aSequence from 1 to stop (if step is 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
     positive) or the end to stop (if  
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
     step is negative). Note: if step is not 1 or -1, there is a chance that the 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
     index specified by stop may  
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
     not be in the interval."
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
    ^ self
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
        on: aSequence
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
        from: (step > 0
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
                ifTrue: [1]
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
                ifFalse: [aSequence size])
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
        to: stop
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
        by: step
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
on: aSequence with: anInterval 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
    "Create a reindexedCollection on aSequence"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
    ^ self new initialize: aSequence with: anInterval
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
! !
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
!ReindexedCollection methodsFor:'accessing'!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
at: index 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
    "Answer the value of an indexable field in the sequence instance variable. "
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
    ^ sequence at: (interval at: index)
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
at: index put: value 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
    "Store the argument value in the indexable field of the sequence 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
     instance variable indicated by index.  
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
     Answer the value that was stored."
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
    ^ sequence at: (interval at: index) put: value
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
size
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
    "Answer how many elements the receiver contains."
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
    ^ interval size
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
slide
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
    "slide by 1"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
    self slide: 1
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
slide: anIncrement 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
    "given an increment, adjust the reindex map by sliding it that far"
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
    interval := interval + anIncrement
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
! !
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
!ReindexedCollection methodsFor:'adding & removing'!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
add: anObject 
2931
0c2ab359a232 comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 1924
diff changeset
   216
    "report an error; reindexedCollections cannot add elements"
0c2ab359a232 comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 1924
diff changeset
   217
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
    self shouldNotImplement
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
! !
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
1453
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   221
!ReindexedCollection methodsFor:'converting-reindexed'!
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   222
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   223
from:startIndex
3783
fcc30a3af3df #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3687
diff changeset
   224
    "return a new collection representing the receiver's elements starting at startIndex."
1453
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   225
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   226
    interval step == 1 ifTrue:[
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   227
        ^ ReindexedCollection 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   228
            on:sequence 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   229
            from:(interval start + startIndex - 1) 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   230
            to:(interval stop)
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   231
            by:(interval step)
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   232
    ].
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   233
    "could be more intelligent here..."
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   234
    ^ super from:startIndex
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   235
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   236
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   237
     |coll cdr cddr cdddr|
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   238
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   239
     coll := #(1 2 3 4 5 6 7 8 9 10).
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   240
     cdr := coll from:2.            
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   241
     cddr := cdr from:2.        
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   242
     cdddr := cddr from:2.
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   243
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   244
!
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   245
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   246
from:startIndex to:stopIndex
3783
fcc30a3af3df #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3687
diff changeset
   247
    "return a new collection representing the receiver's elements 
1453
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   248
     starting at startIndex upTo and including endIndex."
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   249
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   250
    interval step == 1 ifTrue:[
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   251
        ^ ReindexedCollection 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   252
            on:sequence 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   253
            from:(interval start + startIndex - 1) 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   254
            to:((interval start + stopIndex - 1) min:interval stop)
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   255
            by:1
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   256
    ].
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   257
    "could be more intelligent here..."
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   258
    ^ super from:startIndex to:stopIndex
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   259
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   260
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   261
     |coll cdrButLast cddrButLast2 cdddrButLast3|
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   262
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   263
     coll := #(1 2 3 4 5 6 7 8 9 10).
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   264
     cdrButLast := coll from:2 to:9.             
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   265
     cddrButLast2 := cdrButLast from:2 to:7.          
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   266
     cdddrButLast3 := cddrButLast2 from:2 to:5.     
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   267
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   268
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   269
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   270
     |coll cdrButLast cddrButLast2 cdddrButLast3|
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   271
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   272
     coll := 1 to:100.
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   273
     cdrButLast := coll from:2 to:99.             
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   274
     cddrButLast2 := cdrButLast from:2 to:97.          
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   275
     cdddrButLast3 := cddrButLast2 from:2 to:95.     
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   276
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   277
!
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   278
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   279
to:stopIndex
3783
fcc30a3af3df #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 3687
diff changeset
   280
    "return a new collection representing the receiver's elements upTo and including endIndex."
1453
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   281
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   282
    interval step == 1 ifTrue:[
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   283
        ^ ReindexedCollection 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   284
            on:sequence 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   285
            from:(interval start) 
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   286
            to:((interval start + stopIndex - 1) min:interval stop)
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   287
            by:1
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   288
    ].
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   289
    "could be more intelligent here..."
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   290
    ^ super to:stopIndex
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   291
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   292
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   293
     |coll butLast butLast2 butLast3|
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   294
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   295
     coll := #(1 2 3 4 5 6 7 8 9 10).
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   296
     butLast := coll to:9.              
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   297
     butLast2 := butLast to:8.          
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   298
     butLast3 := butLast2 to:7.     
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   299
    "
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   300
! !
ecdb7d5e8c13 reindexing & comments
Claus Gittinger <cg@exept.de>
parents: 1191
diff changeset
   301
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
!ReindexedCollection methodsFor:'initialization'!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
initialize: aSequence from: start to: stop by: step 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
    sequence := aSequence.
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
    interval := Interval from: start to: stop by: step
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
initialize: aSequence with: anInterval 
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
        sequence := aSequence.
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
        interval := anInterval
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
! !
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
!ReindexedCollection methodsFor:'queries'!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
3020
a88b56ea6629 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2931
diff changeset
   316
isFixedSize
a88b56ea6629 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2931
diff changeset
   317
    "return true if the receiver cannot grow"
a88b56ea6629 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2931
diff changeset
   318
a88b56ea6629 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2931
diff changeset
   319
    ^ sequence isFixedSize
a88b56ea6629 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2931
diff changeset
   320
!
a88b56ea6629 added isFixedSize query
Claus Gittinger <cg@exept.de>
parents: 2931
diff changeset
   321
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
species
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
    "Answer the preferred class for reconstructing the receiver, that is, the sequence."
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
    ^ sequence species
3687
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
   326
!
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
   327
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
   328
speciesForAdding
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
   329
    "Answer the preferred class for reconstructing the receiver incrementally"
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
   330
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
   331
    ^ sequence speciesForAdding
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
! !
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
!ReindexedCollection class methodsFor:'documentation'!
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
version
3687
Claus Gittinger <cg@exept.de>
parents: 3020
diff changeset
   337
    ^ '$Header$'
1190
82af4e7d6723 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
! !
2931
0c2ab359a232 comment/format in: #add:
Claus Gittinger <cg@exept.de>
parents: 1924
diff changeset
   339