| 1 |
#!r6rs |
| 2 |
|
| 3 |
;; Extension to SRFI 214 that allows controlling some internal behaviors by |
| 4 |
;; overriding parameters. |
| 5 |
|
| 6 |
(library (srfi :214 parameters) |
| 7 |
(export flexvector-min-capacity |
| 8 |
flexvector-capacity-estimator) |
| 9 |
(import (rnrs) |
| 10 |
(srfi :39)) |
| 11 |
|
| 12 |
;; Check for capacity parameter. |
| 13 |
(define (assert-valid-capacity-value value) |
| 14 |
(assert (and (number? value) (> value 0))) |
| 15 |
value) |
| 16 |
|
| 17 |
;; Parameter controlling minimal flexvector capacity. |
| 18 |
(define flexvector-min-capacity |
| 19 |
(make-parameter 4 assert-valid-capacity-value)) |
| 20 |
|
| 21 |
;; Check for capacity estimator. |
| 22 |
;; Note that check by default calls the value to verify that |
| 23 |
;; it takes correct number of arguments and returns a number, so |
| 24 |
;; no side effects are expected. |
| 25 |
(define (assert-valid-capacity-estimator value) |
| 26 |
(assert (procedure? value)) |
| 27 |
(let* ([example-current 4] |
| 28 |
[example-requested 8] |
| 29 |
[example-cap (value example-current example-requested)]) |
| 30 |
(assert (and (number? example-cap) |
| 31 |
(exact? example-cap) |
| 32 |
(>= example-cap example-requested))) |
| 33 |
value)) |
| 34 |
|
| 35 |
;; Returns capacity that is returns multiple of current capacity enough to cover |
| 36 |
;; requested one. |
| 37 |
(define (multiple-of-current-estimator current requested) |
| 38 |
(assert (and (number? current) (number? requested) |
| 39 |
(> current 0) (> requested 0))) |
| 40 |
(let ([quot (exact (ceiling (/ requested current)))]) |
| 41 |
(* quot current))) |
| 42 |
|
| 43 |
;; Parameter whose value is a function, that, given current and required capacity |
| 44 |
;; returns the actual capacity to use for the flexvector. Basically, estimate |
| 45 |
;; how much to extend the vector to avoid reallocation in the future. |
| 46 |
(define flexvector-capacity-estimator |
| 47 |
(make-parameter multiple-of-current-estimator assert-valid-capacity-estimator))) |