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