Skip to content

Use counter() with unsigned count #985

Closed

Description

@Simon-Brandt

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(T::Type) = Accumulator{T, Int}()
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`.
"""
function counter(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?

Activity

  1. oxinabox commented on Sep 4, 2026

    @oxinabox
    Member

    There is not a way to do this right now, but can I ask why you need this?

  2. oxinabox commented on Sep 4, 2026

    @oxinabox
    Member

    for the specific case of a UInt8 counter over UInt8 objects,
    you might be better off doing:

    julia> counter = zeros(UInt8, typemax(UInt8));
    
    julia> for x in [0x4, 0x2, 0x0, 0x0]
               counter[x+0x1] += 1
           end
    
    julia> counter
    255-element Vector{UInt8}:
     0x02
     0x00
     0x01
     0x00
     0x01
     0x00
     0x00
    

    Since under the hood the default counter uses a full Dict

  3. Simon-Brandt commented on Sep 4, 2026

    @Simon-Brandt
    Author

    Thanks for the quick answer!

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions