expand-vec.scm 4.4 KB

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