From 91bdaf7ffa76dee079a07fd54fcaf1d0f84428f0 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Tue, 21 Sep 2021 17:33:03 +0200 Subject: store recipes incrementally --- slice/slice.go | 28 ++++++++++++++-------------- slice/slice_test.go | 8 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'slice') diff --git a/slice/slice.go b/slice/slice.go index 15be5dd..6d7cf10 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -4,25 +4,25 @@ import "reflect" type Slice []interface{} -type SliceDel int +type Del int -type SliceIns struct { +type Ins struct { Idx int Value []interface{} } -type SlicePatch struct { - Del []SliceDel - Ins []SliceIns +type Delta struct { + Del []Del + Ins []Ins } -func PatchSlice(source Slice, patch SlicePatch) (target Slice) { +func Patch(source Slice, delta Delta) (target Slice) { // apply Del part from patch to source into temp - size := len(source) - len(patch.Del) + size := len(source) - len(delta.Del) temp := make(Slice, size) fill := 0 prev := 0 - for _, del := range patch.Del { + for _, del := range delta.Del { di := int(del) copy(temp[fill:], source[prev:di]) fill += di - prev @@ -30,14 +30,14 @@ func PatchSlice(source Slice, patch SlicePatch) (target Slice) { } copy(temp[fill:], source[prev:]) // apply Ins part from patch to temp into target - for _, ins := range patch.Ins { + for _, ins := range delta.Ins { size += len(ins.Value) } target = make(Slice, size) fill = 0 prev = 0 tpos := 0 - for _, ins := range patch.Ins { + for _, ins := range delta.Ins { offset := ins.Idx - prev copy(target[fill:], temp[tpos:tpos+offset]) fill += offset @@ -49,7 +49,7 @@ func PatchSlice(source Slice, patch SlicePatch) (target Slice) { return } -func DiffSlice(source Slice, target Slice) (patch SlicePatch) { +func Diff(source Slice, target Slice) (delta Delta) { var si, ti int var found bool for ; si < len(source); si++ { @@ -57,18 +57,18 @@ func DiffSlice(source Slice, target Slice) (patch SlicePatch) { found = reflect.DeepEqual(target[i], source[si]) if found { if i != ti { - patch.Ins = append(patch.Ins, SliceIns{ti, target[ti:i]}) + delta.Ins = append(delta.Ins, Ins{ti, target[ti:i]}) } ti = i + 1 break } } if !found { - patch.Del = append(patch.Del, SliceDel(si)) + delta.Del = append(delta.Del, Del(si)) } } if ti < len(target) { - patch.Ins = append(patch.Ins, SliceIns{ti, target[ti:]}) + delta.Ins = append(delta.Ins, Ins{ti, target[ti:]}) } return } diff --git a/slice/slice_test.go b/slice/slice_test.go index b73946e..f6aecbc 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -9,13 +9,13 @@ import ( func TestPatch(t *testing.T) { source := Slice{1, 2, 3, 4} target := Slice{2, 5, 3, 6, 4, 7, 8} - patch := DiffSlice(source, target) - testutils.AssertSame(t, []SliceDel{0}, patch.Del, "Patch del part") - testutils.AssertSame(t, []SliceIns{ + patch := Diff(source, target) + testutils.AssertSame(t, []Del{0}, patch.Del, "Patch del part") + testutils.AssertSame(t, []Ins{ {1, Slice{5}}, {3, Slice{6}}, {5, Slice{7, 8}}, }, patch.Ins, "Patch ins part") - actual := PatchSlice(source, patch) + actual := Patch(source, patch) testutils.AssertSame(t, target, actual, "Target obtained from patch application") } -- cgit v1.2.3