
in the original GitHub fork: https://github.com/ntoronto/racket Some things about this are known to be broken (most egregious is that the array tests DO NOT RUN because of a problem in typed/rackunit), about half has no coverage in the tests, and half has no documentation. Fixes and docs are coming. This is committed now to allow others to find errors and inconsistency in the things that appear to be working, and to give the author a (rather incomplete) sense of closure.
30 lines
911 B
Racket
30 lines
911 B
Racket
#lang typed/racket/base
|
|
|
|
(require racket/fixnum
|
|
racket/performance-hint
|
|
"../unsafe.rkt")
|
|
|
|
(provide for/vector: for*/vector: vector-ref!)
|
|
|
|
(begin-encourage-inline
|
|
|
|
(: vector-ref!
|
|
(All (A B) (case-> ((Vectorof (U A False)) Integer (-> A) -> A)
|
|
((Vectorof (U A B)) Integer (-> A) (Any -> Boolean : B) -> A))))
|
|
(define vector-ref!
|
|
(case-lambda
|
|
[(vs i thnk) (vector-ref! vs i thnk not)]
|
|
[(vs i thnk nothing?)
|
|
(define n (vector-length vs))
|
|
(cond [(and (fixnum? i) (i . fx>= . 0) (i . fx< . n))
|
|
(define v (unsafe-vector-ref vs i))
|
|
(if (nothing? v)
|
|
(let ([v (thnk)])
|
|
(unsafe-vector-set! vs i v)
|
|
v)
|
|
v)]
|
|
[else
|
|
(raise-range-error 'vector-ref! "Vector" "" i vs 0 n #f)])]))
|
|
|
|
) ; begin-encourage-inline
|