use super::Error;
use std::iter::Sum;
use std::ops::Add;
use std::ops::Sub;
fn add<T: PartialOrd>(v: &mut Vec<T>, e0: T) -> usize {
match v.binary_search_by(|e| crate::partial_cmp(e, &e0)) {
Ok(index) | Err(index) => {
v.insert(index, e0);
index
}
}
}
struct Step {
a: usize,
b: usize,
separate: bool,
}
fn ckk_bipart_build(partition: &mut [usize], last_weight: usize, steps: &[Step]) {
partition[last_weight] = 0;
for Step { a, b, separate } in steps.iter().rev() {
if *separate {
partition[*b] = 1 - partition[*a];
} else {
partition[*b] = partition[*a];
}
}
}
#[allow(clippy::ptr_arg)] fn ckk_bipart_rec<T>(
partition: &mut [usize],
weights: &mut Vec<(T, usize)>,
tolerance: T,
steps: &mut Vec<Step>,
) -> bool
where
T: CkkWeight,
{
debug_assert_ne!(weights.len(), 0);
if weights.len() == 1 {
let (last_weight, last_id) = weights[0];
if last_weight <= tolerance {
ckk_bipart_build(partition, last_id, steps);
return true;
}
return false;
}
let (a_weight, a_id) = weights.pop().unwrap();
let (b_weight, b_id) = weights.pop().unwrap();
let a_minus_b = (a_weight - b_weight, a_id);
let a_minus_b_idx = add(weights, a_minus_b);
steps.push(Step {
a: a_id,
b: b_id,
separate: true,
});
if ckk_bipart_rec(partition, weights, tolerance, steps) {
return true;
}
weights.remove(a_minus_b_idx);
steps.pop();
let a_plus_b = (a_weight + b_weight, a_id);
let a_plus_b_idx = add(weights, a_plus_b);
steps.push(Step {
a: a_id,
b: b_id,
separate: true,
});
if ckk_bipart_rec(partition, weights, tolerance, steps) {
return true;
}
weights.remove(a_plus_b_idx);
steps.pop();
weights.push((b_weight, b_id));
weights.push((a_weight, a_id));
false
}
fn ckk_bipart<I, T>(partition: &mut [usize], weights: I, tolerance: f64) -> Result<(), Error>
where
I: IntoIterator<Item = T>,
T: CkkWeight,
{
let mut weights: Vec<(T, usize)> = weights.into_iter().zip(0..).collect();
if weights.len() != partition.len() {
return Err(Error::InputLenMismatch {
expected: partition.len(),
actual: weights.len(),
});
}
if weights.is_empty() {
return Ok(());
}
weights.sort_unstable_by(crate::partial_cmp);
let sum: T = weights.iter().map(|(weight, _idx)| *weight).sum();
let tolerance = T::from_f64(sum.to_f64().unwrap() * tolerance).unwrap();
let mut steps = Vec::new();
if ckk_bipart_rec(partition, &mut weights, tolerance, &mut steps) {
Ok(())
} else {
Err(Error::NotFound)
}
}
pub struct CompleteKarmarkarKarp {
pub tolerance: f64,
}
pub trait CkkWeight
where
Self: Copy + Sum + PartialOrd + num::FromPrimitive + num::ToPrimitive,
Self: Add<Output = Self> + Sub<Output = Self>,
{
}
impl<T> CkkWeight for T
where
Self: Copy + Sum + PartialOrd + num::FromPrimitive + num::ToPrimitive,
Self: Add<Output = Self> + Sub<Output = Self>,
{
}
impl<W> crate::Partition<W> for CompleteKarmarkarKarp
where
W: IntoIterator,
W::Item: CkkWeight,
{
type Metadata = ();
type Error = Error;
fn partition(
&mut self,
part_ids: &mut [usize],
weights: W,
) -> Result<Self::Metadata, Self::Error> {
ckk_bipart(part_ids, weights, self.tolerance)
}
}