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
// Implementations for supporting complexity types: BigOClass, InputVariable,
// ComplexityFlags, and their Display trait implementations.
impl BigOClass {
/// Get a human-readable notation for the complexity class
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::BigOClass;
///
/// assert_eq!(BigOClass::Constant.notation(), "O(1)");
/// assert_eq!(BigOClass::Linear.notation(), "O(n)");
/// assert_eq!(BigOClass::Quadratic.notation(), "O(n²)");
/// ```
#[must_use]
pub fn notation(&self) -> &'static str {
match self {
Self::Constant => "O(1)",
Self::Logarithmic => "O(log n)",
Self::Linear => "O(n)",
Self::Linearithmic => "O(n log n)",
Self::Quadratic => "O(n²)",
Self::Cubic => "O(n³)",
Self::Exponential => "O(2^n)",
Self::Factorial => "O(n!)",
Self::Unknown => "O(?)",
}
}
/// Check if this complexity is better than another
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::BigOClass;
///
/// assert!(BigOClass::Constant.is_better_than(&BigOClass::Linear));
/// assert!(BigOClass::Linear.is_better_than(&BigOClass::Quadratic));
/// assert!(!BigOClass::Quadratic.is_better_than(&BigOClass::Linear));
/// ```
#[must_use]
pub fn is_better_than(&self, other: &Self) -> bool {
(*self as u8) < (*other as u8)
}
/// Get approximate growth factor for small n
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::BigOClass;
///
/// assert_eq!(BigOClass::Constant.growth_factor(100.0), 1.0);
/// assert_eq!(BigOClass::Linear.growth_factor(100.0), 100.0);
/// assert_eq!(BigOClass::Quadratic.growth_factor(10.0), 100.0);
/// ```
#[must_use]
pub fn growth_factor(&self, n: f64) -> f64 {
match self {
Self::Constant => 1.0,
Self::Logarithmic => n.log2(),
Self::Linear => n,
Self::Linearithmic => n * n.log2(),
Self::Quadratic => n * n,
Self::Cubic => n * n * n,
Self::Exponential => 2.0_f64.powf(n),
Self::Factorial => {
// Stirling's approximation for large n
if n <= 20.0 {
(1..=n as u32).map(f64::from).product()
} else {
(2.0 * std::f64::consts::PI * n).sqrt() * (n / std::f64::consts::E).powf(n)
}
}
Self::Unknown => f64::NAN,
}
}
}
impl fmt::Display for BigOClass {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.notation())
}
}
impl fmt::Display for InputVariable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::N => write!(f, "n"),
Self::M => write!(f, "m"),
Self::K => write!(f, "k"),
Self::D => write!(f, "d"),
Self::Custom => write!(f, "x"),
}
}
}
impl ComplexityFlags {
pub const AMORTIZED: u8 = 0b00000001;
pub const WORST_CASE: u8 = 0b00000010;
pub const AVERAGE_CASE: u8 = 0b00000100;
pub const BEST_CASE: u8 = 0b00001000;
pub const TIGHT_BOUND: u8 = 0b00010000;
pub const EMPIRICAL: u8 = 0b00100000;
pub const PROVEN: u8 = 0b01000000;
pub const RECURSIVE: u8 = 0b10000000;
#[must_use]
pub fn new() -> Self {
Self(0)
}
/// Adds a flag to the complexity flags
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::ComplexityFlags;
///
/// let flags = ComplexityFlags::new()
/// .with(ComplexityFlags::WORST_CASE)
/// .with(ComplexityFlags::PROVEN);
/// assert!(flags.has(ComplexityFlags::WORST_CASE));
/// assert!(flags.has(ComplexityFlags::PROVEN));
/// ```
#[must_use]
pub fn with(mut self, flag: u8) -> Self {
self.0 |= flag;
self
}
/// Checks if a flag is set
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::ComplexityFlags;
///
/// let flags = ComplexityFlags::new().with(ComplexityFlags::AMORTIZED);
/// assert!(flags.has(ComplexityFlags::AMORTIZED));
/// assert!(!flags.has(ComplexityFlags::WORST_CASE));
/// ```
#[must_use]
pub fn has(&self, flag: u8) -> bool {
self.0 & flag != 0
}
/// Checks if this represents worst-case complexity
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::ComplexityFlags;
///
/// let flags = ComplexityFlags::new()
/// .with(ComplexityFlags::WORST_CASE);
/// assert!(flags.is_worst_case());
///
/// let avg_case = ComplexityFlags::new()
/// .with(ComplexityFlags::AVERAGE_CASE);
/// assert!(!avg_case.is_worst_case());
/// ```
#[must_use]
pub fn is_worst_case(&self) -> bool {
self.has(Self::WORST_CASE)
}
/// Checks if this complexity has been formally proven
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::ComplexityFlags;
///
/// let proven = ComplexityFlags::new()
/// .with(ComplexityFlags::PROVEN);
/// assert!(proven.is_proven());
///
/// let empirical = ComplexityFlags::new()
/// .with(ComplexityFlags::EMPIRICAL);
/// assert!(!empirical.is_proven());
/// ```
#[must_use]
pub fn is_proven(&self) -> bool {
self.has(Self::PROVEN)
}
}