1use std::collections::{HashMap, HashSet, VecDeque};
9
10use crate::bonds::is_aromatic_order;
11use crate::scene::{AtomIn, MoleculeIn};
12
13pub const AROMATIC_CIRCLE_FRAC: f64 = 0.60;
17
18#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct Ring {
21 pub atoms: Vec<i32>,
22}
23
24impl Ring {
25 pub fn size(&self) -> usize {
26 self.atoms.len()
27 }
28
29 pub fn bonds(&self) -> HashSet<(i32, i32)> {
30 let n = self.atoms.len();
31 let mut out = HashSet::with_capacity(n);
32 for i in 0..n {
33 out.insert(bond_key(self.atoms[i], self.atoms[(i + 1) % n]));
34 }
35 out
36 }
37}
38
39fn bond_key(a: i32, b: i32) -> (i32, i32) {
40 if a < b {
41 (a, b)
42 } else {
43 (b, a)
44 }
45}
46
47fn adjacency(mol: &MoleculeIn) -> HashMap<i32, Vec<i32>> {
48 let mut adj: HashMap<i32, Vec<i32>> = mol.atoms.iter().map(|a| (a.index, Vec::new())).collect();
49 for b in &mol.bonds {
50 adj.entry(b.begin).or_default().push(b.end);
51 adj.entry(b.end).or_default().push(b.begin);
52 }
53 adj
54}
55
56fn shortest_cycle_through_bond(
58 adj: &HashMap<i32, Vec<i32>>,
59 u: i32,
60 v: i32,
61 max_size: usize,
62) -> Option<Vec<i32>> {
63 let mut prev: HashMap<i32, Option<i32>> = HashMap::new();
64 prev.insert(u, None);
65 let mut q = VecDeque::new();
66 q.push_back(u);
67 let mut found = false;
68
69 while let Some(cur) = q.pop_front() {
70 if found {
71 break;
72 }
73 let mut depth = 0usize;
74 let mut t = Some(cur);
75 while let Some(node) = t {
76 if node == u {
77 break;
78 }
79 depth += 1;
80 t = prev.get(&node).copied().flatten();
81 }
82 if depth > max_size.saturating_sub(1) {
83 continue;
84 }
85 let Some(neighbors) = adj.get(&cur) else {
86 continue;
87 };
88 for &nxt in neighbors {
89 if (cur == u && nxt == v) || (cur == v && nxt == u) {
90 continue;
91 }
92 if nxt == v && cur != u {
93 prev.insert(v, Some(cur));
94 found = true;
95 break;
96 }
97 if prev.contains_key(&nxt) {
98 continue;
99 }
100 prev.insert(nxt, Some(cur));
101 q.push_back(nxt);
102 }
103 }
104
105 if !found || !prev.contains_key(&v) {
106 return None;
107 }
108 let mut path = vec![v];
109 let mut cur = Some(v);
110 while cur != Some(u) {
111 cur = prev.get(&cur?)?.as_ref().copied();
112 let node = cur?;
113 path.push(node);
114 if path.len() > max_size {
115 return None;
116 }
117 }
118 path.reverse();
119 if path.len() < 3 || path.len() > max_size {
120 return None;
121 }
122 Some(path)
123}
124
125const MAX_SHORTEST_PATHS_PER_BOND: usize = 32;
127
128fn all_shortest_paths_avoiding_edge(
130 adj: &HashMap<i32, Vec<i32>>,
131 u: i32,
132 v: i32,
133 max_path_edges: usize,
134) -> Vec<Vec<i32>> {
135 if max_path_edges == 0 {
136 return Vec::new();
137 }
138 let mut dist: HashMap<i32, usize> = HashMap::new();
139 let mut parents: HashMap<i32, Vec<i32>> = HashMap::new();
140 dist.insert(u, 0);
141 let mut q = VecDeque::new();
142 q.push_back(u);
143 while let Some(cur) = q.pop_front() {
144 let d = dist[&cur];
145 if d >= max_path_edges {
146 continue;
147 }
148 if let Some(&dv) = dist.get(&v) {
149 if d >= dv {
150 continue;
151 }
152 }
153 let Some(neighbors) = adj.get(&cur) else {
154 continue;
155 };
156 for &nxt in neighbors {
157 if (cur == u && nxt == v) || (cur == v && nxt == u) {
158 continue;
159 }
160 match dist.get(&nxt).copied() {
161 None => {
162 dist.insert(nxt, d + 1);
163 parents.insert(nxt, vec![cur]);
164 q.push_back(nxt);
165 }
166 Some(dn) if dn == d + 1 => {
167 parents.entry(nxt).or_default().push(cur);
168 }
169 _ => {}
170 }
171 }
172 }
173 let Some(&dv) = dist.get(&v) else {
174 return Vec::new();
175 };
176 if dv == 0 || dv > max_path_edges {
177 return Vec::new();
178 }
179 let mut out = Vec::new();
180 let mut path = Vec::new();
181 fn walk(
182 cur: i32,
183 start: i32,
184 parents: &HashMap<i32, Vec<i32>>,
185 path: &mut Vec<i32>,
186 out: &mut Vec<Vec<i32>>,
187 cap: usize,
188 ) {
189 if out.len() >= cap {
190 return;
191 }
192 path.push(cur);
193 if cur == start {
194 let mut p = path.clone();
195 p.reverse();
196 out.push(p);
197 } else if let Some(ps) = parents.get(&cur) {
198 for &p in ps {
199 walk(p, start, parents, path, out, cap);
200 }
201 }
202 path.pop();
203 }
204 walk(
205 v,
206 u,
207 &parents,
208 &mut path,
209 &mut out,
210 MAX_SHORTEST_PATHS_PER_BOND,
211 );
212 out
213}
214
215fn canonicalize_cycle(atoms: &[i32]) -> Vec<i32> {
216 let n = atoms.len();
217 if n == 0 {
218 return Vec::new();
219 }
220 let start = (0..n).min_by_key(|&i| atoms[i]).expect("nonempty");
221 let next = atoms[(start + 1) % n];
222 let prev = atoms[(start + n - 1) % n];
223 let mut out = Vec::with_capacity(n);
224 if next <= prev {
225 for k in 0..n {
226 out.push(atoms[(start + k) % n]);
227 }
228 } else {
229 for k in 0..n {
230 out.push(atoms[(start + n - k) % n]);
231 }
232 }
233 out
234}
235
236pub fn find_relevant_rings_from_adj(
242 adj: &HashMap<i32, Vec<i32>>,
243 bonds: impl IntoIterator<Item = (i32, i32)>,
244 max_size: usize,
245) -> Vec<Ring> {
246 if max_size < 3 {
247 return Vec::new();
248 }
249 let mut by_bonds: HashMap<Vec<(i32, i32)>, Ring> = HashMap::new();
250 for (u, v) in bonds {
251 for path in all_shortest_paths_avoiding_edge(adj, u, v, max_size.saturating_sub(1)) {
252 if path.len() < 2 || path.len() > max_size {
253 continue;
254 }
255 let ring = Ring {
256 atoms: canonicalize_cycle(&path),
257 };
258 if ring.size() < 3 || ring.size() > max_size {
259 continue;
260 }
261 let mut keys: Vec<_> = ring.bonds().into_iter().collect();
262 keys.sort_unstable();
263 by_bonds.entry(keys).or_insert(ring);
264 }
265 }
266 let mut rings: Vec<Ring> = by_bonds.into_values().collect();
267 rings.sort_by(|a, b| a.size().cmp(&b.size()).then_with(|| a.atoms.cmp(&b.atoms)));
268 rings
269}
270
271pub fn find_sssr_from_adj(
277 adj: &HashMap<i32, Vec<i32>>,
278 bonds: impl IntoIterator<Item = (i32, i32)>,
279 max_size: usize,
280) -> Vec<Ring> {
281 let mut by_bonds: HashMap<Vec<(i32, i32)>, Ring> = HashMap::new();
282 for (u, v) in bonds {
283 let Some(cyc) = shortest_cycle_through_bond(adj, u, v, max_size) else {
284 continue;
285 };
286 let ring = Ring { atoms: cyc };
287 let mut keys: Vec<_> = ring.bonds().into_iter().collect();
288 keys.sort_unstable();
289 match by_bonds.get(&keys) {
290 Some(prev) if prev.size() <= ring.size() => {}
291 _ => {
292 by_bonds.insert(keys, ring);
293 }
294 }
295 }
296 let mut rings: Vec<Ring> = by_bonds.into_values().collect();
297 rings.sort_by(|a, b| a.size().cmp(&b.size()).then_with(|| a.atoms.cmp(&b.atoms)));
298 let mut kept = Vec::new();
300 let mut covered: HashSet<(i32, i32)> = HashSet::new();
301 for ring in &rings {
302 let bonds = ring.bonds();
303 if bonds.iter().any(|b| !covered.contains(b)) {
304 covered.extend(bonds);
305 kept.push(ring.clone());
306 }
307 }
308 if kept.is_empty() {
309 rings
310 } else {
311 kept
312 }
313}
314
315pub fn find_sssr(mol: &MoleculeIn, max_size: usize) -> Vec<Ring> {
317 if mol.bonds.is_empty() {
318 return Vec::new();
319 }
320 let adj = adjacency(mol);
321 find_sssr_from_adj(&adj, mol.bonds.iter().map(|b| (b.begin, b.end)), max_size)
322}
323
324fn ring_centroid(ring: &Ring, coords: &HashMap<i32, (f64, f64)>) -> Option<(f64, f64)> {
325 if ring.atoms.is_empty() {
326 return None;
327 }
328 let mut sx = 0.0;
329 let mut sy = 0.0;
330 for &i in &ring.atoms {
331 let (x, y) = *coords.get(&i)?;
332 sx += x;
333 sy += y;
334 }
335 let n = ring.atoms.len() as f64;
336 Some((sx / n, sy / n))
337}
338
339pub fn bond_interior_normals(
341 rings: &[Ring],
342 coords: &HashMap<i32, (f64, f64)>,
343) -> HashMap<(i32, i32), (f64, f64)> {
344 let mut normals = HashMap::new();
345 let mut sorted: Vec<&Ring> = rings.iter().collect();
346 sorted.sort_by_key(|r| r.size());
347 for ring in sorted {
348 let Some((cx, cy)) = ring_centroid(ring, coords) else {
349 continue;
350 };
351 let n = ring.size();
352 for i in 0..n {
353 let a = ring.atoms[i];
354 let b = ring.atoms[(i + 1) % n];
355 let key = bond_key(a, b);
356 if normals.contains_key(&key) {
357 continue;
358 }
359 let Some(&(x1, y1)) = coords.get(&a) else {
360 continue;
361 };
362 let Some(&(x2, y2)) = coords.get(&b) else {
363 continue;
364 };
365 let dx = x2 - x1;
366 let dy = y2 - y1;
367 let length = (dx * dx + dy * dy).sqrt().max(1e-9);
368 let mut nx = -dy / length;
369 let mut ny = dx / length;
370 let mx = 0.5 * (x1 + x2);
371 let my = 0.5 * (y1 + y2);
372 if (cx - mx) * nx + (cy - my) * ny < 0.0 {
373 nx = -nx;
374 ny = -ny;
375 }
376 normals.insert(key, (nx, ny));
377 }
378 }
379 normals
380}
381
382fn ring_is_fully_aromatic(ring: &Ring, order_by_bond: &HashMap<(i32, i32), f64>) -> bool {
383 let bonds = ring.bonds();
384 !bonds.is_empty()
385 && bonds
386 .iter()
387 .all(|k| order_by_bond.get(k).copied().is_some_and(is_aromatic_order))
388}
389
390fn point_line_distance(px: f64, py: f64, ax: f64, ay: f64, bx: f64, by: f64) -> f64 {
392 let dx = bx - ax;
393 let dy = by - ay;
394 let len = (dx * dx + dy * dy).sqrt().max(1e-9);
395 ((px - ax) * dy - (py - ay) * dx).abs() / len
396}
397
398pub fn aromatic_ring_circles(
403 mol: &MoleculeIn,
404 rings: &[Ring],
405 coords: &HashMap<i32, (f64, f64)>,
406) -> Vec<(f64, f64, f64)> {
407 let mut order_by_bond: HashMap<(i32, i32), f64> = HashMap::with_capacity(mol.bonds.len());
408 for b in &mol.bonds {
409 order_by_bond.insert(bond_key(b.begin, b.end), b.order);
410 }
411 let mut out = Vec::new();
412 for ring in rings {
413 if !ring_is_fully_aromatic(ring, &order_by_bond) {
414 continue;
415 }
416 let Some((cx, cy)) = ring_centroid(ring, coords) else {
417 continue;
418 };
419 let n = ring.size();
420 let mut inradius = f64::INFINITY;
421 for i in 0..n {
422 let a = ring.atoms[i];
423 let b = ring.atoms[(i + 1) % n];
424 let Some(&(x1, y1)) = coords.get(&a) else {
425 continue;
426 };
427 let Some(&(x2, y2)) = coords.get(&b) else {
428 continue;
429 };
430 inradius = inradius.min(point_line_distance(cx, cy, x1, y1, x2, y2));
431 }
432 if !inradius.is_finite() || inradius <= 1e-6 {
433 continue;
434 }
435 let r = inradius * AROMATIC_CIRCLE_FRAC;
436 if r > 1e-6 {
437 out.push((cx, cy, r));
438 }
439 }
440 out
441}
442
443pub fn kekule_paint_orders(mol: &MoleculeIn, rings: &[Ring]) -> HashMap<(i32, i32), f64> {
448 let mut orders: HashMap<(i32, i32), f64> = HashMap::with_capacity(mol.bonds.len());
449 for b in &mol.bonds {
450 orders.insert(bond_key(b.begin, b.end), b.order);
451 }
452 let aromatic_rings: Vec<&Ring> = {
453 let mut v: Vec<&Ring> = rings
454 .iter()
455 .filter(|r| ring_is_fully_aromatic(r, &orders))
456 .collect();
457 v.sort_by_key(|r| r.size());
458 v
459 };
460 for ring in aromatic_rings {
461 let n = ring.size();
462 let mut start = 0usize;
463 let mut want_double = true;
464 for i in 0..n {
465 let key = bond_key(ring.atoms[i], ring.atoms[(i + 1) % n]);
466 let cur = orders.get(&key).copied().unwrap_or(1.0);
467 if !is_aromatic_order(cur) {
468 start = (i + 1) % n;
469 want_double = !(cur >= 1.5 && cur < 2.5);
470 break;
471 }
472 }
473 for k in 0..n {
474 let i = (start + k) % n;
475 let key = bond_key(ring.atoms[i], ring.atoms[(i + 1) % n]);
476 let cur = orders.get(&key).copied().unwrap_or(1.0);
477 if is_aromatic_order(cur) {
478 orders.insert(key, if want_double { 2.0 } else { 1.0 });
479 } else {
480 want_double = !(cur >= 1.5 && cur < 2.5);
481 continue;
482 }
483 want_double = !want_double;
484 }
485 }
486 orders
487}
488
489pub fn apply_ring_interiors(mol: &mut MoleculeIn) {
494 let rings = find_sssr(mol, 8);
495 if rings.is_empty() {
496 return;
497 }
498 let coords: HashMap<i32, (f64, f64)> = mol
499 .atoms
500 .iter()
501 .map(|a: &AtomIn| (a.index, (a.x, a.y)))
502 .collect();
503 let normals = bond_interior_normals(&rings, &coords);
504 for bond in &mut mol.bonds {
505 let key = bond_key(bond.begin, bond.end);
506 if let Some(n) = normals.get(&key) {
507 bond.interior = Some(*n);
508 }
509 }
510}
511
512#[cfg(test)]
513mod tests {
514 use super::*;
515 use crate::scene::BondIn as SceneBond;
516
517 fn benzene() -> MoleculeIn {
518 let mut atoms = Vec::new();
520 for i in 0..6 {
521 let ang = std::f64::consts::PI / 2.0 + i as f64 * std::f64::consts::TAU / 6.0;
522 atoms.push(AtomIn {
523 index: i,
524 element: Some("C".into()),
525 z: None,
526 x: 20.0 * ang.cos(),
527 y: 20.0 * ang.sin(),
528 label: None,
529 charge: 0,
530 mapno: None,
531 });
532 }
533 let orders = [1.0, 2.0, 1.0, 2.0, 1.0, 2.0];
534 let mut bonds = Vec::new();
535 for i in 0..6 {
536 bonds.push(SceneBond {
537 index: i,
538 begin: i,
539 end: (i + 1) % 6,
540 order: orders[i as usize],
541 stereo: None,
542 interior: None,
543 });
544 }
545 MoleculeIn {
546 id: Some("benzene".into()),
547 atoms,
548 bonds,
549 color: None,
550 atom_shade: None,
551 bond_shade: None,
552 shade_vmin: None,
553 shade_vmax: None,
554 weight: 1.0,
555 scale: 1.0,
556 aromatic: Default::default(),
557 debug_box: false,
558 debug_align: false,
559 align_debug: None,
560 ..Default::default()
561 }
562 }
563
564 #[test]
565 fn benzene_sssr_is_one_hexagon() {
566 let rings = find_sssr(&benzene(), 8);
567 assert_eq!(rings.len(), 1);
568 assert_eq!(rings[0].size(), 6);
569 }
570
571 #[test]
572 fn aromatic_orders_yield_one_circle_kekule_yields_none() {
573 let coords: HashMap<i32, (f64, f64)> = benzene()
574 .atoms
575 .iter()
576 .map(|a| (a.index, (a.x, a.y)))
577 .collect();
578 let rings = find_sssr(&benzene(), 8);
579 assert!(aromatic_ring_circles(&benzene(), &rings, &coords).is_empty());
580
581 let mut aromatic = benzene();
582 for b in &mut aromatic.bonds {
583 b.order = 4.0;
584 }
585 let circles = aromatic_ring_circles(&aromatic, &rings, &coords);
586 assert_eq!(circles.len(), 1);
587 let (cx, cy, r) = circles[0];
588 assert!(r > 9.0 && r < 12.0, "near-edge circle, r={r}");
590 assert!(cx.abs() < 1e-6 && cy.abs() < 1e-6);
591 }
592
593 #[test]
594 fn benzene_doubles_get_interior() {
595 let mut mol = benzene();
596 apply_ring_interiors(&mut mol);
597 let doubles: Vec<_> = mol.bonds.iter().filter(|b| b.order >= 1.5).collect();
598 assert_eq!(doubles.len(), 3);
599 for b in doubles {
600 assert!(b.interior.is_some(), "bond {} missing interior", b.index);
601 }
602 }
603
604 fn ethanol_chain() -> MoleculeIn {
605 MoleculeIn {
606 id: Some("etoh".into()),
607 atoms: vec![
608 AtomIn {
609 index: 0,
610 element: Some("C".into()),
611 z: None,
612 x: 0.0,
613 y: 0.0,
614 label: None,
615 charge: 0,
616 mapno: None,
617 },
618 AtomIn {
619 index: 1,
620 element: Some("C".into()),
621 z: None,
622 x: 20.0,
623 y: 0.0,
624 label: None,
625 charge: 0,
626 mapno: None,
627 },
628 AtomIn {
629 index: 2,
630 element: Some("O".into()),
631 z: None,
632 x: 30.0,
633 y: 10.0,
634 label: Some("OH".into()),
635 charge: 0,
636 mapno: None,
637 },
638 ],
639 bonds: vec![
640 SceneBond {
641 index: 0,
642 begin: 0,
643 end: 1,
644 order: 1.0,
645 stereo: None,
646 interior: None,
647 },
648 SceneBond {
649 index: 1,
650 begin: 1,
651 end: 2,
652 order: 1.0,
653 stereo: None,
654 interior: None,
655 },
656 ],
657 color: None,
658 atom_shade: None,
659 bond_shade: None,
660 shade_vmin: None,
661 shade_vmax: None,
662 weight: 1.0,
663 scale: 1.0,
664 aromatic: Default::default(),
665 debug_box: false,
666 debug_align: false,
667 align_debug: None,
668 ..Default::default()
669 }
670 }
671
672 #[test]
673 fn acyclic_find_sssr_empty_and_apply_is_noop() {
674 let mut mol = ethanol_chain();
675 assert!(find_sssr(&mol, 8).is_empty());
676 apply_ring_interiors(&mut mol);
677 assert!(mol.bonds.iter().all(|b| b.interior.is_none()));
678
679 let empty = MoleculeIn {
680 id: None,
681 atoms: vec![],
682 bonds: vec![],
683 color: None,
684 atom_shade: None,
685 bond_shade: None,
686 shade_vmin: None,
687 shade_vmax: None,
688 weight: 1.0,
689 scale: 1.0,
690 aromatic: Default::default(),
691 debug_box: false,
692 debug_align: false,
693 align_debug: None,
694 ..Default::default()
695 };
696 assert!(find_sssr(&empty, 8).is_empty());
697 }
698
699 #[test]
700 fn naphthalene_sssr_has_two_hexagons() {
701 let mut atoms = Vec::new();
703 for i in 0..6 {
704 let ang = std::f64::consts::PI / 2.0 + i as f64 * std::f64::consts::TAU / 6.0;
705 atoms.push(AtomIn {
706 index: i,
707 element: Some("C".into()),
708 z: None,
709 x: 20.0 * ang.cos(),
710 y: 20.0 * ang.sin(),
711 label: None,
712 charge: 0,
713 mapno: None,
714 });
715 }
716 let mx = 0.5 * (atoms[0].x + atoms[1].x);
717 let my = 0.5 * (atoms[0].y + atoms[1].y);
718 let reflected: Vec<(f64, f64)> = (2..6)
719 .map(|i| {
720 let a = &atoms[i as usize];
721 (mx - (a.x - mx), my - (a.y - my))
722 })
723 .collect();
724 for (k, &(x, y)) in reflected.iter().enumerate() {
725 atoms.push(AtomIn {
726 index: 6 + k as i32,
727 element: Some("C".into()),
728 z: None,
729 x,
730 y,
731 label: None,
732 charge: 0,
733 mapno: None,
734 });
735 }
736 let mut bonds = Vec::new();
737 let mut bi = 0i32;
738 for i in 0..6 {
739 bonds.push(SceneBond {
740 index: bi,
741 begin: i,
742 end: (i + 1) % 6,
743 order: 1.0,
744 stereo: None,
745 interior: None,
746 });
747 bi += 1;
748 }
749 for &(a, b) in &[(1, 6), (6, 7), (7, 8), (8, 9), (9, 0)] {
750 bonds.push(SceneBond {
751 index: bi,
752 begin: a,
753 end: b,
754 order: 1.0,
755 stereo: None,
756 interior: None,
757 });
758 bi += 1;
759 }
760 let mol = MoleculeIn {
761 id: Some("naph".into()),
762 atoms,
763 bonds,
764 color: None,
765 atom_shade: None,
766 bond_shade: None,
767 shade_vmin: None,
768 shade_vmax: None,
769 weight: 1.0,
770 scale: 1.0,
771 aromatic: Default::default(),
772 debug_box: false,
773 debug_align: false,
774 align_debug: None,
775 ..Default::default()
776 };
777 let rings = find_sssr(&mol, 8);
778 assert_eq!(rings.len(), 2, "expected two hexagons, got {:?}", rings);
779 assert!(rings.iter().all(|r| r.size() == 6));
780 }
781
782 #[test]
783 fn large_cycle_sssr_needs_max_size() {
784 let n = 12i32;
785 let mut adj: HashMap<i32, Vec<i32>> = HashMap::new();
786 let mut bonds = Vec::new();
787 for i in 0..n {
788 let j = (i + 1) % n;
789 adj.entry(i).or_default().push(j);
790 adj.entry(j).or_default().push(i);
791 bonds.push((i, j));
792 }
793 assert!(find_sssr_from_adj(&adj, bonds.iter().copied(), 8).is_empty());
794 let rings = find_sssr_from_adj(&adj, bonds, 20);
795 assert_eq!(rings.len(), 1);
796 assert_eq!(rings[0].size(), 12);
797 }
798
799 fn cube_graph() -> (HashMap<i32, Vec<i32>>, Vec<(i32, i32)>) {
800 let edges = [
802 (0, 1),
803 (1, 2),
804 (2, 3),
805 (3, 0),
806 (4, 5),
807 (5, 6),
808 (6, 7),
809 (7, 4),
810 (0, 4),
811 (1, 5),
812 (2, 6),
813 (3, 7),
814 ];
815 let mut adj: HashMap<i32, Vec<i32>> = HashMap::new();
816 for (a, b) in edges {
817 adj.entry(a).or_default().push(b);
818 adj.entry(b).or_default().push(a);
819 }
820 (adj, edges.to_vec())
821 }
822
823 #[test]
824 fn cubane_relevant_rings_keep_all_six_faces() {
825 let (adj, bonds) = cube_graph();
826 let sssr = find_sssr_from_adj(&adj, bonds.iter().copied(), 8);
827 assert_eq!(sssr.len(), 5, "SSSR is a 5-cycle basis, got {:?}", sssr);
828 assert!(sssr.iter().all(|r| r.size() == 4));
829
830 let relevant = find_relevant_rings_from_adj(&adj, bonds, 8);
831 assert_eq!(
832 relevant.len(),
833 6,
834 "relevant rings are all six faces, got {:?}",
835 relevant
836 );
837 assert!(relevant.iter().all(|r| r.size() == 4));
838 }
839
840 #[test]
841 fn naphthalene_relevant_rings_match_two_hexagons() {
842 let edges = [
844 (0, 1),
845 (1, 2),
846 (2, 3),
847 (3, 4),
848 (4, 5),
849 (5, 0),
850 (4, 6),
851 (6, 7),
852 (7, 8),
853 (8, 9),
854 (9, 5),
855 ];
856 let mut adj: HashMap<i32, Vec<i32>> = HashMap::new();
857 for (a, b) in edges {
858 adj.entry(a).or_default().push(b);
859 adj.entry(b).or_default().push(a);
860 }
861 let relevant = find_relevant_rings_from_adj(&adj, edges, 8);
862 assert_eq!(relevant.len(), 2, "got {:?}", relevant);
863 assert!(relevant.iter().all(|r| r.size() == 6));
864 }
865}