expand-vec.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/expand-vec.scm
  8. ;;;
  9. ;;; Vectors of infinite length. These work as do regular vectors except that
  10. ;;; they have no fixed size. XVECTOR-LENGTH is one more than the highest index
  11. ;;; that has been passed to XVECTOR-SET!.
  12. ;;;
  13. ;;; (MAKE-XVECTOR <fill>)
  14. ;;; (XVECTOR-LENGTH <xvec>)
  15. ;;; (XVECTOR-REF <xvec> <index>)
  16. ;;; (XVECTOR-SET! <xvec> <index> <value>)
  17. ;;; (XVECTOR->VECTOR <xvec>)
  18. ;;;
  19. ;;; The actual record fields are:
  20. ;;; default - the fill value
  21. ;;; length - total length of internal vector(s)
  22. ;;; contents - either:
  23. ;;; a: vector of entries, or
  24. ;;; b: vector of vectors of entries, vector has #f's after the last needed
  25. ;;; sub-vector, each sub-vector has length XVEC-MAX-CONTENTS-SIZE
  26. ;;; max - maximum index used in a XVECTOR-SET!
  27. (define-module (ps-compiler util expand-vec)
  28. #:use-module (srfi srfi-9)
  29. #:export (export make-xvector
  30. xvector-length
  31. xvector-ref
  32. xvector-set!
  33. xvector-length
  34. xvector->vector))
  35. (define-record-type :expanding-vector
  36. (make-expanding-vector default length contents max)
  37. expanding-vector?
  38. (default expanding-vector-default)
  39. (length expanding-vector-length set-expanding-vector-length!)
  40. (contents expanding-vector-contents set-expanding-vector-contents!)
  41. (max expanding-vector-max set-expanding-vector-max!))
  42. (define (make-xvector default)
  43. (make-expanding-vector default 0 '#() -1))
  44. ;; Maximum size of any internal vector
  45. (define xvec-max-contents-size 1024)
  46. ;; XVECTOR-LENGTH returns 1 + the maximum index passed to XVECTOR-SET!
  47. (define (xvector-length xvec)
  48. (+ 1 (expanding-vector-max xvec)))
  49. ;; If INDEX points outside current storage, the default is returned, otherwise
  50. ;; the value is extracted from the one-or-two-level contents vector.
  51. (define (xvector-ref xvec index)
  52. (let ((length (expanding-vector-length xvec)))
  53. (cond ((>= index length)
  54. (expanding-vector-default xvec))
  55. ((<= length xvec-max-contents-size)
  56. (vector-ref (expanding-vector-contents xvec) index))
  57. (else
  58. (vector-ref (vector-ref (expanding-vector-contents xvec)
  59. (quotient index xvec-max-contents-size))
  60. (remainder index xvec-max-contents-size))))))
  61. ;; Same as the above, setting instead of extracting. INDEX is checked agains
  62. ;; the maximum index, and the contents may have to be expanded.
  63. (define (xvector-set! xvec index value)
  64. (let ((length (expanding-vector-length xvec)))
  65. (if (> index (expanding-vector-max xvec))
  66. (set-expanding-vector-max! xvec index))
  67. (cond ((>= index length)
  68. (expand-expanding-vector xvec)
  69. (xvector-set! xvec index value))
  70. ((<= length xvec-max-contents-size)
  71. (vector-set! (expanding-vector-contents xvec) index value))
  72. (else
  73. (vector-set! (vector-ref (expanding-vector-contents xvec)
  74. (quotient index xvec-max-contents-size))
  75. (remainder index xvec-max-contents-size)
  76. value)))))
  77. ;; Copy an expanding vector into a regular vector.
  78. (define (xvector->vector xvec)
  79. (let ((new (make-vector (xvector-length xvec))))
  80. (do ((i 0 (+ i 1)))
  81. ((>= i (xvector-length xvec)))
  82. (vector-set! new i (xvector-ref xvec i)))
  83. new))
  84. ;; Expand an expanding vector. A new length and contents are produced and
  85. ;; put in XVEC. If LENGTH is below the maximum for a contents vector it is
  86. ;; doubled in size. If the two-level structure is being used, a new sub-
  87. ;; vector is added to it.
  88. (define (expand-expanding-vector xvec)
  89. (let ((length (expanding-vector-length xvec))
  90. (contents (expanding-vector-contents xvec))
  91. (default (expanding-vector-default xvec))
  92. (finish (lambda (length contents)
  93. (set-expanding-vector-length! xvec length)
  94. (set-expanding-vector-contents! xvec contents))))
  95. (if (< length xvec-max-contents-size)
  96. (let ((new (expand-vector contents default)))
  97. (finish (vector-length new) new))
  98. (let ((contents (cond ((= length xvec-max-contents-size)
  99. (let ((new (make-vector 4 #f)))
  100. (vector-set! new 0 contents)
  101. new))
  102. ((vector-ref contents
  103. (- (vector-length contents) 1))
  104. (expand-vector contents #f))
  105. (else
  106. contents))))
  107. (vector-set! contents
  108. (quotient length xvec-max-contents-size)
  109. (make-vector xvec-max-contents-size default))
  110. (finish (+ length xvec-max-contents-size) contents)))))
  111. ;; Make a new vector twice the length of OLD and copy the contents of OLD into
  112. ;; it. DEFAULT is used to fill in the other slots.
  113. (define (expand-vector old default)
  114. (let* ((length (vector-length old))
  115. (new (make-vector (if (= 0 length) 4 (* 2 length)) default)))
  116. (do ((i 0 (+ i 1)))
  117. ((>= i length))
  118. (vector-set! new i (vector-ref old i)))
  119. new))