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 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
|
14 |
SequenceableCollection subclass:#Cons
|
|
15 |
instanceVariableNames:'car cdr'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Collections-Linked'
|
|
19 |
!
|
|
20 |
|
|
21 |
!Cons class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
|
39 |
A pair as in lisp.
|
|
40 |
Create with:
|
1074
|
41 |
a !! b
|
|
42 |
or:
|
|
43 |
Cons car:a cdr:b
|
|
44 |
|
|
45 |
Conses are not heavily used by Smalltalk (actually: not at all).
|
1377
|
46 |
Consider this a demo class.
|
1043
|
47 |
|
|
48 |
[author:]
|
|
49 |
Claus Gittinger
|
|
50 |
|
|
51 |
[see also:]
|
|
52 |
|
|
53 |
"
|
1627
|
54 |
!
|
|
55 |
|
|
56 |
examples
|
|
57 |
"
|
|
58 |
[exBegin]
|
|
59 |
|p1 p2 p3|
|
|
60 |
|
|
61 |
p3 := Cons car:3 cdr:nil.
|
|
62 |
p2 := Cons car:2 cdr:p3.
|
|
63 |
p1 := Cons car:1 cdr:p2.
|
|
64 |
p1 head.
|
|
65 |
p1 tail.
|
|
66 |
p1 size.
|
|
67 |
p1 do:[:each | Transcript showCR:each].
|
|
68 |
p1 at:2
|
|
69 |
[exEnd]
|
|
70 |
"
|
1043
|
71 |
! !
|
|
72 |
|
|
73 |
!Cons class methodsFor:'instance creation'!
|
|
74 |
|
|
75 |
car:carArg cdr:cdrArg
|
|
76 |
^ self basicNew car:carArg cdr:cdrArg
|
|
77 |
!
|
|
78 |
|
|
79 |
fromArray:anArray
|
|
80 |
|p last first|
|
|
81 |
|
|
82 |
anArray do:[:el |
|
|
83 |
p := self car:el cdr:nil.
|
|
84 |
first isNil ifTrue:[
|
|
85 |
first := p.
|
|
86 |
] ifFalse:[
|
|
87 |
last cdr:p.
|
|
88 |
].
|
|
89 |
last := p.
|
|
90 |
].
|
|
91 |
^ first.
|
|
92 |
|
|
93 |
"
|
1074
|
94 |
Cons fromArray:#(1 2 3 4)
|
|
95 |
Cons fromArray:#()
|
|
96 |
Cons fromArray:#(1)
|
1248
|
97 |
Cons fromArray:(1 to:10000)
|
1043
|
98 |
"
|
|
99 |
! !
|
|
100 |
|
2469
|
101 |
!Cons class methodsFor:'sExpressions'!
|
|
102 |
|
|
103 |
readLispFrom:aStream
|
|
104 |
aStream skipSeparators.
|
2470
|
105 |
aStream atEnd ifTrue:[^ nil].
|
|
106 |
|
2469
|
107 |
aStream peek ==$; ifTrue:[
|
|
108 |
"/ comment
|
|
109 |
aStream skipLine.
|
|
110 |
thisContext restart
|
|
111 |
].
|
|
112 |
|
|
113 |
aStream peek ==$( ifTrue:[
|
|
114 |
^ self realLispListFrom:aStream
|
|
115 |
].
|
|
116 |
^ self realLispAtomFrom:aStream
|
|
117 |
|
|
118 |
"
|
|
119 |
self readLispFrom:('(cons 1 2)' readStream).
|
|
120 |
"
|
|
121 |
|
|
122 |
"Created: / 08-08-2010 / 17:07:49 / cg"
|
|
123 |
!
|
|
124 |
|
|
125 |
realLispAtomFrom:aStream
|
|
126 |
|atom|
|
|
127 |
|
|
128 |
aStream skipSeparators.
|
2470
|
129 |
aStream atEnd ifTrue:[^ nil].
|
|
130 |
|
2469
|
131 |
aStream peek isDigit ifTrue:[
|
|
132 |
^ Number readFrom:aStream
|
|
133 |
].
|
|
134 |
atom := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]].
|
|
135 |
^ atom asSymbol
|
|
136 |
|
|
137 |
"
|
|
138 |
self readLispFrom:('(cons 1 2)' readStream).
|
|
139 |
"
|
|
140 |
|
|
141 |
"Created: / 08-08-2010 / 17:15:18 / cg"
|
|
142 |
!
|
|
143 |
|
|
144 |
realLispListFrom:aStream
|
|
145 |
|first this prev element|
|
|
146 |
|
|
147 |
aStream next. "/ the leading '('
|
|
148 |
[
|
|
149 |
aStream skipSeparators.
|
|
150 |
aStream peek ~= $)
|
|
151 |
] whileTrue:[
|
|
152 |
element := self readLispFrom:aStream.
|
|
153 |
this := Cons car:element cdr:nil.
|
|
154 |
prev isNil ifTrue:[
|
|
155 |
first := this
|
|
156 |
] ifFalse:[
|
|
157 |
prev cdr:this.
|
|
158 |
].
|
|
159 |
prev := this.
|
|
160 |
].
|
|
161 |
|
|
162 |
aStream next. "/ the trailing ')'
|
|
163 |
^ first.
|
|
164 |
|
|
165 |
"
|
|
166 |
self readLispFrom:('(cons 1 2)' readStream).
|
|
167 |
"
|
|
168 |
|
|
169 |
"Modified: / 08-08-2010 / 17:15:54 / cg"
|
|
170 |
! !
|
|
171 |
|
1043
|
172 |
!Cons methodsFor:'accessing'!
|
|
173 |
|
|
174 |
at:n
|
1074
|
175 |
"for collection compatibility:
|
|
176 |
a slow indexed accessor"
|
|
177 |
|
1627
|
178 |
^ (self nth:n)
|
1043
|
179 |
|
|
180 |
"
|
1074
|
181 |
(Cons fromArray:#(1)) at:1
|
|
182 |
(Cons fromArray:#(1 2 3 4)) at:1
|
|
183 |
(Cons fromArray:#(1 2 3 4)) at:3
|
|
184 |
(Cons fromArray:#(1 2 3 4)) at:4
|
|
185 |
(Cons fromArray:#(1 2 3 4)) at:5
|
1043
|
186 |
"
|
|
187 |
!
|
|
188 |
|
|
189 |
at:n put:newValue
|
1627
|
190 |
"destructive:
|
|
191 |
for collection compatibility: a slow indexed accessor"
|
1074
|
192 |
|
1627
|
193 |
(self nthPair:n) car:newValue.
|
1043
|
194 |
^ newValue.
|
|
195 |
|
|
196 |
"
|
|
197 |
|l|
|
|
198 |
|
1074
|
199 |
l := Cons fromArray:#(1 2 3 4).
|
1043
|
200 |
l at:1 put:'one'.
|
|
201 |
l at:3 put:'three'.
|
|
202 |
l
|
|
203 |
"
|
|
204 |
!
|
|
205 |
|
1623
|
206 |
first
|
|
207 |
"return the head, first or car - whatever you wonna call it"
|
|
208 |
|
2469
|
209 |
^ car
|
|
210 |
|
|
211 |
"Modified: / 08-08-2010 / 17:04:23 / cg"
|
1623
|
212 |
!
|
|
213 |
|
1237
|
214 |
head
|
|
215 |
"return the head, first or car - whatever you wonna call it"
|
|
216 |
|
2469
|
217 |
^ car
|
|
218 |
|
|
219 |
"Modified: / 08-08-2010 / 17:04:20 / cg"
|
1237
|
220 |
!
|
|
221 |
|
1043
|
222 |
last
|
1074
|
223 |
"for lispers:
|
|
224 |
return the last element of a list"
|
|
225 |
|
1043
|
226 |
|p rest|
|
|
227 |
|
|
228 |
p := self.
|
|
229 |
[(rest := p cdr) notNil] whileTrue:[
|
|
230 |
p := rest
|
|
231 |
].
|
1237
|
232 |
^ p car
|
1043
|
233 |
|
|
234 |
"
|
1074
|
235 |
(Cons fromArray:#(1)) last
|
|
236 |
(Cons fromArray:#(1 2 3 4)) last
|
1043
|
237 |
"
|
|
238 |
!
|
|
239 |
|
|
240 |
nth:n
|
1074
|
241 |
"for lispers:
|
|
242 |
return the nth element of a list"
|
|
243 |
|
1627
|
244 |
^ (self nthPair:n) car
|
1043
|
245 |
|
|
246 |
"
|
1074
|
247 |
(Cons fromArray:#(1)) nth:1
|
|
248 |
(Cons fromArray:#(1 2 3 4)) nth:1
|
|
249 |
(Cons fromArray:#(1 2 3 4)) nth:3
|
|
250 |
(Cons fromArray:#(1 2 3 4)) nth:4
|
|
251 |
(Cons fromArray:#(1 2 3 4)) nth:5
|
1248
|
252 |
(Cons fromArray:#( )) nth:1 -> error
|
1043
|
253 |
"
|
1237
|
254 |
!
|
|
255 |
|
1623
|
256 |
rest
|
2469
|
257 |
"return the tail, rest or cdr - whatever you wonna call it"
|
1623
|
258 |
|
2469
|
259 |
^ cdr
|
|
260 |
|
|
261 |
"Modified: / 08-08-2010 / 17:04:48 / cg"
|
1623
|
262 |
!
|
|
263 |
|
1243
|
264 |
reversed
|
|
265 |
"for lispers:
|
|
266 |
return a new list with the cars in reverse order"
|
|
267 |
|
|
268 |
"/ for now, tail recursion is not yet optimized by the st/x jitter...
|
|
269 |
"/
|
|
270 |
"/ |rev|
|
|
271 |
"/
|
|
272 |
"/ rev := [:lst :acc |
|
|
273 |
"/ lst isNil ifTrue:[
|
|
274 |
"/ acc
|
|
275 |
"/ ] ifFalse:[
|
|
276 |
"/ rev value:(lst tail)
|
|
277 |
"/ value:(Cons car:(lst head) cdr:acc)
|
|
278 |
"/ ]
|
|
279 |
"/ ].
|
|
280 |
"/ ^ rev value:self value:nil
|
|
281 |
|
|
282 |
| lst acc|
|
|
283 |
|
|
284 |
lst := self.
|
|
285 |
acc := nil.
|
|
286 |
|
|
287 |
[
|
|
288 |
|nLst nAcc|
|
|
289 |
|
|
290 |
lst isNil ifTrue:[ ^ acc].
|
|
291 |
|
|
292 |
nLst := lst tail.
|
|
293 |
nAcc := Cons car:(lst head) cdr:acc.
|
|
294 |
lst := nLst.
|
|
295 |
acc := nAcc.
|
|
296 |
] loop
|
|
297 |
|
|
298 |
"
|
|
299 |
(Cons fromArray:#(1)) reversed
|
|
300 |
(Cons fromArray:#(1 2)) reversed
|
|
301 |
(Cons fromArray:#(1 2 3 4)) reversed
|
|
302 |
(Cons fromArray:(1 to:10000)) reversed
|
|
303 |
"
|
|
304 |
!
|
|
305 |
|
1237
|
306 |
tail
|
2469
|
307 |
"return the tail, rest or cdr - whatever you wonna call it"
|
1237
|
308 |
|
2469
|
309 |
^ cdr
|
|
310 |
|
|
311 |
"Modified: / 08-08-2010 / 17:04:59 / cg"
|
1043
|
312 |
! !
|
|
313 |
|
|
314 |
!Cons methodsFor:'accessing - basic'!
|
|
315 |
|
2470
|
316 |
cadddr
|
|
317 |
"return the fourth element"
|
|
318 |
|
|
319 |
^ cdr cdr cdr car
|
|
320 |
|
|
321 |
"Created: / 08-08-2010 / 17:29:40 / cg"
|
|
322 |
!
|
|
323 |
|
|
324 |
caddr
|
|
325 |
"return the third element"
|
|
326 |
|
|
327 |
^ cdr cdr car
|
|
328 |
|
|
329 |
"Created: / 08-08-2010 / 17:29:32 / cg"
|
|
330 |
!
|
|
331 |
|
|
332 |
cadr
|
|
333 |
"return the second element"
|
|
334 |
|
|
335 |
^ cdr car
|
|
336 |
|
|
337 |
"Created: / 08-08-2010 / 17:29:25 / cg"
|
|
338 |
!
|
|
339 |
|
1043
|
340 |
car
|
1074
|
341 |
"return the head, first or car - whatever you wonna call it"
|
1043
|
342 |
|
|
343 |
^ car
|
|
344 |
!
|
|
345 |
|
|
346 |
car:something
|
1074
|
347 |
"set the head, first or car - whatever you wonna call it"
|
1043
|
348 |
|
|
349 |
car := something.
|
|
350 |
!
|
|
351 |
|
|
352 |
car:carArg cdr:cdrArg
|
1074
|
353 |
"set both car and cdr"
|
1043
|
354 |
|
|
355 |
car := carArg.
|
|
356 |
cdr := cdrArg.
|
|
357 |
!
|
|
358 |
|
2472
|
359 |
cddr
|
|
360 |
"return the rest after the second element"
|
|
361 |
|
|
362 |
^ cdr cdr
|
|
363 |
|
|
364 |
"Created: / 08-08-2010 / 17:47:11 / cg"
|
|
365 |
!
|
|
366 |
|
1043
|
367 |
cdr
|
1074
|
368 |
"return the tail, second or cdr - whatever you wonna call it"
|
1043
|
369 |
|
|
370 |
^ cdr
|
|
371 |
!
|
|
372 |
|
|
373 |
cdr:something
|
1074
|
374 |
"set the tail, second or cdr - whatever you wonna call it"
|
1043
|
375 |
|
|
376 |
cdr := something.
|
1623
|
377 |
!
|
|
378 |
|
|
379 |
first:carArg rest:cdrArg
|
|
380 |
"set both car and cdr"
|
|
381 |
|
|
382 |
car := carArg.
|
|
383 |
cdr := cdrArg.
|
|
384 |
!
|
|
385 |
|
|
386 |
head:carArg tail:cdrArg
|
|
387 |
"set both car and cdr"
|
|
388 |
|
|
389 |
car := carArg.
|
|
390 |
cdr := cdrArg.
|
1627
|
391 |
!
|
|
392 |
|
|
393 |
nthPair:n
|
|
394 |
"a helper:
|
|
395 |
return the nth pair of a list"
|
|
396 |
|
|
397 |
|cnt p|
|
|
398 |
|
|
399 |
cnt := n.
|
|
400 |
p := self.
|
|
401 |
[
|
|
402 |
cnt := cnt - 1.
|
|
403 |
cnt == 0 ifTrue:[^ p].
|
|
404 |
p := p cdr.
|
|
405 |
p isNil ifTrue:[
|
|
406 |
self error:'no such element' mayProceed:true.
|
|
407 |
^ nil
|
|
408 |
].
|
|
409 |
] loop.
|
|
410 |
! !
|
|
411 |
|
2437
|
412 |
!Cons methodsFor:'comparing'!
|
|
413 |
|
|
414 |
= aCons
|
|
415 |
^ aCons class == self class
|
|
416 |
and:[ car = aCons car
|
|
417 |
and:[ cdr = aCons cdr ]]
|
|
418 |
!
|
|
419 |
|
|
420 |
hash
|
|
421 |
^ car hash bitXor: cdr hash
|
|
422 |
! !
|
|
423 |
|
1627
|
424 |
!Cons methodsFor:'enumerating'!
|
|
425 |
|
|
426 |
do:aBlock
|
2471
|
427 |
"evaluate aBlock for each car"
|
|
428 |
|
1627
|
429 |
|ptr|
|
|
430 |
|
|
431 |
aBlock value:car.
|
|
432 |
ptr := cdr.
|
|
433 |
[ ptr notNil ] whileTrue:[
|
|
434 |
aBlock value:ptr car.
|
|
435 |
ptr := ptr cdr.
|
|
436 |
].
|
2471
|
437 |
|
|
438 |
"Modified: / 08-08-2010 / 17:33:44 / cg"
|
1043
|
439 |
! !
|
|
440 |
|
|
441 |
!Cons methodsFor:'list processing'!
|
|
442 |
|
|
443 |
append:aCons
|
1074
|
444 |
"for lispers:
|
1248
|
445 |
append the arg. Return a new list, where the 2nd part is shared.
|
|
446 |
Destructive: the receivers last cdr is modified."
|
1074
|
447 |
|
1043
|
448 |
|p rest|
|
|
449 |
|
|
450 |
p := self.
|
|
451 |
[(rest := p cdr) notNil] whileTrue:[
|
|
452 |
p := rest
|
|
453 |
].
|
|
454 |
p cdr:aCons.
|
|
455 |
^ self
|
|
456 |
|
|
457 |
"
|
1074
|
458 |
(Cons fromArray:#(1 2 3 4))
|
|
459 |
append:(Cons fromArray:#(5 6 7 8))
|
|
460 |
"
|
|
461 |
|
|
462 |
"sharing demonstrated:
|
|
463 |
|
|
464 |
|a b ab|
|
|
465 |
|
|
466 |
a := Cons fromArray:#(1 2 3 4).
|
|
467 |
b := Cons fromArray:#(5 6 7 8).
|
|
468 |
ab := a append:b.
|
|
469 |
b car:'five'.
|
1248
|
470 |
ab
|
|
471 |
"
|
|
472 |
|
|
473 |
"destruction demonstrated:
|
|
474 |
|
|
475 |
|a b ab|
|
|
476 |
|
|
477 |
a := Cons fromArray:#(1 2 3 4).
|
|
478 |
b := Cons fromArray:#(5 6 7 8).
|
|
479 |
ab := a append:b.
|
|
480 |
a
|
1043
|
481 |
"
|
1237
|
482 |
!
|
|
483 |
|
1248
|
484 |
take:nTaken
|
1237
|
485 |
"for lispers:
|
|
486 |
take n elements from the list; return a new list"
|
|
487 |
|
1248
|
488 |
|nRemain l rslt lastCons cons|
|
|
489 |
|
|
490 |
nTaken > 0 ifTrue:[
|
|
491 |
"/ avoiding recursion here...
|
|
492 |
"/ instead of:
|
|
493 |
"/ ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
|
|
494 |
"/ we do:
|
|
495 |
nRemain := nTaken.
|
|
496 |
l := self.
|
|
497 |
rslt := lastCons := Cons car:(l car) cdr:nil.
|
|
498 |
[nRemain > 1] whileTrue:[
|
|
499 |
l := l cdr.
|
|
500 |
cons := Cons car:(l car) cdr:nil.
|
|
501 |
lastCons cdr:cons.
|
|
502 |
lastCons := cons.
|
|
503 |
nRemain := nRemain - 1.
|
|
504 |
].
|
|
505 |
^ rslt.
|
1237
|
506 |
].
|
|
507 |
^ nil
|
|
508 |
|
|
509 |
"
|
|
510 |
(Cons fromArray:#(1 2 3 4)) take:3
|
|
511 |
(Cons fromArray:#(1)) take:0
|
|
512 |
(Cons fromArray:#()) take:3
|
1248
|
513 |
(Cons fromArray:(1 to: 1000)) take:999
|
1237
|
514 |
"
|
1043
|
515 |
! !
|
|
516 |
|
|
517 |
!Cons methodsFor:'printing'!
|
|
518 |
|
|
519 |
displayString
|
|
520 |
^ self printString
|
|
521 |
!
|
|
522 |
|
|
523 |
printOn:aStream
|
2448
|
524 |
thisContext isRecursive ifTrue:[
|
|
525 |
'Cons [error]: printOn: of self referencing collection.' errorPrintCR.
|
|
526 |
aStream nextPutAll:'#("recursive")'.
|
|
527 |
^ self.
|
|
528 |
].
|
|
529 |
|
1043
|
530 |
(car isLazyValue not and:[ car isCons ]) ifTrue:[
|
|
531 |
aStream nextPutAll:'('.
|
|
532 |
car printOn:aStream.
|
|
533 |
aStream nextPutAll:')'.
|
|
534 |
] ifFalse:[
|
|
535 |
car printOn:aStream.
|
|
536 |
].
|
|
537 |
|
1073
|
538 |
aStream nextPutAll:'!!'.
|
1043
|
539 |
|
|
540 |
(cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
|
|
541 |
aStream nextPutAll:'('.
|
|
542 |
cdr printOn:aStream.
|
|
543 |
aStream nextPutAll:')'.
|
|
544 |
] ifFalse:[
|
|
545 |
cdr printOn:aStream.
|
|
546 |
].
|
2448
|
547 |
|
|
548 |
"Modified: / 18-05-2010 / 10:25:49 / cg"
|
1043
|
549 |
! !
|
|
550 |
|
|
551 |
!Cons methodsFor:'queries'!
|
|
552 |
|
|
553 |
isCons
|
|
554 |
^ true
|
|
555 |
!
|
|
556 |
|
|
557 |
size
|
1074
|
558 |
"for smalltalkers: the lists length"
|
|
559 |
|
1043
|
560 |
|l p rest|
|
|
561 |
|
|
562 |
l := 1.
|
|
563 |
p := self.
|
|
564 |
[(rest := p cdr) notNil] whileTrue:[
|
|
565 |
l := l + 1.
|
|
566 |
p := rest
|
|
567 |
].
|
|
568 |
^ l
|
|
569 |
|
|
570 |
"
|
1074
|
571 |
(Cons fromArray:#( )) size
|
|
572 |
(Cons fromArray:#(1)) size
|
|
573 |
(Cons fromArray:#(1 2 3 4)) size
|
1043
|
574 |
"
|
|
575 |
! !
|
|
576 |
|
1377
|
577 |
!Cons methodsFor:'streaming'!
|
|
578 |
|
|
579 |
readStream
|
|
580 |
^ ConsStream on:self.
|
|
581 |
! !
|
|
582 |
|
1043
|
583 |
!Cons class methodsFor:'documentation'!
|
|
584 |
|
|
585 |
version
|
2472
|
586 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.15 2010-08-08 18:59:46 cg Exp $'
|
2437
|
587 |
!
|
|
588 |
|
|
589 |
version_CVS
|
2472
|
590 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.15 2010-08-08 18:59:46 cg Exp $'
|
1043
|
591 |
! !
|