author | Claus Gittinger <cg@exept.de> |
Mon, 13 Feb 2017 20:44:08 +0100 | |
changeset 4317 | 03c69a595f88 |
parent 4247 | 11f26c6f2d17 |
child 4526 | 5ca205fde08f |
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 |
||
4109 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1043 | 16 |
SequenceableCollection subclass:#Cons |
17 |
instanceVariableNames:'car cdr' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Linked' |
|
21 |
! |
|
22 |
||
23 |
!Cons class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
A pair as in lisp. |
|
1074 | 42 |
Cons car:a cdr:b |
43 |
||
44 |
Conses are not heavily used by Smalltalk (actually: not at all). |
|
1377 | 45 |
Consider this a demo class. |
1043 | 46 |
|
47 |
[author:] |
|
2662 | 48 |
Claus Gittinger (Jun 2002) |
1043 | 49 |
|
50 |
[see also:] |
|
51 |
||
52 |
" |
|
1627 | 53 |
! |
54 |
||
55 |
examples |
|
56 |
" |
|
57 |
[exBegin] |
|
58 |
|p1 p2 p3| |
|
59 |
||
60 |
p3 := Cons car:3 cdr:nil. |
|
61 |
p2 := Cons car:2 cdr:p3. |
|
62 |
p1 := Cons car:1 cdr:p2. |
|
63 |
p1 head. |
|
64 |
p1 tail. |
|
65 |
p1 size. |
|
66 |
p1 do:[:each | Transcript showCR:each]. |
|
67 |
p1 at:2 |
|
68 |
[exEnd] |
|
69 |
" |
|
1043 | 70 |
! ! |
71 |
||
72 |
!Cons class methodsFor:'instance creation'! |
|
73 |
||
74 |
car:carArg cdr:cdrArg |
|
75 |
^ self basicNew car:carArg cdr:cdrArg |
|
76 |
! |
|
77 |
||
78 |
fromArray:anArray |
|
79 |
|p last first| |
|
80 |
||
81 |
anArray do:[:el | |
|
82 |
p := self car:el cdr:nil. |
|
83 |
first isNil ifTrue:[ |
|
84 |
first := p. |
|
85 |
] ifFalse:[ |
|
86 |
last cdr:p. |
|
87 |
]. |
|
88 |
last := p. |
|
89 |
]. |
|
90 |
^ first. |
|
91 |
||
92 |
" |
|
1074 | 93 |
Cons fromArray:#(1 2 3 4) |
94 |
Cons fromArray:#() |
|
95 |
Cons fromArray:#(1) |
|
1248 | 96 |
Cons fromArray:(1 to:10000) |
1043 | 97 |
" |
2556 | 98 |
! |
99 |
||
100 |
makeList:size |
|
101 |
|first prev this| |
|
102 |
||
103 |
size == 0 ifTrue:[^ nil]. |
|
2557
35bc2d58019d
comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents:
2556
diff
changeset
|
104 |
|
2556 | 105 |
first := prev := self car:nil cdr:nil. |
106 |
2 to:size do:[:n | |
|
107 |
this := self car:nil cdr:nil. |
|
108 |
prev cdr:this. |
|
109 |
prev := this |
|
110 |
]. |
|
111 |
^ first |
|
112 |
||
113 |
" |
|
2557
35bc2d58019d
comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents:
2556
diff
changeset
|
114 |
(self makeList:0) size |
35bc2d58019d
comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents:
2556
diff
changeset
|
115 |
(self makeList:1) size |
2556 | 116 |
(self makeList:100) size |
117 |
(self makeList:1000) size |
|
118 |
(self makeList:10000) size |
|
119 |
" |
|
120 |
||
121 |
"Created: / 28-04-2011 / 00:37:11 / cg" |
|
2557
35bc2d58019d
comment/format in: #makeList:
Claus Gittinger <cg@exept.de>
parents:
2556
diff
changeset
|
122 |
"Modified: / 29-04-2011 / 10:38:11 / cg" |
1043 | 123 |
! ! |
124 |
||
2469 | 125 |
!Cons class methodsFor:'sExpressions'! |
126 |
||
3269 | 127 |
readLispAtomFrom:aStream |
128 |
|chars n| |
|
129 |
||
130 |
aStream skipSeparators. |
|
131 |
aStream atEnd ifTrue:[^ nil]. |
|
132 |
||
133 |
chars := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]]. |
|
134 |
(n := Number readFromString:chars onError:nil) notNil ifTrue:[ |
|
135 |
"/ smalltalk allows +n; scheme does not |
|
136 |
(chars startsWith:'+') ifFalse:[ |
|
137 |
^ n |
|
138 |
]. |
|
139 |
]. |
|
140 |
^ chars asSymbol |
|
141 |
||
142 |
" |
|
143 |
self readLispFrom:('(cons 1 2)' readStream). |
|
144 |
self readLispFrom:('(cons 1+ 2)' readStream). |
|
145 |
self readLispFrom:('(cons +1 2)' readStream). |
|
146 |
" |
|
147 |
||
148 |
"Created: / 08-08-2010 / 17:15:18 / cg" |
|
149 |
! |
|
150 |
||
2469 | 151 |
readLispFrom:aStream |
4109 | 152 |
[ |
153 |
aStream skipSeparators. |
|
154 |
aStream atEnd ifTrue:[^ nil]. |
|
2470 | 155 |
|
4109 | 156 |
aStream peek ==$; ifFalse:[ |
157 |
aStream peek ==$( ifTrue:[ |
|
158 |
^ self readLispListFrom:aStream |
|
159 |
]. |
|
160 |
^ self readLispAtomFrom:aStream |
|
161 |
]. |
|
162 |
"/ EOL comment |
|
2469 | 163 |
aStream skipLine. |
4109 | 164 |
] loop |
165 |
||
2469 | 166 |
" |
167 |
self readLispFrom:('(cons 1 2)' readStream). |
|
168 |
" |
|
169 |
||
170 |
"Created: / 08-08-2010 / 17:07:49 / cg" |
|
171 |
! |
|
172 |
||
3269 | 173 |
readLispListFrom:aStream |
2469 | 174 |
|first this prev element| |
175 |
||
176 |
aStream next. "/ the leading '(' |
|
177 |
[ |
|
178 |
aStream skipSeparators. |
|
179 |
aStream peek ~= $) |
|
180 |
] whileTrue:[ |
|
181 |
element := self readLispFrom:aStream. |
|
4247 | 182 |
this := self car:element cdr:nil. |
2469 | 183 |
prev isNil ifTrue:[ |
184 |
first := this |
|
185 |
] ifFalse:[ |
|
186 |
prev cdr:this. |
|
187 |
]. |
|
188 |
prev := this. |
|
189 |
]. |
|
190 |
||
191 |
aStream next. "/ the trailing ')' |
|
192 |
^ first. |
|
193 |
||
194 |
" |
|
195 |
self readLispFrom:('(cons 1 2)' readStream). |
|
196 |
" |
|
197 |
||
198 |
"Modified: / 08-08-2010 / 17:15:54 / cg" |
|
199 |
! ! |
|
200 |
||
1043 | 201 |
!Cons methodsFor:'accessing'! |
202 |
||
203 |
at:n |
|
1074 | 204 |
"for collection compatibility: |
205 |
a slow indexed accessor" |
|
206 |
||
1627 | 207 |
^ (self nth:n) |
1043 | 208 |
|
209 |
" |
|
1074 | 210 |
(Cons fromArray:#(1)) at:1 |
211 |
(Cons fromArray:#(1 2 3 4)) at:1 |
|
212 |
(Cons fromArray:#(1 2 3 4)) at:3 |
|
213 |
(Cons fromArray:#(1 2 3 4)) at:4 |
|
214 |
(Cons fromArray:#(1 2 3 4)) at:5 |
|
1043 | 215 |
" |
216 |
! |
|
217 |
||
218 |
at:n put:newValue |
|
1627 | 219 |
"destructive: |
220 |
for collection compatibility: a slow indexed accessor" |
|
1074 | 221 |
|
1627 | 222 |
(self nthPair:n) car:newValue. |
1043 | 223 |
^ newValue. |
224 |
||
225 |
" |
|
226 |
|l| |
|
227 |
||
1074 | 228 |
l := Cons fromArray:#(1 2 3 4). |
1043 | 229 |
l at:1 put:'one'. |
230 |
l at:3 put:'three'. |
|
231 |
l |
|
232 |
" |
|
233 |
! |
|
234 |
||
1623 | 235 |
first |
236 |
"return the head, first or car - whatever you wonna call it" |
|
237 |
||
2469 | 238 |
^ car |
239 |
||
240 |
"Modified: / 08-08-2010 / 17:04:23 / cg" |
|
1623 | 241 |
! |
242 |
||
1237 | 243 |
head |
244 |
"return the head, first or car - whatever you wonna call it" |
|
245 |
||
2469 | 246 |
^ car |
247 |
||
248 |
"Modified: / 08-08-2010 / 17:04:20 / cg" |
|
1237 | 249 |
! |
250 |
||
1043 | 251 |
last |
1074 | 252 |
"for lispers: |
253 |
return the last element of a list" |
|
254 |
||
1043 | 255 |
|p rest| |
256 |
||
257 |
p := self. |
|
258 |
[(rest := p cdr) notNil] whileTrue:[ |
|
259 |
p := rest |
|
260 |
]. |
|
1237 | 261 |
^ p car |
1043 | 262 |
|
263 |
" |
|
1074 | 264 |
(Cons fromArray:#(1)) last |
265 |
(Cons fromArray:#(1 2 3 4)) last |
|
1043 | 266 |
" |
267 |
! |
|
268 |
||
269 |
nth:n |
|
1074 | 270 |
"for lispers: |
271 |
return the nth element of a list" |
|
272 |
||
1627 | 273 |
^ (self nthPair:n) car |
1043 | 274 |
|
275 |
" |
|
1074 | 276 |
(Cons fromArray:#(1)) nth:1 |
277 |
(Cons fromArray:#(1 2 3 4)) nth:1 |
|
278 |
(Cons fromArray:#(1 2 3 4)) nth:3 |
|
279 |
(Cons fromArray:#(1 2 3 4)) nth:4 |
|
280 |
(Cons fromArray:#(1 2 3 4)) nth:5 |
|
1248 | 281 |
(Cons fromArray:#( )) nth:1 -> error |
1043 | 282 |
" |
1237 | 283 |
! |
284 |
||
1623 | 285 |
rest |
2469 | 286 |
"return the tail, rest or cdr - whatever you wonna call it" |
1623 | 287 |
|
2469 | 288 |
^ cdr |
289 |
||
290 |
"Modified: / 08-08-2010 / 17:04:48 / cg" |
|
1623 | 291 |
! |
292 |
||
1243 | 293 |
reversed |
294 |
"for lispers: |
|
295 |
return a new list with the cars in reverse order" |
|
296 |
||
297 |
"/ for now, tail recursion is not yet optimized by the st/x jitter... |
|
298 |
"/ |
|
299 |
"/ |rev| |
|
300 |
"/ |
|
301 |
"/ rev := [:lst :acc | |
|
302 |
"/ lst isNil ifTrue:[ |
|
303 |
"/ acc |
|
304 |
"/ ] ifFalse:[ |
|
305 |
"/ rev value:(lst tail) |
|
306 |
"/ value:(Cons car:(lst head) cdr:acc) |
|
307 |
"/ ] |
|
308 |
"/ ]. |
|
309 |
"/ ^ rev value:self value:nil |
|
310 |
||
311 |
| lst acc| |
|
312 |
||
313 |
lst := self. |
|
314 |
acc := nil. |
|
315 |
||
316 |
[ |
|
317 |
|nLst nAcc| |
|
318 |
||
319 |
lst isNil ifTrue:[ ^ acc]. |
|
320 |
||
321 |
nLst := lst tail. |
|
322 |
nAcc := Cons car:(lst head) cdr:acc. |
|
323 |
lst := nLst. |
|
324 |
acc := nAcc. |
|
325 |
] loop |
|
326 |
||
327 |
" |
|
328 |
(Cons fromArray:#(1)) reversed |
|
329 |
(Cons fromArray:#(1 2)) reversed |
|
330 |
(Cons fromArray:#(1 2 3 4)) reversed |
|
331 |
(Cons fromArray:(1 to:10000)) reversed |
|
332 |
" |
|
333 |
! |
|
334 |
||
1237 | 335 |
tail |
2469 | 336 |
"return the tail, rest or cdr - whatever you wonna call it" |
1237 | 337 |
|
2469 | 338 |
^ cdr |
339 |
||
340 |
"Modified: / 08-08-2010 / 17:04:59 / cg" |
|
1043 | 341 |
! ! |
342 |
||
343 |
!Cons methodsFor:'accessing - basic'! |
|
344 |
||
2470 | 345 |
cadddr |
346 |
"return the fourth element" |
|
347 |
||
348 |
^ cdr cdr cdr car |
|
349 |
||
350 |
"Created: / 08-08-2010 / 17:29:40 / cg" |
|
351 |
! |
|
352 |
||
353 |
caddr |
|
354 |
"return the third element" |
|
355 |
||
356 |
^ cdr cdr car |
|
357 |
||
358 |
"Created: / 08-08-2010 / 17:29:32 / cg" |
|
359 |
! |
|
360 |
||
361 |
cadr |
|
362 |
"return the second element" |
|
363 |
||
364 |
^ cdr car |
|
365 |
||
366 |
"Created: / 08-08-2010 / 17:29:25 / cg" |
|
367 |
! |
|
368 |
||
1043 | 369 |
car |
1074 | 370 |
"return the head, first or car - whatever you wonna call it" |
1043 | 371 |
|
372 |
^ car |
|
373 |
! |
|
374 |
||
375 |
car:something |
|
1074 | 376 |
"set the head, first or car - whatever you wonna call it" |
1043 | 377 |
|
378 |
car := something. |
|
379 |
! |
|
380 |
||
381 |
car:carArg cdr:cdrArg |
|
1074 | 382 |
"set both car and cdr" |
1043 | 383 |
|
384 |
car := carArg. |
|
385 |
cdr := cdrArg. |
|
386 |
! |
|
387 |
||
2472 | 388 |
cddr |
389 |
"return the rest after the second element" |
|
390 |
||
391 |
^ cdr cdr |
|
392 |
||
393 |
"Created: / 08-08-2010 / 17:47:11 / cg" |
|
394 |
! |
|
395 |
||
1043 | 396 |
cdr |
1074 | 397 |
"return the tail, second or cdr - whatever you wonna call it" |
1043 | 398 |
|
399 |
^ cdr |
|
400 |
! |
|
401 |
||
402 |
cdr:something |
|
1074 | 403 |
"set the tail, second or cdr - whatever you wonna call it" |
1043 | 404 |
|
405 |
cdr := something. |
|
1623 | 406 |
! |
407 |
||
408 |
first:carArg rest:cdrArg |
|
409 |
"set both car and cdr" |
|
410 |
||
411 |
car := carArg. |
|
412 |
cdr := cdrArg. |
|
413 |
! |
|
414 |
||
415 |
head:carArg tail:cdrArg |
|
416 |
"set both car and cdr" |
|
417 |
||
418 |
car := carArg. |
|
419 |
cdr := cdrArg. |
|
1627 | 420 |
! |
421 |
||
422 |
nthPair:n |
|
423 |
"a helper: |
|
424 |
return the nth pair of a list" |
|
425 |
||
426 |
|cnt p| |
|
427 |
||
428 |
cnt := n. |
|
429 |
p := self. |
|
430 |
[ |
|
431 |
cnt := cnt - 1. |
|
432 |
cnt == 0 ifTrue:[^ p]. |
|
3310 | 433 |
|
1627 | 434 |
p := p cdr. |
435 |
p isNil ifTrue:[ |
|
436 |
self error:'no such element' mayProceed:true. |
|
437 |
^ nil |
|
438 |
]. |
|
439 |
] loop. |
|
440 |
! ! |
|
441 |
||
2437 | 442 |
!Cons methodsFor:'comparing'! |
443 |
||
444 |
= aCons |
|
445 |
^ aCons class == self class |
|
446 |
and:[ car = aCons car |
|
447 |
and:[ cdr = aCons cdr ]] |
|
448 |
! |
|
449 |
||
450 |
hash |
|
451 |
^ car hash bitXor: cdr hash |
|
452 |
! ! |
|
453 |
||
1627 | 454 |
!Cons methodsFor:'enumerating'! |
455 |
||
456 |
do:aBlock |
|
2471 | 457 |
"evaluate aBlock for each car" |
458 |
||
1627 | 459 |
|ptr| |
460 |
||
461 |
aBlock value:car. |
|
462 |
ptr := cdr. |
|
463 |
[ ptr notNil ] whileTrue:[ |
|
464 |
aBlock value:ptr car. |
|
465 |
ptr := ptr cdr. |
|
466 |
]. |
|
2471 | 467 |
|
468 |
"Modified: / 08-08-2010 / 17:33:44 / cg" |
|
1043 | 469 |
! ! |
470 |
||
471 |
!Cons methodsFor:'list processing'! |
|
472 |
||
473 |
append:aCons |
|
1074 | 474 |
"for lispers: |
1248 | 475 |
append the arg. Return a new list, where the 2nd part is shared. |
476 |
Destructive: the receivers last cdr is modified." |
|
1074 | 477 |
|
1043 | 478 |
|p rest| |
479 |
||
480 |
p := self. |
|
481 |
[(rest := p cdr) notNil] whileTrue:[ |
|
482 |
p := rest |
|
483 |
]. |
|
484 |
p cdr:aCons. |
|
485 |
^ self |
|
486 |
||
487 |
" |
|
1074 | 488 |
(Cons fromArray:#(1 2 3 4)) |
489 |
append:(Cons fromArray:#(5 6 7 8)) |
|
490 |
" |
|
491 |
||
492 |
"sharing demonstrated: |
|
493 |
||
494 |
|a b ab| |
|
495 |
||
496 |
a := Cons fromArray:#(1 2 3 4). |
|
497 |
b := Cons fromArray:#(5 6 7 8). |
|
498 |
ab := a append:b. |
|
499 |
b car:'five'. |
|
1248 | 500 |
ab |
501 |
" |
|
502 |
||
503 |
"destruction demonstrated: |
|
504 |
||
505 |
|a b ab| |
|
506 |
||
507 |
a := Cons fromArray:#(1 2 3 4). |
|
508 |
b := Cons fromArray:#(5 6 7 8). |
|
509 |
ab := a append:b. |
|
510 |
a |
|
1043 | 511 |
" |
1237 | 512 |
! |
513 |
||
1248 | 514 |
take:nTaken |
1237 | 515 |
"for lispers: |
516 |
take n elements from the list; return a new list" |
|
517 |
||
1248 | 518 |
|nRemain l rslt lastCons cons| |
519 |
||
520 |
nTaken > 0 ifTrue:[ |
|
521 |
"/ avoiding recursion here... |
|
522 |
"/ instead of: |
|
523 |
"/ ^ Cons car:(self car) cdr:(self cdr take:nTaken-1) |
|
524 |
"/ we do: |
|
525 |
nRemain := nTaken. |
|
526 |
l := self. |
|
527 |
rslt := lastCons := Cons car:(l car) cdr:nil. |
|
528 |
[nRemain > 1] whileTrue:[ |
|
529 |
l := l cdr. |
|
530 |
cons := Cons car:(l car) cdr:nil. |
|
531 |
lastCons cdr:cons. |
|
532 |
lastCons := cons. |
|
533 |
nRemain := nRemain - 1. |
|
534 |
]. |
|
535 |
^ rslt. |
|
1237 | 536 |
]. |
537 |
^ nil |
|
538 |
||
539 |
" |
|
540 |
(Cons fromArray:#(1 2 3 4)) take:3 |
|
541 |
(Cons fromArray:#(1)) take:0 |
|
542 |
(Cons fromArray:#()) take:3 |
|
1248 | 543 |
(Cons fromArray:(1 to: 1000)) take:999 |
2656 | 544 |
|
545 |
|gen allNumbers| |
|
546 |
gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]]. |
|
547 |
allNumbers := gen value:1. |
|
548 |
allNumbers take:10 |
|
1237 | 549 |
" |
2656 | 550 |
|
551 |
"Modified (comment): / 27-09-2011 / 11:31:38 / cg" |
|
1043 | 552 |
! ! |
553 |
||
554 |
!Cons methodsFor:'printing'! |
|
555 |
||
556 |
displayString |
|
557 |
^ self printString |
|
558 |
! |
|
559 |
||
560 |
printOn:aStream |
|
2448 | 561 |
thisContext isRecursive ifTrue:[ |
562 |
'Cons [error]: printOn: of self referencing collection.' errorPrintCR. |
|
563 |
aStream nextPutAll:'#("recursive")'. |
|
564 |
^ self. |
|
565 |
]. |
|
566 |
||
1043 | 567 |
(car isLazyValue not and:[ car isCons ]) ifTrue:[ |
568 |
aStream nextPutAll:'('. |
|
569 |
car printOn:aStream. |
|
570 |
aStream nextPutAll:')'. |
|
571 |
] ifFalse:[ |
|
572 |
car printOn:aStream. |
|
573 |
]. |
|
574 |
||
1073 | 575 |
aStream nextPutAll:'!!'. |
1043 | 576 |
|
577 |
(cdr isLazyValue not and:[ cdr isCons ]) ifTrue:[ |
|
578 |
aStream nextPutAll:'('. |
|
579 |
cdr printOn:aStream. |
|
580 |
aStream nextPutAll:')'. |
|
581 |
] ifFalse:[ |
|
582 |
cdr printOn:aStream. |
|
583 |
]. |
|
2448 | 584 |
|
585 |
"Modified: / 18-05-2010 / 10:25:49 / cg" |
|
1043 | 586 |
! ! |
587 |
||
588 |
!Cons methodsFor:'queries'! |
|
589 |
||
2753 | 590 |
beginAndSizeOfCycle |
3310 | 591 |
"return the begin and size of a cycle, if the list contains one. |
592 |
Nil otherwise. |
|
2753 | 593 |
Floyd's cycle-finding algorithm" |
594 |
||
595 |
|t h i loopStartIndex loopSize| |
|
596 |
||
597 |
t := self cdr. t isNil ifTrue:[^ nil]. |
|
598 |
h := t cdr. h isNil ifTrue:[^ nil]. |
|
599 |
||
600 |
[t ~~ h] whileTrue:[ |
|
601 |
t := t cdr. |
|
602 |
h := h cdr. h isNil ifTrue:[^ nil]. |
|
603 |
h := h cdr. h isNil ifTrue:[^ nil]. |
|
604 |
]. |
|
605 |
||
606 |
"/ both h and t are now inside the cycle, |
|
607 |
"/ equidistant from the start of the cycle |
|
608 |
t := self. |
|
609 |
i := 1. |
|
610 |
[t ~~ h] whileTrue:[ |
|
611 |
t := t cdr. |
|
612 |
h := h cdr. |
|
613 |
i := i + 1. |
|
614 |
]. |
|
615 |
loopStartIndex := i. |
|
616 |
||
617 |
loopSize := 1. |
|
618 |
h := t cdr. |
|
619 |
[t ~~ h] whileTrue:[ |
|
620 |
h := h cdr. |
|
621 |
i := i + 1. |
|
622 |
loopSize := loopSize + 1. |
|
623 |
]. |
|
624 |
||
625 |
^ { loopStartIndex. loopSize } |
|
626 |
||
627 |
" |
|
628 |
|n1 n2 n3 n4 n5| |
|
629 |
||
630 |
n1 := Cons new car:1. |
|
631 |
n2 := Cons new car:2. |
|
632 |
n3 := Cons new car:3. |
|
633 |
n4 := Cons new car:4. |
|
634 |
n5 := Cons new car:5. |
|
635 |
n1 cdr: n2. |
|
636 |
n2 cdr: n3. |
|
637 |
n3 cdr: n4. |
|
638 |
n4 cdr: n5. |
|
639 |
n1 beginAndSizeOfCycle. |
|
640 |
n5 cdr: n3. |
|
641 |
n1 beginAndSizeOfCycle. |
|
642 |
" |
|
643 |
||
644 |
"Created: / 27-07-2012 / 00:00:36 / cg" |
|
645 |
! |
|
646 |
||
647 |
isCyclic |
|
648 |
"true if the list contains a cycle" |
|
649 |
||
650 |
^ self beginAndSizeOfCycle notNil |
|
651 |
||
652 |
" |
|
653 |
|n1 n2 n3 n4 n5| |
|
654 |
||
655 |
n1 := Cons new car:1. |
|
656 |
n2 := Cons new car:2. |
|
657 |
n3 := Cons new car:3. |
|
658 |
n4 := Cons new car:4. |
|
659 |
n5 := Cons new car:5. |
|
660 |
n1 cdr: n2. |
|
661 |
n2 cdr: n3. |
|
662 |
n3 cdr: n4. |
|
663 |
n4 cdr: n5. |
|
664 |
n1 isCyclic. |
|
665 |
n5 cdr: n3. |
|
666 |
n1 isCyclic. |
|
667 |
" |
|
668 |
||
669 |
"Created: / 26-07-2012 / 23:32:52 / cg" |
|
670 |
! |
|
671 |
||
3310 | 672 |
size |
3307 | 673 |
"the list's length" |
1074 | 674 |
|
3307 | 675 |
|len p rest| |
1043 | 676 |
|
3307 | 677 |
len := 1. |
1043 | 678 |
p := self. |
3309 | 679 |
|
3310 | 680 |
[ |
681 |
p isLazyValue not |
|
682 |
and:[ (rest := p cdr) notNil ] |
|
683 |
] whileTrue:[ |
|
3307 | 684 |
len := len + 1. |
1043 | 685 |
p := rest |
686 |
]. |
|
3307 | 687 |
^ len |
688 |
||
689 |
" |
|
3310 | 690 |
(Cons fromArray:#(1)) size |
1074 | 691 |
(Cons fromArray:#(1 2 3 4)) size |
3310 | 692 |
(Cons car:1 cdr:2) size --> error (degenerated list) |
1043 | 693 |
" |
694 |
! ! |
|
695 |
||
1377 | 696 |
!Cons methodsFor:'streaming'! |
697 |
||
698 |
readStream |
|
699 |
^ ConsStream on:self. |
|
700 |
! ! |
|
701 |
||
3310 | 702 |
!Cons methodsFor:'testing'! |
703 |
||
704 |
isCons |
|
705 |
^ true |
|
706 |
! |
|
707 |
||
708 |
isLazy |
|
709 |
^ false |
|
710 |
! ! |
|
711 |
||
1043 | 712 |
!Cons class methodsFor:'documentation'! |
713 |
||
714 |
version |
|
4109 | 715 |
^ '$Header$' |
2437 | 716 |
! |
717 |
||
718 |
version_CVS |
|
4109 | 719 |
^ '$Header$' |
1043 | 720 |
! ! |
3269 | 721 |