43.sld 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. (define-library (srfi 43)
  2. (export
  3. ;; Constructors
  4. vector-unfold vector-unfold-right
  5. vector-reverse-copy
  6. vector-concatenate
  7. ;; Predicates
  8. vector-empty?
  9. vector=
  10. ;; Iteration
  11. vector-fold vector-fold-right
  12. vector-map vector-map!
  13. vector-for-each
  14. vector-count
  15. ;; Searching
  16. vector-index vector-index-right
  17. vector-skip vector-skip-right
  18. vector-binary-search
  19. vector-any vector-every
  20. ;; Mutators
  21. vector-swap!
  22. vector-reverse!
  23. vector-reverse-copy!
  24. ;; Conversion
  25. reverse-vector->list
  26. list->vector
  27. reverse-list->vector
  28. )
  29. (import
  30. (rename (scheme base) (list->vector %list->vector))
  31. (scheme case-lambda)
  32. (scheme cxr)
  33. (srfi 8)
  34. (srfi aux))
  35. (begin
  36. (define-aux-forms check-type let-optionals* :optional)
  37. ;; (CHECK-INDEX <vector> <index> <callee>) -> index
  38. ;; Ensure that INDEX is a valid index into VECTOR; if not, signal an
  39. ;; error stating that it is not and that this happened in a call to
  40. ;; CALLEE. Return INDEX when it is valid. (Note that this does NOT
  41. ;; check that VECTOR is indeed a vector.)
  42. (define check-index
  43. (if (debug-mode)
  44. (lambda (vec index callee)
  45. (let ((index (check-type integer? index callee)))
  46. (cond ((< index 0)
  47. (check-index vec
  48. (error "vector index too low"
  49. index
  50. `(into vector ,vec)
  51. `(while calling ,callee))
  52. callee))
  53. ((>= index (vector-length vec))
  54. (check-index vec
  55. (error "vector index too high"
  56. index
  57. `(into vector ,vec)
  58. `(while calling ,callee))
  59. callee))
  60. (else index))))
  61. (lambda (vec index callee)
  62. index)))
  63. ;; (CHECK-INDICES <vector>
  64. ;; <start> <start-name>
  65. ;; <end> <end-name>
  66. ;; <caller>) -> [start end]
  67. ;; Ensure that START and END are valid bounds of a range within
  68. ;; VECTOR; if not, signal an error stating that they are not, with
  69. ;; the message being informative about what the argument names were
  70. ;; called -- by using START-NAME & END-NAME --, and that it occurred
  71. ;; while calling CALLEE. Also ensure that VEC is in fact a vector.
  72. ;; Returns no useful value.
  73. (define check-indices
  74. (if (debug-mode)
  75. (lambda (vec start start-name end end-name callee)
  76. (let ((lose (lambda things
  77. (apply error "vector range out of bounds"
  78. (append things
  79. `(vector was ,vec)
  80. `(,start-name was ,start)
  81. `(,end-name was ,end)
  82. `(while calling ,callee)))))
  83. (start (check-type integer? start callee))
  84. (end (check-type integer? end callee)))
  85. (cond ((> start end)
  86. ;; I'm not sure how well this will work. The intent is that
  87. ;; the programmer tells the debugger to proceed with both a
  88. ;; new START & a new END by returning multiple values
  89. ;; somewhere.
  90. (receive (new-start new-end)
  91. (lose `(,end-name < ,start-name))
  92. (check-indices vec
  93. new-start start-name
  94. new-end end-name
  95. callee)))
  96. ((< start 0)
  97. (check-indices vec
  98. (lose `(,start-name < 0))
  99. start-name
  100. end end-name
  101. callee))
  102. ((>= start (vector-length vec))
  103. (check-indices vec
  104. (lose `(,start-name > len)
  105. `(len was ,(vector-length vec)))
  106. start-name
  107. end end-name
  108. callee))
  109. ((> end (vector-length vec))
  110. (check-indices vec
  111. start start-name
  112. (lose `(,end-name > len)
  113. `(len was ,(vector-length vec)))
  114. end-name
  115. callee))
  116. (else
  117. (values start end)))))
  118. (lambda (vec start start-name end end-name callee)
  119. (values start end))))
  120. )
  121. (include "43.body.scm"))