Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rawler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ toml = "0.8"
uuid = {version = "1.0", features = ["serde", "v4"]}
weezl = "0.1.7"
memmap2 = "0.9.4"
num-integer = "0.1.46"
impl_ops = "0.1.1"

[dev-dependencies]
criterion = {version = "0.6", features = ["html_reports"]}
Expand Down
68 changes: 68 additions & 0 deletions rawler/src/formats/tiff/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use byteorder::{NativeEndian, WriteBytesExt};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{convert::Infallible, ffi::CString, fmt::Display, io::Write, num::TryFromIntError};
use std::iter::Sum;
use num_integer::lcm;
use std::ops;

use super::{Result, TiffError};

Expand Down Expand Up @@ -53,6 +56,51 @@ impl Display for SRational {
}
}

impl_op_ex!(+ |lhs: &Rational, rhs: &Rational| -> Rational {
if lhs.d == rhs.d {
return Rational::new(lhs.n + rhs.n, lhs.d);
}

let lcm = lcm(lhs.d, rhs.d);
let lhs_n = lhs.n * (lcm / lhs.d);
let rhs_n = rhs.n * (lcm / rhs.d);

Rational::new(lhs_n + rhs_n, lcm)
});

impl_op_ex!(+ |lhs: &SRational, rhs: &SRational| -> SRational {
if lhs.d == rhs.d {
return SRational::new(lhs.n + rhs.n, lhs.d);
}

let lcm = lcm(lhs.d, rhs.d);
let lhs_n = lhs.n * (lcm / lhs.d);
let rhs_n = rhs.n * (lcm / rhs.d);

SRational::new(lhs_n + rhs_n, lcm)
});

impl Sum for Rational {
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
iter.fold(Self{n: 0, d: 1}, |acc, x| acc + x)
}
}
impl<'a> Sum<&'a Rational> for Rational {
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold(Self{n: 0, d: 1}, |acc, x| acc + x)
}
}
impl Sum for SRational {
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
iter.fold(Self{n: 0, d: 1}, |acc, x| acc + x)
}
}
impl<'a> Sum<&'a SRational> for SRational {
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
iter.fold(Self{n: 0, d: 1}, |acc, x| acc + x)
}
}

impl TryFrom<Rational> for usize {
type Error = TryFromIntError;

Expand Down Expand Up @@ -300,6 +348,10 @@ impl SRational {
pub fn new(n: i32, d: i32) -> Self {
Self { n, d }
}

pub fn as_f32(&self) -> f32 {
self.n as f32 / self.d as f32
}
}

impl PartialEq for SRational {
Expand Down Expand Up @@ -1354,4 +1406,20 @@ mod tests {
let b = SRational::new(-300, 10);
assert!(a < b);
}

#[test]
fn rational_sum() {
let a = Rational::new(1, 2);
let b = Rational::new(1, 4);

assert_eq!([a, b].iter().sum::<Rational>(), Rational::new(3, 4));
}

#[test]
fn srational_sum() {
let a = SRational::new(1, 2);
let b = SRational::new(-3, 4);

assert_eq!([a, b].iter().sum::<SRational>(), SRational::new(-1, 4));
}
}
2 changes: 2 additions & 0 deletions rawler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ use std::io::Seek;
use std::path::Path;
use thiserror::Error;

#[macro_use] extern crate impl_ops;

pub(crate) const ISSUE_HINT: &str = "Please open an issue at https://github.com/dnglab/dnglab/issues and provide this message (optionally the RAW file, if you can license it under CC0-license).";

pub trait ReadTrait: Read + Seek {}
Expand Down
Loading