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
#[inline]
pub fn clone_array_and_insert_at_pos<A: Clone>(v: &[A], a: A, pos: usize) -> Box<[A]> {
    // copy all elements but insert a new elements at position pos
    let mut new_array: Vec<A> = Vec::with_capacity(v.len() + 1);
    new_array.extend_from_slice(&v[0..pos]);
    new_array.push(a);
    new_array.extend_from_slice(&v[pos..]);
    new_array.into()
}

#[inline]
pub fn clone_array_and_set_at_pos<A: Clone>(v: &[A], a: A, pos: usize) -> Box<[A]> {
    // copy all elements except at pos where a replaces it.
    let mut new_array: Vec<A> = Vec::with_capacity(v.len());
    if pos > 0 {
        new_array.extend_from_slice(&v[0..pos]);
    }
    new_array.push(a);
    if pos + 1 < v.len() {
        new_array.extend_from_slice(&v[(pos + 1)..]);
    }
    new_array.into()
}

#[inline]
pub fn clone_array_and_remove_at_pos<A: Clone>(v: &[A], pos: usize) -> Box<[A]> {
    let mut v: Vec<_> = v.to_vec();
    v.remove(pos);
    v.into()
}

pub fn clone_array_and_extend<A: Clone>(v: &[A], end: A) -> Box<[A]> {
    let mut new_array: Vec<A> = Vec::with_capacity(v.len() + 1);
    new_array.extend_from_slice(v);
    new_array.push(end);
    new_array.into()
}