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