1043
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
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 |
"{ Package: 'stx:libbasic2' }"
|
|
14 |
|
|
15 |
SequenceableCollection subclass:#Cons
|
|
16 |
instanceVariableNames:'car cdr'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Collections-Linked'
|
|
20 |
!
|
|
21 |
|
|
22 |
!Cons class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
|
40 |
A pair as in lisp.
|
|
41 |
Create with:
|
1074
|
42 |
a !! b
|
|
43 |
or:
|
|
44 |
Cons car:a cdr:b
|
|
45 |
|
|
46 |
Conses are not heavily used by Smalltalk (actually: not at all).
|
1377
|
47 |
Consider this a demo class.
|
1043
|
48 |
|
|
49 |
[author:]
|
|
50 |
Claus Gittinger
|
|
51 |
|
|
52 |
[see also:]
|
|
53 |
|
|
54 |
"
|
|
55 |
! !
|
|
56 |
|
|
57 |
!Cons class methodsFor:'instance creation'!
|
|
58 |
|
|
59 |
car:carArg cdr:cdrArg
|
|
60 |
^ self basicNew car:carArg cdr:cdrArg
|
|
61 |
!
|
|
62 |
|
|
63 |
fromArray:anArray
|
|
64 |
|p last first|
|
|
65 |
|
|
66 |
anArray do:[:el |
|
|
67 |
p := self car:el cdr:nil.
|
|
68 |
first isNil ifTrue:[
|
|
69 |
first := p.
|
|
70 |
] ifFalse:[
|
|
71 |
last cdr:p.
|
|
72 |
].
|
|
73 |
last := p.
|
|
74 |
].
|
|
75 |
^ first.
|
|
76 |
|
|
77 |
"
|
1074
|
78 |
Cons fromArray:#(1 2 3 4)
|
|
79 |
Cons fromArray:#()
|
|
80 |
Cons fromArray:#(1)
|
1248
|
81 |
Cons fromArray:(1 to:10000)
|
1043
|
82 |
"
|
|
83 |
! !
|
|
84 |
|
|
85 |
!Cons methodsFor:'accessing'!
|
|
86 |
|
|
87 |
at:n
|
1074
|
88 |
"for collection compatibility:
|
|
89 |
a slow indexed accessor"
|
|
90 |
|
1043
|
91 |
^ (self nth:n) car
|
|
92 |
|
|
93 |
"
|
1074
|
94 |
(Cons fromArray:#(1)) at:1
|
|
95 |
(Cons fromArray:#(1 2 3 4)) at:1
|
|
96 |
(Cons fromArray:#(1 2 3 4)) at:3
|
|
97 |
(Cons fromArray:#(1 2 3 4)) at:4
|
|
98 |
(Cons fromArray:#(1 2 3 4)) at:5
|
1043
|
99 |
"
|
|
100 |
!
|
|
101 |
|
|
102 |
at:n put:newValue
|
1074
|
103 |
"for collection compatibility:
|
|
104 |
a slow indexed accessor"
|
|
105 |
|
1043
|
106 |
(self nth:n) car:newValue.
|
|
107 |
^ newValue.
|
|
108 |
|
|
109 |
"
|
|
110 |
|l|
|
|
111 |
|
1074
|
112 |
l := Cons fromArray:#(1 2 3 4).
|
1043
|
113 |
l at:1 put:'one'.
|
|
114 |
l at:3 put:'three'.
|
|
115 |
l
|
|
116 |
"
|
|
117 |
!
|
|
118 |
|
1237
|
119 |
head
|
|
120 |
"return the head, first or car - whatever you wonna call it"
|
|
121 |
|
|
122 |
^ self car
|
|
123 |
!
|
|
124 |
|
1043
|
125 |
last
|
1074
|
126 |
"for lispers:
|
|
127 |
return the last element of a list"
|
|
128 |
|
1043
|
129 |
|p rest|
|
|
130 |
|
|
131 |
p := self.
|
|
132 |
[(rest := p cdr) notNil] whileTrue:[
|
|
133 |
p := rest
|
|
134 |
].
|
1237
|
135 |
^ p car
|
1043
|
136 |
|
|
137 |
"
|
1074
|
138 |
(Cons fromArray:#(1)) last
|
|
139 |
(Cons fromArray:#(1 2 3 4)) last
|
1043
|
140 |
"
|
|
141 |
!
|
|
142 |
|
|
143 |
nth:n
|
1074
|
144 |
"for lispers:
|
|
145 |
return the nth element of a list"
|
|
146 |
|
1043
|
147 |
|cnt p|
|
|
148 |
|
|
149 |
cnt := n.
|
|
150 |
p := self.
|
1248
|
151 |
[
|
1043
|
152 |
cnt := cnt - 1.
|
1237
|
153 |
cnt == 0 ifTrue:[^ p car].
|
1043
|
154 |
p := p cdr.
|
|
155 |
p isNil ifTrue:[
|
|
156 |
self error:'no such element' mayProceed:true.
|
|
157 |
^ nil
|
|
158 |
].
|
1248
|
159 |
] loop.
|
1043
|
160 |
|
|
161 |
"
|
1074
|
162 |
(Cons fromArray:#(1)) nth:1
|
|
163 |
(Cons fromArray:#(1 2 3 4)) nth:1
|
|
164 |
(Cons fromArray:#(1 2 3 4)) nth:3
|
|
165 |
(Cons fromArray:#(1 2 3 4)) nth:4
|
|
166 |
(Cons fromArray:#(1 2 3 4)) nth:5
|
1248
|
167 |
(Cons fromArray:#( )) nth:1 -> error
|
1043
|
168 |
"
|
1237
|
169 |
!
|
|
170 |
|
1243
|
171 |
reversed
|
|
172 |
"for lispers:
|
|
173 |
return a new list with the cars in reverse order"
|
|
174 |
|
|
175 |
"/ for now, tail recursion is not yet optimized by the st/x jitter...
|
|
176 |
"/
|
|
177 |
"/ |rev|
|
|
178 |
"/
|
|
179 |
"/ rev := [:lst :acc |
|
|
180 |
"/ lst isNil ifTrue:[
|
|
181 |
"/ acc
|
|
182 |
"/ ] ifFalse:[
|
|
183 |
"/ rev value:(lst tail)
|
|
184 |
"/ value:(Cons car:(lst head) cdr:acc)
|
|
185 |
"/ ]
|
|
186 |
"/ ].
|
|
187 |
"/ ^ rev value:self value:nil
|
|
188 |
|
|
189 |
| lst acc|
|
|
190 |
|
|
191 |
lst := self.
|
|
192 |
acc := nil.
|
|
193 |
|
|
194 |
[
|
|
195 |
|nLst nAcc|
|
|
196 |
|
|
197 |
lst isNil ifTrue:[ ^ acc].
|
|
198 |
|
|
199 |
nLst := lst tail.
|
|
200 |
nAcc := Cons car:(lst head) cdr:acc.
|
|
201 |
lst := nLst.
|
|
202 |
acc := nAcc.
|
|
203 |
] loop
|
|
204 |
|
|
205 |
"
|
|
206 |
(Cons fromArray:#(1)) reversed
|
|
207 |
(Cons fromArray:#(1 2)) reversed
|
|
208 |
(Cons fromArray:#(1 2 3 4)) reversed
|
|
209 |
(Cons fromArray:(1 to:10000)) reversed
|
|
210 |
"
|
|
211 |
!
|
|
212 |
|
1237
|
213 |
tail
|
|
214 |
"return the tail, second or cdr - whatever you wonna call it"
|
|
215 |
|
|
216 |
^ self cdr
|
1043
|
217 |
! !
|
|
218 |
|
|
219 |
!Cons methodsFor:'accessing - basic'!
|
|
220 |
|
|
221 |
car
|
1074
|
222 |
"return the head, first or car - whatever you wonna call it"
|
1043
|
223 |
|
|
224 |
^ car
|
|
225 |
!
|
|
226 |
|
|
227 |
car:something
|
1074
|
228 |
"set the head, first or car - whatever you wonna call it"
|
1043
|
229 |
|
|
230 |
car := something.
|
|
231 |
!
|
|
232 |
|
|
233 |
car:carArg cdr:cdrArg
|
1074
|
234 |
"set both car and cdr"
|
1043
|
235 |
|
|
236 |
car := carArg.
|
|
237 |
cdr := cdrArg.
|
|
238 |
!
|
|
239 |
|
|
240 |
cdr
|
1074
|
241 |
"return the tail, second or cdr - whatever you wonna call it"
|
1043
|
242 |
|
|
243 |
^ cdr
|
|
244 |
!
|
|
245 |
|
|
246 |
cdr:something
|
1074
|
247 |
"set the tail, second or cdr - whatever you wonna call it"
|
1043
|
248 |
|
|
249 |
cdr := something.
|
|
250 |
! !
|
|
251 |
|
|
252 |
!Cons methodsFor:'list processing'!
|
|
253 |
|
|
254 |
append:aCons
|
1074
|
255 |
"for lispers:
|
1248
|
256 |
append the arg. Return a new list, where the 2nd part is shared.
|
|
257 |
Destructive: the receivers last cdr is modified."
|
1074
|
258 |
|
1043
|
259 |
|p rest|
|
|
260 |
|
|
261 |
p := self.
|
|
262 |
[(rest := p cdr) notNil] whileTrue:[
|
|
263 |
p := rest
|
|
264 |
].
|
|
265 |
p cdr:aCons.
|
|
266 |
^ self
|
|
267 |
|
|
268 |
"
|
1074
|
269 |
(Cons fromArray:#(1 2 3 4))
|
|
270 |
append:(Cons fromArray:#(5 6 7 8))
|
|
271 |
"
|
|
272 |
|
|
273 |
"sharing demonstrated:
|
|
274 |
|
|
275 |
|a b ab|
|
|
276 |
|
|
277 |
a := Cons fromArray:#(1 2 3 4).
|
|
278 |
b := Cons fromArray:#(5 6 7 8).
|
|
279 |
ab := a append:b.
|
|
280 |
b car:'five'.
|
1248
|
281 |
ab
|
|
282 |
"
|
|
283 |
|
|
284 |
"destruction demonstrated:
|
|
285 |
|
|
286 |
|a b ab|
|
|
287 |
|
|
288 |
a := Cons fromArray:#(1 2 3 4).
|
|
289 |
b := Cons fromArray:#(5 6 7 8).
|
|
290 |
ab := a append:b.
|
|
291 |
a
|
1043
|
292 |
"
|
1237
|
293 |
!
|
|
294 |
|
1248
|
295 |
take:nTaken
|
1237
|
296 |
"for lispers:
|
|
297 |
take n elements from the list; return a new list"
|
|
298 |
|
1248
|
299 |
|nRemain l rslt lastCons cons|
|
|
300 |
|
|
301 |
nTaken > 0 ifTrue:[
|
|
302 |
"/ avoiding recursion here...
|
|
303 |
"/ instead of:
|
|
304 |
"/ ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
|
|
305 |
"/ we do:
|
|
306 |
nRemain := nTaken.
|
|
307 |
l := self.
|
|
308 |
rslt := lastCons := Cons car:(l car) cdr:nil.
|
|
309 |
[nRemain > 1] whileTrue:[
|
|
310 |
l := l cdr.
|
|
311 |
cons := Cons car:(l car) cdr:nil.
|
|
312 |
lastCons cdr:cons.
|
|
313 |
lastCons := cons.
|
|
314 |
nRemain := nRemain - 1.
|
|
315 |
].
|
|
316 |
^ rslt.
|
1237
|
317 |
].
|
|
318 |
^ nil
|
|
319 |
|
|
320 |
"
|
|
321 |
(Cons fromArray:#(1 2 3 4)) take:3
|
|
322 |
(Cons fromArray:#(1)) take:0
|
|
323 |
(Cons fromArray:#()) take:3
|
1248
|
324 |
(Cons fromArray:(1 to: 1000)) take:999
|
1237
|
325 |
"
|
1043
|
326 |
! !
|
|
327 |
|
|
328 |
!Cons methodsFor:'printing'!
|
|
329 |
|
|
330 |
displayString
|
|
331 |
^ self printString
|
|
332 |
!
|
|
333 |
|
|
334 |
printOn:aStream
|
|
335 |
(car isLazyValue not and:[ car isCons ]) ifTrue:[
|
|
336 |
aStream nextPutAll:'('.
|
|
337 |
car printOn:aStream.
|
|
338 |
aStream nextPutAll:')'.
|
|
339 |
] ifFalse:[
|
|
340 |
car printOn:aStream.
|
|
341 |
].
|
|
342 |
|
1073
|
343 |
aStream nextPutAll:'!!'.
|
1043
|
344 |
|
|
345 |
(cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
|
|
346 |
aStream nextPutAll:'('.
|
|
347 |
cdr printOn:aStream.
|
|
348 |
aStream nextPutAll:')'.
|
|
349 |
] ifFalse:[
|
|
350 |
cdr printOn:aStream.
|
|
351 |
].
|
|
352 |
! !
|
|
353 |
|
|
354 |
!Cons methodsFor:'queries'!
|
|
355 |
|
|
356 |
isCons
|
|
357 |
^ true
|
|
358 |
!
|
|
359 |
|
|
360 |
size
|
1074
|
361 |
"for smalltalkers: the lists length"
|
|
362 |
|
1043
|
363 |
|l p rest|
|
|
364 |
|
|
365 |
l := 1.
|
|
366 |
p := self.
|
|
367 |
[(rest := p cdr) notNil] whileTrue:[
|
|
368 |
l := l + 1.
|
|
369 |
p := rest
|
|
370 |
].
|
|
371 |
^ l
|
|
372 |
|
|
373 |
"
|
1074
|
374 |
(Cons fromArray:#( )) size
|
|
375 |
(Cons fromArray:#(1)) size
|
|
376 |
(Cons fromArray:#(1 2 3 4)) size
|
1043
|
377 |
"
|
|
378 |
! !
|
|
379 |
|
1377
|
380 |
!Cons methodsFor:'streaming'!
|
|
381 |
|
|
382 |
readStream
|
|
383 |
^ ConsStream on:self.
|
|
384 |
! !
|
|
385 |
|
1043
|
386 |
!Cons class methodsFor:'documentation'!
|
|
387 |
|
|
388 |
version
|
1377
|
389 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.7 2003-12-09 14:26:02 cg Exp $'
|
1043
|
390 |
! !
|