vec_utils.rs 3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
use std::fmt;

pub fn is_identical_to_str_ignore_order<T: Ord + fmt::Display + Clone, Q: Ord + fmt::Display + Clone> (vec: &Vec<T>, mut expect: Vec<Q>) -> bool {
    let mut vec_copy = vec.to_vec();
    vec_copy.sort();

    expect.sort();

    let a = as_str(&vec_copy);
    let b = as_str(&expect);

    a == b
}

pub fn is_identical_ignore_order<T: Ord + Clone> (vec: &Vec<T>, vec2: &Vec<T>) -> bool {
    if vec.len() != vec2.len() {
        return false;
18
    }
19 20 21 22 23 24 25 26

    let mut vec = vec.to_vec();
    let mut vec2 = vec2.to_vec();
    vec.sort();
    vec2.sort();

    for i in 0..vec.len() {
        if vec[i] != vec2[i] {
27 28 29
            return false;
        }
    }
30 31 32 33 34 35 36 37 38 39

    return true;
}

pub fn as_str<T: fmt::Display>(vec: &Vec<T>) -> String {
    let mut ret = String::new();
    for i in 0..vec.len() {
        ret.push_str(format!("{}", vec[i]).as_str());
        if i != vec.len() - 1 {
            ret.push_str(", ");
40 41
        }
    }
42 43
    ret
}
44

45 46
pub fn add_all<T: Copy + PartialEq> (vec: &mut Vec<T>, vec2: &Vec<T>) -> bool {
    let mut is_changed = false;
47

48 49 50 51
    for i in vec2.iter() {
        if !vec.contains(i) {
            vec.push(*i);
            is_changed = true;
52
        }
53 54 55 56
    }

    is_changed
}
57

58 59 60
pub fn add_unique<T: PartialEq> (vec: &mut Vec<T>, val: T) {
    if !vec.contains(&val) {
        vec.push(val);
61
    }
62 63 64 65 66 67
}

pub fn append_unique<T: PartialEq> (vec: &mut Vec<T>, vec2: &mut Vec<T>) {
    while !vec2.is_empty() {
        let val = vec2.pop().unwrap();
        add_unique(vec, val);
68
    }
69 70 71 72 73
}

pub fn append_clone_unique<T: PartialEq + Clone> (vec: &mut Vec<T>, vec2: &Vec<T>) {
    for ele in vec2 {
        add_unique(vec, ele.clone());
74
    }
75 76 77 78 79 80
}

pub fn find_value<T: PartialEq> (vec: &Vec<T>, val: T) -> Option<usize> {
    for i in 0..vec.len() {
        if vec[i] == val {
            return Some(i);
81 82
        }
    }
83

84 85
    None
}
86

87 88 89 90
pub fn remove_value<T: PartialEq> (vec: &mut Vec<T>, val: T) {
    match find_value(vec, val) {
        Some(index) => {vec.remove(index);},
        None => {} // do nothing
91
    }
92 93 94 95 96 97 98
}

pub fn remove_value_if_true<T, F> (vec: &mut Vec<T>, cond: F) -> Vec<T>
    where F : Fn(&T) -> bool
{
    let mut new_vec = vec![];
    let mut removed = vec![];
99

100 101 102 103 104 105 106
    while !vec.is_empty() {
        let val = vec.pop().unwrap();
        if cond(&val) {
            // true - remove
            removed.push(val);
        } else {
            new_vec.push(val);
107 108
        }
    }
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    vec.append(&mut new_vec);

    removed
}

pub fn remove_value_if_false<T, F> (vec: &mut Vec<T>, cond: F) -> Vec<T>
    where F : Fn(&T) -> bool
{
    let mut new_vec = vec![];
    let mut removed = vec![];

    while !vec.is_empty() {
        let val = vec.pop().unwrap();
        if cond(&val) {
            new_vec.push(val)
        } else {
            // false - remove
            removed.push(val)
128
        }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    }

    vec.append(&mut new_vec);

    removed
}

pub fn map<T, Q, F> (vec: &Vec<T>, map_func: F) -> Vec<Q>
    where F : Fn(&T) -> Q {
    let mut ret = vec![];

    for t in vec {
        ret.push(map_func(t));
    }

    ret
145
}