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 |
"
|
1627
|
55 |
!
|
|
56 |
|
|
57 |
examples
|
|
58 |
"
|
|
59 |
[exBegin]
|
|
60 |
|p1 p2 p3|
|
|
61 |
|
|
62 |
p3 := Cons car:3 cdr:nil.
|
|
63 |
p2 := Cons car:2 cdr:p3.
|
|
64 |
p1 := Cons car:1 cdr:p2.
|
|
65 |
p1 head.
|
|
66 |
p1 tail.
|
|
67 |
p1 size.
|
|
68 |
p1 do:[:each | Transcript showCR:each].
|
|
69 |
p1 at:2
|
|
70 |
[exEnd]
|
|
71 |
"
|
1043
|
72 |
! !
|
|
73 |
|
|
74 |
!Cons class methodsFor:'instance creation'!
|
|
75 |
|
|
76 |
car:carArg cdr:cdrArg
|
|
77 |
^ self basicNew car:carArg cdr:cdrArg
|
|
78 |
!
|
|
79 |
|
|
80 |
fromArray:anArray
|
|
81 |
|p last first|
|
|
82 |
|
|
83 |
anArray do:[:el |
|
|
84 |
p := self car:el cdr:nil.
|
|
85 |
first isNil ifTrue:[
|
|
86 |
first := p.
|
|
87 |
] ifFalse:[
|
|
88 |
last cdr:p.
|
|
89 |
].
|
|
90 |
last := p.
|
|
91 |
].
|
|
92 |
^ first.
|
|
93 |
|
|
94 |
"
|
1074
|
95 |
Cons fromArray:#(1 2 3 4)
|
|
96 |
Cons fromArray:#()
|
|
97 |
Cons fromArray:#(1)
|
1248
|
98 |
Cons fromArray:(1 to:10000)
|
1043
|
99 |
"
|
|
100 |
! !
|
|
101 |
|
|
102 |
!Cons methodsFor:'accessing'!
|
|
103 |
|
|
104 |
at:n
|
1074
|
105 |
"for collection compatibility:
|
|
106 |
a slow indexed accessor"
|
|
107 |
|
1627
|
108 |
^ (self nth:n)
|
1043
|
109 |
|
|
110 |
"
|
1074
|
111 |
(Cons fromArray:#(1)) at:1
|
|
112 |
(Cons fromArray:#(1 2 3 4)) at:1
|
|
113 |
(Cons fromArray:#(1 2 3 4)) at:3
|
|
114 |
(Cons fromArray:#(1 2 3 4)) at:4
|
|
115 |
(Cons fromArray:#(1 2 3 4)) at:5
|
1043
|
116 |
"
|
|
117 |
!
|
|
118 |
|
|
119 |
at:n put:newValue
|
1627
|
120 |
"destructive:
|
|
121 |
for collection compatibility: a slow indexed accessor"
|
1074
|
122 |
|
1627
|
123 |
(self nthPair:n) car:newValue.
|
1043
|
124 |
^ newValue.
|
|
125 |
|
|
126 |
"
|
|
127 |
|l|
|
|
128 |
|
1074
|
129 |
l := Cons fromArray:#(1 2 3 4).
|
1043
|
130 |
l at:1 put:'one'.
|
|
131 |
l at:3 put:'three'.
|
|
132 |
l
|
|
133 |
"
|
|
134 |
!
|
|
135 |
|
1623
|
136 |
first
|
|
137 |
"return the head, first or car - whatever you wonna call it"
|
|
138 |
|
|
139 |
^ self car
|
|
140 |
!
|
|
141 |
|
1237
|
142 |
head
|
|
143 |
"return the head, first or car - whatever you wonna call it"
|
|
144 |
|
|
145 |
^ self car
|
|
146 |
!
|
|
147 |
|
1043
|
148 |
last
|
1074
|
149 |
"for lispers:
|
|
150 |
return the last element of a list"
|
|
151 |
|
1043
|
152 |
|p rest|
|
|
153 |
|
|
154 |
p := self.
|
|
155 |
[(rest := p cdr) notNil] whileTrue:[
|
|
156 |
p := rest
|
|
157 |
].
|
1237
|
158 |
^ p car
|
1043
|
159 |
|
|
160 |
"
|
1074
|
161 |
(Cons fromArray:#(1)) last
|
|
162 |
(Cons fromArray:#(1 2 3 4)) last
|
1043
|
163 |
"
|
|
164 |
!
|
|
165 |
|
|
166 |
nth:n
|
1074
|
167 |
"for lispers:
|
|
168 |
return the nth element of a list"
|
|
169 |
|
1627
|
170 |
^ (self nthPair:n) car
|
1043
|
171 |
|
|
172 |
"
|
1074
|
173 |
(Cons fromArray:#(1)) nth:1
|
|
174 |
(Cons fromArray:#(1 2 3 4)) nth:1
|
|
175 |
(Cons fromArray:#(1 2 3 4)) nth:3
|
|
176 |
(Cons fromArray:#(1 2 3 4)) nth:4
|
|
177 |
(Cons fromArray:#(1 2 3 4)) nth:5
|
1248
|
178 |
(Cons fromArray:#( )) nth:1 -> error
|
1043
|
179 |
"
|
1237
|
180 |
!
|
|
181 |
|
1623
|
182 |
rest
|
|
183 |
"return the head, first or car - whatever you wonna call it"
|
|
184 |
|
|
185 |
^ self cdr
|
|
186 |
!
|
|
187 |
|
1243
|
188 |
reversed
|
|
189 |
"for lispers:
|
|
190 |
return a new list with the cars in reverse order"
|
|
191 |
|
|
192 |
"/ for now, tail recursion is not yet optimized by the st/x jitter...
|
|
193 |
"/
|
|
194 |
"/ |rev|
|
|
195 |
"/
|
|
196 |
"/ rev := [:lst :acc |
|
|
197 |
"/ lst isNil ifTrue:[
|
|
198 |
"/ acc
|
|
199 |
"/ ] ifFalse:[
|
|
200 |
"/ rev value:(lst tail)
|
|
201 |
"/ value:(Cons car:(lst head) cdr:acc)
|
|
202 |
"/ ]
|
|
203 |
"/ ].
|
|
204 |
"/ ^ rev value:self value:nil
|
|
205 |
|
|
206 |
| lst acc|
|
|
207 |
|
|
208 |
lst := self.
|
|
209 |
acc := nil.
|
|
210 |
|
|
211 |
[
|
|
212 |
|nLst nAcc|
|
|
213 |
|
|
214 |
lst isNil ifTrue:[ ^ acc].
|
|
215 |
|
|
216 |
nLst := lst tail.
|
|
217 |
nAcc := Cons car:(lst head) cdr:acc.
|
|
218 |
lst := nLst.
|
|
219 |
acc := nAcc.
|
|
220 |
] loop
|
|
221 |
|
|
222 |
"
|
|
223 |
(Cons fromArray:#(1)) reversed
|
|
224 |
(Cons fromArray:#(1 2)) reversed
|
|
225 |
(Cons fromArray:#(1 2 3 4)) reversed
|
|
226 |
(Cons fromArray:(1 to:10000)) reversed
|
|
227 |
"
|
|
228 |
!
|
|
229 |
|
1237
|
230 |
tail
|
|
231 |
"return the tail, second or cdr - whatever you wonna call it"
|
|
232 |
|
|
233 |
^ self cdr
|
1043
|
234 |
! !
|
|
235 |
|
|
236 |
!Cons methodsFor:'accessing - basic'!
|
|
237 |
|
|
238 |
car
|
1074
|
239 |
"return the head, first or car - whatever you wonna call it"
|
1043
|
240 |
|
|
241 |
^ car
|
|
242 |
!
|
|
243 |
|
|
244 |
car:something
|
1074
|
245 |
"set the head, first or car - whatever you wonna call it"
|
1043
|
246 |
|
|
247 |
car := something.
|
|
248 |
!
|
|
249 |
|
|
250 |
car:carArg cdr:cdrArg
|
1074
|
251 |
"set both car and cdr"
|
1043
|
252 |
|
|
253 |
car := carArg.
|
|
254 |
cdr := cdrArg.
|
|
255 |
!
|
|
256 |
|
|
257 |
cdr
|
1074
|
258 |
"return the tail, second or cdr - whatever you wonna call it"
|
1043
|
259 |
|
|
260 |
^ cdr
|
|
261 |
!
|
|
262 |
|
|
263 |
cdr:something
|
1074
|
264 |
"set the tail, second or cdr - whatever you wonna call it"
|
1043
|
265 |
|
|
266 |
cdr := something.
|
1623
|
267 |
!
|
|
268 |
|
|
269 |
first:carArg rest:cdrArg
|
|
270 |
"set both car and cdr"
|
|
271 |
|
|
272 |
car := carArg.
|
|
273 |
cdr := cdrArg.
|
|
274 |
!
|
|
275 |
|
|
276 |
head:carArg tail:cdrArg
|
|
277 |
"set both car and cdr"
|
|
278 |
|
|
279 |
car := carArg.
|
|
280 |
cdr := cdrArg.
|
1627
|
281 |
!
|
|
282 |
|
|
283 |
nthPair:n
|
|
284 |
"a helper:
|
|
285 |
return the nth pair of a list"
|
|
286 |
|
|
287 |
|cnt p|
|
|
288 |
|
|
289 |
cnt := n.
|
|
290 |
p := self.
|
|
291 |
[
|
|
292 |
cnt := cnt - 1.
|
|
293 |
cnt == 0 ifTrue:[^ p].
|
|
294 |
p := p cdr.
|
|
295 |
p isNil ifTrue:[
|
|
296 |
self error:'no such element' mayProceed:true.
|
|
297 |
^ nil
|
|
298 |
].
|
|
299 |
] loop.
|
|
300 |
! !
|
|
301 |
|
|
302 |
!Cons methodsFor:'enumerating'!
|
|
303 |
|
|
304 |
do:aBlock
|
|
305 |
|ptr|
|
|
306 |
|
|
307 |
aBlock value:car.
|
|
308 |
ptr := cdr.
|
|
309 |
[ ptr notNil ] whileTrue:[
|
|
310 |
aBlock value:ptr car.
|
|
311 |
ptr := ptr cdr.
|
|
312 |
].
|
1043
|
313 |
! !
|
|
314 |
|
|
315 |
!Cons methodsFor:'list processing'!
|
|
316 |
|
|
317 |
append:aCons
|
1074
|
318 |
"for lispers:
|
1248
|
319 |
append the arg. Return a new list, where the 2nd part is shared.
|
|
320 |
Destructive: the receivers last cdr is modified."
|
1074
|
321 |
|
1043
|
322 |
|p rest|
|
|
323 |
|
|
324 |
p := self.
|
|
325 |
[(rest := p cdr) notNil] whileTrue:[
|
|
326 |
p := rest
|
|
327 |
].
|
|
328 |
p cdr:aCons.
|
|
329 |
^ self
|
|
330 |
|
|
331 |
"
|
1074
|
332 |
(Cons fromArray:#(1 2 3 4))
|
|
333 |
append:(Cons fromArray:#(5 6 7 8))
|
|
334 |
"
|
|
335 |
|
|
336 |
"sharing demonstrated:
|
|
337 |
|
|
338 |
|a b ab|
|
|
339 |
|
|
340 |
a := Cons fromArray:#(1 2 3 4).
|
|
341 |
b := Cons fromArray:#(5 6 7 8).
|
|
342 |
ab := a append:b.
|
|
343 |
b car:'five'.
|
1248
|
344 |
ab
|
|
345 |
"
|
|
346 |
|
|
347 |
"destruction demonstrated:
|
|
348 |
|
|
349 |
|a b ab|
|
|
350 |
|
|
351 |
a := Cons fromArray:#(1 2 3 4).
|
|
352 |
b := Cons fromArray:#(5 6 7 8).
|
|
353 |
ab := a append:b.
|
|
354 |
a
|
1043
|
355 |
"
|
1237
|
356 |
!
|
|
357 |
|
1248
|
358 |
take:nTaken
|
1237
|
359 |
"for lispers:
|
|
360 |
take n elements from the list; return a new list"
|
|
361 |
|
1248
|
362 |
|nRemain l rslt lastCons cons|
|
|
363 |
|
|
364 |
nTaken > 0 ifTrue:[
|
|
365 |
"/ avoiding recursion here...
|
|
366 |
"/ instead of:
|
|
367 |
"/ ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
|
|
368 |
"/ we do:
|
|
369 |
nRemain := nTaken.
|
|
370 |
l := self.
|
|
371 |
rslt := lastCons := Cons car:(l car) cdr:nil.
|
|
372 |
[nRemain > 1] whileTrue:[
|
|
373 |
l := l cdr.
|
|
374 |
cons := Cons car:(l car) cdr:nil.
|
|
375 |
lastCons cdr:cons.
|
|
376 |
lastCons := cons.
|
|
377 |
nRemain := nRemain - 1.
|
|
378 |
].
|
|
379 |
^ rslt.
|
1237
|
380 |
].
|
|
381 |
^ nil
|
|
382 |
|
|
383 |
"
|
|
384 |
(Cons fromArray:#(1 2 3 4)) take:3
|
|
385 |
(Cons fromArray:#(1)) take:0
|
|
386 |
(Cons fromArray:#()) take:3
|
1248
|
387 |
(Cons fromArray:(1 to: 1000)) take:999
|
1237
|
388 |
"
|
1043
|
389 |
! !
|
|
390 |
|
|
391 |
!Cons methodsFor:'printing'!
|
|
392 |
|
|
393 |
displayString
|
|
394 |
^ self printString
|
|
395 |
!
|
|
396 |
|
|
397 |
printOn:aStream
|
|
398 |
(car isLazyValue not and:[ car isCons ]) ifTrue:[
|
|
399 |
aStream nextPutAll:'('.
|
|
400 |
car printOn:aStream.
|
|
401 |
aStream nextPutAll:')'.
|
|
402 |
] ifFalse:[
|
|
403 |
car printOn:aStream.
|
|
404 |
].
|
|
405 |
|
1073
|
406 |
aStream nextPutAll:'!!'.
|
1043
|
407 |
|
|
408 |
(cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
|
|
409 |
aStream nextPutAll:'('.
|
|
410 |
cdr printOn:aStream.
|
|
411 |
aStream nextPutAll:')'.
|
|
412 |
] ifFalse:[
|
|
413 |
cdr printOn:aStream.
|
|
414 |
].
|
|
415 |
! !
|
|
416 |
|
|
417 |
!Cons methodsFor:'queries'!
|
|
418 |
|
|
419 |
isCons
|
|
420 |
^ true
|
|
421 |
!
|
|
422 |
|
|
423 |
size
|
1074
|
424 |
"for smalltalkers: the lists length"
|
|
425 |
|
1043
|
426 |
|l p rest|
|
|
427 |
|
|
428 |
l := 1.
|
|
429 |
p := self.
|
|
430 |
[(rest := p cdr) notNil] whileTrue:[
|
|
431 |
l := l + 1.
|
|
432 |
p := rest
|
|
433 |
].
|
|
434 |
^ l
|
|
435 |
|
|
436 |
"
|
1074
|
437 |
(Cons fromArray:#( )) size
|
|
438 |
(Cons fromArray:#(1)) size
|
|
439 |
(Cons fromArray:#(1 2 3 4)) size
|
1043
|
440 |
"
|
|
441 |
! !
|
|
442 |
|
1377
|
443 |
!Cons methodsFor:'streaming'!
|
|
444 |
|
|
445 |
readStream
|
|
446 |
^ ConsStream on:self.
|
|
447 |
! !
|
|
448 |
|
1043
|
449 |
!Cons class methodsFor:'documentation'!
|
|
450 |
|
|
451 |
version
|
1627
|
452 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.9 2006-04-13 15:21:03 cg Exp $'
|
1043
|
453 |
! !
|