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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Implementations for core complexity types: ComplexityBound, CacheComplexity,
// RecurrenceRelation, and their Default/Display trait implementations.
impl ComplexityBound {
/// Create a new complexity bound
#[must_use]
pub fn new(class: BigOClass, coefficient: u16, input_var: InputVariable) -> Self {
Self {
class,
coefficient,
input_var,
confidence: 50,
flags: ComplexityFlags::new().with(ComplexityFlags::WORST_CASE),
_padding: [0; 2],
}
}
/// Create a constant time bound O(1)
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass};
///
/// let bound = ComplexityBound::constant();
/// assert_eq!(bound.class, BigOClass::Constant);
/// assert_eq!(bound.confidence, 100);
/// assert_eq!(bound.notation(), "O(1)");
/// ```
#[must_use]
pub fn constant() -> Self {
Self::new(BigOClass::Constant, 1, InputVariable::N)
.with_confidence(100)
.with_flags(ComplexityFlags::PROVEN)
}
/// Create a linear time bound O(n)
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass};
///
/// let bound = ComplexityBound::linear();
/// assert_eq!(bound.class, BigOClass::Linear);
/// assert_eq!(bound.notation(), "O(n)");
/// ```
#[must_use]
pub fn linear() -> Self {
Self::new(BigOClass::Linear, 1, InputVariable::N)
}
/// Create a quadratic time bound O(n^2)
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass};
///
/// let bound = ComplexityBound::quadratic();
/// assert_eq!(bound.class, BigOClass::Quadratic);
/// assert_eq!(bound.notation(), "O(n²)");
/// ```
#[must_use]
pub fn quadratic() -> Self {
Self::new(BigOClass::Quadratic, 1, InputVariable::N)
}
/// Create a logarithmic time bound O(log n)
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass};
///
/// let bound = ComplexityBound::logarithmic();
/// assert_eq!(bound.class, BigOClass::Logarithmic);
/// assert_eq!(bound.notation(), "O(log n)");
/// ```
#[must_use]
pub fn logarithmic() -> Self {
Self::new(BigOClass::Logarithmic, 1, InputVariable::N)
}
/// Create a linearithmic time bound O(n log n)
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass};
///
/// let bound = ComplexityBound::linearithmic();
/// assert_eq!(bound.class, BigOClass::Linearithmic);
/// assert_eq!(bound.notation(), "O(n log n)");
/// ```
#[must_use]
pub fn linearithmic() -> Self {
Self::new(BigOClass::Linearithmic, 1, InputVariable::N)
}
/// Create a polynomial bound with given exponent
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass};
///
/// let constant = ComplexityBound::polynomial(0, 5);
/// assert_eq!(constant.class, BigOClass::Constant);
///
/// let cubic = ComplexityBound::polynomial(3, 2);
/// assert_eq!(cubic.class, BigOClass::Cubic);
/// assert_eq!(cubic.coefficient, 2);
/// ```
#[must_use]
pub fn polynomial(exponent: u32, coefficient: u16) -> Self {
let class = match exponent {
0 => BigOClass::Constant,
1 => BigOClass::Linear,
2 => BigOClass::Quadratic,
3 => BigOClass::Cubic,
_ => BigOClass::Unknown,
};
Self::new(class, coefficient, InputVariable::N)
}
/// Create a polynomial-logarithmic bound
#[must_use]
pub fn polynomial_log(degree: u32, log_power: u32) -> Self {
match (degree, log_power) {
(1, 1) => Self::linearithmic(),
_ => Self::unknown(), // More complex cases need custom representation
}
}
/// Create an unknown complexity bound
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass};
///
/// let unknown = ComplexityBound::unknown();
/// assert_eq!(unknown.class, BigOClass::Unknown);
/// assert_eq!(unknown.confidence, 0);
/// assert_eq!(unknown.notation(), "O(?)");
/// ```
#[must_use]
pub fn unknown() -> Self {
Self::new(BigOClass::Unknown, 0, InputVariable::N).with_confidence(0)
}
/// Set confidence level (0-100)
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::ComplexityBound;
///
/// let bound = ComplexityBound::linear()
/// .with_confidence(85);
/// assert_eq!(bound.confidence, 85);
///
/// // Values over 100 are clamped
/// let clamped = ComplexityBound::linear()
/// .with_confidence(150);
/// assert_eq!(clamped.confidence, 100);
/// ```
#[must_use]
pub fn with_confidence(mut self, confidence: u8) -> Self {
self.confidence = confidence.min(100);
self
}
/// Add flags to the bound
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, ComplexityFlags};
///
/// let bound = ComplexityBound::linear()
/// .with_flags(ComplexityFlags::PROVEN | ComplexityFlags::TIGHT_BOUND);
/// assert!(bound.flags.has(ComplexityFlags::PROVEN));
/// assert!(bound.flags.has(ComplexityFlags::TIGHT_BOUND));
/// ```
#[must_use]
pub fn with_flags(mut self, flags: u8) -> Self {
self.flags = self.flags.with(flags);
self
}
/// Get notation string for this bound
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass, InputVariable};
///
/// let simple = ComplexityBound::linear();
/// assert_eq!(simple.notation(), "O(n)");
///
/// let complex = ComplexityBound::new(BigOClass::Linear, 5, InputVariable::N);
/// assert_eq!(complex.notation(), "5·O(n)");
/// ```
#[must_use]
pub fn notation(&self) -> String {
if self.coefficient <= 1 {
format!("{}", self.class)
} else {
format!("{}·{}", self.coefficient, self.class)
}
}
/// Estimate operations for given input size
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::ComplexityBound;
///
/// let linear = ComplexityBound::linear();
/// assert_eq!(linear.estimate_operations(100.0), 100.0);
///
/// let quadratic = ComplexityBound::quadratic();
/// assert_eq!(quadratic.estimate_operations(10.0), 100.0);
/// ```
#[must_use]
pub fn estimate_operations(&self, n: f64) -> f64 {
f64::from(self.coefficient) * self.class.growth_factor(n)
}
/// Check if this bound is better than another
///
/// # Examples
///
/// ```rust
/// use pmat::models::complexity_bound::{ComplexityBound, BigOClass, InputVariable};
///
/// let linear = ComplexityBound::linear();
/// let quadratic = ComplexityBound::quadratic();
/// assert!(linear.is_better_than(&quadratic));
///
/// // Same class but different coefficients
/// let fast = ComplexityBound::new(BigOClass::Linear, 2, InputVariable::N);
/// let slow = ComplexityBound::new(BigOClass::Linear, 5, InputVariable::N);
/// assert!(fast.is_better_than(&slow));
/// ```
#[must_use]
pub fn is_better_than(&self, other: &Self) -> bool {
if self.class == other.class {
self.coefficient < other.coefficient
} else {
self.class.is_better_than(&other.class)
}
}
}
impl Default for ComplexityBound {
fn default() -> Self {
Self::unknown()
}
}
impl fmt::Display for ComplexityBound {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({}% confidence)", self.notation(), self.confidence)
}
}
impl CacheComplexity {
#[must_use]
pub fn new(hit_ratio: u8, miss_penalty: u8, working_set: BigOClass) -> Self {
Self {
hit_ratio: hit_ratio.min(100),
miss_penalty,
working_set,
flags: 0,
_padding: [0; 4],
}
}
}
impl Default for CacheComplexity {
fn default() -> Self {
Self::new(0, 1, BigOClass::Unknown)
}
}
impl RecurrenceRelation {
/// Attempt to solve using the Master Theorem
#[must_use]
pub fn solve_master_theorem(&self) -> Option<ComplexityBound> {
// Check if recurrence fits Master Theorem form: T(n) = aT(n/b) + f(n)
if self.recursive_calls.len() != 1 {
return None;
}
let call = &self.recursive_calls[0];
if call.size_reduction != 0 || call.division_factor <= 1 {
return None;
}
let a = call.count;
let b = call.division_factor;
let work = &self.work_per_call;
// Apply Master Theorem based on work complexity class
match work.class {
BigOClass::Constant => {
// T(n) = aT(n/b) + O(1)
let log_b_a = f64::from(a).log(f64::from(b));
Some(ComplexityBound::polynomial(log_b_a.ceil() as u32, 1))
}
BigOClass::Linear => {
// T(n) = aT(n/b) + O(n)
let log_b_a = f64::from(a).log(f64::from(b));
if (log_b_a - 1.0).abs() < 0.01 {
// Case 2: a = b
Some(ComplexityBound::linearithmic())
} else if log_b_a < 1.0 {
// Case 3: a < b
Some(ComplexityBound::linear())
} else {
// Case 1: a > b
Some(ComplexityBound::polynomial(log_b_a.ceil() as u32, 1))
}
}
_ => None,
}
}
}