There's really no such thing as "concurrent lesser" and "concurrent greater" in version vectors, just "concurrent". But it's useful to be able to get a strict ordering between versions for stable sorts and so on, so we return both variants. The convenience method Concurrent() can be used to check fo
(b Vector)
| 261 | |
| 262 | // Compare returns the Ordering that describes a's relation to b. |
| 263 | func (v Vector) Compare(b Vector) Ordering { |
| 264 | var ai, bi int // index into a and b |
| 265 | var av, bv Counter // value at current index |
| 266 | |
| 267 | result := Equal |
| 268 | |
| 269 | for ai < len(v.Counters) || bi < len(b.Counters) { |
| 270 | var aMissing, bMissing bool |
| 271 | |
| 272 | if ai < len(v.Counters) { |
| 273 | av = v.Counters[ai] |
| 274 | } else { |
| 275 | av = Counter{} |
| 276 | aMissing = true |
| 277 | } |
| 278 | |
| 279 | if bi < len(b.Counters) { |
| 280 | bv = b.Counters[bi] |
| 281 | } else { |
| 282 | bv = Counter{} |
| 283 | bMissing = true |
| 284 | } |
| 285 | |
| 286 | switch { |
| 287 | case av.ID == bv.ID: |
| 288 | // We have a counter value for each side |
| 289 | if av.Value > bv.Value { |
| 290 | if result == Lesser { |
| 291 | return ConcurrentLesser |
| 292 | } |
| 293 | result = Greater |
| 294 | } else if av.Value < bv.Value { |
| 295 | if result == Greater { |
| 296 | return ConcurrentGreater |
| 297 | } |
| 298 | result = Lesser |
| 299 | } |
| 300 | |
| 301 | case !aMissing && av.ID < bv.ID || bMissing: |
| 302 | // Value is missing on the b side |
| 303 | if av.Value > 0 { |
| 304 | if result == Lesser { |
| 305 | return ConcurrentLesser |
| 306 | } |
| 307 | result = Greater |
| 308 | } |
| 309 | |
| 310 | case !bMissing && bv.ID < av.ID || aMissing: |
| 311 | // Value is missing on the a side |
| 312 | if bv.Value > 0 { |
| 313 | if result == Greater { |
| 314 | return ConcurrentGreater |
| 315 | } |
| 316 | result = Lesser |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if ai < len(v.Counters) && (av.ID <= bv.ID || bMissing) { |
no outgoing calls