1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! # Multidimensional Point
//! A crate that provides a simple multidimensional point struct, base on a vector.

#![allow(dead_code)]
use num::traits::Signed;
use std::ops::{Add, Div, Mul, Sub};
/// multidimensional point type.
pub struct Point<T> {
    values: Vec<T>,
    dim: usize,
}

impl<T> Point<T>
where
    T: Clone + Default,
{
    /// Creates a new point with default values. The size of the point base on the argument.
    /// # Examples
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new(3);
    /// assert_eq!(p1.get_size(), 3);
    /// ```
    ///
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new(3);
    /// assert_eq!(p1.get_vector(), &[0,0,0]);
    /// ```
    pub fn new(dimension: usize) -> Point<T> {
        Point {
            values: vec![T::default(); dimension],
            dim: dimension,
        }
    }
}
impl<T> Point<T>
where
    T: Clone,
{
    /// Creates a new point from a vector.
    /// # Examples
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// assert_eq!(p1.get_vector(), &vec![1,2,3]);
    /// ```
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// assert_eq!(p1.get_size(), 3);
    pub fn new_from_vec(values_vec: &Vec<T>) -> Point<T> {
        Point {
            values: values_vec.clone(),
            dim: values_vec.len(),
        }
    }

    /// Return a vector with the point values.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// assert_eq!(p1.get_vector(), &vec![1,2,3]);
    /// ```
    pub fn get_vector(&self) -> &Vec<T> {
        &self.values
    }

    /// Return a value in a specific dimension.
    /// # Examples
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,10,3]);
    /// assert_eq!(p1.get_value(2), &10);
    /// ```
    ///
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<f32> = Point::new_from_vec(&vec![1.2, 3.2, 1.6]);
    /// assert_eq!(p1.get_value(1), &1.2);
    /// ```
    /// # Panic
    /// This function will panic when the dimension index is smaller than 1, or bigger than the
    /// point's dimension.
    ///
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// let p1: Point<f32> = Point::new_from_vec(&vec![1.2, 3.2, 1.6]);
    /// let _ = p1.get_value(0);
    /// ```
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// let p1: Point<f32> = Point::new_from_vec(&vec![1.2, 3.2, 1.6]);
    /// let _ = p1.get_value(10);
    /// ```
    pub fn get_value(&self, dim_index: usize) -> &T {
        self.check_valid_dim(dim_index);
        self.get_vector().get(dim_index - 1).unwrap() // dim start from 1, vector index from 0.
    }
    /// Change the value of the point in a specific dimension.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let mut p1: Point<i32> = Point::new_from_vec(&vec![1,2]);
    /// assert_eq!(p1.get_vector(), &vec![1,2]);
    /// p1.set_value(1,&5);
    /// assert_eq!(p1.get_vector(), &vec![5,2]);
    /// ```
    ///
    /// # Panic
    /// This function will panic when the dimension is invalid.
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// let mut p1: Point<i32> = Point::new_from_vec(&vec![1,2]);
    /// p1.set_value(3,&3);
    /// ```
    ///
    pub fn set_value(&mut self, dim: usize, new_val: &T) {
        self.check_valid_dim(dim);
        self.values[dim - 1] = new_val.clone(); // dim start from 1, index from 0.
    }
    fn check_valid_dim(&self, dim: usize) {
        if dim < 1 {
            panic!("dimenstion start from 1 ({} < 1)", dim);
        }
        if dim > self.dim {
            panic!("{} is bigger than {} (point max dimension)", dim, self.dim);
        }
    }
    /// This function will apply a function on every pair of values in the same dimension, and
    /// return a vector of the result.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    ///
    /// fn add_f(num1: &i32, num2: &i32) -> i32 {
    ///     num1 + num2
    /// }
    /// let p1: Point<i32> = Point::new_from_vec(&vec![2, 8, 64, 256, 0]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![2, 8, 14, 6, 0]);
    /// assert_eq!((p1.apply_func(&p2, &add_f)), &[4, 16, 78, 262, 0])
    /// ```
    /// # Panic
    /// The function will panic when the points are without equal dimensions
    ///
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// fn add_f(num1: &i32, num2: &i32) -> i32 {
    ///     num1 + num2
    /// }
    /// let p1: Point<i32> = Point::new_from_vec(&vec![2, 8, 64, 256, 0]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![2, 14, 6, 0]);
    /// p1.apply_func(&p2, &add_f);
    ///
    ///```
    pub fn apply_func<S>(&self, other: &Point<T>, f: &dyn Fn(&T, &T) -> S) -> Vec<S> {
        if self.dim != other.dim {
            panic!("dimensions are not equal");
        }
        self.get_vector()
            .iter()
            .zip(other.get_vector().iter())
            .map(|(a, b)| f(a, b))
            .collect()
    }

    /// The function will return the number of dimensions of the point.
    /// # Exampl
    /// ```
    /// use multi_dim_point::Point;
    /// let p1 : Point<bool> = Point::new(3);
    /// assert_eq!(p1.get_size(), 3);
    /// ```
    pub fn get_size(&self) -> usize {
        self.dim.clone()
    }
}

impl<T> Add for &Point<T>
where
    T: Clone + Copy + Default + Add<Output = T>,
{
    type Output = Point<T>;
    /// \+ operator. Adding values in each dimension.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![4,5,6]);
    /// assert_eq!((&p1+&p2).get_vector(), &vec![5,7,9]);
    /// ```
    /// # Panic
    /// This function will panic if the dimensions of the points are not equal.
    ///
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![5,6]);
    /// &p1+&p2;
    /// ```
    fn add(self, other: Self) -> Point<T> {
        if self.dim != other.dim {
            panic!("dimensions are not equal, can't add");
        }
        Point::new_from_vec(
            &self
                .get_vector()
                .iter()
                .zip(other.get_vector().iter())
                .map(|(a, b)| *a + *b)
                .collect(),
        )
    }
}
impl<T> Add for Point<T>
where
    T: Clone + Copy + Default + Add<Output = T>,
{
    type Output = Point<T>;
    /// \+ operator. Adding values in each dimension.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![4,5,6]);
    /// assert_eq!((p1+p2).get_vector(), &vec![5,7,9]);
    /// ```
    /// # Panic
    /// This function will panic if the dimensions of the points are not equal.
    ///
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![5,6]);
    /// p1+p2;
    /// ```
    fn add(self, other: Self) -> Point<T> {
        &self + &other
    }
}

impl<T> Sub for &Point<T>
where
    T: Clone + Copy + Default + Sub<Output = T>,
{
    type Output = Point<T>;
    /// \- operator. Subtraction values in each dimension.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![4,5,6]);
    /// assert_eq!((&p1-&p2).get_vector(), &vec![-3,-3,-3]);
    /// ```
    /// # Panic
    /// This function will panic if the dimensions of the points are not equal.
    ///
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![5,6]);
    /// &p1-&p2;
    /// ```
    fn sub(self, other: Self) -> Point<T> {
        if self.dim != other.dim {
            panic!("dimensions are not equle, can't sub");
        }
        Point::new_from_vec(
            &self
                .get_vector()
                .iter()
                .zip(other.get_vector().iter())
                .map(|(a, b)| *a - *b)
                .collect(),
        )
    }
}
impl<T> Sub for Point<T>
where
    T: Clone + Copy + Default + Sub<Output = T>,
{
    type Output = Point<T>;
    /// \- operator. Subtraction values in each dimension.
    /// # Examples
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![4,5,6]);
    /// assert_eq!((p1-p2).get_vector(), &vec![-3,-3,-3]);
    /// ```
    /// # Panic
    ///
    /// ```should_panic
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![5,6]);
    /// p1-p2;
    /// ```
    fn sub(self, other: Self) -> Point<T> {
        &self - &other
    }
}
impl<T, S> Mul<&S> for &Point<T>
where
    T: Default + Copy + Clone + Mul<S, Output = T>,
    S: Copy,
{
    type Output = Point<T>;
    /// \* operator. Multiply each value in the point.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// assert_eq!((&p1 * &5).get_vector(), &vec![5,10,15]);
    /// ```
    fn mul(self, scalar: &S) -> Point<T> {
        Point::new_from_vec(&self.get_vector().iter().map(|a| *a * *scalar).collect())
    }
}

impl<T, S> Mul<S> for Point<T>
where
    T: Default + Copy + Clone + Mul<S, Output = T>,
    S: Copy,
{
    type Output = Point<T>;
    /// \* operator. Multiply each value in the point.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![1,2,3]);
    /// assert_eq!((p1 * 5).get_vector(), &vec![5,10,15]);
    /// ```
    fn mul(self, scalar: S) -> Point<T> {
        &self * &scalar
    }
}

impl<T, S> Div<&S> for &Point<T>
where
    T: Default + Copy + Clone + Div<S, Output = T>,
    S: Copy,
{
    type Output = Point<T>;
    /// / operator. Divide each value in the point.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![5,10,15]);
    /// assert_eq!((&p1 / &5).get_vector(), &vec![1,2,3]);
    /// ```
    fn div(self, scalar: &S) -> Point<T> {
        Point::new_from_vec(&self.get_vector().iter().map(|a| *a / *scalar).collect())
    }
}

impl<T, S> Div<S> for Point<T>
where
    T: Default + Copy + Clone + Div<S, Output = T>,
    S: Copy,
{
    type Output = Point<T>;
    /// / operator. Divide each value in the point.
    /// # Example
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![5,10,15]);
    /// assert_eq!((p1 / 5).get_vector(), &vec![1,2,3]);
    /// ```
    fn div(self, scalar: S) -> Point<T> {
        &self / &scalar
    }
}

impl<T> PartialEq for Point<T>
where
    T: PartialEq + Clone,
{
    /// == operator. Check if 2 points are the same (in all dimensions).
    /// # Examples
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![5,10,15]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![5,10,15]);
    /// assert_eq!(&p1 == &p2, true);
    /// ```
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![10,15]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![5,10,15]);
    /// assert_eq!(&p1 == &p2, false);
    /// ```
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![20,10,15]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![5,10,15]);
    /// assert_eq!(&p1 == &p2, false);
    /// ```
    ///
    fn eq(&self, other: &Self) -> bool {
        self.get_size() == other.get_size()
            && self
                .get_vector()
                .iter()
                .zip(other.get_vector().iter())
                .all(|(a, b)| a == b)
    }
}

impl<T> Point<T>
where
    T: Sub<Output = T> + PartialOrd + Clone + Copy + Signed,
{
    /// Check if the points are close to each other, in each dimension, up to epsilon.
    ///
    /// # Examples:
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![5,10,15]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![7,8,14]);
    /// assert_eq!(p1.close(&p2,3), true);
    /// ```
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![11,10,15]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![7,8,14]);
    /// assert_eq!(p1.close(&p2,3), false);
    /// ```
    /// ```
    /// use multi_dim_point::Point;
    /// let p1: Point<i32> = Point::new_from_vec(&vec![7,8,14]);
    /// let p2: Point<i32> = Point::new_from_vec(&vec![7,8,14]);
    /// assert_eq!(p1.close(&p2,0), true);
    /// ```
    pub fn close(&self, other: &Self, eps: T) -> bool {
        self.get_size() == other.get_size()
            && self
                .get_vector()
                .iter()
                .zip(other.get_vector().iter())
                .all(|(a, b)| (*a - *b).abs() <= eps)
    }
}
impl<T> Clone for Point<T>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        Point::new_from_vec(&self.get_vector())
    }
}

#[cfg(test)]
mod test;