vector.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (C) 2015 The Protocol Authors.
  2. package protocol
  3. // The Vector type represents a version vector. The zero value is a usable
  4. // version vector. The vector has slice semantics and some operations on it
  5. // are "append-like" in that they may return the same vector modified, or a
  6. // new allocated Vector with the modified contents.
  7. type Vector []Counter
  8. // Counter represents a single counter in the version vector.
  9. type Counter struct {
  10. ID uint64
  11. Value uint64
  12. }
  13. // Update returns a Vector with the index for the specific ID incremented by
  14. // one. If it is possible, the vector v is updated and returned. If it is not,
  15. // a copy will be created, updated and returned.
  16. func (v Vector) Update(ID uint64) Vector {
  17. for i := range v {
  18. if v[i].ID == ID {
  19. // Update an existing index
  20. v[i].Value++
  21. return v
  22. } else if v[i].ID > ID {
  23. // Insert a new index
  24. nv := make(Vector, len(v)+1)
  25. copy(nv, v[:i])
  26. nv[i].ID = ID
  27. nv[i].Value = 1
  28. copy(nv[i+1:], v[i:])
  29. return nv
  30. }
  31. }
  32. // Append a new new index
  33. return append(v, Counter{ID, 1})
  34. }
  35. // Merge returns the vector containing the maximum indexes from a and b. If it
  36. // is possible, the vector a is updated and returned. If it is not, a copy
  37. // will be created, updated and returned.
  38. func (a Vector) Merge(b Vector) Vector {
  39. var ai, bi int
  40. for bi < len(b) {
  41. if ai == len(a) {
  42. // We've reach the end of a, all that remains are appends
  43. return append(a, b[bi:]...)
  44. }
  45. if a[ai].ID > b[bi].ID {
  46. // The index from b should be inserted here
  47. n := make(Vector, len(a)+1)
  48. copy(n, a[:ai])
  49. n[ai] = b[bi]
  50. copy(n[ai+1:], a[ai:])
  51. a = n
  52. }
  53. if a[ai].ID == b[bi].ID {
  54. if v := b[bi].Value; v > a[ai].Value {
  55. a[ai].Value = v
  56. }
  57. }
  58. if bi < len(b) && a[ai].ID == b[bi].ID {
  59. bi++
  60. }
  61. ai++
  62. }
  63. return a
  64. }
  65. // Copy returns an identical vector that is not shared with v.
  66. func (v Vector) Copy() Vector {
  67. nv := make(Vector, len(v))
  68. copy(nv, v)
  69. return nv
  70. }
  71. // Equal returns true when the two vectors are equivalent.
  72. func (a Vector) Equal(b Vector) bool {
  73. return a.Compare(b) == Equal
  74. }
  75. // LesserEqual returns true when the two vectors are equivalent or a is Lesser
  76. // than b.
  77. func (a Vector) LesserEqual(b Vector) bool {
  78. comp := a.Compare(b)
  79. return comp == Lesser || comp == Equal
  80. }
  81. // LesserEqual returns true when the two vectors are equivalent or a is Greater
  82. // than b.
  83. func (a Vector) GreaterEqual(b Vector) bool {
  84. comp := a.Compare(b)
  85. return comp == Greater || comp == Equal
  86. }
  87. // Concurrent returns true when the two vectors are concrurrent.
  88. func (a Vector) Concurrent(b Vector) bool {
  89. comp := a.Compare(b)
  90. return comp == ConcurrentGreater || comp == ConcurrentLesser
  91. }
  92. // Counter returns the current value of the given counter ID.
  93. func (v Vector) Counter(id uint64) uint64 {
  94. for _, c := range v {
  95. if c.ID == id {
  96. return c.Value
  97. }
  98. }
  99. return 0
  100. }