population.scm 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ;;; Portd from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Jonathan Rees
  6. (define-module (prescheme population)
  7. #:use-module (prescheme scheme48)
  8. #:export (make-population
  9. add-to-population!
  10. population->list
  11. walk-population))
  12. (define (make-population)
  13. (list '<population>))
  14. (define (add-to-population! x pop)
  15. (if (not x) (assertion-violation 'add-to-population! "can't put #f in a population"))
  16. (if (not (weak-memq x (cdr pop)))
  17. (set-cdr! pop (cons (make-weak-pointer x) (cdr pop)))))
  18. (define (weak-memq x weaks)
  19. (if (null? weaks)
  20. #f
  21. (if (eq? x (weak-pointer-ref (car weaks)))
  22. weaks
  23. (weak-memq x (cdr weaks)))))
  24. (define (population-reduce cons nil pop)
  25. (do ((l (cdr pop) (cdr l))
  26. (prev pop l)
  27. (m nil (let ((w (weak-pointer-ref (car l))))
  28. (if w
  29. (cons w m)
  30. (begin (set-cdr! prev (cdr l))
  31. m)))))
  32. ((null? l) m)))
  33. (define (population->list pop)
  34. (population-reduce cons '() pop))
  35. (define (walk-population proc pop)
  36. (population-reduce (lambda (thing junk) (proc thing))
  37. #f
  38. pop))
  39. ;; FIXME: do we *actually* need weak refs?
  40. (define (make-weak-pointer x) x)
  41. (define (weak-pointer-ref x) x)