hilbert.scm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Hilbert vectors are like vectors that grow as large as they need to.
  3. ; That is, they can be indexed by arbitrarily large nonnegative integers.
  4. ; The implementation allows for arbitrarily large gaps by arranging
  5. ; the entries in a tree.
  6. ; So-called because they live in an infinite-dimensional vector
  7. ; space...
  8. (define hilbert-log 8)
  9. (define hilbert-node-size (arithmetic-shift 1 hilbert-log))
  10. (define hilbert-mask (- hilbert-node-size 1))
  11. (define minus-hilbert-log (- 0 hilbert-log))
  12. (define-record-type hilbert :hilbert
  13. (make-hilbert height root)
  14. (height hilbert-height set-hilbert-height!)
  15. (root hilbert-root set-hilbert-root!))
  16. (define-record-discloser :hilbert
  17. (lambda (h)
  18. '(sparse-vector)))
  19. (define (make-sparse-vector)
  20. (make-hilbert 1 (make-vector hilbert-node-size #f)))
  21. (define (sparse-vector-ref hilbert index)
  22. (let recur ((height (hilbert-height hilbert))
  23. (index index))
  24. (if (= height 1)
  25. (let ((root (hilbert-root hilbert)))
  26. (if (< index (vector-length root))
  27. (vector-ref root index)
  28. #f))
  29. (let ((node (recur (- height 1)
  30. (arithmetic-shift index minus-hilbert-log))))
  31. (if node
  32. (vector-ref node (bitwise-and index hilbert-mask))
  33. #f)))))
  34. (define (sparse-vector-set! hilbert index value)
  35. (vector-set!
  36. (let recur ((height (hilbert-height hilbert))
  37. (index index))
  38. (if (= height 1)
  39. (make-higher-if-necessary hilbert index)
  40. (let ((index (arithmetic-shift index minus-hilbert-log)))
  41. (make-node-if-necessary
  42. (recur (- height 1) index)
  43. (bitwise-and index hilbert-mask)))))
  44. (bitwise-and index hilbert-mask)
  45. value))
  46. (define (make-higher-if-necessary hilbert index)
  47. (if (< index hilbert-node-size)
  48. (hilbert-root hilbert)
  49. (let ((new-root (make-vector hilbert-node-size #f)))
  50. (vector-set! new-root 0 (hilbert-root hilbert))
  51. (set-hilbert-root! hilbert new-root)
  52. (set-hilbert-height! hilbert (+ (hilbert-height hilbert) 1))
  53. (let ((index (arithmetic-shift index minus-hilbert-log)))
  54. (make-node-if-necessary (make-higher-if-necessary hilbert index)
  55. (bitwise-and index hilbert-mask))))))
  56. (define (make-node-if-necessary node index)
  57. (or (vector-ref node index)
  58. (let ((new (make-vector hilbert-node-size #f)))
  59. (vector-set! node index new)
  60. new)))
  61. ; For debugging
  62. (define (sparse-vector->list h)
  63. (let recur ((node (hilbert-root h))
  64. (height (hilbert-height h))
  65. (more '()))
  66. (if (= height 0)
  67. (if (or node (pair? more))
  68. (cons node more)
  69. '())
  70. (do ((i (- hilbert-node-size 1) (- i 1))
  71. (more more (recur (if node
  72. (vector-ref node i)
  73. #f)
  74. (- height 1) more)))
  75. ((< i 0) more)))))