2015-09-15 21:08:59 +03:00
|
|
|
public enum Diff: CustomDebugStringConvertible, CustomDocConvertible, Equatable {
|
2015-09-15 21:07:35 +03:00
|
|
|
case Empty
|
2015-09-11 18:38:42 +03:00
|
|
|
case Patch(Fix, Fix)
|
2015-09-11 18:40:01 +03:00
|
|
|
indirect case Copy(Syntax<Diff>)
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-16 20:52:53 +03:00
|
|
|
public static func Insert(term: Fix) -> Diff {
|
|
|
|
return .Patch(.Empty, term)
|
|
|
|
}
|
|
|
|
|
|
|
|
public static func Delete(term: Fix) -> Diff {
|
|
|
|
return .Patch(term, .Empty)
|
|
|
|
}
|
|
|
|
|
2015-09-15 20:55:09 +03:00
|
|
|
public var doc: Doc {
|
2015-07-18 22:43:49 +03:00
|
|
|
switch self {
|
2015-09-15 21:07:35 +03:00
|
|
|
case .Empty:
|
|
|
|
return .Empty
|
2015-07-18 22:43:49 +03:00
|
|
|
case let .Patch(a, b):
|
|
|
|
return .Horizontal([
|
2015-09-15 21:33:09 +03:00
|
|
|
.Wrap(.Text("{-"), Doc(a), .Text("-}")),
|
|
|
|
.Wrap(.Text("{+"), Doc(b), .Text("+}"))
|
2015-07-18 22:43:49 +03:00
|
|
|
])
|
|
|
|
case let .Copy(a):
|
2015-09-11 18:40:01 +03:00
|
|
|
return a.doc
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 21:08:59 +03:00
|
|
|
public var debugDescription: String {
|
2015-07-18 22:43:49 +03:00
|
|
|
switch self {
|
2015-09-15 21:07:35 +03:00
|
|
|
case .Empty:
|
|
|
|
return ".Empty"
|
2015-07-18 22:43:49 +03:00
|
|
|
case let .Patch(a, b):
|
2015-09-15 21:09:45 +03:00
|
|
|
return ".Patch(\(String(reflecting: a)), \(String(reflecting: b)))"
|
2015-07-18 22:43:49 +03:00
|
|
|
case let .Copy(a):
|
2015-09-15 21:09:45 +03:00
|
|
|
return ".Copy(\(String(reflecting: a)))"
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 19:23:24 +03:00
|
|
|
public var magnitude: Int {
|
|
|
|
switch self {
|
|
|
|
case .Empty:
|
|
|
|
return 0
|
|
|
|
case .Patch:
|
|
|
|
return 1
|
|
|
|
case let .Copy(s):
|
|
|
|
return s.map { $0.magnitude }.reduce(0, combine: +)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-11 18:38:42 +03:00
|
|
|
public init(_ a: Fix, _ b: Fix) {
|
2015-09-15 21:07:35 +03:00
|
|
|
switch (a, b) {
|
|
|
|
case (.Empty, .Empty):
|
|
|
|
self = .Empty
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-15 21:07:35 +03:00
|
|
|
case let (.Roll(a), .Roll(b)):
|
|
|
|
switch (a, b) {
|
|
|
|
case let (.Apply(a, aa), .Apply(b, bb)):
|
|
|
|
// fixme: SES
|
2015-09-16 22:00:06 +03:00
|
|
|
self = .Copy(.Apply(Diff(a, b), Diff.diff(aa, bb)))
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-15 21:07:35 +03:00
|
|
|
case let (.Abstract(p1, b1), .Abstract(p2, b2)):
|
2015-09-16 22:00:06 +03:00
|
|
|
self = .Copy(.Abstract(Diff.diff(p1, p2), Diff(b1, b2)))
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-15 21:07:35 +03:00
|
|
|
case let (.Assign(n1, v1), .Assign(n2, v2)) where n1 == n2:
|
|
|
|
self = .Copy(.Assign(n2, Diff(v1, v2)))
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-15 21:07:35 +03:00
|
|
|
case let (.Variable(n1), .Variable(n2)) where n1 == n2:
|
|
|
|
self = .Copy(.Variable(n2))
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-15 21:07:35 +03:00
|
|
|
case let (.Literal(v1), .Literal(v2)) where v1 == v2:
|
|
|
|
self = .Copy(.Literal(v2))
|
|
|
|
|
|
|
|
case let (.Group(n1, v1), .Group(n2, v2)):
|
2015-09-16 22:00:06 +03:00
|
|
|
self = .Copy(.Group(Diff(n1, n2), Diff.diff(v1, v2)))
|
2015-09-15 21:07:35 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
self = .Patch(Fix(a), Fix(b))
|
|
|
|
}
|
2015-07-18 22:43:49 +03:00
|
|
|
|
2015-09-11 18:35:51 +03:00
|
|
|
default:
|
2015-09-15 21:07:35 +03:00
|
|
|
self = Patch(a, b)
|
2015-09-11 18:35:51 +03:00
|
|
|
}
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
2015-09-16 16:56:15 +03:00
|
|
|
|
2015-09-16 16:59:13 +03:00
|
|
|
public static func diff<C1: CollectionType, C2: CollectionType where C1.Index : RandomAccessIndexType, C1.Generator.Element == Fix, C2.Index : RandomAccessIndexType, C2.Generator.Element == Fix>(a: C1, _ b: C2) -> [Diff] {
|
2015-09-16 21:58:53 +03:00
|
|
|
var (aa, bb) = (Stream(sequence: a), Stream(sequence: b))
|
|
|
|
var diffs: [Diff] = []
|
|
|
|
repeat {
|
|
|
|
if aa.isEmpty {
|
|
|
|
diffs += bb.lazy.map(Diff.Insert)
|
|
|
|
break
|
|
|
|
} else if bb.isEmpty {
|
|
|
|
diffs += aa.lazy.map(Diff.Delete)
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
diffs.append(Diff(aa.first!, bb.first!))
|
|
|
|
}
|
|
|
|
|
|
|
|
aa = aa.rest
|
|
|
|
bb = bb.rest
|
|
|
|
} while !aa.isEmpty || !bb.isEmpty
|
|
|
|
return diffs
|
2015-09-16 16:56:15 +03:00
|
|
|
}
|
2015-07-18 22:43:49 +03:00
|
|
|
}
|
2015-09-11 18:35:51 +03:00
|
|
|
|