z-set.scm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/util/z-set.scm
  8. ;;;
  9. ;;; Sets of integers implemented as integers.
  10. (define-module (ps-compiler util z-set)
  11. #:use-module (prescheme scheme48)
  12. #:export (make-empty-integer-set
  13. add-to-integer-set
  14. integer-set-not
  15. integer-set-ior
  16. integer-set-and
  17. integer-set-subtract
  18. integer-set-equal?
  19. map-over-integer-set))
  20. (define (make-empty-integer-set)
  21. 0)
  22. (define (add-to-integer-set set integer)
  23. (bitwise-ior set (arithmetic-shift 1 integer)))
  24. (define integer-set-chunk-size 24)
  25. (define word-mask (- (arithmetic-shift 1 integer-set-chunk-size) 1))
  26. ;; The nested loop reduces the amount of bignum arithmetic needed (and reduces
  27. ;; the time by as much as a factor of 10).
  28. (define (map-over-integer-set proc set)
  29. (do ((set set (arithmetic-shift set (- integer-set-chunk-size)))
  30. (i 0 (+ i integer-set-chunk-size))
  31. (l '() (do ((set (bitwise-and set word-mask) (arithmetic-shift set -1))
  32. (j 0 (+ j 1))
  33. (l l (if (odd? set)
  34. (cons (proc (+ i j)) l)
  35. l)))
  36. ((or (= 0 set) (>= j integer-set-chunk-size))
  37. l))))
  38. ((= 0 set)
  39. (reverse l))))
  40. (define integer-set-and bitwise-and)
  41. (define integer-set-ior bitwise-ior)
  42. (define integer-set-not bitwise-not)
  43. (define (integer-set-subtract set1 set2)
  44. (bitwise-and set1 (bitwise-not set2)))
  45. (define integer-set-equal? =)