ArrayedCollection.st
author Claus Gittinger <cg@exept.de>
Thu, 25 Apr 1996 13:11:38 +0200
changeset 1283 2c533653efa3
parent 1171 7b8924ce3424
child 1289 3abde2c376de
permissions -rw-r--r--
commentary
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1989 by Claus Gittinger
155
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
a27a279701f8 Initial revision
claus
parents:
diff changeset
    13
SequenceableCollection subclass:#ArrayedCollection
1111
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
    14
	instanceVariableNames:''
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
    15
	classVariableNames:''
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
    16
	poolDictionaries:''
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
    17
	category:'Collections-Abstract'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    18
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    19
82
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    20
!ArrayedCollection class methodsFor:'documentation'!
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    21
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    22
copyright
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    23
"
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    24
 COPYRIGHT (c) 1989 by Claus Gittinger
155
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
    25
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    26
82
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    27
 This software is furnished under a license and may be used
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    28
 only in accordance with the terms of that license and with the
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    30
 be provided or otherwise made available to, or used by, any
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    31
 other person.  No title to or ownership of the software is
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    32
 hereby transferred.
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    33
"
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    34
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    35
82
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    36
documentation
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    37
"
249
claus
parents: 213
diff changeset
    38
    ArrayedCollection is an abstract superclass for all collections where 
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    39
    the elements can be accessed via an integer index,
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    40
    AND the collection is a fixed size collection. 
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    41
    Those fixed size collections cannot easily grow, since they store the 
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    42
    elements directly within the object and a grow operation can only be done 
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    43
    by #becoming another object.
360
claus
parents: 359
diff changeset
    44
    (other collections keep a reference to the physical container, which
claus
parents: 359
diff changeset
    45
     can be easily replaced)
claus
parents: 359
diff changeset
    46
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    47
    Notice: 
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    48
        currently, ST/X supports growing fix-size collections
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    49
        (such as Arrays, ByteArrays and Strings). However, this
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    50
        is done in a very slow way (using #become).
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    51
        Become is a very slow operation in a direct-pointer smalltalk
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    52
        system.
360
claus
parents: 359
diff changeset
    53
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    54
        Therefore, you SHOULD rewrite any application that do this,
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    55
        to make use of OrderedCollection or any other collection which
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    56
        can grow faster.
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    57
        To remind you of that, a warning message is sent to the
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    58
        standard error whenever such an operation is performed (see #grow).
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    59
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    60
        Also note, that some other smalltalk systems do NOT allow
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    61
        fix size collection to change their size, and that future
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    62
        ST/X versions may be changed to trigger an error (instead of a
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
    63
        warning) in those situations.
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1171
diff changeset
    64
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1171
diff changeset
    65
    [see also:]
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1171
diff changeset
    66
        OrderedCollection
82
0147b4f725ae *** empty log message ***
claus
parents: 68
diff changeset
    67
"
92
0c73b48551ac *** empty log message ***
claus
parents: 82
diff changeset
    68
! !
0c73b48551ac *** empty log message ***
claus
parents: 82
diff changeset
    69
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    70
!ArrayedCollection class methodsFor:'instance creation'!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    71
a27a279701f8 Initial revision
claus
parents:
diff changeset
    72
with:element
a27a279701f8 Initial revision
claus
parents:
diff changeset
    73
    "return a new SequenceableCollection with one element:anObject"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    74
a27a279701f8 Initial revision
claus
parents:
diff changeset
    75
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
    76
a27a279701f8 Initial revision
claus
parents:
diff changeset
    77
    newCollection := self new:1.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    78
    newCollection at:1 put:element.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    79
    ^newCollection
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    80
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    81
    "
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    82
     OrderedCollection with:1
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    83
     SortedCollection with:99 
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    84
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    85
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    86
a27a279701f8 Initial revision
claus
parents:
diff changeset
    87
with:first with:second
a27a279701f8 Initial revision
claus
parents:
diff changeset
    88
    "return a new SequenceableCollection with two elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    89
a27a279701f8 Initial revision
claus
parents:
diff changeset
    90
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
    91
a27a279701f8 Initial revision
claus
parents:
diff changeset
    92
    newCollection := self new:2.
916
a050d17c7e1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 628
diff changeset
    93
    newCollection at:1 put:first. newCollection at:2 put:second.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    94
    ^newCollection
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    95
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    96
    "
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    97
     OrderedCollection with:1 with:2
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    98
     SortedCollection with:99 with:3
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
    99
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   100
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   101
a27a279701f8 Initial revision
claus
parents:
diff changeset
   102
with:first with:second with:third
a27a279701f8 Initial revision
claus
parents:
diff changeset
   103
    "return a new SequenceableCollection with three elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   104
a27a279701f8 Initial revision
claus
parents:
diff changeset
   105
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   106
a27a279701f8 Initial revision
claus
parents:
diff changeset
   107
    newCollection := self new:3.
916
a050d17c7e1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 628
diff changeset
   108
    newCollection at:1 put:first. newCollection at:2 put:second. newCollection at:3 put:third.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   109
    ^newCollection
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   110
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   111
    "
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   112
     OrderedCollection with:1 with:2 with:3
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   113
     SortedCollection with:99 with:3 with:301
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   114
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   115
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   116
a27a279701f8 Initial revision
claus
parents:
diff changeset
   117
with:first with:second with:third with:forth
a27a279701f8 Initial revision
claus
parents:
diff changeset
   118
    "return a new SequenceableCollection with four elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   119
a27a279701f8 Initial revision
claus
parents:
diff changeset
   120
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   121
a27a279701f8 Initial revision
claus
parents:
diff changeset
   122
    newCollection := self new:4.
916
a050d17c7e1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 628
diff changeset
   123
    newCollection at:1 put:first. newCollection at:2 put:second.
a050d17c7e1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 628
diff changeset
   124
    newCollection at:3 put:third. newCollection at:4 put:forth.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   125
    ^newCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   126
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   127
a27a279701f8 Initial revision
claus
parents:
diff changeset
   128
with:one with:two with:three with:four with:five
a27a279701f8 Initial revision
claus
parents:
diff changeset
   129
    "return a new SequenceableCollection with five elements"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
    |newCollection|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
    newCollection := self new:5.
916
a050d17c7e1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 628
diff changeset
   134
    newCollection at:1 put:one. newCollection at:2 put:two.
a050d17c7e1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 628
diff changeset
   135
    newCollection at:3 put:three. newCollection at:4 put:four. newCollection at:5 put:five.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   136
    ^newCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   137
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
a27a279701f8 Initial revision
claus
parents:
diff changeset
   139
withAll:aCollection
a27a279701f8 Initial revision
claus
parents:
diff changeset
   140
    "return a new Collection with all elements taken from the argument,
a27a279701f8 Initial revision
claus
parents:
diff changeset
   141
     aCollection"
a27a279701f8 Initial revision
claus
parents:
diff changeset
   142
a27a279701f8 Initial revision
claus
parents:
diff changeset
   143
    |newCollection newSize
a27a279701f8 Initial revision
claus
parents:
diff changeset
   144
     index "{ Class: SmallInteger }" |
a27a279701f8 Initial revision
claus
parents:
diff changeset
   145
a27a279701f8 Initial revision
claus
parents:
diff changeset
   146
    newSize := aCollection size.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   147
    newCollection := self new:newSize.
359
claus
parents: 356
diff changeset
   148
    aCollection isSequenceable ifTrue:[
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   149
        "/
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   150
        "/ aCollection has indexed elements
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   151
        "/ a block-replace may be faster
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   152
        "/
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   153
        newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   154
    ] ifFalse:[
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   155
        "/
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   156
        "/ must enumerate the elements individually
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   157
        "/
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   158
        index := 1.
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   159
        aCollection do:[:element |
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   160
            newCollection at:index put:element.
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   161
            index := index + 1
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   162
        ]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   163
    ].
a27a279701f8 Initial revision
claus
parents:
diff changeset
   164
    ^ newCollection
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   165
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   166
    "
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   167
     OrderedCollection withAll:#(1 2 3 4 5)
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   168
     SortedCollection withAll:#(99 17 53 1 101) 
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   169
    "
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   170
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   171
    "Modified: 13.4.1996 / 12:14:38 / cg"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   172
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   173
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   174
!ArrayedCollection class methodsFor:'queries'!
68
59faa75185ba *** empty log message ***
claus
parents: 28
diff changeset
   175
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   176
growIsCheap
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   177
    "return true, if this collection can easily grow
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   178
     (i.e. without a need for become:).
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   179
     Since this is the superclass of all indexed fix-size collections,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   180
     return false here."
68
59faa75185ba *** empty log message ***
claus
parents: 28
diff changeset
   181
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   182
    ^ false
2
claus
parents: 1
diff changeset
   183
! !
claus
parents: 1
diff changeset
   184
249
claus
parents: 213
diff changeset
   185
!ArrayedCollection methodsFor:'copying'!
claus
parents: 213
diff changeset
   186
claus
parents: 213
diff changeset
   187
copyEmptyAndGrow:size
claus
parents: 213
diff changeset
   188
    "return a new instance of the receivers species with size
claus
parents: 213
diff changeset
   189
     nilled elements and any named instance variables copied."
claus
parents: 213
diff changeset
   190
claus
parents: 213
diff changeset
   191
    "special case for Array, which has no named instance vars"
claus
parents: 213
diff changeset
   192
356
claus
parents: 333
diff changeset
   193
    |cls|
claus
parents: 333
diff changeset
   194
claus
parents: 333
diff changeset
   195
    (cls := self class) instSize == 0 ifTrue:[
claus
parents: 333
diff changeset
   196
	^ cls new:size
249
claus
parents: 213
diff changeset
   197
    ].
claus
parents: 213
diff changeset
   198
    ^ super copyEmptyAndGrow:size
claus
parents: 213
diff changeset
   199
! !
claus
parents: 213
diff changeset
   200
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   201
!ArrayedCollection methodsFor:'error handling'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   202
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   203
fixedSizeError
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   204
    "report an error that size of the collection cannot be changed.
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   205
     This is not used right now (instead, a warning is sent to stderr
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   206
     in the #greo method); however, future versions of ST/X may no longer
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   207
     allow fixed size collection to grow.
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   208
     Read the documentation on why things are that way ..."
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   209
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   210
    ^ self error:'cannot change size'
1171
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   211
7b8924ce3424 commentary
Claus Gittinger <cg@exept.de>
parents: 1164
diff changeset
   212
    "Modified: 13.4.1996 / 12:26:13 / cg"
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   213
! !
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   214
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   215
!ArrayedCollection methodsFor:'printing & storing'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   216
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   217
storeOn:aStream
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   218
    "output a printed representation (which can be re-read with readFrom:)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   219
     onto the argument aStream. Redefined to output index access."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   220
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   221
    |index "{ Class: SmallInteger }"|
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   222
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   223
    thisContext isRecursive ifTrue:[
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   224
	Transcript showCr:'Error: storeOn: of self referencing collection.'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   225
	aStream nextPutAll:'#recursive'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   226
	^ self
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   227
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   228
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   229
    aStream nextPutAll:'(('; nextPutAll:self class name; nextPutAll:' new:'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   230
    self size printOn:aStream.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   231
    aStream nextPutAll:')'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   232
    index := 1.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   233
    self do:[:element |
916
a050d17c7e1f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 628
diff changeset
   234
	aStream nextPutAll:' at:'. index printOn:aStream. aStream nextPutAll:' put:('.
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   235
	element storeOn:aStream.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   236
	aStream nextPutAll:');'.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   237
	index := index + 1
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   238
    ].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   239
    index > 1 ifTrue:[aStream nextPutAll:' yourself'].
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   240
    aStream nextPut:$)
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   241
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   242
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   243
     (Array with:(1@2) with:(1->2)) storeString    
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   244
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   245
! !
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   246
2
claus
parents: 1
diff changeset
   247
!ArrayedCollection methodsFor:'resizing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   248
2
claus
parents: 1
diff changeset
   249
grow:newSize
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   250
    "grow the receiver i.e. cut off everything after newSize.
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   251
     Warning: this may be a slow operation due to the use of become 
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   252
     - you should write your collection classes to avoid the use of become. 
155
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
   253
     You have been warned."
2
claus
parents: 1
diff changeset
   254
599
8a0fefb0a725 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 590
diff changeset
   255
    |newArray oldSize sender|
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   256
27
d98f9dd437f7 *** empty log message ***
claus
parents: 5
diff changeset
   257
    oldSize := self size.
d98f9dd437f7 *** empty log message ***
claus
parents: 5
diff changeset
   258
    (newSize ~~ oldSize) ifTrue:[
990
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   259
	InfoPrinting ifTrue:[
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   260
	    "/
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   261
	    "/ output a warning - you should rewrite your application
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   262
	    "/ to use some collection which implements grow: more efficient
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   263
	    "/ (i.e. use OrderedCollection instead of Array ..)
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   264
	    "/
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   265
	    'ARRCOLL: Warning: slow grow operation (' infoPrint.
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   266
	    self class name infoPrint. ') via ' infoPrint.
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   267
            sender := thisContext sender.
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   268
	    sender methodPrintString infoPrint. 
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   269
	    ' from ' infoPrint. sender sender methodPrintString infoPrintNL.
284134c88e19 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 916
diff changeset
   270
	].
360
claus
parents: 359
diff changeset
   271
155
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
   272
	newArray := self species new:newSize.
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
   273
	newArray replaceFrom:1 to:(newSize min:oldSize) with:self.
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
   274
	self become:newArray.
2
claus
parents: 1
diff changeset
   275
    ]
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   276
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   277
    "
590
3ffb78fb2716 report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   278
     #(1 2 3 4 5 6) add:7
3ffb78fb2716 report sender in slow grow warning
Claus Gittinger <cg@exept.de>
parents: 528
diff changeset
   279
     #(1 2 3 4 5 6) remove:5 
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   280
     #(1 2 3 4 5 6) copy grow:3  
155
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
   281
     #(1 2 3 4 5 6) copy grow:10  
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   282
     'hello world' copy grow:5   
155
edd7fc34e104 *** empty log message ***
claus
parents: 125
diff changeset
   283
     'hello' copy grow:20   
125
5fdcb4b2567f *** empty log message ***
claus
parents: 92
diff changeset
   284
    "
360
claus
parents: 359
diff changeset
   285
!
claus
parents: 359
diff changeset
   286
claus
parents: 359
diff changeset
   287
removeAll
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
   288
    "remove all elements from the receiver. Returns the receiver.
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
   289
1111
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   290
     For ArrayedCollections (which are actually fixed-size collections),
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   291
     this is a slow operation, since a #become: is required to update
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   292
     all owners. Better use a collection which is prepared for growing
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   293
     (i.e. an OrderedCollection).
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   294
     We output a warning message here, to remind you about that."
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   295
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   296
    'ARRCOLL: Warning: slow removeAll operation (' infoPrint.
360
claus
parents: 359
diff changeset
   297
    self class name infoPrint. ')' infoPrintNL.
claus
parents: 359
diff changeset
   298
claus
parents: 359
diff changeset
   299
    self become:(self copyEmpty)
claus
parents: 359
diff changeset
   300
claus
parents: 359
diff changeset
   301
    "
claus
parents: 359
diff changeset
   302
     #(1 2 3 4 5) copy removeAll    
claus
parents: 359
diff changeset
   303
     #(1 2 3 4 5) removeAll    
claus
parents: 359
diff changeset
   304
    "
1111
d8e423b7d5a1 commentary
Claus Gittinger <cg@exept.de>
parents: 990
diff changeset
   305
1164
38c54a4f1273 commentary
Claus Gittinger <cg@exept.de>
parents: 1111
diff changeset
   306
    "Modified: 12.4.1996 / 13:34:21 / cg"
2
claus
parents: 1
diff changeset
   307
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   308
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   309
!ArrayedCollection methodsFor:'testing'!
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   310
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   311
includesKey:anIndex
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   312
    "return true, if anIndex is a valid key.
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   313
     NOTICE: in ST-80, this message is only defined for Dictionaries,
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   314
	     however, having a common protocol with indexed collections
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   315
	     often simplifies things."
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   316
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   317
    ^ (anIndex >= 1) and:[anIndex <= self size]
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   318
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   319
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   320
     #(1 2 3) includesKey:4 
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   321
     #(1 2 3) includesKey:3  
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   322
    "
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   323
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   324
602
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   325
size
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   326
    "redefined to re-enable size->basicSize forwarding
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   327
     (it is cought in SequencableCollection)"
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   328
771ab7a8c4bf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 599
diff changeset
   329
    ^ self basicSize
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   330
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   331
628
7aa563e4c64a version at the end
Claus Gittinger <cg@exept.de>
parents: 602
diff changeset
   332
!ArrayedCollection class methodsFor:'documentation'!
7aa563e4c64a version at the end
Claus Gittinger <cg@exept.de>
parents: 602
diff changeset
   333
7aa563e4c64a version at the end
Claus Gittinger <cg@exept.de>
parents: 602
diff changeset
   334
version
1283
2c533653efa3 commentary
Claus Gittinger <cg@exept.de>
parents: 1171
diff changeset
   335
    ^ '$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.34 1996-04-25 11:10:32 cg Exp $'
628
7aa563e4c64a version at the end
Claus Gittinger <cg@exept.de>
parents: 602
diff changeset
   336
! !