Advertisement
zephyrtronium

Untitled

Mar 9th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.89 KB | None | 0 0
  1. type Write interface {
  2.     Org() uint64
  3.     Len() uint64
  4.     Write(b io.WriterAt) error
  5.     String() string
  6. }
  7.  
  8. type Patch []Write
  9.  
  10. func Conflict(p1, p2 Patch) (c1, c2 []Write) {
  11.     i, j := 0, 0
  12.     w1, w2 := p1[0], p2[0]
  13.     org1, org2 := w1.Org(), w2.Org()
  14.     end1, end2 := org1 + w1.Len(), org2 + w2.Len()
  15.     for {
  16.         if org1 >= org2 && org1 < end2 || org2 >= org1 && org2 < end1 {
  17.             c1 = append(c1, w1)
  18.             c2 = append(c2, w2)
  19.         }
  20.         if end1 < end2 {
  21.             i++
  22.             if i >= len(p1) {
  23.                 return
  24.             }
  25.             w1 = p1[i]
  26.             org1 = w1.Org()
  27.             end1 = org1 + w1.Len()
  28.         } else if end1 > end2 {
  29.             j++
  30.             if j >= len(p2) {
  31.                 return
  32.             }
  33.             w2 = p2[j]
  34.             org2 = w2.Org()
  35.             end2 = org2 + w2.Len()
  36.         } else {
  37.             i++
  38.             j++
  39.             if i >= len(p1) || j >= len(p2) {
  40.                 return
  41.             }
  42.             w1, w2 = p1[i], p2[j]
  43.             org1, org2 = w1.Org(), w2.Org()
  44.             end1, end2 = org1 + w1.Len(), org2 + w2.Len()
  45.         }
  46.     }
  47.     return
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement