File size: 5,607 Bytes
224e773 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | //! Conflict Registry — expert conflict declaration and querying.
//!
//! Stores pairwise conflicts between experts. When two experts are declared
//! conflicting, at most one may be active at any time.
use std::collections::HashSet;
/// Expert identifier — a unique 64-bit tag for each expert in the system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ExpertId(pub u64);
impl ExpertId {
/// Create a new ExpertId
pub fn new(id: u64) -> Self {
ExpertId(id)
}
}
/// Registry of pairwise conflicts between experts.
///
/// A conflict between A and B means at most one of {A, B} may be active
/// in any valid route configuration.
#[derive(Debug, Clone)]
pub struct ConflictRegistry {
/// Stored as canonical pairs (min, max) to avoid duplicates
conflicts: HashSet<(ExpertId, ExpertId)>,
}
impl ConflictRegistry {
/// Create an empty conflict registry
pub fn new() -> Self {
ConflictRegistry {
conflicts: HashSet::new(),
}
}
/// Canonicalize a pair so (min, max) is always stored
fn canonical(a: ExpertId, b: ExpertId) -> (ExpertId, ExpertId) {
if a.0 <= b.0 {
(a, b)
} else {
(b, a)
}
}
/// Register a conflict between two experts.
///
/// Declaring a conflict with oneself is a no-op.
pub fn register_conflict(&mut self, a: ExpertId, b: ExpertId) {
if a == b {
return;
}
let pair = Self::canonical(a, b);
self.conflicts.insert(pair);
}
/// Check whether two experts are in conflict.
pub fn are_conflicting(&self, a: ExpertId, b: ExpertId) -> bool {
if a == b {
return false;
}
let pair = Self::canonical(a, b);
self.conflicts.contains(&pair)
}
/// Find all conflicting pairs within a set of experts.
///
/// Returns pairs in deterministic order (sorted by canonical pair).
pub fn find_conflicts(&self, experts: &[ExpertId]) -> Vec<(ExpertId, ExpertId)> {
let mut found = Vec::new();
let n = experts.len();
for i in 0..n {
for j in (i + 1)..n {
if self.are_conflicting(experts[i], experts[j]) {
let pair = Self::canonical(experts[i], experts[j]);
found.push(pair);
}
}
}
found.sort();
found.dedup();
found
}
/// Return the number of registered conflicts
pub fn len(&self) -> usize {
self.conflicts.len()
}
/// Check if the registry is empty
pub fn is_empty(&self) -> bool {
self.conflicts.is_empty()
}
}
impl Default for ConflictRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_register_and_query() {
let mut reg = ConflictRegistry::new();
let a = ExpertId(1);
let b = ExpertId(2);
let c = ExpertId(3);
reg.register_conflict(a, b);
assert!(reg.are_conflicting(a, b));
assert!(reg.are_conflicting(b, a)); // symmetric
assert!(!reg.are_conflicting(a, c));
assert!(!reg.are_conflicting(b, c));
}
#[test]
fn test_self_conflict_is_noop() {
let mut reg = ConflictRegistry::new();
let a = ExpertId(1);
reg.register_conflict(a, a);
assert!(!reg.are_conflicting(a, a));
assert_eq!(reg.len(), 0);
}
#[test]
fn test_find_conflicts_in_set() {
let mut reg = ConflictRegistry::new();
let a = ExpertId(1);
let b = ExpertId(2);
let c = ExpertId(3);
let d = ExpertId(4);
reg.register_conflict(a, b);
reg.register_conflict(c, d);
let experts = vec![a, b, c, d];
let conflicts = reg.find_conflicts(&experts);
assert_eq!(conflicts.len(), 2);
assert!(conflicts.contains(&(a, b)));
assert!(conflicts.contains(&(c, d)));
}
#[test]
fn test_find_conflicts_subset() {
let mut reg = ConflictRegistry::new();
let a = ExpertId(1);
let b = ExpertId(2);
let c = ExpertId(3);
reg.register_conflict(a, b);
reg.register_conflict(a, c);
reg.register_conflict(b, c);
// Only query subset {a, c}
let conflicts = reg.find_conflicts(&[a, c]);
assert_eq!(conflicts.len(), 1);
assert!(conflicts.contains(&(a, c)));
}
#[test]
fn test_no_conflicts() {
let reg = ConflictRegistry::new();
let experts = vec![ExpertId(1), ExpertId(2), ExpertId(3)];
let conflicts = reg.find_conflicts(&experts);
assert!(conflicts.is_empty());
}
#[test]
fn test_duplicate_registration() {
let mut reg = ConflictRegistry::new();
let a = ExpertId(1);
let b = ExpertId(2);
reg.register_conflict(a, b);
reg.register_conflict(a, b);
reg.register_conflict(b, a);
assert_eq!(reg.len(), 1);
assert!(reg.are_conflicting(a, b));
}
#[test]
fn test_many_conflicts() {
let mut reg = ConflictRegistry::new();
// Register conflicts between consecutive experts
for i in 0..10 {
reg.register_conflict(ExpertId(i), ExpertId(i + 1));
}
assert_eq!(reg.len(), 10);
assert!(reg.are_conflicting(ExpertId(0), ExpertId(1)));
assert!(reg.are_conflicting(ExpertId(9), ExpertId(10)));
assert!(!reg.are_conflicting(ExpertId(0), ExpertId(2)));
}
}
|