-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvclock.py
70 lines (63 loc) · 1.67 KB
/
vclock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class vclock:
def __init__(self, n, id):
"""
Constructs a zeroed, *n* sized vector clock.
"""
self.vclock = [0] * n
self.id = id
def merge(self, b):
"""
Given two clocks, return a new clock with all
values greater or equal to those of the merged
clocks.
"""
if self.id <= 4:
if b.id <= 4:
self.vclock = list(map(max, zip(self.vclock, b.vclock)))
else:
self.vclock[b.id] = max(self.vclock[b.id], b.vclock[b.id])
else:
self.vclock = list(map(max, zip(self.vclock, b.vclock)))
def getTimestamp(self):
return self.vclock[self.id]
def increment(self):
"""
Increment the clock at *index*.
"""
self.vclock[self.id] = max(self.vclock) + 1
return
# def compare(self, b):
# """
# Compares two vector clocks, returns -1 if ``a < b``,
# 1 if ``a > b`` else 0 for concurrent events
# or identical values.
# """
# gt = False
# lt = False
# for j, k in zip(self.vclock, b):
# gt |= j > k
# lt |= j < k
# if gt and lt:
# break
# return int(gt) - int(lt)
#
# def isConcurrent(self, b):
# """
# Returns whether the given clocks are concurrent.
# They must not be equal in value.
# """
#
# return (self.vclock != b) and self.compare(b) == 0
'''
example code
import vclock
a = vclock(3)
b = vclock(3)
a.increment()
b.increment()
a.merge(b.vclock)
flag = vclock.is_concurrent(a, c)
print a
print c
print flag
'''