-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency_vector.py
30 lines (24 loc) · 1.01 KB
/
dependency_vector.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
from collections import defaultdict
from typing import Dict
class DependencyVector:
def __init__(self, id: int) -> None:
self.id = id
self.dict: Dict[int, int] = defaultdict(int)
def on_checkpoint_taken(self):
self.dict[self.id] += 1
def merge(self, dependency_vector: Dict[int, int]):
if type(dependency_vector) != dict:
raise Exception("Merging invalid dependency vector: not a dict")
for key in dependency_vector:
if type(key) != int:
raise Exception("Mergin invalid dependency vector: key not int", key)
if type(dependency_vector[key]) != int:
raise Exception(
"Mergin invalid dependency vector: value not int",
dependency_vector[key],
)
self.dict[key] = max(self.dict[key], dependency_vector[key])
def as_dict(self) -> Dict[int, int]:
return dict(self.dict)
def get(self, i: int) -> int:
return self.dict[i]