author | Claus Gittinger <cg@exept.de> |
Wed, 30 Apr 2014 13:03:07 +0200 | |
changeset 3269 | 3f964738277d |
parent 2753 | aaebdee8dba9 |
child 3307 | 4298986dd5de |
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 |
||
3269 | 128 |
readLispAtomFrom:aStream |
129 |
|chars n| |
|
130 |
||
131 |
aStream skipSeparators. |
|
132 |
aStream atEnd ifTrue:[^ nil]. |
|
133 |
||
134 |
chars := aStream upToMatching:[:ch | ch isSeparator or:[ch = $( or:[ch = $)]]]. |
|
135 |
(n := Number readFromString:chars onError:nil) notNil ifTrue:[ |
|
136 |
"/ smalltalk allows +n; scheme does not |
|
137 |
(chars startsWith:'+') ifFalse:[ |
|
138 |
^ n |
|
139 |
]. |
|
140 |
]. |
|
141 |
^ chars asSymbol |
|
142 |
||
143 |
" |
|
144 |
self readLispFrom:('(cons 1 2)' readStream). |
|
145 |
self readLispFrom:('(cons 1+ 2)' readStream). |
|
146 |
self readLispFrom:('(cons +1 2)' readStream). |
|
147 |
" |
|
148 |
||
149 |
"Created: / 08-08-2010 / 17:15:18 / cg" |
|
150 |
! |
|
151 |
||
2469 | 152 |
readLispFrom:aStream |
153 |
aStream skipSeparators. |
|
2470 | 154 |
aStream atEnd ifTrue:[^ nil]. |
155 |
||
2469 | 156 |
aStream peek ==$; ifTrue:[ |
157 |
"/ comment |
|
158 |
aStream skipLine. |
|
159 |
thisContext restart |
|
160 |
]. |
|
161 |
||
162 |
aStream peek ==$( ifTrue:[ |
|
3269 | 163 |
^ self readLispListFrom:aStream |
2469 | 164 |
]. |
3269 | 165 |
^ self readLispAtomFrom:aStream |
2469 | 166 |
|
167 |
" |
|
168 |
self readLispFrom:('(cons 1 2)' readStream). |
|
169 |
" |
|
170 |
||
171 |
"Created: / 08-08-2010 / 17:07:49 / cg" |
|
172 |
! |
|
173 |
||
3269 | 174 |
readLispListFrom:aStream |
2469 | 175 |
|first this prev element| |
176 |
||
177 |
aStream next. "/ the leading '(' |
|
178 |
[ |
|
179 |
aStream skipSeparators. |
|
180 |
aStream peek ~= $) |
|
181 |
] whileTrue:[ |
|
182 |
element := self readLispFrom:aStream. |
|
183 |
this := Cons car:element cdr:nil. |
|
184 |
prev isNil ifTrue:[ |
|
185 |
first := this |
|
186 |
] ifFalse:[ |
|
187 |
prev cdr:this. |
|
188 |
]. |
|
189 |
prev := this. |
|
190 |
]. |
|
191 |
||
192 |
aStream next. "/ the trailing ')' |
|
193 |
^ first. |
|
194 |
||
195 |
" |
|
196 |
self readLispFrom:('(cons 1 2)' readStream). |
|
197 |
" |
|
198 |
||
199 |
"Modified: / 08-08-2010 / 17:15:54 / cg" |
|
200 |
! ! |
|
201 |
||
1043 | 202 |
!Cons methodsFor:'accessing'! |
203 |
||
204 |
at:n |
|
1074 | 205 |
"for collection compatibility: |
206 |
a slow indexed accessor" |
|
207 |
||
1627 | 208 |
^ (self nth:n) |
1043 | 209 |
|
210 |
" |
|
1074 | 211 |
(Cons fromArray:#(1)) at:1 |
212 |
(Cons fromArray:#(1 2 3 4)) at:1 |
|
213 |
(Cons fromArray:#(1 2 3 4)) at:3 |
|
214 |
(Cons fromArray:#(1 2 3 4)) at:4 |
|
215 |
(Cons fromArray:#(1 2 3 4)) at:5 |
|
1043 | 216 |
" |
217 |
! |
|
218 |
||
219 |
at:n put:newValue |
|
1627 | 220 |
"destructive: |
221 |
for collection compatibility: a slow indexed accessor" |
|
1074 | 222 |
|
1627 | 223 |
(self nthPair:n) car:newValue. |
1043 | 224 |
^ newValue. |
225 |
||
226 |
" |
|
227 |
|l| |
|
228 |
||
1074 | 229 |
l := Cons fromArray:#(1 2 3 4). |
1043 | 230 |
l at:1 put:'one'. |
231 |
l at:3 put:'three'. |
|
232 |
l |
|
233 |
" |
|
234 |
! |
|
235 |
||
1623 | 236 |
first |
237 |
"return the head, first or car - whatever you wonna call it" |
|
238 |
||
2469 | 239 |
^ car |
240 |
||
241 |
"Modified: / 08-08-2010 / 17:04:23 / cg" |
|
1623 | 242 |
! |
243 |
||
1237 | 244 |
head |
245 |
"return the head, first or car - whatever you wonna call it" |
|
246 |
||
2469 | 247 |
^ car |
248 |
||
249 |
"Modified: / 08-08-2010 / 17:04:20 / cg" |
|
1237 | 250 |
! |
251 |
||
1043 | 252 |
last |
1074 | 253 |
"for lispers: |
254 |
return the last element of a list" |
|
255 |
||
1043 | 256 |
|p rest| |
257 |
||
258 |
p := self. |
|
259 |
[(rest := p cdr) notNil] whileTrue:[ |
|
260 |
p := rest |
|
261 |
]. |
|
1237 | 262 |
^ p car |
1043 | 263 |
|
264 |
" |
|
1074 | 265 |
(Cons fromArray:#(1)) last |
266 |
(Cons fromArray:#(1 2 3 4)) last |
|
1043 | 267 |
" |
268 |
! |
|
269 |
||
270 |
nth:n |
|
1074 | 271 |
"for lispers: |
272 |
return the nth element of a list" |
|
273 |
||
1627 | 274 |
^ (self nthPair:n) car |
1043 | 275 |
|
276 |
" |
|
1074 | 277 |
(Cons fromArray:#(1)) nth:1 |
278 |
(Cons fromArray:#(1 2 3 4)) nth:1 |
|
279 |
(Cons fromArray:#(1 2 3 4)) nth:3 |
|
280 |
(Cons fromArray:#(1 2 3 4)) nth:4 |
|
281 |
(Cons fromArray:#(1 2 3 4)) nth:5 |
|
1248 | 282 |
(Cons fromArray:#( )) nth:1 -> error |
1043 | 283 |
" |
1237 | 284 |
! |
285 |
||
1623 | 286 |
rest |
2469 | 287 |
"return the tail, rest or cdr - whatever you wonna call it" |
1623 | 288 |
|
2469 | 289 |
^ cdr |
290 |
||
291 |
"Modified: / 08-08-2010 / 17:04:48 / cg" |
|
1623 | 292 |
! |
293 |
||
1243 | 294 |
reversed |
295 |
"for lispers: |
|
296 |
return a new list with the cars in reverse order" |
|
297 |
||
298 |
"/ for now, tail recursion is not yet optimized by the st/x jitter... |
|
299 |
"/ |
|
300 |
"/ |rev| |
|
301 |
"/ |
|
302 |
"/ rev := [:lst :acc | |
|
303 |
"/ lst isNil ifTrue:[ |
|
304 |
"/ acc |
|
305 |
"/ ] ifFalse:[ |
|
306 |
"/ rev value:(lst tail) |
|
307 |
"/ value:(Cons car:(lst head) cdr:acc) |
|
308 |
"/ ] |
|
309 |
"/ ]. |
|
310 |
"/ ^ rev value:self value:nil |
|
311 |
||
312 |
| lst acc| |
|
313 |
||
314 |
lst := self. |
|
315 |
acc := nil. |
|
316 |
||
317 |
[ |
|
318 |
|nLst nAcc| |
|
319 |
||
320 |
lst isNil ifTrue:[ ^ acc]. |
|
321 |
||
322 |
nLst := lst tail. |
|
323 |
nAcc := Cons car:(lst head) cdr:acc. |
|
324 |
lst := nLst. |
|
325 |
acc := nAcc. |
|
326 |
] loop |
|
327 |
||
328 |
" |
|
329 |
(Cons fromArray:#(1)) reversed |
|
330 |
(Cons fromArray:#(1 2)) reversed |
|
331 |
(Cons fromArray:#(1 2 3 4)) reversed |
|
332 |
(Cons fromArray:(1 to:10000)) reversed |
|
333 |
" |
|
334 |
! |
|
335 |
||
1237 | 336 |
tail |
2469 | 337 |
"return the tail, rest or cdr - whatever you wonna call it" |
1237 | 338 |
|
2469 | 339 |
^ cdr |
340 |
||
341 |
"Modified: / 08-08-2010 / 17:04:59 / cg" |
|
1043 | 342 |
! ! |
343 |
||
344 |
!Cons methodsFor:'accessing - basic'! |
|
345 |
||
2470 | 346 |
cadddr |
347 |
"return the fourth element" |
|
348 |
||
349 |
^ cdr cdr cdr car |
|
350 |
||
351 |
"Created: / 08-08-2010 / 17:29:40 / cg" |
|
352 |
! |
|
353 |
||
354 |
caddr |
|
355 |
"return the third element" |
|
356 |
||
357 |
^ cdr cdr car |
|
358 |
||
359 |
"Created: / 08-08-2010 / 17:29:32 / cg" |
|
360 |
! |
|
361 |
||
362 |
cadr |
|
363 |
"return the second element" |
|
364 |
||
365 |
^ cdr car |
|
366 |
||
367 |
"Created: / 08-08-2010 / 17:29:25 / cg" |
|
368 |
! |
|
369 |
||
1043 | 370 |
car |
1074 | 371 |
"return the head, first or car - whatever you wonna call it" |
1043 | 372 |
|
373 |
^ car |
|
374 |
! |
|
375 |
||
376 |
car:something |
|
1074 | 377 |
"set the head, first or car - whatever you wonna call it" |
1043 | 378 |
|
379 |
car := something. |
|
380 |
! |
|
381 |
||
382 |
car:carArg cdr:cdrArg |
|
1074 | 383 |
"set both car and cdr" |
1043 | 384 |
|
385 |
car := carArg. |
|
386 |
cdr := cdrArg. |
|
387 |
! |
|
388 |
||
2472 | 389 |
cddr |
390 |
"return the rest after the second element" |
|
391 |
||
392 |
^ cdr cdr |
|
393 |
||
394 |
"Created: / 08-08-2010 / 17:47:11 / cg" |
|
395 |
! |
|
396 |
||
1043 | 397 |
cdr |
1074 | 398 |
"return the tail, second or cdr - whatever you wonna call it" |
1043 | 399 |
|
400 |
^ cdr |
|
401 |
! |
|
402 |
||
403 |
cdr:something |
|
1074 | 404 |
"set the tail, second or cdr - whatever you wonna call it" |
1043 | 405 |
|
406 |
cdr := something. |
|
1623 | 407 |
! |
408 |
||
409 |
first:carArg rest:cdrArg |
|
410 |
"set both car and cdr" |
|
411 |
||
412 |
car := carArg. |
|
413 |
cdr := cdrArg. |
|
414 |
! |
|
415 |
||
416 |
head:carArg tail:cdrArg |
|
417 |
"set both car and cdr" |
|
418 |
||
419 |
car := carArg. |
|
420 |
cdr := cdrArg. |
|
1627 | 421 |
! |
422 |
||
423 |
nthPair:n |
|
424 |
"a helper: |
|
425 |
return the nth pair of a list" |
|
426 |
||
427 |
|cnt p| |
|
428 |
||
429 |
cnt := n. |
|
430 |
p := self. |
|
431 |
[ |
|
432 |
cnt := cnt - 1. |
|
433 |
cnt == 0 ifTrue:[^ p]. |
|
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 |
591 |
"true if the list contains a cycle. |
|
592 |
Floyd's cycle-finding algorithm" |
|
593 |
||
594 |
|t h i loopStartIndex loopSize| |
|
595 |
||
596 |
t := self cdr. t isNil ifTrue:[^ nil]. |
|
597 |
h := t cdr. h isNil ifTrue:[^ nil]. |
|
598 |
||
599 |
[t ~~ h] whileTrue:[ |
|
600 |
t := t cdr. |
|
601 |
h := h cdr. h isNil ifTrue:[^ nil]. |
|
602 |
h := h cdr. h isNil ifTrue:[^ nil]. |
|
603 |
]. |
|
604 |
||
605 |
"/ both h and t are now inside the cycle, |
|
606 |
"/ equidistant from the start of the cycle |
|
607 |
t := self. |
|
608 |
i := 1. |
|
609 |
[t ~~ h] whileTrue:[ |
|
610 |
t := t cdr. |
|
611 |
h := h cdr. |
|
612 |
i := i + 1. |
|
613 |
]. |
|
614 |
loopStartIndex := i. |
|
615 |
||
616 |
loopSize := 1. |
|
617 |
h := t cdr. |
|
618 |
[t ~~ h] whileTrue:[ |
|
619 |
h := h cdr. |
|
620 |
i := i + 1. |
|
621 |
loopSize := loopSize + 1. |
|
622 |
]. |
|
623 |
||
624 |
^ { loopStartIndex. loopSize } |
|
625 |
||
626 |
" |
|
627 |
|n1 n2 n3 n4 n5| |
|
628 |
||
629 |
n1 := Cons new car:1. |
|
630 |
n2 := Cons new car:2. |
|
631 |
n3 := Cons new car:3. |
|
632 |
n4 := Cons new car:4. |
|
633 |
n5 := Cons new car:5. |
|
634 |
n1 cdr: n2. |
|
635 |
n2 cdr: n3. |
|
636 |
n3 cdr: n4. |
|
637 |
n4 cdr: n5. |
|
638 |
n1 beginAndSizeOfCycle. |
|
639 |
n5 cdr: n3. |
|
640 |
n1 beginAndSizeOfCycle. |
|
641 |
" |
|
642 |
||
643 |
"Created: / 27-07-2012 / 00:00:36 / cg" |
|
644 |
! |
|
645 |
||
1043 | 646 |
isCons |
647 |
^ true |
|
648 |
! |
|
649 |
||
2753 | 650 |
isCyclic |
651 |
"true if the list contains a cycle" |
|
652 |
||
653 |
^ self beginAndSizeOfCycle notNil |
|
654 |
||
655 |
" |
|
656 |
|n1 n2 n3 n4 n5| |
|
657 |
||
658 |
n1 := Cons new car:1. |
|
659 |
n2 := Cons new car:2. |
|
660 |
n3 := Cons new car:3. |
|
661 |
n4 := Cons new car:4. |
|
662 |
n5 := Cons new car:5. |
|
663 |
n1 cdr: n2. |
|
664 |
n2 cdr: n3. |
|
665 |
n3 cdr: n4. |
|
666 |
n4 cdr: n5. |
|
667 |
n1 isCyclic. |
|
668 |
n5 cdr: n3. |
|
669 |
n1 isCyclic. |
|
670 |
" |
|
671 |
||
672 |
"Created: / 26-07-2012 / 23:32:52 / cg" |
|
673 |
! |
|
674 |
||
1043 | 675 |
size |
1074 | 676 |
"for smalltalkers: the lists length" |
677 |
||
1043 | 678 |
|l p rest| |
679 |
||
680 |
l := 1. |
|
681 |
p := self. |
|
682 |
[(rest := p cdr) notNil] whileTrue:[ |
|
683 |
l := l + 1. |
|
684 |
p := rest |
|
685 |
]. |
|
686 |
^ l |
|
687 |
||
688 |
" |
|
1074 | 689 |
(Cons fromArray:#( )) size |
690 |
(Cons fromArray:#(1)) size |
|
691 |
(Cons fromArray:#(1 2 3 4)) size |
|
1043 | 692 |
" |
693 |
! ! |
|
694 |
||
1377 | 695 |
!Cons methodsFor:'streaming'! |
696 |
||
697 |
readStream |
|
698 |
^ ConsStream on:self. |
|
699 |
! ! |
|
700 |
||
1043 | 701 |
!Cons class methodsFor:'documentation'! |
702 |
||
703 |
version |
|
3269 | 704 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.22 2014-04-30 11:03:07 cg Exp $' |
2437 | 705 |
! |
706 |
||
707 |
version_CVS |
|
3269 | 708 |
^ '$Header: /cvs/stx/stx/libbasic2/Cons.st,v 1.22 2014-04-30 11:03:07 cg Exp $' |
1043 | 709 |
! ! |
3269 | 710 |