You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently facing the problem of writing functions for unsigned integers and using an Accumulator as data structure. I would like to use an Accumulator{T, T} where T <: Unsigned and can easily instantiate e.g. an Accumulator{UInt8, UInt8} with Accumulator(0x01 => 0x02) and populate it manually, but I would like to use the simpler counter function to create the Accumulator from my Vector{UInt8}. counter, however, is defined to use Int as data type for the count:
counter(dct::AbstractDict{T, V}) where {T, V<:Integer} =Accumulator{T, V}(Dict(dct))
"""
counter(seq)
Returns an `Accumulator` object containing the elements from `seq`.
"""
functioncounter(seq)
ct =counter(eltype_for_accumulator(seq))
for x in seq
inc!(ct, x)
end
return ct
end
Is there already a way to create the counter with an <: Unsigned type as value? (And if not, should the ability be added once DataStructures.jl has reached v1.0?) Or do I indeed need to copy counter's behavior for me?
There is not a way to do this right now, but can I ask why you need this?
I thought internally using unsigned integers would simplify verifying the algorithm without needing to check for negative values over and over—besides, counts cannot be negative. While restructuring the code, I now realized this leads to an annoying API, so I just decided to give up this idea and keep using normal signed integers.
FWIW, in the meantime, I had used something similar to:
xs = [0x00, 0x01, 0x00, 0x02, 0xff]
T =eltype(xs) # Actually, a type parameter in the function definition
counts =Accumulator{T, T}(Dict(x =>count(==(x), xs, init=zero(T)) for x in xs))
as quick and far from optimized workaround. Now I'm back using the default counter and don't need this (or your kind suggestion) anymore, so I'll close the issue.
Activity
oxinabox commentedon Sep 4, 2026
There is not a way to do this right now, but can I ask why you need this?
oxinabox commentedon Sep 4, 2026
for the specific case of a
UInt8counter overUInt8objects,you might be better off doing:
Since under the hood the default counter uses a full
DictSimon-Brandt commentedon Sep 4, 2026
Thanks for the quick answer!
I thought internally using unsigned integers would simplify verifying the algorithm without needing to check for negative values over and over—besides, counts cannot be negative. While restructuring the code, I now realized this leads to an annoying API, so I just decided to give up this idea and keep using normal signed integers.
FWIW, in the meantime, I had used something similar to:
as quick and far from optimized workaround. Now I'm back using the default
counterand don't need this (or your kind suggestion) anymore, so I'll close the issue.