Skip to content

Commit

Permalink
✨ (main.rs): add new main entry point for the application
Browse files Browse the repository at this point in the history
♻️ (lib.rs): refactor functions to use slices instead of vectors for better performance
✅ (tests.rs): update tests to use slices instead of vectors
  • Loading branch information
PMassicotte committed May 27, 2024
1 parent 3f26001 commit 4a7ba6e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// pub mod isin;
use l3bin::Isin;

fn main() {
let isin = Isin::new(18);

let res = isin.bin2bounds(&[367]);

println!("{:?}", res);
}
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Isin {
/// println!("Row: {:?}", row);
/// ```
pub fn lat2row(&self, lat: f64) -> usize {
assert_eq!(is_vector_within_bounds(&vec![lat], MIN_LAT, MAX_LAT), true);
assert!(is_vector_within_bounds(&[lat], MIN_LAT, MAX_LAT));

let row = (90.0 + lat) * (self.numrows as f64) / 180.0 + 1.0;
row as usize
Expand All @@ -76,12 +76,12 @@ impl Isin {
/// # Example
/// ```
/// let is = l3bin::Isin::new(4320);
/// let bin = is.lonlat2bin(vec![45.0], vec![45.0]);
/// let bin = is.lonlat2bin(&[45.0], &[45.0]);
/// println!("Bin: {:?}", bin);
/// ```
pub fn lonlat2bin(&self, lon: Vec<f64>, lat: Vec<f64>) -> Vec<usize> {
assert_eq!(is_vector_within_bounds(&lon, MIN_LON, MAX_LON), true);
assert_eq!(is_vector_within_bounds(&lat, MIN_LAT, MAX_LAT), true);
pub fn lonlat2bin(&self, lon: &[f64], lat: &[f64]) -> Vec<usize> {
assert!(is_vector_within_bounds(lon, MIN_LON, MAX_LON));
assert!(is_vector_within_bounds(lat, MIN_LAT, MAX_LAT));

let mut bin: Vec<usize> = Vec::with_capacity(lat.len());

Expand All @@ -93,7 +93,7 @@ impl Isin {
col = self.numbin[row] - 1;
}

bin.push(self.basebin[row] + col as usize);
bin.push(self.basebin[row] + col);
}

bin
Expand All @@ -110,7 +110,7 @@ impl Isin {
/// println!("Lonlat: {:?}", lonlat);
/// ```
pub fn bin2lonlat(&self, bin: &[usize]) -> Vec<(f64, f64)> {
assert_eq!(bin.iter().all(|&b| b >= 1 && b <= self.totbin), true);
assert!(bin.iter().all(|&b| b >= 1 && b <= self.totbin));

let mut result: Vec<(f64, f64)> = Vec::with_capacity(bin.len());

Expand Down Expand Up @@ -144,7 +144,7 @@ impl Isin {
/// # Note
/// The bounds are returned in the order north, south, west, east.
pub fn bin2bounds(&self, bin: &[usize]) -> Vec<(f64, f64, f64, f64)> {
assert_eq!(bin.iter().all(|&b| b >= 1 && b <= self.totbin), true);
assert!(bin.iter().all(|&b| b >= 1 && b <= self.totbin));

let mut result: Vec<(f64, f64, f64, f64)> = Vec::with_capacity(bin.len());

Expand Down Expand Up @@ -173,7 +173,7 @@ impl Isin {
}
}

fn is_vector_within_bounds(numbers: &Vec<f64>, lower_bound: f64, upper_bound: f64) -> bool {
fn is_vector_within_bounds(numbers: &[f64], lower_bound: f64, upper_bound: f64) -> bool {
numbers
.iter()
.all(|&num| num >= lower_bound && num <= upper_bound)
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use l3bin::Isin;

fn main() {
let isin = Isin::new(18);

let res = isin.bin2bounds(&[367]);

println!("{:?}", res);

println!("{:?}", isin.lonlat2bin(&[78.0], &[-36.0]));
}
8 changes: 4 additions & 4 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Implement tests for isin
#[cfg(test)]
use l3bin::Isin;

mod tests {
use super::*;
use l3bin::Isin;

//check modis resturn 4320 rows
// #[test]
Expand All @@ -25,7 +25,7 @@ mod tests {
let isin = Isin::new(4320);
let lon = vec![181.0, 0.0];
let lat = vec![0.0, 0.0];
isin.lonlat2bin(lon, lat);
isin.lonlat2bin(&lon, &lat);
}

// Check lonlat fails if lat is out of bounds
Expand All @@ -35,7 +35,7 @@ mod tests {
let isin = Isin::new(4320);
let lon = vec![0.0, 0.0];
let lat = vec![91.0, 0.0];
isin.lonlat2bin(lon, lat);
isin.lonlat2bin(&lon, &lat);
}

// Check lat2row fails if lat is out of bounds
Expand Down

0 comments on commit 4a7ba6e

Please sign in to comment.