|
1 " |
|
2 COPYRIGHT (c) 1989-92 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:#ArrayedCollection |
|
14 instanceVariableNames:'' |
|
15 classVariableNames:'' |
|
16 poolDictionaries:'' |
|
17 category:'Collections-Abstract' |
|
18 ! |
|
19 |
|
20 ArrayedCollection comment:' |
|
21 |
|
22 COPYRIGHT (c) 1989-92 by Claus Gittinger |
|
23 All Rights Reserved |
|
24 |
|
25 ArrayedCollections are collections where the elements can be accessed via an integer index. |
|
26 |
|
27 %W% %E% |
|
28 written spring 89 by claus |
|
29 '! |
|
30 |
|
31 !ArrayedCollection class methodsFor:'instance creation'! |
|
32 |
|
33 with:element |
|
34 "return a new SequenceableCollection with one element:anObject" |
|
35 |
|
36 |newCollection| |
|
37 |
|
38 newCollection := self new:1. |
|
39 newCollection at:1 put:element. |
|
40 ^newCollection |
|
41 ! |
|
42 |
|
43 with:first with:second |
|
44 "return a new SequenceableCollection with two elements" |
|
45 |
|
46 |newCollection| |
|
47 |
|
48 newCollection := self new:2. |
|
49 newCollection at:1 put:first. |
|
50 newCollection at:2 put:second. |
|
51 ^newCollection |
|
52 ! |
|
53 |
|
54 with:first with:second with:third |
|
55 "return a new SequenceableCollection with three elements" |
|
56 |
|
57 |newCollection| |
|
58 |
|
59 newCollection := self new:3. |
|
60 newCollection at:1 put:first. |
|
61 newCollection at:2 put:second. |
|
62 newCollection at:3 put:third. |
|
63 ^newCollection |
|
64 ! |
|
65 |
|
66 with:first with:second with:third with:forth |
|
67 "return a new SequenceableCollection with four elements" |
|
68 |
|
69 |newCollection| |
|
70 |
|
71 newCollection := self new:4. |
|
72 newCollection at:1 put:first. |
|
73 newCollection at:2 put:second. |
|
74 newCollection at:3 put:third. |
|
75 newCollection at:4 put:forth. |
|
76 ^newCollection |
|
77 ! |
|
78 |
|
79 with:one with:two with:three with:four with:five |
|
80 "return a new SequenceableCollection with five elements" |
|
81 |
|
82 |newCollection| |
|
83 |
|
84 newCollection := self new:5. |
|
85 newCollection at:1 put:one. |
|
86 newCollection at:2 put:two. |
|
87 newCollection at:3 put:three. |
|
88 newCollection at:4 put:four. |
|
89 newCollection at:5 put:five. |
|
90 ^newCollection |
|
91 ! |
|
92 |
|
93 withAll:aCollection |
|
94 "return a new Collection with all elements taken from the argument, |
|
95 aCollection" |
|
96 |
|
97 |newCollection newSize |
|
98 index "{ Class: SmallInteger }" | |
|
99 |
|
100 newSize := aCollection size. |
|
101 newCollection := self new:newSize. |
|
102 (aCollection isKindOf:SequenceableCollection) ifTrue:[ |
|
103 "aCollection has indexed elements" |
|
104 newCollection replaceFrom:1 to:newSize with:aCollection startingAt:1 |
|
105 ] ifFalse:[ |
|
106 "must enumerate the elements" |
|
107 index := 1. |
|
108 aCollection do:[:element | |
|
109 newCollection at:index put:element. |
|
110 index := index + 1 |
|
111 ] |
|
112 ]. |
|
113 ^ newCollection |
|
114 ! ! |
|
115 |
|
116 !ArrayedCollection methodsFor:'error handling'! |
|
117 |
|
118 indexMustBeInteger |
|
119 "report an error that index must be Integer" |
|
120 |
|
121 ^ self error:'index must be integer' |
|
122 ! |
|
123 |
|
124 indexOutOfRange:theIndex |
|
125 "report an error that index is out of range" |
|
126 |
|
127 ^ self error:'index is out of range' |
|
128 ! |
|
129 |
|
130 fixedSizeError |
|
131 "report an error that size of the collection cannot be changed" |
|
132 |
|
133 ^ self error:'cannot change size' |
|
134 ! ! |
|
135 |
|
136 !ArrayedCollection methodsFor:'accessing'! |
|
137 |
|
138 size |
|
139 "return the ArrayedCollections size - redefined since SequenceableCollection |
|
140 does it in a slow way" |
|
141 |
|
142 ^ self basicSize |
|
143 ! |
|
144 |
|
145 at:index |
|
146 "return the index's element of the collection" |
|
147 |
|
148 ^ self basicAt:index |
|
149 ! |
|
150 |
|
151 at:index put:anObject |
|
152 "put the argument as index's element into the collection" |
|
153 |
|
154 ^ self basicAt:index put:anObject |
|
155 ! ! |
|
156 |
|
157 !ArrayedCollection methodsFor:'storing'! |
|
158 |
|
159 storeOn:aStream |
|
160 "output a printed representation (which can be re-read) |
|
161 onto the argument aStream" |
|
162 |
|
163 |index "{ Class: SmallInteger }"| |
|
164 |
|
165 aStream nextPutAll:'('. |
|
166 aStream nextPutAll:self class name. |
|
167 aStream nextPutAll:' new:'. |
|
168 self size printOn:aStream. |
|
169 aStream nextPutAll:')'. |
|
170 index := 1. |
|
171 self do:[:element | |
|
172 aStream nextPutAll:' at:'. |
|
173 index printOn:aStream. |
|
174 aStream nextPutAll:' put:'. |
|
175 element storeOn:aStream. |
|
176 aStream nextPut:$;. |
|
177 index := index + 1 |
|
178 ]. |
|
179 index > 1 ifTrue:[aStream nextPutAll:' yourself']. |
|
180 aStream nextPut:$) |
|
181 ! ! |