Skip to content

allow precomputation of suffix array #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func Diff(old, new io.Reader, patch io.Writer) error {
return err
}

pbuf, err := diffBytes(obuf, nbuf)
pbuf, err := diffBytes(nil, obuf, nbuf)
if err != nil {
return err
}
Expand All @@ -200,18 +200,56 @@ func Diff(old, new io.Reader, patch io.Writer) error {
return err
}

func diffBytes(obuf, nbuf []byte) ([]byte, error) {
// BufSufData contains a buffer and its corresponding suffix array.
type BufSufData struct {
Buf []byte
Suf []int
}

// ComputeSuf computes a BufSufData from reader using qsufsort algorithm.
func ComputeSuf(reader io.Reader) (*BufSufData, error) {
buf, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return &BufSufData{
Buf: buf,
Suf: qsufsort(buf),
}, nil
}

// DiffWithSuf does the same operations as Diff with the exception that
// it optionally takes obufsuf (*BufSufData of old io.Reader) to avoid
// recreating the suffix array.
func DiffWithSuf(obufsuf *BufSufData, new io.Reader, patch io.Writer) error {
nbuf, err := ioutil.ReadAll(new)
if err != nil {
return err
}

pbuf, err := diffBytes(obufsuf.Suf, obufsuf.Buf, nbuf)
if err != nil {
return err
}

_, err = patch.Write(pbuf)
return err
}

func diffBytes(I []int, obuf, nbuf []byte) ([]byte, error) {
var patch seekBuffer
err := diff(obuf, nbuf, &patch)
err := diff(I, obuf, nbuf, &patch)
if err != nil {
return nil, err
}
return patch.buf, nil
}

func diff(obuf, nbuf []byte, patch io.WriteSeeker) error {
func diff(I []int, obuf, nbuf []byte, patch io.WriteSeeker) error {
var lenf int
I := qsufsort(obuf)
if I == nil {
I = qsufsort(obuf)
}
db := make([]byte, len(nbuf))
eb := make([]byte, len(nbuf))
var dblen, eblen int
Expand Down
49 changes: 34 additions & 15 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ package binarydist

import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"testing"
)

var diffT = []struct {
old *os.File
new *os.File
}{
{
old: mustWriteRandFile("test.old", 1e3, 1),
new: mustWriteRandFile("test.new", 1e3, 2),
},
{
old: mustOpen("testdata/sample.old"),
new: mustOpen("testdata/sample.new"),
},
}
func testFunc(t *testing.T, fDiff func(old, new io.Reader, patch io.Writer) error) {
t.Helper()

diffT := []struct {
old *os.File
new *os.File
}{
{
old: mustWriteRandFile("test.old", 1e3, 1),
new: mustWriteRandFile("test.new", 1e3, 2),
},
{
old: mustOpen("testdata/sample.old"),
new: mustOpen("testdata/sample.new"),
},
}

func TestDiff(t *testing.T) {
for _, s := range diffT {
got, err := ioutil.TempFile("/tmp", "bspatch.")
if err != nil {
Expand All @@ -43,7 +46,7 @@ func TestDiff(t *testing.T) {
panic(err)
}

err = Diff(s.old, s.new, got)
err = fDiff(s.old, s.new, got)
if err != nil {
t.Fatal("err", err)
}
Expand All @@ -65,3 +68,19 @@ func TestDiff(t *testing.T) {
}
}
}

func TestDiff(t *testing.T) {
testFunc(t, Diff)
}

func TestDiffWithSuf(t *testing.T) {
fDiff := func(old, new io.Reader, patch io.Writer) error {
oldSufStruct, err := ComputeSuf(old)
if err != nil {
return err
}

return DiffWithSuf(oldSufStruct, new, patch)
}
testFunc(t, fDiff)
}