-
Notifications
You must be signed in to change notification settings - Fork 484
/
weight.rs
69 lines (55 loc) · 1.33 KB
/
weight.rs
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
use std::ops::{Add, AddAssign, Sub, SubAssign};
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Weight {
weight: usize,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Priority {
priority: usize,
}
impl Weight {
pub fn new(w: usize) -> Weight {
Weight { weight: w }
}
pub fn zero() -> Weight {
Weight::new(0)
}
pub fn max() -> Weight {
Weight { weight: usize::MAX }
}
pub fn to_usize(self) -> usize {
self.weight
}
pub fn is_max(self) -> bool {
self.weight == usize::MAX
}
/// Returns a priority for tours with this weight; lighter tours
/// have higher priority.
pub fn to_priority(self) -> Priority {
Priority {
priority: usize::MAX - self.weight,
}
}
}
impl Add for Weight {
type Output = Weight;
fn add(self, rhs: Weight) -> Weight {
Weight::new(self.weight + rhs.weight)
}
}
impl AddAssign for Weight {
fn add_assign(&mut self, rhs: Weight) {
*self = *self + rhs;
}
}
impl Sub for Weight {
type Output = Weight;
fn sub(self, rhs: Weight) -> Weight {
Weight::new(self.weight - rhs.weight)
}
}
impl SubAssign for Weight {
fn sub_assign(&mut self, rhs: Weight) {
*self = *self - rhs;
}
}