55 lines
1.2 KiB
Rust
55 lines
1.2 KiB
Rust
use std::fmt::Display;
|
|
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct InterpLUT<T> {
|
|
inner: Vec<(T, T)>,
|
|
}
|
|
|
|
impl InterpLUT<f64> {
|
|
/// Creates a new, empty table.
|
|
pub fn new() -> InterpLUT<f64> {
|
|
Self::default()
|
|
}
|
|
|
|
fn sort(&mut self) {
|
|
for i in 0..self.inner.len() {
|
|
for j in 0..i {
|
|
if self.inner[j].0 > self.inner[i].0 {
|
|
self.inner.swap(i, j);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Inserts a new point into the table. Worst case is O(n^2) due to needing to sort the table
|
|
/// after inserting.
|
|
pub fn insert(&mut self, m: f64, n: f64) {
|
|
self.inner.push((m, n));
|
|
self.sort();
|
|
}
|
|
}
|
|
|
|
impl Display for InterpLUT<f64> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
let num_digits_largest = self
|
|
.inner
|
|
.last()
|
|
.copied()
|
|
.unwrap_or_default()
|
|
.0
|
|
.log10()
|
|
.ceil() as usize;
|
|
|
|
for &(m, n) in &self.inner {
|
|
writeln!(
|
|
f,
|
|
"|{: ^width$}|{: ^width$}|",
|
|
m,
|
|
n,
|
|
width = num_digits_largest + 2
|
|
)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|