Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comparison as new parameter of the group function #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/group.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,42 @@ end

@deprecate groupinds groupfind

function groupfind2(array::AbstractArray)
# In a single pass we collect the size of each group and the token to which group each element belongs
group_sizes = Dictionary{eltype(array), Int}()
to_group_token = similar(array, Int)

@inbounds for i in eachindex(array)
value = array[i]
(hadindex, token) = gettoken!(group_sizes, value)
dict_index = token[2]
if hadindex
settokenvalue!(group_sizes, token, gettokenvalue(group_sizes, token) + 1)
else
settokenvalue!(group_sizes, token, 1)
end
to_group_token[i] = dict_index # Insertion-only, the token is stable...
end

# We find the sort permutation and the ranges of the sort permutation for each group
sorted_indices = sortperm(to_group_token)

this_start = 1
this_stop = 0
ranges = similar(group_sizes, UnitRange{Int})
@inbounds for token in tokens(group_sizes)
this_size = gettokenvalue(group_sizes, token)
this_stop += this_size
settokenvalue!(ranges, token, this_start:this_stop)
this_start += this_size
end

# We lazily index these ranges onto sorted_indices
return mapview((r -> @inbounds(view(sorted_indices, r))), ranges)
end



"""
groupfind([by], container)

Expand Down