/
edit.cljc
239 lines (201 loc) · 6.45 KB
/
edit.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
;;
;; Copyright (c) Huahai Yang. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file LICENSE at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
;;
(ns editscript.edit
#?(:clj (:import [clojure.lang PersistentVector IPersistentList IPersistentMap
IPersistentSet IPersistentVector])))
(defprotocol ^:no-doc IEdit
(auto-sizing [this path value])
(add-data [this path value])
(delete-data [this path])
(replace-data [this path value])
(replace-str [this path ops]))
(defprotocol IEditScript
(combine [this that]
"Concate that editscript onto this editscript, return the new editscript")
(get-size [this] "Report the size of the editscript")
(^:no-doc set-size [this size] "Set the size, return the script")
(edit-distance [this] "Report the edit distance, i.e number of operations")
(get-edits [this] "Report the edits as a vector")
(get-adds-num [this] "Report the number of additions")
(get-dels-num [this] "Report the number of deletions")
(get-reps-num [this] "Report the number of replacements"))
(defprotocol ^:no-doc IType
(get-type [this] "Return a type keyword, :val, :map, :lst, etc."))
(defn ^:no-doc nada
"A special type means 'not present'"
[]
(reify IType
(get-type [_] :nil)))
#?(:clj
(extend-protocol IType
IPersistentList
(get-type [_] :lst)
IPersistentMap
(get-type [_] :map)
IPersistentVector
(get-type [_] :vec)
IPersistentSet
(get-type [_] :set)
nil
(get-type [_] :val)
String
(get-type [_] :str)
Object
(get-type [_] :val))
:cljs
(extend-protocol IType
List
(get-type [_] :lst)
EmptyList
(get-type [_] :lst)
Cons
(get-type [_] :lst)
PersistentArrayMap
(get-type [_] :map)
PersistentHashMap
(get-type [_] :map)
PersistentTreeMap
(get-type [_] :map)
PersistentVector
(get-type [_] :vec)
Subvec
(get-type [_] :vec)
MapEntry
(get-type [_] :vec)
PersistentHashSet
(get-type [_] :set)
PersistentTreeSet
(get-type [_] :set)
nil
(get-type [_] :val)
string
(get-type [_] :str)
default
(get-type [_] :val)))
(defn- sizing*
[data size]
(let [up (fn [s] (inc ^long s))]
(if (#{:vec :lst :map :set} (get-type data))
(do (vswap! size up)
(doseq [child data]
(sizing* child size)))
(vswap! size up))))
(defn- sizing
[data]
(let [size (volatile! 0) ]
(sizing* data size)
@size))
(deftype ^:no-doc EditScript [^:unsynchronized-mutable ^PersistentVector edits
^boolean auto-sizing?
^:unsynchronized-mutable ^long size
^:unsynchronized-mutable ^long adds-num
^:unsynchronized-mutable ^long dels-num
^:unsynchronized-mutable ^long reps-num]
IEdit
(auto-sizing [this path value]
(when auto-sizing?
(set! size (long (+ 2 size (sizing path) (if value (sizing value) 0)))))
this)
(add-data [this path value]
(locking this
(set! adds-num (inc adds-num))
(set! edits (conj edits [path :+ value]))
(auto-sizing this path value)))
(delete-data [this path]
(locking this
(set! dels-num (inc dels-num))
(set! edits (conj edits [path :-]))
(auto-sizing this path nil)))
(replace-data [this path value]
(locking this
(set! reps-num (inc reps-num))
(set! edits (conj edits [path :r value]))
(auto-sizing this path value)))
(replace-str [this path ops]
(locking this
(set! reps-num (inc reps-num))
(set! edits (conj edits [path :s ops]))
(auto-sizing this path "")))
IEditScript
(combine [_ that]
(EditScript. (into edits (get-edits that))
auto-sizing?
(+ size (get-size that))
(+ adds-num (get-adds-num that))
(+ dels-num (get-dels-num that))
(+ reps-num (get-reps-num that))))
(get-size [_] size)
(set-size [this s] (set! size (long s)) this)
(get-edits [_] edits)
(get-adds-num [_] adds-num)
(get-dels-num [_] dels-num)
(get-reps-num [_] reps-num)
(edit-distance [_] (+ adds-num dels-num reps-num)))
(defn- valid-str-edits?
[data]
(and (vector? data)
(every? (fn [x]
(or (nat-int? x)
(and (vector? x)
(= 2 (count x))
(let [[op y] x]
(and
(#{:- :r :+} op)
(case op
:- (nat-int? y)
(:+ :r) (string? y)))))))
data)))
(defn- valid-edit?
[edit]
(when (vector? edit)
(let [c (count edit)]
(when (< 1 c 4)
(let [[path op data] edit]
(and (vector? path)
(#{:- :r :+ :s} op)
(if (= :- op)
(nil? data)
(= c 3))
(if (= :s op)
(valid-str-edits? data)
true)))))))
(defn valid-edits?
"Check if the given vector represents valid edits that can be turned into an
EditScript"
[edits]
(when (vector? edits)
(if (seq edits)
(every? valid-edit? edits)
true)))
(defn- count-ops
[edits]
(let [adds (volatile! 0)
dels (volatile! 0)
reps (volatile! 0)]
(doseq [[_ op _] edits]
(case op
:+ (vswap! adds inc)
:- (vswap! dels inc)
:r (vswap! reps inc)))
[@adds @dels @reps]))
(defn edits->script
"Create an EditScript instance from a vector of edits, like those obtained
through calling `get-edits` on an EditScript"
[edits]
(assert (valid-edits? edits) "Not a vector of valid edits")
(let [[adds dels reps] (count-ops edits)]
(->EditScript edits true (sizing edits) adds dels reps)))
#?(:clj (defmethod print-method EditScript
[x ^java.io.Writer writer]
(print-method (get-edits x) writer))
:cljs (extend-protocol IPrintWithWriter
EditScript
(-pr-writer [o writer opts]
(write-all writer (str (get-edits o))))))