1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
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:#OrderedCollection
|
|
14 |
instanceVariableNames:'contentsArray firstIndex lastIndex'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Sequenceable'
|
|
18 |
!
|
|
19 |
|
|
20 |
OrderedCollection comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
OrderedCollection have ordered elements. Insertion and removal at both ends
|
|
26 |
is possible - therefore they can be used for queues and stacks.
|
|
27 |
|
|
28 |
Instance variables:
|
|
29 |
|
|
30 |
contentsArray <Array> the actual contents
|
|
31 |
firstIndex <SmallInteger> index of first valid element
|
|
32 |
lastIndex <SmallInteger> index of last valid element
|
|
33 |
|
|
34 |
%W% %E%
|
|
35 |
written spring 89 by claus
|
|
36 |
'!
|
|
37 |
|
|
38 |
!OrderedCollection class methodsFor:'instance creation'!
|
|
39 |
|
|
40 |
new:size
|
|
41 |
"create a new OrderedCollection"
|
|
42 |
|
|
43 |
^ (self basicNew) initContents:size
|
|
44 |
!
|
|
45 |
|
|
46 |
new
|
|
47 |
"create a new OrderedCollection"
|
|
48 |
|
|
49 |
^ (self basicNew) initContents:10
|
|
50 |
! !
|
|
51 |
|
|
52 |
!OrderedCollection methodsFor:'testing'!
|
|
53 |
|
|
54 |
size
|
|
55 |
"return the number of elements in the collection"
|
|
56 |
|
|
57 |
^ lastIndex - firstIndex + 1
|
|
58 |
!
|
|
59 |
|
|
60 |
isFixedSize
|
|
61 |
"return true if the receiver cannot grow - this will vanish once
|
|
62 |
Arrays and Strings learn how to grow ..."
|
|
63 |
|
|
64 |
^ false
|
|
65 |
! !
|
|
66 |
|
|
67 |
!OrderedCollection methodsFor:'copying'!
|
|
68 |
|
|
69 |
reverse
|
|
70 |
"return a new collection with all elements of the receiver
|
|
71 |
in reverse order"
|
|
72 |
|
|
73 |
|newCollection
|
|
74 |
size "{ Class:SmallInteger }"
|
|
75 |
dstIndex "{ Class:SmallInteger }" |
|
|
76 |
|
|
77 |
size := self size.
|
|
78 |
newCollection := self species new:size.
|
|
79 |
newCollection setFirstIndex:firstIndex lastIndex:lastIndex.
|
|
80 |
dstIndex := size.
|
|
81 |
1 to:size do:[:srcIndex |
|
|
82 |
newCollection at:dstIndex put:(self at:srcIndex).
|
|
83 |
dstIndex := dstIndex - 1
|
|
84 |
].
|
|
85 |
^ newCollection
|
|
86 |
!
|
|
87 |
|
|
88 |
, aCollection
|
|
89 |
"return a new collection formed from concatenating the receiver with
|
|
90 |
the argument"
|
|
91 |
|
|
92 |
|newCollection|
|
|
93 |
|
|
94 |
newCollection := self species new:(self size + aCollection size).
|
|
95 |
self do:[:element |
|
|
96 |
newCollection add:element
|
|
97 |
].
|
|
98 |
aCollection do:[:element |
|
|
99 |
newCollection add:element
|
|
100 |
].
|
|
101 |
^ newCollection
|
|
102 |
! !
|
|
103 |
|
|
104 |
!OrderedCollection methodsFor:'adding & removing'!
|
|
105 |
|
|
106 |
removeFirst
|
|
107 |
"remove the first element from the collection; return the element"
|
|
108 |
|
|
109 |
|anObject |
|
|
110 |
|
|
111 |
anObject := contentsArray at:firstIndex.
|
|
112 |
firstIndex := firstIndex + 1.
|
|
113 |
^ anObject
|
|
114 |
!
|
|
115 |
|
|
116 |
removeLast
|
|
117 |
"remove the last element from the collection; return the element"
|
|
118 |
|
|
119 |
|anObject |
|
|
120 |
|
|
121 |
anObject := contentsArray at:lastIndex.
|
|
122 |
lastIndex := lastIndex - 1.
|
|
123 |
^ anObject
|
|
124 |
!
|
|
125 |
|
|
126 |
remove:anObject ifAbsent:exceptionBlock
|
|
127 |
"remove the first occurrence of anObject from the collection;
|
|
128 |
return the value of exceptionBlock if anObject is not in
|
|
129 |
the collection"
|
|
130 |
|
|
131 |
|index "{ Class:SmallInteger }"|
|
|
132 |
|
|
133 |
index := firstIndex.
|
|
134 |
[index <= lastIndex] whileTrue:[
|
|
135 |
anObject = (contentsArray at:index) ifTrue:[
|
|
136 |
contentsArray replaceFrom:index to:(contentsArray size - 1)
|
|
137 |
with:contentsArray startingAt:(index + 1).
|
|
138 |
lastIndex := lastIndex - 1.
|
|
139 |
^ anObject
|
|
140 |
].
|
|
141 |
index := index + 1
|
|
142 |
].
|
|
143 |
^ exceptionBlock value
|
|
144 |
!
|
|
145 |
|
|
146 |
add:anObject
|
|
147 |
"add the argument, anObject to the end of the collection
|
|
148 |
Return the argument, anObject."
|
|
149 |
|
|
150 |
^ self addLast:anObject
|
|
151 |
!
|
|
152 |
|
|
153 |
addLast:anObject
|
|
154 |
"add the argument, anObject to the end of the collection
|
|
155 |
Return the argument, anObject."
|
|
156 |
|
|
157 |
(lastIndex == contentsArray size) ifTrue:[
|
|
158 |
self makeRoomAtLast
|
|
159 |
].
|
|
160 |
lastIndex := lastIndex + 1.
|
|
161 |
contentsArray at:lastIndex put:anObject.
|
|
162 |
^ anObject
|
|
163 |
!
|
|
164 |
|
|
165 |
addFirst:anObject
|
|
166 |
"add the argument, anObject to the beginning of the collection.
|
|
167 |
Return the argument, anObject."
|
|
168 |
|
|
169 |
(firstIndex == 1) ifTrue:[
|
|
170 |
self makeRoomAtFront
|
|
171 |
].
|
|
172 |
firstIndex := firstIndex - 1.
|
|
173 |
contentsArray at:firstIndex put:anObject.
|
|
174 |
^ anObject
|
|
175 |
!
|
|
176 |
|
|
177 |
add:anObject beforeIndex:index
|
|
178 |
"insert the argument, anObject to become located at index.
|
|
179 |
Return the argument, anObject."
|
|
180 |
|
|
181 |
self makeRoomAtIndex:(index - firstIndex + 1).
|
|
182 |
contentsArray at:(index - firstIndex + 1) put:anObject.
|
|
183 |
^ anObject
|
|
184 |
! !
|
|
185 |
|
|
186 |
!OrderedCollection methodsFor:'grow & shrink'!
|
|
187 |
|
|
188 |
grow:newSize
|
|
189 |
"return the number of elements in the collection"
|
|
190 |
|
|
191 |
|newContents|
|
|
192 |
|
|
193 |
newSize <= (lastIndex - firstIndex + 1) ifTrue:[
|
|
194 |
lastIndex := firstIndex + newSize - 1
|
|
195 |
] ifFalse:[
|
|
196 |
newContents := Array new:newSize.
|
|
197 |
newContents replaceFrom:1 to:(lastIndex - firstIndex + 1) with:contentsArray.
|
|
198 |
contentsArray := newContents.
|
|
199 |
firstIndex := 1.
|
|
200 |
lastIndex := newSize
|
|
201 |
]
|
|
202 |
! !
|
|
203 |
|
|
204 |
!OrderedCollection methodsFor:'accessing'!
|
|
205 |
|
|
206 |
at:anInteger
|
|
207 |
"return the element at index, anInteger"
|
|
208 |
|
|
209 |
((anInteger < 1) or:[(anInteger + firstIndex - 1) > lastIndex])
|
|
210 |
ifTrue:[
|
|
211 |
self errorNoSuchElement
|
|
212 |
] ifFalse:[
|
|
213 |
^ contentsArray at:(anInteger + firstIndex - 1)
|
|
214 |
]
|
|
215 |
!
|
|
216 |
|
|
217 |
at:anInteger put:anObject
|
|
218 |
"set the element at index, to be anInteger"
|
|
219 |
|
|
220 |
((anInteger < 1) or:[(anInteger + firstIndex - 1) > lastIndex])
|
|
221 |
ifTrue:[
|
|
222 |
self errorNoSuchElement
|
|
223 |
] ifFalse:[
|
|
224 |
^ contentsArray at:(anInteger + firstIndex - 1) put:anObject
|
|
225 |
]
|
|
226 |
! !
|
|
227 |
|
|
228 |
!OrderedCollection methodsFor:'private'!
|
|
229 |
|
|
230 |
errorNoSuchElement
|
|
231 |
self error:'indexing non existing element'
|
|
232 |
!
|
|
233 |
|
|
234 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex
|
|
235 |
firstIndex := newFirstIndex.
|
|
236 |
lastIndex := newLastIndex.
|
|
237 |
!
|
|
238 |
|
|
239 |
makeRoomAtLast
|
|
240 |
"grow the contents for more room at the end"
|
|
241 |
|
|
242 |
|newContents
|
|
243 |
oldSize "{ Class:SmallInteger }" |
|
|
244 |
|
|
245 |
oldSize := contentsArray size.
|
|
246 |
newContents := Array new:(oldSize * 2).
|
|
247 |
newContents replaceFrom:1 to:oldSize with:contentsArray.
|
|
248 |
contentsArray := newContents
|
|
249 |
!
|
|
250 |
|
|
251 |
makeRoomAtFront
|
|
252 |
"grow the contents for more room at the beginning"
|
|
253 |
|
|
254 |
|newContents
|
|
255 |
oldSize "{ Class:SmallInteger }" |
|
|
256 |
|
|
257 |
oldSize := contentsArray size.
|
|
258 |
newContents := Array new:(oldSize * 2).
|
|
259 |
newContents replaceFrom:(oldSize + 1) to:(oldSize * 2)
|
|
260 |
with:contentsArray startingAt:1.
|
|
261 |
contentsArray := newContents.
|
|
262 |
firstIndex := firstIndex + oldSize.
|
|
263 |
lastIndex := lastIndex + oldSize
|
|
264 |
!
|
|
265 |
|
|
266 |
makeRoomAtIndex:index
|
|
267 |
"grow the contents for inserting at index
|
|
268 |
i.e.
|
|
269 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6)
|
|
270 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6)
|
|
271 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)"
|
|
272 |
|
|
273 |
|newContents
|
|
274 |
newSize "{ Class:SmallInteger }"
|
|
275 |
oldSize "{ Class:SmallInteger }" |
|
|
276 |
|
|
277 |
oldSize := contentsArray size.
|
|
278 |
newSize := oldSize + 1.
|
|
279 |
newContents := Array new:newSize.
|
|
280 |
index == 1 ifFalse:[
|
|
281 |
newContents replaceFrom:1 to:index-1 with:contentsArray startingAt:1.
|
|
282 |
].
|
|
283 |
index == newSize ifFalse:[
|
|
284 |
newContents replaceFrom:index + 1 to:newSize with:contentsArray startingAt:index.
|
|
285 |
].
|
|
286 |
contentsArray := newContents.
|
|
287 |
lastIndex := lastIndex + 1
|
|
288 |
!
|
|
289 |
|
|
290 |
initContents:size
|
|
291 |
contentsArray := Array new:size.
|
|
292 |
firstIndex := 1.
|
|
293 |
lastIndex := 0
|
|
294 |
! !
|
|
295 |
|
|
296 |
!OrderedCollection methodsFor:'testing'!
|
|
297 |
|
|
298 |
includes:anObject
|
|
299 |
"return true if the object is in the collection"
|
|
300 |
|
|
301 |
|index|
|
|
302 |
|
|
303 |
index := contentsArray indexOf:anObject.
|
|
304 |
^ index between:firstIndex and:lastIndex
|
|
305 |
!
|
|
306 |
|
|
307 |
identityIndexOf:anObject
|
|
308 |
"return the index of the object, 0 if not in the collection"
|
|
309 |
|
|
310 |
|index|
|
|
311 |
|
|
312 |
index := contentsArray identityIndexOf:anObject.
|
|
313 |
index < firstIndex ifTrue:[^ 0].
|
|
314 |
index > lastIndex ifTrue:[^ 0].
|
|
315 |
^ index - firstIndex + 1
|
|
316 |
!
|
|
317 |
|
|
318 |
identityIndexOf:anObject startingAt:startIndex
|
|
319 |
"return the index of the object, 0 if not in the collection"
|
|
320 |
|
|
321 |
|index|
|
|
322 |
|
|
323 |
index := contentsArray identityIndexOf:anObject startingAt:(startIndex + firstIndex - 1).
|
|
324 |
index < firstIndex ifTrue:[^ 0].
|
|
325 |
index > lastIndex ifTrue:[^ 0].
|
|
326 |
^ index - firstIndex + 1
|
|
327 |
!
|
|
328 |
|
|
329 |
indexOf:anObject
|
|
330 |
"return the index of the object, 0 if not in the collection"
|
|
331 |
|
|
332 |
|index|
|
|
333 |
|
|
334 |
index := contentsArray indexOf:anObject.
|
|
335 |
index < firstIndex ifTrue:[^ 0].
|
|
336 |
index > lastIndex ifTrue:[^ 0].
|
|
337 |
^ index - firstIndex + 1
|
|
338 |
!
|
|
339 |
|
|
340 |
indexOf:anObject startingAt:startIndex
|
|
341 |
"return the index of the object, 0 if not in the collection"
|
|
342 |
|
|
343 |
|index|
|
|
344 |
|
|
345 |
index := contentsArray indexOf:anObject startingAt:(startIndex + firstIndex - 1).
|
|
346 |
index < firstIndex ifTrue:[^ 0].
|
|
347 |
index > lastIndex ifTrue:[^ 0].
|
|
348 |
^ index - firstIndex + 1
|
|
349 |
! !
|
|
350 |
|
|
351 |
!OrderedCollection methodsFor:'enumeration'!
|
|
352 |
|
|
353 |
do:aBlock
|
|
354 |
"evaluate the argument, aBlock for every element in the collection."
|
|
355 |
|
|
356 |
contentsArray from:firstIndex to:lastIndex do:aBlock
|
|
357 |
!
|
|
358 |
|
|
359 |
collect:aBlock
|
|
360 |
"evaluate the argument, aBlock for every element in the collection
|
|
361 |
and return a collection of the results"
|
|
362 |
|
|
363 |
|newCollection
|
|
364 |
index "{ Class:SmallInteger }"
|
|
365 |
end "{ Class:SmallInteger }" |
|
|
366 |
|
|
367 |
end := lastIndex.
|
|
368 |
newCollection := (self species new).
|
|
369 |
index := firstIndex.
|
|
370 |
[index <= end] whileTrue:[
|
|
371 |
newCollection add:(aBlock value:(contentsArray at:index)).
|
|
372 |
index := index + 1
|
|
373 |
].
|
|
374 |
^ newCollection
|
|
375 |
! !
|