Debian Codegraph @

%3a214/impl.sls

raw
1 #!r6rs
2
3 (library (srfi :214 impl)
4 (export
5 ;; Constructors
6 make-flexvector flexvector
7 flexvector-unfold flexvector-unfold-right
8 flexvector-copy flexvector-reverse-copy
9 flexvector-append flexvector-concatenate flexvector-append-subvectors
10
11 ;; Predicates
12 flexvector? flexvector-empty? flexvector=?
13
14 ;; Selectors
15 flexvector-ref flexvector-front flexvector-back flexvector-length
16
17 ;; Mutators
18 flexvector-add! flexvector-add-front! flexvector-add-back!
19 flexvector-remove! flexvector-remove-front! flexvector-remove-back!
20 flexvector-add-all! flexvector-remove-range! flexvector-clear!
21 flexvector-set! flexvector-swap!
22 flexvector-fill! flexvector-reverse!
23 flexvector-copy! flexvector-reverse-copy!
24 flexvector-append!
25
26 ;; Iteration
27 flexvector-fold flexvector-fold-right
28 flexvector-map flexvector-map! flexvector-map/index flexvector-map/index!
29 flexvector-append-map flexvector-append-map/index
30 flexvector-filter flexvector-filter! flexvector-filter/index flexvector-filter/index!
31 flexvector-for-each flexvector-for-each/index
32 flexvector-count flexvector-cumulate
33
34 ;; Searching
35 flexvector-index flexvector-index-right
36 flexvector-skip flexvector-skip-right
37 flexvector-binary-search
38 flexvector-any flexvector-every flexvector-partition
39
40 ;; Conversion
41 flexvector->vector flexvector->list flexvector->string
42 vector->flexvector list->flexvector string->flexvector
43 reverse-flexvector->list reverse-list->flexvector
44 generator->flexvector flexvector->generator)
45 (import (except (rnrs) vector-fill!)
46 (srfi :26)
47 (srfi :214 parameters)
48 (except (srfi :133 vectors) vector->list list->vector)
49 (srfi :158))
50
51 ;;; Utilities
52
53 ;; Checks if number is non-negative
54 (define (nonnegative? num)
55 (and (number? num) (>= num 0)))
56
57 ;; Returns negated predicate.
58 (define (negate pred?)
59 (lambda args
60 (not (apply pred? args))))
61
62 ;; Asserts that index is valid for the given flexvector.
63 (define (assert-index-validity fv . ids)
64 (let ([len (flexvector-len fv)])
65 (for-each (lambda (i)
66 (assert (and (>= i 0) (< i len))))
67 ids)))
68
69 ;; Collects groups of `num` length in lst into a new list.
70 (define (group lst num)
71 (define (split l n)
72 (let loop ([acc (list)]
73 [ls l]
74 [count 0])
75 (cond [(>= count n) (values (reverse acc) ls)]
76 [(null? ls) (error group "Not enough elements for a group" lst num)]
77 [else (loop (cons (car ls) acc)
78 (cdr ls)
79 (+ count 1))])))
80 (assert (positive? num))
81 (let loop ([acc (list)]
82 [source lst])
83 (cond [(null? source) (reverse acc)]
84 [else (let-values ([(grp rest) (split source num)])
85 (loop (cons grp acc) rest))])))
86
87 ;; Flexvector is represented as backing storage and number of used slots within.
88 (define-record-type (%flexvector %make-flexvector flexvector?)
89 (nongenerative flexvector-vtlmh5fxbj)
90 (fields (mutable vec flexvector-vec flexvector-vec-set!)
91 (mutable len flexvector-len flexvector-len-set!)))
92
93 ;; Convenience wrapper to deconstruct flexvectors into parts.
94 (define-syntax with-flexvectors
95 (syntax-rules ()
96 [(_ ([(vec len) fv] rest ...) body ...)
97 (let* ([fv* fv]
98 [vec (flexvector-vec fv*)]
99 [len (flexvector-len fv*)])
100 (with-flexvectors (rest ...) body ...))]
101 [(_ () body ...)
102 (let ()
103 body ...)]))
104
105 ;; Calls body with start and end clamped to dimensions of the flexvector.
106 (define-syntax with-clamped-range
107 (syntax-rules ()
108 [(_ (fv start end) body ...)
109 (let ([start (max 0 start)]
110 [end (min end (flexvector-len fv))])
111 (assert (<= start end))
112 (let ()
113 body ...))]))
114
115 ;; Convenience definition facility for procedures that accept [start, [end]] optional arguments.
116 (define-syntax define-with-range
117 (syntax-rules (of)
118 [(_ (name fv ... start (end of what)) body ...)
119 (define name
120 (case-lambda
121 [(fv ...)
122 (name fv ... 0)]
123 [(fv ... start)
124 (name fv ... start (flexvector-len what))]
125 [(fv ... start end)
126 (with-clamped-range (what start end)
127 body ...)]))]))
128
129 ;; Returns shortest length of given vectors.
130 (define (shortest-length vecs)
131 (apply min (map flexvector-len vecs)))
132
133 ;; Shortcut macro for over-vectors iterations.
134 (define-syntax with-shortest-length
135 (syntax-rules ()
136 [(_ [(vecs shortest-len) vecs-expr] body ...)
137 (let* ([vecs vecs-expr]
138 [shortest-len (shortest-length vecs)])
139 body ...)]))
140
141 ;; Returns flexvector capacity.
142 (define (flexvector-cap fv)
143 (vector-length (flexvector-vec fv)))
144
145 ;; Makes sure that the flexvector has enough capacity
146 (define (ensure-capacity! fv required-cap)
147 (let ([current-cap (flexvector-cap fv)])
148 (unless (>= current-cap required-cap)
149 (let* ([vec (flexvector-vec fv)]
150 [new-cap ((flexvector-capacity-estimator) current-cap required-cap)]; (exact (expt 2 (+ (floor (log required-cap 2)) 1)))]
151 [new-vec (make-vector new-cap)])
152 (vector-copy! new-vec 0 vec 0 (vector-length vec))
153 (flexvector-vec-set! fv new-vec)))))
154
155 ;; Makes sure capacity requested for flexvector creation is no less than
156 ;; allowed minimal one.
157 (define (ensure-valid-capacity requested-size)
158 (assert (and (exact? requested-size) (>= requested-size 0)))
159 (max requested-size (flexvector-min-capacity)))
160
161 ;; Constructs flexvector with given size and fill.
162 (define make-flexvector
163 (case-lambda
164 [(size)
165 (assert (nonnegative? size))
166 (%make-flexvector (make-vector (ensure-valid-capacity size)) size)]
167 [(size fill)
168 (assert (nonnegative? size))
169 (%make-flexvector (make-vector (ensure-valid-capacity size) fill) size)]))
170
171 ;; Constructs flexvector from given elements.
172 (define (flexvector . els)
173 (cond [(null? els) (make-flexvector 0)]
174 [else (list->flexvector els)]))
175
176 ;; Generates flexvector by applying func to seeds generated by gen until pred returns true.
177 (define (flexvector-unfold pred func gen . seeds)
178 (let ([fv (flexvector)])
179 (do [(seeds seeds (let-values [(seeds (apply gen seeds))]
180 seeds))]
181 [(apply pred seeds) fv]
182 (flexvector-add-back! fv (apply func seeds)))))
183
184 ;; Generates flexvector like flexvector-unfold does but fill the resulting vector starting
185 ;; from the right end.
186 (define (flexvector-unfold-right pred func gen . seeds)
187 (let ([fv (apply flexvector-unfold pred func gen seeds)])
188 (flexvector-reverse! fv)
189 fv))
190
191 ;; Makes a copy of the flexvector.
192 (define-with-range (flexvector-copy fv start (end of fv))
193 (with-flexvectors ([(vec len) fv])
194 (let ([result (make-flexvector (- end start))])
195 (vector-copy! (flexvector-vec result) 0 vec start end)
196 result)))
197
198 ;; Makes a reverse copy of the flexvector.
199 (define (flexvector-reverse-copy . args)
200 (let ([copy (apply flexvector-copy args)])
201 (flexvector-reverse! copy)
202 copy))
203
204 ;; Appends flexvectors together.
205 (define (flexvector-append fv . rest)
206 (flexvector-concatenate (cons fv rest)))
207
208 ;; Concatenates given list of flexvectors together.
209 (define (flexvector-concatenate fvs)
210 (let* ([total-len (fold-left + 0 (map flexvector-len fvs))]
211 [result (make-flexvector total-len)])
212 (let loop ([vecs fvs]
213 [offset 0])
214 (cond [(null? vecs) result]
215 [else
216 (let ([current (car vecs)])
217 (flexvector-copy! result offset current)
218 (loop (cdr vecs) (+ offset (flexvector-len current))))]))))
219
220 ;; Appends sequence of subvectors specified by vector + offset + count.
221 (define (flexvector-append-subvectors . args)
222 (let* ([triplets (group args 3)]
223 [subvecs (map (lambda (triplet)
224 (apply flexvector-copy triplet))
225 triplets)])
226 (flexvector-concatenate subvecs)))
227
228 ;; Is the flexvector empty?
229 (define (flexvector-empty? fv)
230 (zero? (flexvector-len fv)))
231
232 ;; Compares flexvectors. They should have same lengths and their
233 ;; elements should be equal according to elt=?.
234 (define (flexvector=? elt=? . fvs)
235 (define (combine result . es)
236 (and result (apply elt=? es)))
237 (let ([lens (map flexvector-len fvs)])
238 (or (null? fvs) (null? (cdr fvs))
239 (and (apply = lens)
240 (apply flexvector-fold combine #t fvs)))))
241
242 ;; References element at given index in the flexvector.
243 (define (flexvector-ref fv index)
244 (with-flexvectors ([(vec len) fv])
245 (assert-index-validity fv index)
246 (vector-ref vec index)))
247
248 ;; References the first element of an (assumed) non empty flexvector.
249 (define (flexvector-front fv)
250 (flexvector-ref fv 0))
251
252 ;; References the last element of an (assumed) non empty flexvector.
253 (define (flexvector-back fv)
254 (let ([len (flexvector-len fv)])
255 (flexvector-ref fv (- len 1))))
256
257 ;; Returns the length of the flexvector.
258 (define flexvector-length flexvector-len)
259
260 ;; Adds elements els at position i shifting existing elements to the right.
261 (define (flexvector-add! fv i . els)
262 (flexvector-add-all! fv i els))
263
264 ;; Adds elements els at the beginning of the flexvector.
265 (define (flexvector-add-front! fv . els)
266 (flexvector-add-all! fv 0 els))
267
268 ;; Adds elements els at the end of the flexvector.
269 (define (flexvector-add-back! fv . els)
270 (flexvector-add-all! fv (flexvector-len fv) els))
271
272 ;; Adds elements to the flexvector starting at index i.
273 (define (flexvector-add-all! fv i els)
274 (let* ([count (length els)]
275 [len (flexvector-len fv)]
276 [total-len (+ len count)])
277 (ensure-capacity! fv total-len)
278 (let ([vec (flexvector-vec fv)])
279 (vector-copy! vec (+ i count) vec i len)
280 (let loop ([ls els]
281 [offset 0])
282 (cond [(null? ls)
283 (flexvector-len-set! fv total-len)
284 fv]
285 [else (let ([el (car ls)])
286 (vector-set! vec (+ i offset) el)
287 (loop (cdr ls) (+ offset 1)))])))))
288
289 ;; Appends flexvector to the first one.
290 (define (flexvector-append! fv . fvs)
291 (let* ([len (flexvector-len fv)]
292 [total-len (fold-left + len (map flexvector-len fvs))])
293 (ensure-capacity! fv total-len)
294 (let ([vec (flexvector-vec fv)])
295 (let loop ([fvs fvs]
296 [offset len])
297 (cond [(null? fvs)
298 (flexvector-len-set! fv total-len)
299 fv]
300 [else (with-flexvectors ([(current-vec current-len) (car fvs)])
301 (vector-copy! vec offset current-vec 0 current-len)
302 (loop (cdr fvs) (+ offset current-len)))])))))
303
304 ;; Removes element at position i shifting remaining elements accordingly.
305 (define (flexvector-remove! fv i)
306 (let ([value (flexvector-ref fv i)])
307 (flexvector-remove-range! fv i (+ i 1))
308 value))
309
310 ;; Removes the first element from the flexvector.
311 (define (flexvector-remove-front! fv)
312 (flexvector-remove! fv 0))
313
314 ;; Removes element from the back of flexvector.
315 (define (flexvector-remove-back! fv)
316 (flexvector-remove! fv (- (flexvector-len fv) 1)))
317
318 ;; Removes a range of elements from the vector.
319 (define flexvector-remove-range!
320 (case-lambda
321 [(fv start)
322 (flexvector-remove-range! fv start (flexvector-len fv))]
323 [(fv start end)
324 (with-flexvectors ([(vec len) fv])
325 (let* ([start (max start 0)]
326 [end (min len end)]
327 [move-count (- len end)]
328 [len-delta (- end start)])
329 (assert (<= start end))
330 (unless (zero? move-count)
331 (vector-copy! vec start vec end (+ end move-count)))
332 (flexvector-len-set! fv (- len len-delta))
333 fv))]))
334
335 ;; Clears the flexvector.
336 (define (flexvector-clear! fv)
337 (flexvector-len-set! fv 0)
338 fv)
339
340 ;; Sets the value at index i to el.
341 (define (flexvector-set! fv i el)
342 (assert-index-validity fv i)
343 (with-flexvectors ([(vec len) fv])
344 (let* ([val (vector-ref vec i)])
345 (vector-set! vec i el)
346 val)))
347
348 ;; Swaps elements of flexvector.
349 (define (flexvector-swap! fv i j)
350 (assert-index-validity fv i j)
351 (with-flexvectors ([(vec len) fv])
352 (vector-swap! vec i j)))
353
354 ;; Fills elements between start and end of the flexvector with given fill value.
355 (define-with-range (flexvector-fill! fv fill start (end of fv))
356 (with-flexvectors ([(vec len) fv])
357 (let ([start (max 0 start)]
358 [end (min end len)])
359 (vector-fill! vec fill start end)
360 fv)))
361
362 ;; Reverses the flexvector in place.
363 (define-with-range (flexvector-reverse! fv start (end of fv))
364 (let* ([len (- end start)]
365 [vec (flexvector-vec fv)]
366 [half (floor (/ len 2))])
367 (do [(index 0 (+ index 1))]
368 [(>= index half) fv]
369 (let ([jndex (- len index 1)])
370 (vector-swap! vec (+ start index) (+ start jndex))))))
371
372 ;; Copies section of flexvector `from` defined by `[start, end]` to flexvector `to` starting at index `at`.
373 (define-with-range (flexvector-copy! to at from start (end of from))
374 (let* ([len (flexvector-len to)]
375 [delta (- end start)]
376 [new-len (+ len delta)])
377 (assert (and (>= at 0) (<= at len)))
378 (ensure-capacity! to new-len)
379 (with-flexvectors ([(to-vec to-len) to]
380 [(from-vec from-len) from])
381 (vector-copy! to-vec at from-vec start end)
382 (flexvector-len-set! to (max len (+ at delta)))
383 to)))
384
385 ;; Copies section of flexvector `from` defined by `[start, end]` in reverse order to flexvector `to` starting at index `at`.
386 (define-with-range (flexvector-reverse-copy! to at from start (end of from))
387 (flexvector-copy! to at from start end)
388 (flexvector-reverse! to at (+ at (- end start))))
389
390 ;; Implementation of fold to reuse in left and right folds.
391 (define (flexvector-fold-impl index-proc reductor initial fv . rest)
392 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
393 (let loop ([index 0]
394 [state initial])
395 (cond [(>= index shortest-len) state]
396 [else (loop (+ index 1)
397 (apply reductor state (map (cut flexvector-ref <> (index-proc index shortest-len)) vecs)))]))))
398
399 ;; Folds flexvectors over initial value with reductor
400 (define (flexvector-fold reductor initial fv . rest)
401 (define (left-to-right index len)
402 index)
403 (apply flexvector-fold-impl left-to-right reductor initial fv rest))
404
405 ;; Folds flexvectors over initial value with reductor from right to left.
406 (define (flexvector-fold-right reductor initial fv . rest)
407 (define (right-to-left index len)
408 (- len index 1))
409 (apply flexvector-fold-impl right-to-left reductor initial fv rest))
410
411 ;; Helper map building procedure.
412 (define (flexvector-map/index-into! dst proc fv . rest)
413 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
414 (let loop ([index 0])
415 (cond [(>= index shortest-len) dst]
416 [else (let ([value (apply proc index (map (cut flexvector-ref <> index) vecs))])
417 (flexvector-set! dst index value)
418 (loop (+ index 1)))]))))
419
420 ;; Maps results of application of proc to index and elements of fv + rest back to fv.
421 (define (flexvector-map/index! proc fv . rest)
422 (apply flexvector-map/index-into! fv proc fv rest))
423
424 ;; Maps results of application of proc to elements of fv + rest back to fv.
425 (define (flexvector-map! proc fv . rest)
426 (define (ignore-index . args)
427 (apply proc (cdr args)))
428 (apply flexvector-map/index! ignore-index fv rest))
429
430 ;; Constructs a new flexvector from applications of proc to indices and elements of fv and rest.
431 (define (flexvector-map/index proc fv . rest)
432 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
433 (let ([result (make-flexvector shortest-len)])
434 (apply flexvector-map/index-into! result proc fv rest))))
435
436 ;; Maps results of application of proc to elements of fv + rest into fresh flexvector.
437 (define (flexvector-map proc fv . rest)
438 (define (ignore-index . args)
439 (apply proc (cdr args)))
440 (apply flexvector-map/index ignore-index fv rest))
441
442 ;; Appends results (which should be flexvectors) of application of proc to flexvectors (using index).
443 (define (flexvector-append-map/index proc fv . rest)
444 (flexvector-fold flexvector-append!
445 (flexvector)
446 (apply flexvector-map/index proc fv rest)))
447
448 ;; Appends results (which should be flexvectors) of application of proc to flexvectors.
449 (define (flexvector-append-map proc fv . rest)
450 (flexvector-fold flexvector-append!
451 (flexvector)
452 (apply flexvector-map proc fv rest)))
453
454 ;; Destructively updates fv to retain only those elements for which pred? returns true.
455 ;; pred? is given an index as first argument.
456 (define (flexvector-filter/index! pred? fv)
457 (with-flexvectors ([(vec len) fv])
458 (let loop ([check-index 0]
459 [fill-index 0])
460 (cond [(>= check-index len)
461 (flexvector-len-set! fv fill-index)
462 fv]
463 [(pred? check-index (flexvector-ref fv check-index))
464 (flexvector-set! fv fill-index (flexvector-ref fv check-index))
465 (loop (+ check-index 1) (+ fill-index 1))]
466 [else (loop (+ check-index 1) fill-index)]))))
467
468 ;; Destructively updates fv to retain only those elements for which pred? returns true.
469 (define (flexvector-filter! pred? fv)
470 (define (ignore-index index value)
471 (pred? value))
472 (flexvector-filter/index! ignore-index fv))
473
474 ;; Create a new vector that has only those elements for which pred? returns true.
475 ;; pred? is given an index as first argument.
476 (define (flexvector-filter/index pred? fv)
477 (flexvector-filter/index! pred? (flexvector-copy fv)))
478
479 ;; Create a new vector that has only those elements for which pred? returns true.
480 (define (flexvector-filter pred? fv)
481 (flexvector-filter! pred? (flexvector-copy fv)))
482
483 ;; Applies procedure proc to each element of vectors (stopping at shortest one).
484 ;; proc is given an index as first argument.
485 (define (flexvector-for-each/index proc fv . rest)
486 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
487 (do [(index 0 (+ index 1))]
488 [(>= index shortest-len)]
489 (apply proc index (map (cut flexvector-ref <> index) vecs)))))
490
491 ;; Applies procedure proc to each element of vectors (stopping at shortest one).
492 (define (flexvector-for-each proc fv . rest)
493 (define (ignore-index . args)
494 (apply proc (cdr args)))
495 (apply flexvector-for-each/index ignore-index fv rest))
496
497 ;; Counts elements for which pred? returns true.
498 (define (flexvector-count pred? fv . rest)
499 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
500 (let loop ([index 0]
501 [count 0])
502 (cond [(>= index shortest-len) count]
503 [(apply pred? (map (cut flexvector-ref <> index) vecs))
504 (loop (+ index 1) (+ count 1))]
505 [else (loop (+ index 1) count)]))))
506
507 ;; Constructs new vector using following rule - result[i] = (reductor result[i-1] fv[i])
508 ;; where result[-1] = initial;
509 (define (flexvector-cumulate reductor initial fv)
510 (with-flexvectors ([(vec len) fv])
511 (let* ([result (make-flexvector len)]
512 [new-vec (flexvector-vec result)])
513 (let loop ([index 0]
514 [state initial])
515 (cond [(>= index len) result]
516 [else (let ([new-state (reductor state (vector-ref vec index))])
517 (vector-set! new-vec index new-state)
518 (loop (+ index 1) new-state))])))))
519
520 ;; Finds first index that matches predicate.
521 (define (flexvector-index pred? fv . rest)
522 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
523 (let loop ([index 0])
524 (cond [(>= index shortest-len) #f]
525 [(apply pred? (map (cut flexvector-ref <> index) vecs)) index]
526 [else (loop (+ index 1))]))))
527
528 ;; Finds last index that matches predicate.
529 (define (flexvector-index-right pred? fv . rest)
530 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
531 (let loop ([index 0])
532 (cond [(>= index shortest-len) #f]
533 [(apply pred? (map (cut flexvector-ref <> (- shortest-len index 1)) vecs)) (- shortest-len index 1)]
534 [else (loop (+ index 1))]))))
535
536 ;; Skips elements matching predicate returning first index of non-matching element.
537 (define (flexvector-skip pred? fv . rest)
538 (apply flexvector-index (negate pred?) fv rest))
539
540 ;; Skips elements matching predicate returning first index of non-matching element moving from right to left.
541 (define (flexvector-skip-right pred? fv . rest)
542 (apply flexvector-index-right (negate pred?) fv rest))
543
544 ;; Performs binary search of a value using given direction computation function.
545 (define-with-range (flexvector-binary-search fv value comp start (end of fv))
546 (let ([vec (flexvector-vec fv)])
547 (vector-binary-search vec value comp start end)))
548
549 ;; Searches first element for which pred? returns generalized true value and returns that value. If no such value found returns #f.
550 (define (flexvector-any pred? fv . rest)
551 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
552 (let loop ([index 0])
553 (cond [(>= index shortest-len) #f]
554 [(apply pred? (map (cut flexvector-ref <> index) vecs))]
555 [else (loop (+ index 1))]))))
556
557 ;; Checks if all elements match given predicate and returns last result from it if they do. Otherwise returns #f.
558 (define (flexvector-every pred? fv . rest)
559 (with-shortest-length [(vecs shortest-len) (cons fv rest)]
560 (let loop ([index 0]
561 [last-value #t])
562 (cond [(>= index shortest-len) last-value]
563 [(apply pred? (map (cut flexvector-ref <> index) vecs)) => (lambda (val)
564 (loop (+ index 1) val))]
565 [else #f]))))
566
567 ;; Partitions the flexvector into flexvectors of elements that match predicate and those that do not match.
568 (define (flexvector-partition pred? fv)
569 (let ([matching (flexvector)]
570 [non-matching (flexvector)])
571 (flexvector-for-each (lambda (el)
572 (flexvector-add-back! (if (pred? el) matching non-matching) el))
573 fv)
574 (values matching non-matching)))
575
576 ;;; Conversion
577
578 ;; Creates a vector from given flexvector.
579 (define-with-range (flexvector->vector fv start (end of fv))
580 (let ([vec (flexvector-vec fv)])
581 (vector-copy vec start end)))
582
583 ;; Creates a flexvector from a given vector.
584 (define vector->flexvector
585 (case-lambda
586 [(vec)
587 (vector->flexvector vec 0)]
588 [(vec start)
589 (vector->flexvector vec start (vector-length vec))]
590 [(vec start end)
591 (let* ([len (- end start)]
592 [result (make-flexvector len)]
593 [fvec (flexvector-vec result)])
594 (vector-copy! fvec 0 vec start end)
595 result)]))
596
597 ;; Common implementation for flexvector->list and reverse-flexvector->list
598 (define (flexvector->list-impl finalizer fv start end)
599 (with-flexvectors ([(vec len) fv])
600 (let loop ([acc (list)]
601 [index 0])
602 (cond [(>= index len) (finalizer acc)]
603 [else (loop (cons (vector-ref vec index) acc)
604 (+ index 1))]))))
605
606 ;; Turns flexvector into list.
607 (define-with-range (flexvector->list fv start (end of fv))
608 (flexvector->list-impl reverse fv start end))
609
610 ;; Turns reversed flexvector into list.
611 (define-with-range (reverse-flexvector->list fv start (end of fv))
612 (define (identity l) l)
613 (flexvector->list-impl identity fv start end))
614
615 ;; Conversion helper from list to flexvector.
616 (define (list->flexvector-impl lst index-func)
617 (let* ([len (length lst)]
618 [fv (make-flexvector len)])
619 (let loop ([lst lst]
620 [index 0])
621 (cond [(null? lst) fv]
622 [else (flexvector-set! fv (index-func len index) (car lst))
623 (loop (cdr lst) (+ index 1))]))))
624
625 ;; Converts list into flexvector.
626 (define (list->flexvector lst)
627 (list->flexvector-impl lst (lambda (len index) index)))
628
629 ;; Converts reversed list into flexvector.
630 (define (reverse-list->flexvector lst)
631 (list->flexvector-impl lst (lambda (len index) (- len index 1))))
632
633 ;; Converts flexvector of chars to string.
634 (define-with-range (flexvector->string fv start (end of fv))
635 (let-values ([(out fin) (open-string-output-port)])
636 (with-flexvectors ([(vec len) fv])
637 (let loop ([index start])
638 (cond [(>= index end) (fin)]
639 [else (put-char out (vector-ref vec index))
640 (loop (+ index 1))])))))
641
642 ;; Converts string into a flexvector.
643 (define string->flexvector
644 (case-lambda
645 [(str)
646 (string->flexvector str 0)]
647 [(str start)
648 (string->flexvector str start (string-length str))]
649 [(str start end)
650 (let ([in (open-string-input-port (substring str start end))]
651 [fv (flexvector)])
652 (let loop ([ch (get-char in)])
653 (cond [(eof-object? ch) fv]
654 [else (flexvector-add-back! fv ch)
655 (loop (get-char in))])))]))
656
657 ;; Converts flexvector into generator.
658 (define (flexvector->generator fv)
659 (with-flexvectors ([(vec len) fv])
660 (let ([index 0])
661 (lambda ()
662 (cond [(>= index len) (eof-object)]
663 [else
664 (let ([value (vector-ref vec index)])
665 (set! index (+ index 1))
666 value)])))))
667
668 ;; Converts generator into a flexvector.
669 (define (generator->flexvector gen)
670 (let ([fv (flexvector)])
671 (generator-fold (lambda (v r) (flexvector-add-back! r v))
672 fv
673 gen))))