1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
SequenceableCollection subclass:#ArrayedCollection
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Abstract'
|
|
18 |
!
|
|
19 |
|
|
20 |
ArrayedCollection comment:'
|
82
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
22 |
All Rights Reserved
|
92
|
23 |
|
302
|
24 |
$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.16 1995-03-08 23:36:39 claus Exp $
|
82
|
25 |
'!
|
1
|
26 |
|
82
|
27 |
!ArrayedCollection class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
155
|
32 |
All Rights Reserved
|
1
|
33 |
|
82
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
82
|
43 |
version
|
|
44 |
"
|
302
|
45 |
$Header: /cvs/stx/stx/libbasic/ArrayedCollection.st,v 1.16 1995-03-08 23:36:39 claus Exp $
|
82
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
249
|
51 |
ArrayedCollection is an abstract superclass for all collections where
|
|
52 |
the elements can be accessed via an integer index.
|
82
|
53 |
"
|
|
54 |
! !
|
1
|
55 |
|
92
|
56 |
!ArrayedCollection class methodsFor:'queries'!
|
|
57 |
|
|
58 |
growIsCheap
|
|
59 |
"return true, if this collection can easily grow
|
|
60 |
(i.e. without a need for become:).
|
|
61 |
Since this is the superclass of all indexed fix-size collections,
|
|
62 |
return false here."
|
|
63 |
|
|
64 |
^ false
|
|
65 |
! !
|
|
66 |
|
1
|
67 |
!ArrayedCollection class methodsFor:'instance creation'!
|
|
68 |
|
|
69 |
with:element
|
|
70 |
"return a new SequenceableCollection with one element:anObject"
|
|
71 |
|
|
72 |
|newCollection|
|
|
73 |
|
|
74 |
newCollection := self new:1.
|
|
75 |
newCollection at:1 put:element.
|
|
76 |
^newCollection
|
125
|
77 |
|
|
78 |
"
|
|
79 |
OrderedCollection with:1
|
|
80 |
SortedCollection with:99
|
|
81 |
"
|
1
|
82 |
!
|
|
83 |
|
|
84 |
with:first with:second
|
|
85 |
"return a new SequenceableCollection with two elements"
|
|
86 |
|
|
87 |
|newCollection|
|
|
88 |
|
|
89 |
newCollection := self new:2.
|
|
90 |
newCollection at:1 put:first.
|
|
91 |
newCollection at:2 put:second.
|
|
92 |
^newCollection
|
125
|
93 |
|
|
94 |
"
|
|
95 |
OrderedCollection with:1 with:2
|
|
96 |
SortedCollection with:99 with:3
|
|
97 |
"
|
1
|
98 |
!
|
|
99 |
|
|
100 |
with:first with:second with:third
|
|
101 |
"return a new SequenceableCollection with three elements"
|
|
102 |
|
|
103 |
|newCollection|
|
|
104 |
|
|
105 |
newCollection := self new:3.
|
|
106 |
newCollection at:1 put:first.
|
|
107 |
newCollection at:2 put:second.
|
|
108 |
newCollection at:3 put:third.
|
|
109 |
^newCollection
|
125
|
110 |
|
|
111 |
"
|
|
112 |
OrderedCollection with:1 with:2 with:3
|
|
113 |
SortedCollection with:99 with:3 with:301
|
|
114 |
"
|
1
|
115 |
!
|
|
116 |
|
|
117 |
with:first with:second with:third with:forth
|
|
118 |
"return a new SequenceableCollection with four elements"
|
|
119 |
|
|
120 |
|newCollection|
|
|
121 |
|
|
122 |
newCollection := self new:4.
|
|
123 |
newCollection at:1 put:first.
|
|
124 |
newCollection at:2 put:second.
|
|
125 |
newCollection at:3 put:third.
|
|
126 |
newCollection at:4 put:forth.
|
|
127 |
^newCollection
|
|
128 |
!
|
|
129 |
|
|
130 |
with:one with:two with:three with:four with:five
|
|
131 |
"return a new SequenceableCollection with five elements"
|
|
132 |
|
|
133 |
|newCollection|
|
|
134 |
|
|
135 |
newCollection := self new:5.
|
|
136 |
newCollection at:1 put:one.
|
|
137 |
newCollection at:2 put:two.
|
|
138 |
newCollection at:3 put:three.
|
|
139 |
newCollection at:4 put:four.
|
|
140 |
newCollection at:5 put:five.
|
|
141 |
^newCollection
|
|
142 |
!
|
|
143 |
|
|
144 |
withAll:aCollection
|
|
145 |
"return a new Collection with all elements taken from the argument,
|
|
146 |
aCollection"
|
|
147 |
|
|
148 |
|newCollection newSize
|
|
149 |
index "{ Class: SmallInteger }" |
|
|
150 |
|
|
151 |
newSize := aCollection size.
|
|
152 |
newCollection := self new:newSize.
|
68
|
153 |
aCollection isSequenceableCollection ifTrue:[
|
155
|
154 |
"aCollection has indexed elements"
|
|
155 |
newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1
|
1
|
156 |
] ifFalse:[
|
155
|
157 |
"must enumerate the elements"
|
|
158 |
index := 1.
|
|
159 |
aCollection do:[:element |
|
|
160 |
newCollection at:index put:element.
|
|
161 |
index := index + 1
|
|
162 |
]
|
1
|
163 |
].
|
|
164 |
^ newCollection
|
125
|
165 |
|
|
166 |
"
|
|
167 |
OrderedCollection withAll:#(1 2 3 4 5)
|
|
168 |
SortedCollection withAll:#(99 17 53 1 101)
|
|
169 |
"
|
1
|
170 |
! !
|
|
171 |
|
2
|
172 |
!ArrayedCollection methodsFor:'testing'!
|
1
|
173 |
|
68
|
174 |
includesKey:anIndex
|
|
175 |
"return true, if anIndex is a valid key.
|
|
176 |
NOTICE: in ST-80, this message is only defined for Dictionaries,
|
155
|
177 |
however, having a common protocol with indexed collections
|
|
178 |
often simplifies things."
|
68
|
179 |
|
|
180 |
^ (anIndex >= 1) and:[anIndex <= self size]
|
|
181 |
|
|
182 |
"
|
|
183 |
#(1 2 3) includesKey:4
|
|
184 |
#(1 2 3) includesKey:3
|
|
185 |
"
|
|
186 |
!
|
|
187 |
|
2
|
188 |
size
|
|
189 |
"redefined to re-enable size->basicSize forwarding
|
|
190 |
(it is cought in SequencableCollection)"
|
|
191 |
|
|
192 |
^ self basicSize
|
|
193 |
! !
|
|
194 |
|
249
|
195 |
!ArrayedCollection methodsFor:'copying'!
|
|
196 |
|
|
197 |
copyEmptyAndGrow:size
|
|
198 |
"return a new instance of the receivers species with size
|
|
199 |
nilled elements and any named instance variables copied."
|
|
200 |
|
|
201 |
"special case for Array, which has no named instance vars"
|
|
202 |
|
|
203 |
self class instSize == 0 ifTrue:[
|
|
204 |
^ self class new:size
|
|
205 |
].
|
|
206 |
^ super copyEmptyAndGrow:size
|
|
207 |
! !
|
|
208 |
|
2
|
209 |
!ArrayedCollection methodsFor:'resizing'!
|
1
|
210 |
|
2
|
211 |
grow:newSize
|
125
|
212 |
"grow the receiver i.e. cut off everything after newSize.
|
|
213 |
Warning: this may be a slow operation due to the use of become
|
|
214 |
- you should write your collection classes to avoid the use of become.
|
155
|
215 |
You have been warned."
|
2
|
216 |
|
27
|
217 |
|newArray oldSize|
|
1
|
218 |
|
280
|
219 |
"/ 'ARRCOLL: slow grow operation warning' errorPrintNL.
|
|
220 |
|
27
|
221 |
oldSize := self size.
|
|
222 |
(newSize ~~ oldSize) ifTrue:[
|
155
|
223 |
newArray := self species new:newSize.
|
|
224 |
newArray replaceFrom:1 to:(newSize min:oldSize) with:self.
|
|
225 |
self become:newArray.
|
2
|
226 |
]
|
125
|
227 |
|
|
228 |
"
|
|
229 |
#(1 2 3 4 5 6) copy grow:3
|
155
|
230 |
#(1 2 3 4 5 6) copy grow:10
|
125
|
231 |
'hello world' copy grow:5
|
155
|
232 |
'hello' copy grow:20
|
125
|
233 |
"
|
2
|
234 |
! !
|
1
|
235 |
|
2
|
236 |
!ArrayedCollection methodsFor:'error handling'!
|
1
|
237 |
|
|
238 |
fixedSizeError
|
68
|
239 |
"report an error that size of the collection cannot be changed.
|
|
240 |
This is obsolete now."
|
1
|
241 |
|
|
242 |
^ self error:'cannot change size'
|
|
243 |
! !
|
|
244 |
|
213
|
245 |
!ArrayedCollection methodsFor:'printing & storing'!
|
1
|
246 |
|
|
247 |
storeOn:aStream
|
280
|
248 |
"output a printed representation (which can be re-read with readFrom:)
|
|
249 |
onto the argument aStream. Redefined to output index access."
|
1
|
250 |
|
|
251 |
|index "{ Class: SmallInteger }"|
|
|
252 |
|
68
|
253 |
thisContext isRecursive ifTrue:[
|
155
|
254 |
Transcript showCr:'Error: storeOn: of self referencing collection.'.
|
293
|
255 |
aStream nextPutAll:'#recursive'.
|
155
|
256 |
^ self
|
68
|
257 |
].
|
|
258 |
|
280
|
259 |
aStream nextPutAll:'(('.
|
1
|
260 |
aStream nextPutAll:self class name.
|
|
261 |
aStream nextPutAll:' new:'.
|
|
262 |
self size printOn:aStream.
|
|
263 |
aStream nextPutAll:')'.
|
|
264 |
index := 1.
|
|
265 |
self do:[:element |
|
155
|
266 |
aStream nextPutAll:' at:'.
|
|
267 |
index printOn:aStream.
|
|
268 |
aStream nextPutAll:' put:('.
|
|
269 |
element storeOn:aStream.
|
|
270 |
aStream nextPutAll:');'.
|
|
271 |
index := index + 1
|
1
|
272 |
].
|
|
273 |
index > 1 ifTrue:[aStream nextPutAll:' yourself'].
|
|
274 |
aStream nextPut:$)
|
280
|
275 |
|
|
276 |
"
|
293
|
277 |
(Array with:(1@2) with:(1->2)) storeString
|
280
|
278 |
"
|
1
|
279 |
! !
|