population.scm 895 B

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