@@ -58,23 +58,32 @@ function row_groups end
5858"""
5959 group_by_color(color::Vector{Int})
6060
61- Create `group::Vector{Vector{Int}} ` such that `i ∈ group[c]` iff `color[i] == c`.
61+ Create a color-indexed vector `group` such that `i ∈ group[c]` iff `color[i] == c`.
6262
6363Assumes the colors are contiguously numbered from `1` to some `cmax`.
6464"""
6565function group_by_color (color:: AbstractVector{<:Integer} )
6666 cmin, cmax = extrema (color)
67- @assert cmin == 1
68- group_sizes = zeros (Int, cmax)
67+ @assert cmin >= 1
68+ # Compute group sizes and offsets for a joint storage
69+ group_sizes = zeros (Int, cmax) # allocation 1, size cmax
6970 for c in color
7071 group_sizes[c] += 1
7172 end
72- group = [Vector {Int} (undef, group_sizes[c]) for c in 1 : cmax]
73- fill! (group_sizes, 1 )
73+ group_offsets = cumsum (group_sizes) # allocation 2, size cmax
74+ # Concatenate all groups inside a single vector
75+ group_flat = similar (color) # allocation 3, size n
7476 for (k, c) in enumerate (color)
75- pos = group_sizes[c]
76- group[c][pos] = k
77- group_sizes[c] += 1
77+ i = group_offsets[c] - group_sizes[c] + 1
78+ group_flat[i] = k
79+ group_sizes[c] -= 1
80+ end
81+ # Create views into contiguous blocks of the group vector
82+ group = Vector {typeof(view(group_flat, 1:1))} (undef, cmax) # allocation 4, size cmax
83+ for c in 1 : cmax
84+ i = 1 + (c == 1 ? 0 : group_offsets[c - 1 ])
85+ j = group_offsets[c]
86+ group[c] = view (group_flat, i: j)
7887 end
7988 return group
8089end
@@ -110,7 +119,7 @@ $TYPEDFIELDS
110119
111120- [`AbstractColoringResult`](@ref)
112121"""
113- struct ColumnColoringResult{M<: AbstractMatrix ,G<: BipartiteGraph } < :
122+ struct ColumnColoringResult{M<: AbstractMatrix ,G<: BipartiteGraph ,V } < :
114123 AbstractColoringResult{:nonsymmetric ,:column ,:direct }
115124 " matrix that was colored"
116125 A:: M
@@ -119,7 +128,7 @@ struct ColumnColoringResult{M<:AbstractMatrix,G<:BipartiteGraph} <:
119128 " one integer color for each column or row (depending on `partition`)"
120129 color:: Vector{Int}
121130 " color groups for columns or rows (depending on `partition`)"
122- group:: Vector{Vector{Int}}
131+ group:: V
123132 " flattened indices mapping the compressed matrix `B` to the uncompressed matrix `A` when `A isa SparseMatrixCSC`. They satisfy `nonzeros(A)[k] = vec(B)[compressed_indices[k]]`"
124133 compressed_indices:: Vector{Int}
125134end
@@ -156,12 +165,12 @@ $TYPEDFIELDS
156165
157166- [`AbstractColoringResult`](@ref)
158167"""
159- struct RowColoringResult{M<: AbstractMatrix ,G<: BipartiteGraph } < :
168+ struct RowColoringResult{M<: AbstractMatrix ,G<: BipartiteGraph ,V } < :
160169 AbstractColoringResult{:nonsymmetric ,:row ,:direct }
161170 A:: M
162171 bg:: G
163172 color:: Vector{Int}
164- group:: Vector{Vector{Int}}
173+ group:: V
165174 compressed_indices:: Vector{Int}
166175end
167176
@@ -197,12 +206,12 @@ $TYPEDFIELDS
197206
198207- [`AbstractColoringResult`](@ref)
199208"""
200- struct StarSetColoringResult{M<: AbstractMatrix ,G<: AdjacencyGraph } < :
209+ struct StarSetColoringResult{M<: AbstractMatrix ,G<: AdjacencyGraph ,V } < :
201210 AbstractColoringResult{:symmetric ,:column ,:direct }
202211 A:: M
203212 ag:: G
204213 color:: Vector{Int}
205- group:: Vector{Vector{Int}}
214+ group:: V
206215 star_set:: StarSet
207216 compressed_indices:: Vector{Int}
208217end
@@ -241,12 +250,12 @@ $TYPEDFIELDS
241250
242251- [`AbstractColoringResult`](@ref)
243252"""
244- struct TreeSetColoringResult{M<: AbstractMatrix ,G<: AdjacencyGraph ,R} < :
253+ struct TreeSetColoringResult{M<: AbstractMatrix ,G<: AdjacencyGraph ,V, R} < :
245254 AbstractColoringResult{:symmetric ,:column ,:substitution }
246255 A:: M
247256 ag:: G
248257 color:: Vector{Int}
249- group:: Vector{Vector{Int}}
258+ group:: V
250259 vertices_by_tree:: Vector{Vector{Int}}
251260 reverse_bfs_orders:: Vector{Vector{Tuple{Int,Int}}}
252261 buffer:: Vector{R}
@@ -395,12 +404,12 @@ $TYPEDFIELDS
395404
396405- [`AbstractColoringResult`](@ref)
397406"""
398- struct LinearSystemColoringResult{M<: AbstractMatrix ,G<: AdjacencyGraph ,R,F} < :
407+ struct LinearSystemColoringResult{M<: AbstractMatrix ,G<: AdjacencyGraph ,V, R,F} < :
399408 AbstractColoringResult{:symmetric ,:column ,:substitution }
400409 A:: M
401410 ag:: G
402411 color:: Vector{Int}
403- group:: Vector{Vector{Int}}
412+ group:: V
404413 strict_upper_nonzero_inds:: Vector{Tuple{Int,Int}}
405414 strict_upper_nonzeros_A:: Vector{R} # TODO : adjust type
406415 T_factorization:: F # TODO : adjust type
0 commit comments