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.
|
|
105 |
aStream peek ==$; ifTrue:[
|
|
106 |
"/ comment
|
|
107 |
aStream skipLine.
|
|
108 |
thisContext restart
|
|
109 |
].
|
|
110 |
|
|
111 |
aStream peek ==$( ifTrue:[
|
|
112 |
^ self realLispListFrom:aStream
|
|
113 |
].
|
|
114 |
^ self realLispAtomFrom:aStream
|
|
115 |
|
|
116 |
"
|
|
117 |
self readLispFrom:('(cons 1 2)' readStream).
|
|
118 |
"
|
|
119 |
|
|
120 |
"Created: / 08-08-2010 / 17:07:49 / cg"
|
|
121 |
!
|
|
122 |
|
|
123 |
realLispAtomFrom:aStream
|
|
124 |
|atom|
|
|
125 |
|
|
126 |
aStream skipSeparators.
|
|
127 |
aStream peek isDigit ifTrue:[
|
|
128 |
^ Number readFrom:aStream
|
|
129 |
].
|
|
130 |
atom := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]].
|
|
131 |
^ atom asSymbol
|
|
132 |
|
|
133 |
"
|
|
134 |
self readLispFrom:('(cons 1 2)' readStream).
|
|
135 |
"
|
|
136 |
|
|
137 |
"Created: / 08-08-2010 / 17:15:18 / cg"
|
|
138 |
!
|
|
139 |
|
|
140 |
realLispListFrom:aStream
|
|
141 |
|first this prev element|
|
|
142 |
|
|
143 |
aStream next. "/ the leading '('
|
|
144 |
[
|
|
145 |
aStream skipSeparators.
|
|
146 |
aStream peek ~= $)
|
|
147 |
] whileTrue:[
|
|
148 |
element := self readLispFrom:aStream.
|
|
149 |
this := Cons car:element cdr:nil.
|
|
150 |
prev isNil ifTrue:[
|
|
151 |
first := this
|
|
152 |
] ifFalse:[
|
|
153 |
prev cdr:this.
|
|
154 |
].
|
|
155 |
prev := this.
|
|
156 |
].
|
|
157 |
|
|
158 |
aStream next. "/ the trailing ')'
|
|
159 |
^ first.
|
|
160 |
|
|
161 |
"
|
|
162 |
self readLispFrom:('(cons 1 2)' readStream).
|
|
163 |
"
|
|
164 |
|
|
165 |
"Modified: / 08-08-2010 / 17:15:54 / cg"
|
|
166 |
! !
|
|
167 |
|
1043
|
168 |
!Cons methodsFor:'accessing'!
|
|
169 |
|
|
170 |
at:n
|
1074
|
171 |
"for collection compatibility:
|
|
172 |
a slow indexed accessor"
|
|
173 |
|
1627
|
174 |
^ (self nth:n)
|
1043
|
175 |
|
|
176 |
"
|
1074
|
177 |
(Cons fromArray:#(1)) at:1
|
|
178 |
(Cons fromArray:#(1 2 3 4)) at:1
|
|
179 |
(Cons fromArray:#(1 2 3 4)) at:3
|
|
180 |
(Cons fromArray:#(1 2 3 4)) at:4
|
|
181 |
(Cons fromArray:#(1 2 3 4)) at:5
|
1043
|
182 |
"
|
|
183 |
!
|
|
184 |
|
|
185 |
at:n put:newValue
|
1627
|
186 |
"destructive:
|
|
187 |
for collection compatibility: a slow indexed accessor"
|
1074
|
188 |
|
1627
|
189 |
(self nthPair:n) car:newValue.
|
1043
|
190 |
^ newValue.
|
|
191 |
|
|
192 |
"
|
|
193 |
|l|
|
|
194 |
|
1074
|
195 |
l := Cons fromArray:#(1 2 3 4).
|
1043
|
196 |
l at:1 put:'one'.
|
|
197 |
l at:3 put:'three'.
|
|
198 |
l
|
|
199 |
"
|
|
200 |
!
|
|
201 |
|
1623
|
202 |
first
|
|
203 |
"return the head, first or car - whatever you wonna call it"
|
|
204 |
|
2469
|
205 |
^ car
|
|
206 |
|
|
207 |
"Modified: / 08-08-2010 / 17:04:23 / cg"
|
1623
|
208 |
!
|
|
209 |
|
1237
|
210 |
head
|
|
211 |
"return the head, first or car - whatever you wonna call it"
|
|
212 |
|
2469
|
213 |
^ car
|
|
214 |
|
|
215 |
"Modified: / 08-08-2010 / 17:04:20 / cg"
|
1237
|
216 |
!
|
|
217 |
|
1043
|
218 |
last
|
1074
|
219 |
"for lispers:
|
|
220 |
return the last element of a list"
|
|
221 |
|
1043
|
222 |
|p rest|
|
|
223 |
|
|
224 |
p := self.
|
|
225 |
[(rest := p cdr) notNil] whileTrue:[
|
|
226 |
p := rest
|
|
227 |
].
|
1237
|
228 |
^ p car
|
1043
|
229 |
|
|
230 |
"
|
1074
|
231 |
(Cons fromArray:#(1)) last
|
|
232 |
(Cons fromArray:#(1 2 3 4)) last
|
1043
|
233 |
"
|
|
234 |
!
|
|
235 |
|
|
236 |
nth:n
|
1074
|
237 |
"for lispers:
|
|
238 |
return the nth element of a list"
|
|
239 |
|
1627
|
240 |
^ (self nthPair:n) car
|
1043
|
241 |
|
|
242 |
"
|
1074
|
243 |
(Cons fromArray:#(1)) nth:1
|
|
244 |
(Cons fromArray:#(1 2 3 4)) nth:1
|
|
245 |
(Cons fromArray:#(1 2 3 4)) nth:3
|
|
246 |
(Cons fromArray:#(1 2 3 4)) nth:4
|
|
247 |
(Cons fromArray:#(1 2 3 4)) nth:5
|
1248
|
248 |
(Cons fromArray:#( )) nth:1 -> error
|
1043
|
249 |
"
|
1237
|
250 |
!
|
|
251 |
|
1623
|
252 |
rest
|
2469
|
253 |
"return the tail, rest or cdr - whatever you wonna call it"
|
1623
|
254 |
|
2469
|
255 |
^ cdr
|
|
256 |
|
|
257 |
"Modified: / 08-08-2010 / 17:04:48 / cg"
|
1623
|
258 |
!
|
|
259 |
|
1243
|
260 |
reversed
|
|
261 |
"for lispers:
|
|
262 |
return a new list with the cars in reverse order"
|
|
263 |
|
|
264 |
"/ for now, tail recursion is not yet optimized by the st/x jitter...
|
|
265 |
"/
|
|
266 |
"/ |rev|
|
|
267 |
"/
|
|
268 |
"/ rev := [:lst :acc |
|
|
269 |
"/ lst isNil ifTrue:[
|
|
270 |
"/ acc
|
|
271 |
"/ ] ifFalse:[
|
|
272 |
"/ rev value:(lst tail)
|
|
273 |
"/ value:(Cons car:(lst head) cdr:acc)
|
|
274 |
"/ ]
|
|
275 |
"/ ].
|
|
276 |
"/ ^ rev value:self value:nil
|
|
277 |
|
|
278 |
| lst acc|
|
|
279 |
|
|
280 |
lst := self.
|
|
281 |
acc := nil.
|
|
282 |
|
|
283 |
[
|
|
284 |
|nLst nAcc|
|
|
285 |
|
|
286 |
lst isNil ifTrue:[ ^ acc].
|
|
287 |
|
|
288 |
nLst := lst tail.
|
|
289 |
nAcc := Cons car:(lst head) cdr:acc.
|
|
290 |
lst := nLst.
|
|
291 |
acc := nAcc.
|
|
292 |
] loop
|
|
293 |
|
|
294 |
"
|
|
295 |
(Cons fromArray:#(1)) reversed
|
|
296 |
(Cons fromArray:#(1 2)) reversed
|
|
297 |
(Cons fromArray:#(1 2 3 4)) reversed
|
|
298 |
(Cons fromArray:(1 to:10000)) reversed
|
|
299 |
"
|
|
300 |
!
|
|
301 |
|
1237
|
302 |
tail
|
2469
|
303 |
"return the tail, rest or cdr - whatever you wonna call it"
|
1237
|
304 |
|
2469
|
305 |
^ cdr
|
|
306 |
|
|
307 |
"Modified: / 08-08-2010 / 17:04:59 / cg"
|
1043
|
308 |
! !
|
|
309 |
|
|
310 |
!Cons methodsFor:'accessing - basic'!
|
|
311 |
|
|
312 |
car
|
1074
|
313 |
"return the head, first or car - whatever you wonna call it"
|
1043
|
314 |
|
|
315 |
^ car
|
|
316 |
!
|
|
317 |
|
|
318 |
car:something
|
1074
|
319 |
"set the head, first or car - whatever you wonna call it"
|
1043
|
320 |
|
|
321 |
car := something.
|
|
322 |
!
|
|
323 |
|
|
324 |
car:carArg cdr:cdrArg
|
1074
|
325 |
"set both car and cdr"
|
1043
|
326 |
|
|
327 |
car := carArg.
|
|
328 |
cdr := cdrArg.
|
|
329 |
!
|
|
330 |
|
|
331 |
cdr
|
1074
|
332 |
"return the tail, second or cdr - whatever you wonna call it"
|
1043
|
333 |
|
|
334 |
^ cdr
|
|
335 |
!
|
|
336 |
|
|
337 |
cdr:something
|
1074
|
338 |
"set the tail, second or cdr - whatever you wonna call it"
|
1043
|
339 |
|
|
340 |
cdr := something.
|
1623
|
341 |
!
|
|
342 |
|
|
343 |
first:carArg rest:cdrArg
|
|
344 |
"set both car and cdr"
|
|
345 |
|
|
346 |
car := carArg.
|
|
347 |
cdr := cdrArg.
|
|
348 |
!
|
|
349 |
|
|
350 |
head:carArg tail:cdrArg
|
|
351 |
"set both car and cdr"
|
|
352 |
|
|
353 |
car := carArg.
|
|
354 |
cdr := cdrArg.
|
1627
|
355 |
!
|
|
356 |
|
|
357 |
nthPair:n
|
|
358 |
"a helper:
|
|
359 |
return the nth pair of a list"
|
|
360 |
|
|
361 |
|cnt p|
|
|
362 |
|
|
363 |
cnt := n.
|
|
364 |
p := self.
|
|
365 |
[
|
|
366 |
cnt := cnt - 1.
|
|
367 |
cnt == 0 ifTrue:[^ p].
|
|
368 |
p := p cdr.
|
|
369 |
p isNil ifTrue:[
|
|
370 |
self error:'no such element' mayProceed:true.
|
|
371 |
^ nil
|
|
372 |
].
|
|
373 |
] loop.
|
|
374 |
! !
|
|
375 |
|
2437
|
376 |
!Cons methodsFor:'comparing'!
|
|
377 |
|
|
378 |
= aCons
|
|
379 |
^ aCons class == self class
|
|
380 |
and:[ car = aCons car
|
|
381 |
and:[ cdr = aCons cdr ]]
|
|
382 |
!
|
|
383 |
|
|
384 |
hash
|
|
385 |
^ car hash bitXor: cdr hash
|
|
386 |
! !
|
|
387 |
|
1627
|
388 |
!Cons methodsFor:'enumerating'!
|
|
389 |
|
|
390 |
do:aBlock
|
|
391 |
|ptr|
|
|
392 |
|
|
393 |
aBlock value:car.
|
|
394 |
ptr := cdr.
|
|
395 |
[ ptr notNil ] whileTrue:[
|
|
396 |
aBlock value:ptr car.
|
|
397 |
ptr := ptr cdr.
|
|
398 |
].
|
1043
|
399 |
! !
|
|
400 |
|
|
401 |
!Cons methodsFor:'list processing'!
|
|
402 |
|
|
403 |
append:aCons
|
1074
|
404 |
"for lispers:
|
1248
|
405 |
append the arg. Return a new list, where the 2nd part is shared.
|
|
406 |
Destructive: the receivers last cdr is modified."
|
1074
|
407 |
|
1043
|
408 |
|p rest|
|
|
409 |
|
|
410 |
p := self.
|
|
411 |
[(rest := p cdr) notNil] whileTrue:[
|
|
412 |
p := rest
|
|
413 |
].
|
|
414 |
p cdr:aCons.
|
|
415 |
^ self
|
|
416 |
|
|
417 |
"
|
1074
|
418 |
(Cons fromArray:#(1 2 3 4))
|
|
419 |
append:(Cons fromArray:#(5 6 7 8))
|
|
420 |
"
|
|
421 |
|
|
422 |
"sharing demonstrated:
|
|
423 |
|
|
424 |
|a b ab|
|
|
425 |
|
|
426 |
a := Cons fromArray:#(1 2 3 4).
|
|
427 |
b := Cons fromArray:#(5 6 7 8).
|
|
428 |
ab := a append:b.
|
|
429 |
b car:'five'.
|
1248
|
430 |
ab
|
|
431 |
"
|
|
432 |
|
|
433 |
"destruction demonstrated:
|
|
434 |
|
|
435 |
|a b ab|
|
|
436 |
|
|
437 |
a := Cons fromArray:#(1 2 3 4).
|
|
438 |
b := Cons fromArray:#(5 6 7 8).
|
|
439 |
ab := a append:b.
|
|
440 |
a
|
1043
|
441 |
"
|
1237
|
442 |
!
|
|
443 |
|
1248
|
444 |
take:nTaken
|
1237
|
445 |
"for lispers:
|
|
446 |
take n elements from the list; return a new list"
|
|
447 |
|
1248
|
448 |
|nRemain l rslt lastCons cons|
|
|
449 |
|
|
450 |
nTaken > 0 ifTrue:[
|
|
451 |
"/ avoiding recursion here...
|
|
452 |
"/ instead of:
|
|
453 |
"/ ^ Cons car:(self car) cdr:(self cdr take:nTaken-1)
|
|
454 |
"/ we do:
|
|
455 |
nRemain := nTaken.
|
|
456 |
l := self.
|
|
457 |
rslt := lastCons := Cons car:(l car) cdr:nil.
|
|
458 |
[nRemain > 1] whileTrue:[
|
|
459 |
l := l cdr.
|
|
460 |
cons := Cons car:(l car) cdr:nil.
|
|
461 |
lastCons cdr:cons.
|
|
462 |
lastCons := cons.
|
|
463 |
nRemain := nRemain - 1.
|
|
464 |
].
|
|
465 |
^ rslt.
|
1237
|
466 |
].
|
|
467 |
^ nil
|
|
468 |
|
|
469 |
"
|
|
470 |
(Cons fromArray:#(1 2 3 4)) take:3
|
|
471 |
(Cons fromArray:#(1)) take:0
|
|
472 |
(Cons fromArray:#()) take:3
|
1248
|
473 |
(Cons fromArray:(1 to: 1000)) take:999
|
1237
|
474 |
"
|
1043
|
475 |
! !
|
|
476 |
|
|
477 |
!Cons methodsFor:'printing'!
|
|
478 |
|
|
479 |
displayString
|
|
480 |
^ self printString
|
|
481 |
!
|
|
482 |
|
|
483 |
printOn:aStream
|
2448
|
484 |
thisContext isRecursive ifTrue:[
|
|
485 |
'Cons [error]: printOn: of self referencing collection.' errorPrintCR.
|
|
486 |
aStream nextPutAll:'#("recursive")'.
|
|
487 |
^ self.
|
|
488 |
].
|
|
489 |
|
1043
|
490 |
(car isLazyValue not and:[ car isCons ]) ifTrue:[
|
|
491 |
aStream nextPutAll:'('.
|
|
492 |
car printOn:aStream.
|
|
493 |
aStream nextPutAll:')'.
|
|
494 |
] ifFalse:[
|
|
495 |
car printOn:aStream.
|
|
496 |
].
|
|
497 |
|
1073
|
498 |
aStream nextPutAll:'!!'.
|
1043
|
499 |
|
|
500 |
(cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[
|
|
501 |
aStream nextPutAll:'('.
|
|
502 |
cdr printOn:aStream.
|
|
503 |
aStream nextPutAll:')'.
|
|
504 |
] ifFalse:[
|
|
505 |
cdr printOn:aStream.
|
|
506 |
].
|
2448
|
507 |
|
|
508 |
"Modified: / 18-05-2010 / 10:25:49 / cg"
|
1043
|
509 |
! !
|
|
510 |
|
|
511 |
!Cons methodsFor:'queries'!
|
|
512 |
|
|
513 |
isCons
|
|
514 |
^ true
|
|
515 |
!
|
|
516 |
|
|
517 |
size
|
1074
|
518 |
"for smalltalkers: the lists length"
|
|
519 |
|
1043
|
520 |
|l p rest|
|
|
521 |
|
|
522 |
l := 1.
|
|
523 |
p := self.
|
|
524 |
[(rest := p cdr) notNil] whileTrue:[
|
|
525 |
l := l + 1.
|
|
526 |
p := rest
|
|
527 |
].
|
|
528 |
^ l
|
|
529 |
|
|
530 |
"
|
1074
|
531 |
(Cons fromArray:#( )) size
|
|
532 |
(Cons fromArray:#(1)) size
|
|
533 |
(Cons fromArray:#(1 2 3 4)) size
|
1043
|
534 |
"
|
|
535 |
! !
|
|
536 |
|
1377
|
537 |
!Cons methodsFor:'streaming'!
|
|
538 |
|
|
539 |
readStream
|
|
540 |
^ ConsStream on:self.
|
|
541 |
! !
|
|
542 |
|
1043
|
543 |
!Cons class methodsFor:'documentation'!
|
|
544 |
|
|
545 |
version
|
2469
|
546 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.12 2010-08-08 15:16:23 cg Exp $'
|
2437
|
547 |
!
|
|
548 |
|
|
549 |
version_CVS
|
2469
|
550 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.12 2010-08-08 15:16:23 cg Exp $'
|
1043
|
551 |
! !
|