<commit_before>use std::ops::{Add, Sub};

/// Basic point on a coordinate plane
///
/// The point contains two coordinates QR to describe its position in
/// two dimensions and a third T to describe its third dimension.
///
/// Q can also be considered X on a cube, and R is Z.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Point(pub i32, pub i32, pub i32);

/// Add one point to another
impl<'a, 'b> Add<&'b Point> for &'a Point {
  type Output = Point;

  fn add(self, other: &'b Point) -> Point {
    let &Point(q0, r0, t0) = self;
    let &Point(q1, r1, t1) = other;

    Point(q0 + q1, r0 + r1, t0 + t1)
  }
}

/// Subtract one point from another
impl<'a, 'b> Sub<&'b Point> for &'a Point {
  type Output = Point;

  fn sub(self, other: &'b Point) -> Point {
    let &Point(q0, r0, t0) = self;
    let &Point(q1, r1, t1) = other;

    Point(q0 - q1, r0 - r1, t0 - t1)
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn add() {
    let point: Point = Point(1, 2, 5);
    let other: Point = Point(3, 4, 10);
    let Point(q, r, t) = &point + &other;

    assert!(4 == q);
    assert!(6 == r);
    assert!(15 == t);
  }

  #[test]
  fn sub() {
    let point: Point = Point(1, 2, 5);
    let other: Point = Point(3, 4, 10);
    let Point(q, r, t)= &point - &other;

    assert!(-2 == q);
    assert!(-2 == r);
    assert!(-5 == t);
  }
}
<commit_msg>Issue #86: Point is generic<commit_after>use std::ops::{Add, Sub};

/// Basic point on a coordinate plane
///
/// The point contains two coordinates QR to describe its position in
/// two dimensions and a third T to describe its third dimension.
///
/// Q can also be considered X on a cube, and R is Z.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Point<T = i32>(pub T, pub T, pub T);

/// Add one point to another
impl<'a, 'b, T> Add<&'b Point<T>> for &'a Point<T>
  where &'a T: Add<&'b T, Output = T> {

  type Output = Point<T>;

  fn add(self, other: &'b Point<T>) -> Point<T> {
    let &Point(ref q0, ref r0, ref t0) = self;
    let &Point(ref q1, ref r1, ref t1) = other;

    Point(q0 + q1, r0 + r1, t0 + t1)
  }
}

/// Subtract one point from another
impl<'a, 'b, T> Sub<&'b Point<T>> for &'a Point<T>
  where &'a T: Sub<&'b T, Output = T> {

  type Output = Point<T>;

  fn sub(self, other: &'b Point<T>) -> Point<T> {
    let &Point(ref q0, ref r0, ref t0) = self;
    let &Point(ref q1, ref r1, ref t1) = other;

    Point(q0 - q1, r0 - r1, t0 - t1)
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn add() {
    let point: Point = Point(1, 2, 5);
    let other: Point = Point(3, 4, 10);
    let Point(q, r, t) = &point + &other;

    assert!(4 == q);
    assert!(6 == r);
    assert!(15 == t);
  }

  #[test]
  fn sub() {
    let point: Point = Point(1, 2, 5);
    let other: Point = Point(3, 4, 10);
    let Point(q, r, t)= &point - &other;

    assert!(-2 == q);
    assert!(-2 == r);
    assert!(-5 == t);
  }
}

{'fraction_non_alphanumeric': 0.1888397414086424, 'fraction_numerical': 0.03402517863218782, 'mean_word_length': 2.6162361623616235, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5355481', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1174, 'n_words': 415}