Skip to content

Instantly share code, notes, and snippets.

@rogerrohrbach
Created June 28, 2011 14:08
Show Gist options
  • Save rogerrohrbach/1051205 to your computer and use it in GitHub Desktop.
Save rogerrohrbach/1051205 to your computer and use it in GitHub Desktop.
Version vector class
class VersionVector
# D. S. Parker, et al. Detection of Mutual Inconsistency in
# Distributed Systems. IEEE Trans. Softw. Eng. 9, 3 (May 1983), 240-247.
def initialize
@versions = {}
@versions.default = 0
end
def [](site)
@versions[site]
end
def increment(site)
@versions[site] += 1
end
def sites
@versions.keys
end
protected :sites
def to_s
@versions
end
def equals?(other)
sites = (self.sites + other.sites).uniq
not (sites.collect{ |site| self[site] == other[site] }.include? false)
end
alias :== :equals?
def dominates?(other)
sites = (self.sites + other.sites).uniq
# Every version in the first vector must be greater than or equal to the
# corresponding version in the second
not (sites.collect{ |site| self[site] >= other[site] }.include? false) and
# At least one version in the first vector must be greater than the
# corresponding version in the second
(sites.collect{ |site| self[site] > other[site] }.include? true)
end
def compatible?(other)
equals?(other) || dominates?(other) || other.dominates?(self)
end
def conflicts?(other)
!compatible?(other)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment