Debian Codegraph @

%3a146/gleckler/vector-edit.sls

raw
1 ;;;; `vector-edit'
2
3 ;;; Copyright MMIV-MMXV Arthur A. Gleckler. All rights reserved.
4
5 ;; Permission is hereby granted, free of charge, to any person
6 ;; obtaining a copy of this software and associated documentation
7 ;; files (the "Software"), to deal in the Software without
8 ;; restriction, including without limitation the rights to use, copy,
9 ;; modify, merge, publish, distribute, sublicense, and/or sell copies
10 ;; of the Software, and to permit persons to whom the Software is
11 ;; furnished to do so, subject to the following conditions:
12
13 ;; The above copyright notice and this permission notice shall be
14 ;; included in all copies or substantial portions of the Software.
15
16 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 ;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 ;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 ;; DEALINGS IN THE SOFTWARE.
24
25 ;;; `vector-edit' adds and/or removes elements from a vector
26 ;;; non-destructively, i.e. by returning a new vector. It maps
27 ;;; offsets in the original vector to offsets in the new vector so
28 ;;; that the caller doesn't have to perform these error-prone
29 ;;; calculations itself.
30 (library (srfi :146 gleckler vector-edit)
31 (export vector-edit vector-replace-one vector-without)
32 (import (rnrs)
33 (only (srfi :43) vector-copy! vector-copy))
34
35
36 (define (vector-without v start end)
37 "Return a copy of vector `v' without the elements with indices [start, end)."
38 (let* ((size (vector-length v))
39 (gap-size (- end start))
40 (new-size (- size gap-size))
41 (result (make-vector new-size)))
42 (vector-copy! result 0 v 0 start)
43 (vector-copy! result start v end size)
44 result))
45
46 (define (vector-replace-one v i e)
47 "Return a copy of vector `v' with the `i'th element replaced by `e'."
48 (let ((result (vector-copy v)))
49 (vector-set! result i e)
50 result))
51
52 (define-syntax vector-edit-total-skew
53 (syntax-rules (add drop)
54 ((_ s) s)
55 ((_ s (add i e) . rest)
56 (vector-edit-total-skew (+ s 1) . rest))
57 ((_ s (drop i c) . rest)
58 (vector-edit-total-skew (- s c) . rest))))
59
60 (define-syntax vector-edit-code
61 (syntax-rules (add drop)
62 ((_ v r o s)
63 (let ((index (vector-length v)))
64 (vector-copy! r (+ o s) v o index)
65 r))
66 ((_ v r o s (add i e) . rest)
67 (let ((index i))
68 (vector-copy! r (+ o s) v o index)
69 (vector-set! r (+ s index) e)
70 (let ((skew (+ s 1)))
71 (vector-edit-code v r index skew . rest))))
72 ((_ v r o s (drop i c) . rest)
73 (let ((index i))
74 (vector-copy! r (+ o s) v o index)
75 (let* ((dropped c)
76 (offset (+ index dropped))
77 (skew (- s dropped)))
78 (vector-edit-code v r offset skew . rest))))))
79
80 ;; <> Optimize this by allowing one to supply more than one value in
81 ;; `add' sub-expressions so that adjacent values can be inserted
82 ;; without extra computation.
83
84 ;; Given a vector `v' and a set of `(add i e)' and `(drop i c)' forms,
85 ;; return a new vector that is the result of applying insertions to
86 ;; and deletions from `v'. Interpret each `i' as an index into `v',
87 ;; each `e' as an element to be inserted into the resulting vector at
88 ;; the index corresponding to `i', and each `c' as a count of elements
89 ;; of `v' to be dropped starting at index `i'. The `i' values in the
90 ;; `add' and `drop' forms must never decrease from left to right.
91 ;; This is useful for doing insertions and deletions without
92 ;; constructing an intermediate vector.
93 (define-syntax vector-edit
94 (syntax-rules ()
95 ((_ v . rest)
96 (let ((result (make-vector (+ (vector-length v)
97 (vector-edit-total-skew 0 . rest)))))
98 (vector-edit-code v result 0 0 . rest))))))