PN-Counter Merge
PN-Counter Merge
A PN-Counter is a CRDT built from two grow-only vectors:
Pfor incrementsNfor decrements
To merge two PN-Counters, take the element-wise max of P and N. The counter value is sum(P) - sum(N).
Types
type PNCounter struct {
P []int
N []int
}
Function signature
func MergePNCounter(a, b PNCounter) (PNCounter, int)
Example
a = P:[1,0], N:[0,1]
b = P:[0,2], N:[0,0]
merge = P:[1,2], N:[0,1]
value = 2
Constraints
len(a.P) == len(b.P)len(a.N) == len(b.N)len(a.P) == len(a.N)0 <= values <= 1_000_000
Notes
- If lengths do not match, return empty counter and value 0.
Run tests to see results
No issues detected