Initial commit

This commit is contained in:
Yash Karandikar 2023-02-20 17:56:19 -06:00
commit 97d0354aa9
5 changed files with 86 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "interplut"
version = "0.1.0"

8
Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "interplut"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

54
src/lib.rs Normal file
View file

@ -0,0 +1,54 @@
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(())
}
}

16
src/main.rs Normal file
View file

@ -0,0 +1,16 @@
use interplut::InterpLUT;
fn main() {
let mut lut: InterpLUT<f64> = InterpLUT::new();
lut.insert(31.0, 5.0);
lut.insert(5.0, 31.0);
lut.insert(5.0, 31.0);
lut.insert(5.0, 31.0);
lut.insert(4.0, 31.0);
lut.insert(3.0, 31.0);
lut.insert(2.0, 31.0);
lut.insert(1.0, 31.0);
lut.insert(18.0, 31.0);
println!("{}", lut);
}