aboutsummaryrefslogtreecommitdiff
path: root/slice/slice.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-10-15 12:03:44 +0200
committern-peugnet <n.peugnet@free.fr>2021-10-15 12:03:44 +0200
commit3ffafd6499f50ea71d1ad1a7ac554f4c4448ed6c (patch)
tree6caec73906e90cd3e4fbe6592868e979f7d4479e /slice/slice.go
parent5016267b78261b07ac24190584707bf2745160a8 (diff)
downloaddna-backup-3ffafd6499f50ea71d1ad1a7ac554f4c4448ed6c.tar.gz
dna-backup-3ffafd6499f50ea71d1ad1a7ac554f4c4448ed6c.zip
add copyright and license notice + remove dead code
Diffstat (limited to 'slice/slice.go')
-rw-r--r--slice/slice.go91
1 files changed, 0 insertions, 91 deletions
diff --git a/slice/slice.go b/slice/slice.go
deleted file mode 100644
index e5b959e..0000000
--- a/slice/slice.go
+++ /dev/null
@@ -1,91 +0,0 @@
-package slice
-
-import (
- "fmt"
- "reflect"
-)
-
-type Slice []interface{}
-
-type Del int
-
-type Ins struct {
- Idx int
- Value []interface{}
-}
-
-type insData struct {
- idx int
- count int
-}
-
-type Delta struct {
- Del []Del
- Ins []Ins
-}
-
-func (d Delta) String() string {
- data := make([]insData, len(d.Ins))
- for i, ins := range d.Ins {
- data[i] = insData{ins.Idx, len(ins.Value)}
- }
- return fmt.Sprintf("{Del: %d Ins: %+v}", d.Del, data)
-}
-
-func Patch(source Slice, delta Delta) (target Slice) {
- // apply Del part from patch to source into temp
- size := len(source) - len(delta.Del)
- temp := make(Slice, size)
- fill := 0
- prev := 0
- for _, del := range delta.Del {
- di := int(del)
- copy(temp[fill:], source[prev:di])
- fill += di - prev
- prev = di + 1
- }
- copy(temp[fill:], source[prev:])
- // apply Ins part from patch to temp into target
- for _, ins := range delta.Ins {
- size += len(ins.Value)
- }
- target = make(Slice, size)
- fill = 0
- prev = 0
- tpos := 0
- for _, ins := range delta.Ins {
- offset := ins.Idx - prev
- copy(target[fill:], temp[tpos:tpos+offset])
- fill += offset
- tpos += offset
- copy(target[fill:], ins.Value)
- fill += len(ins.Value)
- prev = ins.Idx + len(ins.Value)
- }
- copy(target[fill:], temp[tpos:])
- return
-}
-
-func Diff(source Slice, target Slice) (delta Delta) {
- var si, ti int
- var found bool
- for ; si < len(source); si++ {
- for i := ti; i < len(target); i++ {
- found = reflect.DeepEqual(target[i], source[si])
- if found {
- if i != ti {
- delta.Ins = append(delta.Ins, Ins{ti, target[ti:i]})
- }
- ti = i + 1
- break
- }
- }
- if !found {
- delta.Del = append(delta.Del, Del(si))
- }
- }
- if ti < len(target) {
- delta.Ins = append(delta.Ins, Ins{ti, target[ti:]})
- }
- return
-}