population.scm 955 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees
  3. (define (make-population)
  4. (list '<population>))
  5. (define (add-to-population! x pop)
  6. (if (not x) (assertion-violation 'add-to-population! "can't put #f in a population"))
  7. (if (not (weak-memq x (cdr pop)))
  8. (set-cdr! pop (cons (make-weak-pointer x) (cdr pop)))))
  9. (define (weak-memq x weaks)
  10. (if (null? weaks)
  11. #f
  12. (if (eq? x (weak-pointer-ref (car weaks)))
  13. weaks
  14. (weak-memq x (cdr weaks)))))
  15. (define (population-reduce cons nil pop)
  16. (do ((l (cdr pop) (cdr l))
  17. (prev pop l)
  18. (m nil (let ((w (weak-pointer-ref (car l))))
  19. (if w
  20. (cons w m)
  21. (begin (set-cdr! prev (cdr l))
  22. m)))))
  23. ((null? l) m)))
  24. (define (population->list pop)
  25. (population-reduce cons '() pop))
  26. (define (walk-population proc pop)
  27. (population-reduce (lambda (thing junk) (proc thing))
  28. #f
  29. pop))