From 2c21bcd58ad7cef42998fe4998f3b44e70a9f91c Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 26 Dec 2014 18:05:21 -0500 Subject: [PATCH] Add apply-patch! to diff.rkt --- rmacs/diff.rkt | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/rmacs/diff.rkt b/rmacs/diff.rkt index 5a42d2b..91d5116 100644 --- a/rmacs/diff.rkt +++ b/rmacs/diff.rkt @@ -5,7 +5,8 @@ ;; comparison, Bell Telephone Laboratories CSTR #41 (1976) ;; http://www.cs.dartmouth.edu/~doug/ -(provide diff-indices) +(provide diff-indices + apply-patch!) (require racket/set) (require racket/match) @@ -69,6 +70,25 @@ (cons (list (+ i 1) li (+ j 1) lj) (loop mi mj rest)) (loop mi mj rest))]))) +;; patch-indices is a result from a call to diff-indices +(define (apply-patch! patch-indices ;; DiffIndices + remove-elements! ;; Nat Nat -> Void + insert-elements! ;; Nat Nat Nat -> Void + ) + (for/fold [(skew 0)] [(patch patch-indices)] + (match-define (list old-i old-n new-i new-n) patch) + (define delta (- new-n old-n)) + (if (negative? delta) + (begin (remove-elements! (+ old-i skew) (- delta)) + (+ skew delta)) + skew)) + (for/fold [(skew 0)] [(patch patch-indices)] + (match-define (list old-i old-n new-i new-n) patch) + (define delta (- new-n old-n)) + (insert-elements! (+ old-i skew) (max 0 delta) new-n) + (+ skew delta)) + (void)) + (module+ test (require rackunit)