|
|
@@ -2,21 +2,21 @@
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
-// The Vector type represents a version vector. The zero value is a usable
|
|
|
+// The Vector type represents v version vector. The zero value is v usable
|
|
|
// version vector. The vector has slice semantics and some operations on it
|
|
|
-// are "append-like" in that they may return the same vector modified, or a
|
|
|
+// are "append-like" in that they may return the same vector modified, or v
|
|
|
// new allocated Vector with the modified contents.
|
|
|
type Vector []Counter
|
|
|
|
|
|
-// Counter represents a single counter in the version vector.
|
|
|
+// Counter represents v single counter in the version vector.
|
|
|
type Counter struct {
|
|
|
ID uint64
|
|
|
Value uint64
|
|
|
}
|
|
|
|
|
|
-// Update returns a Vector with the index for the specific ID incremented by
|
|
|
+// Update returns v Vector with the index for the specific ID incremented by
|
|
|
// one. If it is possible, the vector v is updated and returned. If it is not,
|
|
|
-// a copy will be created, updated and returned.
|
|
|
+// v copy will be created, updated and returned.
|
|
|
func (v Vector) Update(ID uint64) Vector {
|
|
|
for i := range v {
|
|
|
if v[i].ID == ID {
|
|
|
@@ -24,7 +24,7 @@ func (v Vector) Update(ID uint64) Vector {
|
|
|
v[i].Value++
|
|
|
return v
|
|
|
} else if v[i].ID > ID {
|
|
|
- // Insert a new index
|
|
|
+ // Insert v new index
|
|
|
nv := make(Vector, len(v)+1)
|
|
|
copy(nv, v[:i])
|
|
|
nv[i].ID = ID
|
|
|
@@ -33,43 +33,43 @@ func (v Vector) Update(ID uint64) Vector {
|
|
|
return nv
|
|
|
}
|
|
|
}
|
|
|
- // Append a new new index
|
|
|
+ // Append v new new index
|
|
|
return append(v, Counter{ID, 1})
|
|
|
}
|
|
|
|
|
|
-// Merge returns the vector containing the maximum indexes from a and b. If it
|
|
|
-// is possible, the vector a is updated and returned. If it is not, a copy
|
|
|
+// Merge returns the vector containing the maximum indexes from v and b. If it
|
|
|
+// is possible, the vector v is updated and returned. If it is not, v copy
|
|
|
// will be created, updated and returned.
|
|
|
-func (a Vector) Merge(b Vector) Vector {
|
|
|
- var ai, bi int
|
|
|
+func (v Vector) Merge(b Vector) Vector {
|
|
|
+ var vi, bi int
|
|
|
for bi < len(b) {
|
|
|
- if ai == len(a) {
|
|
|
- // We've reach the end of a, all that remains are appends
|
|
|
- return append(a, b[bi:]...)
|
|
|
+ if vi == len(v) {
|
|
|
+ // We've reach the end of v, all that remains are appends
|
|
|
+ return append(v, b[bi:]...)
|
|
|
}
|
|
|
|
|
|
- if a[ai].ID > b[bi].ID {
|
|
|
+ if v[vi].ID > b[bi].ID {
|
|
|
// The index from b should be inserted here
|
|
|
- n := make(Vector, len(a)+1)
|
|
|
- copy(n, a[:ai])
|
|
|
- n[ai] = b[bi]
|
|
|
- copy(n[ai+1:], a[ai:])
|
|
|
- a = n
|
|
|
+ n := make(Vector, len(v)+1)
|
|
|
+ copy(n, v[:vi])
|
|
|
+ n[vi] = b[bi]
|
|
|
+ copy(n[vi+1:], v[vi:])
|
|
|
+ v = n
|
|
|
}
|
|
|
|
|
|
- if a[ai].ID == b[bi].ID {
|
|
|
- if v := b[bi].Value; v > a[ai].Value {
|
|
|
- a[ai].Value = v
|
|
|
+ if v[vi].ID == b[bi].ID {
|
|
|
+ if val := b[bi].Value; val > v[vi].Value {
|
|
|
+ v[vi].Value = val
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if bi < len(b) && a[ai].ID == b[bi].ID {
|
|
|
+ if bi < len(b) && v[vi].ID == b[bi].ID {
|
|
|
bi++
|
|
|
}
|
|
|
- ai++
|
|
|
+ vi++
|
|
|
}
|
|
|
|
|
|
- return a
|
|
|
+ return v
|
|
|
}
|
|
|
|
|
|
// Copy returns an identical vector that is not shared with v.
|
|
|
@@ -80,27 +80,27 @@ func (v Vector) Copy() Vector {
|
|
|
}
|
|
|
|
|
|
// Equal returns true when the two vectors are equivalent.
|
|
|
-func (a Vector) Equal(b Vector) bool {
|
|
|
- return a.Compare(b) == Equal
|
|
|
+func (v Vector) Equal(b Vector) bool {
|
|
|
+ return v.Compare(b) == Equal
|
|
|
}
|
|
|
|
|
|
-// LesserEqual returns true when the two vectors are equivalent or a is Lesser
|
|
|
+// LesserEqual returns true when the two vectors are equivalent or v is Lesser
|
|
|
// than b.
|
|
|
-func (a Vector) LesserEqual(b Vector) bool {
|
|
|
- comp := a.Compare(b)
|
|
|
+func (v Vector) LesserEqual(b Vector) bool {
|
|
|
+ comp := v.Compare(b)
|
|
|
return comp == Lesser || comp == Equal
|
|
|
}
|
|
|
|
|
|
-// LesserEqual returns true when the two vectors are equivalent or a is Greater
|
|
|
+// GreaterEqual returns true when the two vectors are equivalent or v is Greater
|
|
|
// than b.
|
|
|
-func (a Vector) GreaterEqual(b Vector) bool {
|
|
|
- comp := a.Compare(b)
|
|
|
+func (v Vector) GreaterEqual(b Vector) bool {
|
|
|
+ comp := v.Compare(b)
|
|
|
return comp == Greater || comp == Equal
|
|
|
}
|
|
|
|
|
|
// Concurrent returns true when the two vectors are concrurrent.
|
|
|
-func (a Vector) Concurrent(b Vector) bool {
|
|
|
- comp := a.Compare(b)
|
|
|
+func (v Vector) Concurrent(b Vector) bool {
|
|
|
+ comp := v.Compare(b)
|
|
|
return comp == ConcurrentGreater || comp == ConcurrentLesser
|
|
|
}
|
|
|
|