"Counter" (Data Structure)
"Counter"
represents a mutable integer counter.
Details
- A mutable integer counter can be used to count elements in any part of a program:
-
CreateDataStructure[ "Counter",i] create a new "Counter" with specified initial value i Typed[x,"Counter"] give x the type "Counter" - For a data structure of type "Counter", the following operations can be used:
-
ds["AddTo",n] add n to the counter, returning the new value time: O(1) ds["Copy"] return a copy of ds time: O(1) ds["Decrement"] decrement the counter, returning the old value time: O(1) ds["Get"] get the value of the counter stored in ds time: O(1) ds["Increment"] increment the counter, returning the old value time: O(1) ds["PreDecrement"] decrement the counter, returning the new value time: O(1) ds["PreIncrement"] increment the counter, returning the new value time: O(1) ds["Set",i] set the counter stored in ds to be i time: O(1) ds["SubtractFrom",n] subtract n from the counter, returning the new value time: O(1) ds["Visualization"] return a visualization of ds time: O(1) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (1)
A new "Counter" can be created with CreateDataStructure:
ds = CreateDataStructure["Counter", 1]ds["Get"]Increment the value stored in the counter:
ds["Increment"]Confirm that the value has updated:
ds["Get"]Return an expression version of ds:
Normal[ds]A visualization of the data structure can be generated:
ds["Visualization"]Scope (13)
Mutability (2)
Create a new "Counter" data structure and initialize it:
ds = CreateDataStructure["Counter", 0]Define a function that increments the counter if its argument is even:
f = Function[{arg}, If[EvenQ[arg], ds["Increment"]]];Scan the function over all elements of an expression:
Scan[f, {1, 2, {6, 7, 2, {6, 1}}}, {-1}]The number of even elements in the expression:
ds["Get"]Create a new "Counter" data structure and initialize it:
ds = CreateDataStructure["Counter", 0]Incrementing the counter returns the old counter value:
ds["Increment"] === ds["Get"]Pre-incrementing the counter returns the current counter value:
ds["PreIncrement"] === ds["Get"]Setting the counter returns the current counter value:
ds["Set", 100] === ds["Get"]Adding to the counter returns the new value:
{ds["AddTo", 20], ds["Get"]}Subtracting from the counter also returns the new value:
{ds["SubtractFrom", 40], ds["Get"]}Information (1)
A new "Counter" can be created with CreateDataStructure:
ds = CreateDataStructure["Counter", 1]Information about the data structure ds:
Information[ds]Operations (10)
"AddTo" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]Add an integer to the counter, returning the new value:
ds["AddTo", 10]ds["Get"]"Copy" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]ds1 = ds["Copy"]The contents of both counters are the same:
{Normal[ds], Normal[ds1]}Modifying the copy will not affect the original counter:
ds1["Increment"];
{Normal[ds], Normal[ds1]}"Decrement" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]Decrement the counter, returning the old value:
ds["Decrement"]ds["Get"]"Get" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]ds["Get"]"Increment" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]Increment the counter, returning the old value:
ds["Increment"]ds["Get"]"PreDecrement" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]Decrement the counter, returning the new value:
ds["PreDecrement"]ds["Get"]"PreIncrement" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]Increment the counter, returning the old value:
ds["PreIncrement"]ds["Get"]"Set" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 1]Set the counter to a new integer:
ds["Set", 5]ds["Get"]"SubtractFrom" (1)
Create a new "Counter" initialized to 1:
ds = CreateDataStructure["Counter", 52]Subtract an integer from the counter, returning the new value:
ds["SubtractFrom", 10]ds["Get"]"Visualization" (1)
Create a new "Counter" initialized to 42:
ds = CreateDataStructure["Counter", 42]ds["Visualization"]Applications (3)
Generating Unique IDs (1)
Use a "Counter" to generate sequential unique identifiers during evaluation:
id = CreateDataStructure["Counter", 0];
newID[] := id["PreIncrement"];Table[newID[], 5]Recursive Call Counting (1)
Passing Mutable State to a Function (1)
Pass a "Counter" to a function and mutate it during evaluation:
countPrimes[data_, counter_] := Scan[If[PrimeQ[#], counter["Increment"]]&, data];Create a counter and call the function:
counter = CreateDataStructure["Counter", 0];
countPrimes[Range[1000], counter];
counter["Get"]Calling the function again continues mutating the same counter:
countPrimes[Range[1001, 10000], counter];
counter["Get"]Properties & Relations (4)
InputForm (1)
Normal (1)
SameQ (1)
SameQ can be used to test whether two counters contain the same value:
ds1 = CreateDataStructure["Counter", 42];
ds2 = CreateDataStructure["Counter", 42];
ds1 === ds2If one element is changed, the counters are no longer the same:
ds1["Increment"];
ds1 === ds2See Also
Increment Decrement PreIncrement PreDecrement CreateDataStructure DataStructure
Data Structures: Value
Related Guides
History
Introduced in 2020 (12.1)