forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.go
130 lines (116 loc) · 2.62 KB
/
patch.go
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
// Copyright 2016 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package diff
import (
"bytes"
"github.com/ndau/noms/go/types"
)
// Patch is a list of difference objects that can be applied to a graph
// using ApplyPatch(). Patch implements a sort order that is useful for
// applying the patch in an efficient way.
type Patch []Difference
func (r Patch) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r Patch) Len() int {
return len(r)
}
var vals = map[types.DiffChangeType]int{types.DiffChangeRemoved: 0, types.DiffChangeModified: 1, types.DiffChangeAdded: 2}
func (r Patch) Less(i, j int) bool {
if r[i].Path.Equals(r[j].Path) {
return vals[r[i].ChangeType] < vals[r[j].ChangeType]
}
return pathIsLess(r[i].Path, r[j].Path)
}
// Utility methods on path
// TODO: Should these be on types.Path & types.PathPart?
func pathIsLess(p1, p2 types.Path) bool {
for i, pp1 := range p1 {
if len(p2) == i {
return false // p1 > p2
}
switch pathPartCompare(pp1, p2[i]) {
case -1:
return true // p1 < p2
case 1:
return false // p1 > p2
}
}
return len(p2) > len(p1) // if true p1 < p2, else p1 == p2
}
func fieldPathCompare(pp types.FieldPath, o types.PathPart) int {
switch opp := o.(type) {
case types.FieldPath:
if pp.Name == opp.Name {
return 0
}
if pp.Name < opp.Name {
return -1
}
return 1
case types.IndexPath:
return -1
case types.HashIndexPath:
return -1
}
panic("unreachable")
}
func indexPathCompare(pp types.IndexPath, o types.PathPart) int {
switch opp := o.(type) {
case types.FieldPath:
return 1
case types.IndexPath:
if pp.Index.Equals(opp.Index) {
if pp.IntoKey == opp.IntoKey {
return 0
}
if pp.IntoKey {
return -1
}
return 1
}
if pp.Index.Less(opp.Index) {
return -1
}
return 1
case types.HashIndexPath:
return -1
}
panic("unreachable")
}
func hashIndexPathCompare(pp types.HashIndexPath, o types.PathPart) int {
switch opp := o.(type) {
case types.FieldPath:
return 1
case types.IndexPath:
return 1
case types.HashIndexPath:
switch bytes.Compare(pp.Hash[:], opp.Hash[:]) {
case -1:
return -1
case 0:
if pp.IntoKey == opp.IntoKey {
return 0
}
if pp.IntoKey {
return -1
}
return 1
case 1:
return 1
}
}
panic("unreachable")
}
func pathPartCompare(pp, pp2 types.PathPart) int {
switch pp1 := pp.(type) {
case types.FieldPath:
return fieldPathCompare(pp1, pp2)
case types.IndexPath:
return indexPathCompare(pp1, pp2)
case types.HashIndexPath:
return hashIndexPathCompare(pp1, pp2)
}
panic("unreachable")
}