|
| 1 | +package com.thealgorithms.datastructures.crdt; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * PN-Counter (Positive-Negative Counter) is a state-based CRDT (Conflict-free Replicated Data Type) |
| 8 | + * designed for tracking counts with both increments and decrements in a distributed and concurrent environment. |
| 9 | + * It combines two G-Counters, one for increments (P) and one for decrements (N). |
| 10 | + * The total count is obtained by subtracting the value of the decrement counter from the increment counter. |
| 11 | + * This implementation supports incrementing, decrementing, querying the total count, |
| 12 | + * comparing with other PN-Counters, and merging with another PN-Counter |
| 13 | + * to compute the element-wise maximum for both increment and decrement counters. |
| 14 | + * (https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type) |
| 15 | + * |
| 16 | + * @author itakurah (Niklas Hoefflin) (https://github.com/itakurah) |
| 17 | + */ |
| 18 | + |
| 19 | +class PNCounter { |
| 20 | + private final Map<Integer, Integer> P; |
| 21 | + private final Map<Integer, Integer> N; |
| 22 | + private final int myId; |
| 23 | + private final int n; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs a PN-Counter for a cluster of n nodes. |
| 27 | + * |
| 28 | + * @param myId The identifier of the current node. |
| 29 | + * @param n The number of nodes in the cluster. |
| 30 | + */ |
| 31 | + public PNCounter(int myId, int n) { |
| 32 | + this.myId = myId; |
| 33 | + this.n = n; |
| 34 | + this.P = new HashMap<>(); |
| 35 | + this.N = new HashMap<>(); |
| 36 | + |
| 37 | + for (int i = 0; i < n; i++) { |
| 38 | + P.put(i, 0); |
| 39 | + N.put(i, 0); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Increments the increment counter for the current node. |
| 45 | + */ |
| 46 | + public void increment() { |
| 47 | + P.put(myId, P.get(myId) + 1); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Increments the decrement counter for the current node. |
| 52 | + */ |
| 53 | + public void decrement() { |
| 54 | + N.put(myId, N.get(myId) + 1); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets the total value of the counter by subtracting the decrement counter from the increment counter. |
| 59 | + * |
| 60 | + * @return The total value of the counter. |
| 61 | + */ |
| 62 | + public int value() { |
| 63 | + int sumP = P.values().stream().mapToInt(Integer::intValue).sum(); |
| 64 | + int sumN = N.values().stream().mapToInt(Integer::intValue).sum(); |
| 65 | + return sumP - sumN; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Compares the state of this PN-Counter with another PN-Counter. |
| 70 | + * |
| 71 | + * @param other The other PN-Counter to compare with. |
| 72 | + * @return True if the state of this PN-Counter is less than or equal to the state of the other PN-Counter. |
| 73 | + */ |
| 74 | + public boolean compare(PNCounter other) { |
| 75 | + if (this.n != other.n) { |
| 76 | + throw new IllegalArgumentException("Cannot compare PN-Counters with different number of nodes"); |
| 77 | + } |
| 78 | + for (int i = 0; i < n; i++) { |
| 79 | + if (this.P.get(i) > other.P.get(i) && this.N.get(i) > other.N.get(i)) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Merges the state of this PN-Counter with another PN-Counter. |
| 88 | + * |
| 89 | + * @param other The other PN-Counter to merge with. |
| 90 | + */ |
| 91 | + public void merge(PNCounter other) { |
| 92 | + if (this.n != other.n) { |
| 93 | + throw new IllegalArgumentException("Cannot merge PN-Counters with different number of nodes"); |
| 94 | + } |
| 95 | + for (int i = 0; i < n; i++) { |
| 96 | + this.P.put(i, Math.max(this.P.get(i), other.P.get(i))); |
| 97 | + this.N.put(i, Math.max(this.N.get(i), other.N.get(i))); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments