PNCounter[optional A: (Integer[A] val & Unsigned)]¶
A mutable counter, which can be both increased and decreased.
This data type tracks the state seen from each replica, thus the size of the state will grow proportionally with the number of total replicas. New replicas may be added as peers at any time, provided that they use unique ids. Read-only replicas which never change state and only observe need not use unique ids, and should use an id of zero, by convention.
The counter is implemented as a pair of grow-only counters, with one counter representing growth in the positive direction, and the other counter representing growth in the negative direction, with the total value of the counter being calculated from the difference in magnitude.
Because the data type is composed of a pair of eventually consistent CRDTs, the calculated value of the overall counter is also eventually consistent.
All mutator methods accept and return a convergent delta-state.
class ref PNCounter[optional A: (Integer[A] val & Unsigned)] is
Comparable[PNCounter[A] ref] ref,
Convergent[PNCounter[A] ref] ref,
Replicated ref
Implements¶
- Comparable[PNCounter[A] ref] ref
- Convergent[PNCounter[A] ref] ref
- Replicated ref
Constructors¶
create¶
Instantiate the PNCounter under the given unique replica id.
Parameters¶
- id': ID
Returns¶
- PNCounter[A] ref^
Public Functions¶
is_empty¶
Return true if the data structure contains no information (bottom state).
Returns¶
- Bool val
apply¶
Return the current value of the counter (the difference in magnitude).
Returns¶
- A
value¶
Return the current value of the counter (the difference in magnitude).
Returns¶
- A
increment[optional D: PNCounter[A] ref]¶
Increment the counter by the given value. Accepts and returns a convergent delta-state.
fun ref increment[optional D: PNCounter[A] ref](
value': A = 1,
delta': D = recover PNCounter[A](0) end)
: D^
Parameters¶
- value': A = 1
- delta': D = recover PNCounterA end
Returns¶
- D^
decrement[optional D: PNCounter[A] ref]¶
Decrement the counter by the given value. Accepts and returns a convergent delta-state.
fun ref decrement[optional D: PNCounter[A] ref](
value': A = 1,
delta': D = recover PNCounter[A](0) end)
: D^
Parameters¶
- value': A = 1
- delta': D = recover PNCounterA end
Returns¶
- D^
converge¶
Converge from the given PNCounter into this one. We converge the positive and negative counters, pairwise. Returns true if the convergence added new information to the data structure.
Parameters¶
- that: PNCounter[A] box
Returns¶
- Bool val
string¶
Return a best effort at printing the register. If A is Stringable, use the string representation of the value; otherwise print as a question mark.
Returns¶
- String iso^
eq¶
Parameters¶
- that: PNCounter[A] box
Returns¶
- Bool val
ne¶
Parameters¶
- that: PNCounter[A] box
Returns¶
- Bool val
lt¶
Parameters¶
- that: PNCounter[A] box
Returns¶
- Bool val
le¶
Parameters¶
- that: PNCounter[A] box
Returns¶
- Bool val
gt¶
Parameters¶
- that: PNCounter[A] box
Returns¶
- Bool val
ge¶
Parameters¶
- that: PNCounter[A] box
Returns¶
- Bool val
from_tokens¶
Deserialize an instance of this data structure from a stream of tokens.
Parameters¶
- that: TokensIterator ref
Returns¶
- None val ?
each_token¶
Serialize the data structure, capturing each token into the given Tokens.
Parameters¶
- tokens: Tokens ref
Returns¶
- None val
compare¶
Parameters¶
- that: PNCounter[A] box