Skip to content

Commit 620436c

Browse files
authored
Remove dynamic allocations in group_by_color (#113)
1 parent 1abd62e commit 620436c

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/result.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ Assumes the colors are contiguously numbered from `1` to some `cmax`.
6464
function group_by_color(color::AbstractVector{<:Integer})
6565
cmin, cmax = extrema(color)
6666
@assert cmin == 1
67-
group = [Int[] for c in 1:cmax]
67+
group_sizes = zeros(Int, cmax)
68+
for c in color
69+
group_sizes[c] += 1
70+
end
71+
group = [Vector{Int}(undef, group_sizes[c]) for c in 1:cmax]
72+
fill!(group_sizes, 1)
6873
for (k, c) in enumerate(color)
69-
push!(group[c], k)
74+
pos = group_sizes[c]
75+
group[c][pos] = k
76+
group_sizes[c] += 1
7077
end
7178
return group
7279
end

0 commit comments

Comments
 (0)