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
extern crate rand;

use std::usize::MAX;

pub trait ArrayUtils<T> {

    fn swaping(&mut self, a: usize, b: usize)
        -> bool;

    fn index_of(&self, search: &T)
        -> usize;

    fn chunk(&self, size: usize)
        -> Vec<Vec<T>>;

    fn fill_mut(&mut self, value: &T);

    fn unique(&self)
        -> Vec<T>;

    fn unique_adv(&self)
        -> (Vec<T>, Vec<T>);
}

pub fn swaping<T: Ord + Copy>(ary: &mut [T], a: usize, b: usize)
    -> bool {

    if a == b { return true }
    else{
        if a > ary.len()-1 || b > ary.len()-1 { return false }
        else{
            let temp = ary[a];
            ary[a] = ary[b];
            ary[b] = temp;
            true
        }
    }
}

pub fn index_of<T: Ord + Copy>(ary: &[T], search: &T)
    -> usize {

    for i in 0..ary.len() {
        if &ary[i] == search {
            return i;
        }
    }

    MAX
}

pub fn chunk<T: Clone>(ary: &[T], size: usize)
    -> Vec<Vec<T>> {

    let mut iter = ary.chunks(size);
    let mut next = iter.next();
    let mut res = Vec::<Vec<T>>::new();

    while !next.is_none() {
        res.push((*(next.unwrap())).to_vec());
        next = iter.next();
    }

    res
}

pub fn fill_mut<T: Copy>(ary: &mut [T], value: &T) {
    for i in 0..ary.len() {
        ary[i] = *value;
    }
}

pub fn unique<T: Copy + PartialEq>(ary: &[T])
    -> Vec<T> {

    unique_adv(&ary).0
}

pub fn unique_adv<T: Copy + PartialEq>(ary: &[T])
    -> (Vec<T>, Vec<T>) {

    let mut res = Vec::<T>::new();
    let mut removed = Vec::<T>::new();

    'outer: for i in 0..ary.len() {

        for j in 0..res.len() {
            if ary[i] == res[j] {
                removed.push(ary[i]);
                continue 'outer;
            }
        }

        res.push(ary[i]);
    }

    (res, removed)
}