// Conjunctive predicate detection: forbidden when G[j] -> G[i] // (happened-before); advance increments G[j] to the next local state. #include #include bool happenedBefore(int j, const std::vector& G, const std::vector>& vc) { int n = (int)G.size(); int row = j * n + G[j]; for (int i = 0; i < n; ++i) if (i != j && vc[row][i] >= G[i]) return true; return false; } std::vector conjunctiveAlgorithm(const std::vector>& vc, const std::vector& T) { int n = (int)T.size(); std::vector G(n, 1); bool changed = true; while (changed) { changed = false; for (int j = 0; j < n; ++j) { if (G[j] >= T[j]) continue; if (happenedBefore(j, G, vc)) { ++G[j]; changed = true; } } } return G; } int main() { // Two processes, each with two events; trivial vector-clock matrix. std::vector> vc = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; std::vector T = {2, 2}; auto G = conjunctiveAlgorithm(vc, T); std::cout << "G:"; for (int x : G) std::cout << ' ' << x; std::cout << '\n'; return 0; }