0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
42
|
3 |
All Rights Reserved
|
0
|
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 |
ArrayedCollection subclass:#VariableArray
|
807
|
14 |
instanceVariableNames:'tally contentsArray'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Obsolete'
|
0
|
18 |
!
|
|
19 |
|
31
|
20 |
!VariableArray class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
42
|
25 |
All Rights Reserved
|
0
|
26 |
|
31
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
0
|
35 |
|
31
|
36 |
documentation
|
|
37 |
"
|
|
38 |
VariableArrays can grow and shrink - in contrast to Arrays which are
|
42
|
39 |
fixed in size.
|
|
40 |
WARNING: Do not use this class for new applications - its a historic
|
58
|
41 |
leftover from times when no OrderedCollection existed.
|
42
|
42 |
|
31
|
43 |
Use OrderedCollection, which offers more functionality, and is even
|
42
|
44 |
a bit faster in some operations.
|
31
|
45 |
"
|
|
46 |
! !
|
0
|
47 |
|
|
48 |
!VariableArray class methodsFor:'instance creation'!
|
|
49 |
|
|
50 |
new
|
|
51 |
"return a new VariableArray - with size 0"
|
|
52 |
|
|
53 |
^ (self basicNew) setInitialContents:(Array new:10)
|
|
54 |
!
|
|
55 |
|
|
56 |
new:size
|
|
57 |
"return a new VariableArray"
|
|
58 |
|
|
59 |
^ (self basicNew) setContents:(Array new:size)
|
|
60 |
! !
|
|
61 |
|
124
|
62 |
!VariableArray methodsFor:'accessing'!
|
0
|
63 |
|
124
|
64 |
at:index
|
|
65 |
"return the element at index"
|
0
|
66 |
|
124
|
67 |
(index between:1 and:tally) ifFalse:[
|
|
68 |
^ self subscriptBoundsError:index
|
|
69 |
].
|
|
70 |
^ contentsArray at:index
|
0
|
71 |
!
|
|
72 |
|
124
|
73 |
at:index put:anObject
|
|
74 |
"set the element at index"
|
0
|
75 |
|
124
|
76 |
(index between:1 and:tally) ifFalse:[
|
|
77 |
^ self subscriptBoundsError:index
|
|
78 |
].
|
|
79 |
^ contentsArray at:index put:anObject
|
0
|
80 |
! !
|
|
81 |
|
124
|
82 |
!VariableArray methodsFor:'enumerating'!
|
0
|
83 |
|
124
|
84 |
do:aBlock
|
|
85 |
"evaluate the argument, aBlock for each element
|
|
86 |
in the collection"
|
0
|
87 |
|
124
|
88 |
contentsArray from:1 to:tally do:aBlock
|
0
|
89 |
!
|
|
90 |
|
124
|
91 |
from:start to:stop do:aBlock
|
|
92 |
"evaluate the argument, aBlock for some elements
|
|
93 |
in the collection"
|
0
|
94 |
|
124
|
95 |
(stop <= tally) ifTrue:[
|
|
96 |
contentsArray from:start to:stop do:aBlock
|
|
97 |
] ifFalse:[
|
|
98 |
super from:start to:stop do:aBlock
|
|
99 |
]
|
0
|
100 |
! !
|
|
101 |
|
|
102 |
!VariableArray methodsFor:'filling & replacing'!
|
|
103 |
|
|
104 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
105 |
"reimplemented for speed
|
|
106 |
- can use Arrays fast replace if aCollection is Array or VariableArray"
|
|
107 |
|
|
108 |
|col|
|
|
109 |
|
|
110 |
col := aCollection.
|
|
111 |
(aCollection isKindOf:VariableArray) ifTrue:[
|
42
|
112 |
((stop - start + repStart) <= aCollection size) ifTrue:[
|
|
113 |
col := aCollection getContents
|
|
114 |
]
|
0
|
115 |
].
|
|
116 |
(col isMemberOf:Array) ifTrue:[
|
42
|
117 |
(stop <= tally) ifTrue:[
|
|
118 |
contentsArray replaceFrom:start to:stop with:col startingAt:repStart.
|
28
|
119 |
^ self
|
42
|
120 |
]
|
0
|
121 |
].
|
|
122 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
123 |
! !
|
|
124 |
|
124
|
125 |
!VariableArray methodsFor:'grow & shrink'!
|
|
126 |
|
|
127 |
add:anElement
|
|
128 |
"add anElement to the end of the array"
|
|
129 |
|
|
130 |
|newSize "{ Class: SmallInteger }" |
|
|
131 |
|
|
132 |
newSize := tally + 1.
|
|
133 |
(newSize <= contentsArray size) ifTrue:[
|
|
134 |
tally := newSize
|
|
135 |
] ifFalse:[
|
|
136 |
self grow:newSize
|
|
137 |
].
|
|
138 |
contentsArray at:tally put:anElement
|
|
139 |
!
|
|
140 |
|
|
141 |
grow:newSize
|
|
142 |
"grow to newSize"
|
|
143 |
|
|
144 |
|newArray|
|
|
145 |
|
|
146 |
(newSize == tally) ifTrue:[^ self].
|
|
147 |
|
|
148 |
(newSize > tally) ifTrue:[
|
|
149 |
(newSize > contentsArray size) ifTrue:[
|
|
150 |
newArray := Array new:(newSize * 2).
|
|
151 |
newArray replaceFrom:1 to:tally with:contentsArray startingAt:1.
|
|
152 |
contentsArray := newArray
|
|
153 |
]
|
|
154 |
] ifFalse:[
|
|
155 |
contentsArray from:(newSize + 1) to:tally put:nil
|
|
156 |
].
|
|
157 |
tally := newSize
|
|
158 |
! !
|
|
159 |
|
|
160 |
!VariableArray methodsFor:'inquiries'!
|
|
161 |
|
|
162 |
isFixedSize
|
|
163 |
"return true if the receiver cannot grow - this will vanish once
|
|
164 |
Arrays and Strings learn how to grow ..."
|
|
165 |
|
|
166 |
^ false
|
|
167 |
!
|
|
168 |
|
|
169 |
size
|
|
170 |
"return the number of array elements"
|
|
171 |
|
|
172 |
^ tally
|
|
173 |
! !
|
|
174 |
|
|
175 |
!VariableArray methodsFor:'kludges'!
|
|
176 |
|
|
177 |
shallowCopy:anArray
|
|
178 |
"return a shallow copy of the receiver
|
|
179 |
have to kludge the kludge ... - shallow copy the contents array"
|
|
180 |
|
|
181 |
|newText|
|
|
182 |
|
|
183 |
newText := self class new.
|
|
184 |
newText setContents:(contentsArray shallowCopy).
|
|
185 |
^ newText
|
|
186 |
! !
|
|
187 |
|
|
188 |
!VariableArray methodsFor:'private'!
|
|
189 |
|
|
190 |
getContents
|
|
191 |
"return the contents array"
|
|
192 |
|
|
193 |
^ contentsArray
|
|
194 |
!
|
|
195 |
|
|
196 |
setContents:anArray
|
|
197 |
"set the contents array"
|
|
198 |
|
|
199 |
tally := anArray size.
|
|
200 |
contentsArray := anArray
|
|
201 |
!
|
|
202 |
|
|
203 |
setInitialContents:anArray
|
|
204 |
"set the contents array but make size zero"
|
|
205 |
|
|
206 |
tally := 0.
|
|
207 |
contentsArray := anArray
|
|
208 |
! !
|
|
209 |
|
0
|
210 |
!VariableArray methodsFor:'removing'!
|
|
211 |
|
|
212 |
removeFromIndex:startIndex toIndex:endIndex
|
|
213 |
"remove the elements stored at indexes between startIndex and endIndex"
|
|
214 |
|
|
215 |
|newSize|
|
|
216 |
|
|
217 |
(endIndex >= tally) ifTrue:[
|
42
|
218 |
self grow:(startIndex - 1)
|
0
|
219 |
] ifFalse:[
|
42
|
220 |
newSize := tally - endIndex + startIndex - 1.
|
|
221 |
contentsArray replaceFrom:startIndex to:newSize with:contentsArray startingAt:(endIndex + 1).
|
|
222 |
self grow:newSize
|
0
|
223 |
]
|
|
224 |
! !
|
|
225 |
|
|
226 |
!VariableArray methodsFor:'testing'!
|
|
227 |
|
124
|
228 |
identityIndexOf:anElement startingAt:start
|
|
229 |
"search the collection for anElement starting search at index start
|
|
230 |
using == for compares.
|
|
231 |
if found, return the index otherwise return 0"
|
|
232 |
|
|
233 |
|index|
|
0
|
234 |
|
124
|
235 |
(start > tally) ifFalse:[
|
|
236 |
index := contentsArray identityIndexOf:anElement startingAt:start.
|
|
237 |
index == 0 ifFalse:[
|
|
238 |
(index between:1 and:tally) ifTrue:[
|
|
239 |
^ index
|
|
240 |
]
|
|
241 |
]
|
|
242 |
].
|
|
243 |
^ 0
|
0
|
244 |
!
|
|
245 |
|
|
246 |
includes:anObject
|
|
247 |
"return true, if the receiver contains the argument, anObject"
|
|
248 |
|
|
249 |
^ contentsArray includes:anObject
|
|
250 |
!
|
|
251 |
|
|
252 |
indexOf:anElement startingAt:start
|
|
253 |
"search the collection for anElement starting search at index start
|
|
254 |
using = for compares.
|
|
255 |
if found, return the index otherwise return 0"
|
|
256 |
|
|
257 |
|index|
|
|
258 |
|
|
259 |
(start > tally) ifFalse:[
|
42
|
260 |
index := contentsArray indexOf:anElement startingAt:start.
|
|
261 |
index == 0 ifFalse:[
|
|
262 |
(index between:1 and:tally) ifTrue:[
|
|
263 |
^ index
|
|
264 |
]
|
|
265 |
]
|
0
|
266 |
].
|
|
267 |
^ 0
|
|
268 |
!
|
|
269 |
|
124
|
270 |
occurrencesOf:anObject
|
|
271 |
"return the number of occurrences of anObject in the receiver"
|
0
|
272 |
|
124
|
273 |
^ contentsArray occurrencesOf:anObject
|
0
|
274 |
! !
|
|
275 |
|
131
|
276 |
!VariableArray class methodsFor:'documentation'!
|
|
277 |
|
|
278 |
version
|
807
|
279 |
^ '$Header: /cvs/stx/stx/libbasic2/VariableArray.st,v 1.15 1999-09-08 21:28:27 cg Exp $'
|
131
|
280 |
! !
|