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