|
| 1 | +""" |
| 2 | + GroupedColors |
| 3 | +
|
| 4 | +A trait that indicates that a coloring operation should return results as a `GroupedColoring`. |
| 5 | +""" |
| 6 | +struct GroupedColors end |
| 7 | +const GroupedColorConst = GroupedColors() |
| 8 | +function (::GroupedColors)(maxcolor, colors, elements) |
| 9 | + colors = Vector{Int}[elements[findall(isequal(color), colors)] for color in 1:maxcolor] |
| 10 | + return GroupedColoring(colors) |
| 11 | +end |
| 12 | +function (::GroupedColors)(colors) |
| 13 | + return GroupedColoring(colors) |
| 14 | +end |
| 15 | + |
| 16 | +""" |
| 17 | + GroupedColors |
| 18 | +
|
| 19 | +A trait that indicates that a coloring operation should return results as a `Graphs.Coloring`. |
| 20 | +""" |
| 21 | +struct GraphsColors end |
| 22 | +const GraphColorsConst = GraphsColors() |
| 23 | + |
| 24 | +""" |
| 25 | + GroupedColoring{I} |
| 26 | +
|
| 27 | +A data structure that represents a coloring of elements as a vector of vectors, where each |
| 28 | +inner vector contains the indices of elements assigned to a particular color. |
| 29 | +
|
| 30 | +This representation is optimized for efficiently retrieving all elements belonging to a |
| 31 | +specific color group, making it ideal for operations that frequently access elements by color. |
| 32 | +
|
| 33 | +# Type Parameters |
| 34 | +
|
| 35 | + - `I`: The integer type used to represent element indices (e.g., `Int`, `Int64`, `UInt32`). |
| 36 | +
|
| 37 | +# Fields |
| 38 | +
|
| 39 | + - `colors`: A vector of vectors, where `colors[i]` contains the indices of all elements assigned |
| 40 | + to color `i`. The vector is sorted by group size in descending order (largest groups first) |
| 41 | + for consistency. |
| 42 | +
|
| 43 | +# Notes |
| 44 | +
|
| 45 | + The constructor automatically sorts the color groups by size in descending order using `sort!` |
| 46 | + with `by=length, rev=true`. |
| 47 | +""" |
| 48 | +struct GroupedColoring{I} |
| 49 | + colors::Vector{Vector{I}} |
| 50 | + function GroupedColoring(colors) |
| 51 | + return new{eltype(eltype(colors))}(sort!(colors; by=length, rev=true)) |
| 52 | + end |
| 53 | +end |
| 54 | + |
| 55 | +""" |
| 56 | + numcolors(c) |
| 57 | +
|
| 58 | +Return the number of distinct colors used in the coloring. |
| 59 | +
|
| 60 | +# Arguments |
| 61 | +
|
| 62 | + - `c`: A coloring object representing a coloring of a graph or similar structure, |
| 63 | + where elements are grouped by color. |
| 64 | +
|
| 65 | +# Returns |
| 66 | +
|
| 67 | + - An integer representing the number of distinct colors used in the coloring. |
| 68 | +""" |
| 69 | +function numcolors(c::GroupedColoring) |
| 70 | + return length(colors(c)) |
| 71 | +end |
| 72 | + |
| 73 | +function colors(c::GroupedColoring) |
| 74 | + return c.colors |
| 75 | +end |
| 76 | + |
| 77 | +""" |
| 78 | + Base.eachindex(c::Union{GroupedColoring,Graphs.Coloring}) |
| 79 | +
|
| 80 | +Return an iterator over the indices of the color groups in `c`. |
| 81 | +
|
| 82 | +# Arguments |
| 83 | +
|
| 84 | + - `c`: An object representing a coloring. |
| 85 | +
|
| 86 | +# Returns |
| 87 | +
|
| 88 | + - An iterator over the indices of the color groups (typically `1:numcolors(c))`). |
| 89 | +
|
| 90 | +# Notes |
| 91 | +
|
| 92 | +This method implements the `eachindex` interface for `GroupedColoring` and `Graphs.Coloring`, making it compatible |
| 93 | +with Julia's standard iteration patterns. |
| 94 | +""" |
| 95 | +function Base.eachindex(c::GroupedColoring) |
| 96 | + return Base.eachindex(colors(c)) |
| 97 | +end |
| 98 | + |
| 99 | +""" |
| 100 | + Base.getindex(c::Union{GroupedColoring,Graphs.Coloring}, color) |
| 101 | +
|
| 102 | +Return the i-th color group from the coloring object `c`. |
| 103 | +
|
| 104 | +# Arguments |
| 105 | +
|
| 106 | + - `c`: An object representing a coloring. |
| 107 | + - `color`: An integer index specifying which color group to retrieve (must be within valid bounds). |
| 108 | +
|
| 109 | +# Returns |
| 110 | +
|
| 111 | + - The i-th color group (typically a vector of elements assigned to that color). |
| 112 | +""" |
| 113 | +function Base.getindex(c::GroupedColoring, i) |
| 114 | + return Base.getindex(colors(c), i) |
| 115 | +end |
0 commit comments