use crate::atoms::NodeId;
pub fn greedy_color_nodes(
n_cells: usize,
nodes_per_cell: usize,
conn: &[NodeId],
n_nodes: usize,
) -> Vec<Vec<usize>> {
if n_cells == 0 {
return Vec::new();
}
debug_assert_eq!(conn.len(), n_cells * nodes_per_cell);
let n_ids = conn
.iter()
.map(|n| n.0 as usize + 1)
.max()
.unwrap_or(0)
.max(n_nodes);
let nodes_of = |c: usize| &conn[c * nodes_per_cell..(c + 1) * nodes_per_cell];
let mut offsets = vec![0usize; n_ids + 1];
for n in conn {
offsets[n.0 as usize + 1] += 1;
}
for i in 0..n_ids {
offsets[i + 1] += offsets[i];
}
let mut cursor = offsets.clone();
let mut cells = vec![0u32; conn.len()];
for c in 0..n_cells {
for n in nodes_of(c) {
let k = n.0 as usize;
cells[cursor[k]] = c as u32;
cursor[k] += 1;
}
}
let mut color = vec![usize::MAX; n_cells];
let mut forbidden_by: Vec<usize> = Vec::new();
let mut n_colors = 0usize;
for c in 0..n_cells {
for n in nodes_of(c) {
let k = n.0 as usize;
for &other in &cells[offsets[k]..offsets[k + 1]] {
let oc = color[other as usize];
if oc != usize::MAX {
if oc >= forbidden_by.len() {
forbidden_by.resize(oc + 1, usize::MAX);
}
forbidden_by[oc] = c;
}
}
}
let mut chosen = 0;
while chosen < forbidden_by.len() && forbidden_by[chosen] == c {
chosen += 1;
}
color[c] = chosen;
n_colors = n_colors.max(chosen + 1);
}
let mut buckets: Vec<Vec<usize>> = vec![Vec::new(); n_colors];
for (c, &col) in color.iter().enumerate() {
buckets[col].push(c);
}
buckets
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
fn ids(raw: &[u32]) -> Vec<NodeId> {
raw.iter().map(|&i| NodeId(i)).collect()
}
fn assert_valid(
buckets: &[Vec<usize>],
n_cells: usize,
nodes_per_cell: usize,
conn: &[NodeId],
) {
let mut seen_cells = HashSet::new();
for bucket in buckets {
let mut seen_nodes = HashSet::new();
for &c in bucket {
assert!(seen_cells.insert(c), "cell {c} appears in two colours");
for &n in &conn[c * nodes_per_cell..(c + 1) * nodes_per_cell] {
assert!(
seen_nodes.insert(n),
"node {} shared by two cells of the same colour",
n.0
);
}
}
}
assert_eq!(
seen_cells.len(),
n_cells,
"not every cell was coloured once"
);
}
#[test]
fn empty_set_no_colors() {
assert!(greedy_color_nodes(0, 2, &[], 0).is_empty());
}
#[test]
fn disjoint_cells_share_one_color() {
let conn = ids(&[0, 1, 2, 3]);
let buckets = greedy_color_nodes(2, 2, &conn, 4);
assert_eq!(buckets.len(), 1);
assert_valid(&buckets, 2, 2, &conn);
}
#[test]
fn seg2_chain_alternates_two_colors() {
let conn = ids(&[0, 1, 1, 2, 2, 3, 3, 4]);
let buckets = greedy_color_nodes(4, 2, &conn, 5);
assert_eq!(buckets.len(), 2);
assert_eq!(buckets[0], vec![0, 2]);
assert_eq!(buckets[1], vec![1, 3]);
assert_valid(&buckets, 4, 2, &conn);
}
#[test]
fn quad_grid_is_validly_colored() {
let n = 8usize;
let at = |i: usize, j: usize| NodeId((j * (n + 1) + i) as u32);
let mut conn = Vec::with_capacity(n * n * 4);
for j in 0..n {
for i in 0..n {
conn.extend_from_slice(&[at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j + 1)]);
}
}
let buckets = greedy_color_nodes(n * n, 4, &conn, (n + 1) * (n + 1));
assert_valid(&buckets, n * n, 4, &conn);
assert!(buckets.len() <= 6, "too many colours: {}", buckets.len());
}
}