Skip to content

Commit c5e1056

Browse files
committed
add readme and diff.Interface example with different input types
1 parent db34c54 commit c5e1056

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
diff
2+
====
3+
4+
A difference algorithm package for go.
5+
6+
The algorithm is described by Eugene Myers in
7+
["An O(ND) Difference Algorithm and its Variations"](http://www.xmailserver.org/diff2.pdf).
8+
9+
Example
10+
-------
11+
You can use diff.Ints and diff.Runes
12+
13+
d := &diff.Runes{[]rune("sögen"), []rune("mögen")}
14+
d.Diff() // returns []Changes{{0,0,1,1}}
15+
16+
or you can implement diff.Interface and use diff.Diff(data)
17+
18+
type MixedInput struct {
19+
A []int
20+
B []string
21+
}
22+
func (m *MixedInput) N() int {
23+
return len(m.A)
24+
}
25+
func (m *MixedInput) M() int {
26+
return len(m.B)
27+
}
28+
func (m *MixedInput) Equal(a, b int) bool {
29+
return m.A[a] == len(m.B[b])
30+
}
31+
32+
Documentation at http://go.pkgdoc.org/github.com/mb0/diff

example_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2009 Martin Schnabel. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package diff_test
6+
7+
import (
8+
"fmt"
9+
"github.com/mb0/diff"
10+
)
11+
12+
var names = map[string]int {
13+
"one": 1,
14+
"two": 2,
15+
"three": 3,
16+
}
17+
18+
// Diff on inputs with different representations
19+
type MixedInput struct {
20+
A []int
21+
B []string
22+
}
23+
24+
func (m *MixedInput) N() int {
25+
return len(m.A)
26+
}
27+
func (m *MixedInput) M() int {
28+
return len(m.B)
29+
}
30+
func (m *MixedInput) Equal(a, b int) bool {
31+
return m.A[a] == names[m.B[b]]
32+
}
33+
34+
func ExampleInterface() {
35+
m := &MixedInput{
36+
[]int{1, 2, 3, 1, 2, 2, 1},
37+
[]string{"three", "two", "one", "two", "one", "three"},
38+
}
39+
changes := diff.Diff(m)
40+
for _, c := range changes {
41+
fmt.Println("change at", c.A, c.B)
42+
}
43+
}

0 commit comments

Comments
 (0)