use std::cmp::Ordering;
use std::ops::{Deref, DerefMut};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VersionVector(pub Vec<usize>);
impl VersionVector {
pub fn empty() -> VersionVector {
let vec: Vec<usize> = Vec::new();
VersionVector(vec)
}
pub fn new(length: usize) -> VersionVector {
VersionVector(vec![0; length])
}
pub fn push(&mut self, value: usize) {
self.0.push(value);
}
pub fn cmp(a: &VersionVector, b: &VersionVector) -> bool {
let mut ret = true;
for i in 0..a.len() {
ret = ret && (a[i] >= b[i]);
if !ret {
break;
}
}
ret
}
pub fn compare_version_vectors(index: usize, a: &VersionVector, b: &VersionVector) -> bool {
let mut ret = true;
for i in 0..a.0.len() {
if i != index {
ret = ret && (b[i] <= a[i]);
} else {
ret = ret && (b[i] == a[i] + 1);
}
if !ret {
return false;
}
}
ret
}
pub fn equal(&self, b: &VersionVector) -> bool {
let compare = self.0.cmp(&b.0);
compare == Ordering::Equal
}
pub fn dif(
greater: &VersionVector,
lesser: &VersionVector,
length: usize,
) -> Vec<(usize, usize)> {
let mut dots = Vec::new();
for i in 0..length {
if greater[i] - lesser[i] > 0 {
for cntr in lesser[i] + 1..greater[i] + 1 {
dots.push((i, cntr));
}
}
}
dots
}
}
impl Deref for VersionVector {
type Target = Vec<usize>;
fn deref(&self) -> &Vec<usize> {
&self.0
}
}
impl DerefMut for VersionVector {
fn deref_mut(&mut self) -> &mut Vec<usize> {
&mut self.0
}
}