-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCounter.jl
32 lines (26 loc) · 991 Bytes
/
Counter.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""
A counter of type `T` that increments by one each time it is called.
Optionally, a `min_count` value can be specified from when onwards
this operation changes into the "valid" state. This is useful if
other operations need to first be called multiple times before downstream
operations can start processing, so this operation can be used to signal when
these operations are deemed ready and further processing can begin.
"""
mutable struct Counter{T} <: StreamOperation
counter::T
const min_count::T
Counter(start::T=0; min_count::T=0) where{T} = new{T}(start, min_count)
Counter{T}(; min_count::T=0) where{T} = new{T}(zero(T), min_count)
end
@inline function(op::Counter{T})(args...) where {T}
op.counter += one(T)
nothing
end
@inline is_valid(op::Counter) = op.counter >= op.min_count
@inline function get_state(op::Counter{T})::T where {T}
op.counter
end
@inline function reset!(op::Counter{T}) where {T}
op.counter = zero(T)
nothing
end