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
use super::VecMutator;
use crate::mutators::mutations::{Mutation, RevertMutation};
use crate::{Mutator, SubValueProvider};

pub struct Remove;

#[derive(Clone)]
pub struct RemoveStep {
    pub idx: usize,
}

pub struct ConcreteRemove {
    pub idx: usize,
}
pub struct RevertRemove<T> {
    pub idx: usize,
    pub element: T,
}

impl<T, M> RevertMutation<Vec<T>, VecMutator<T, M>> for RevertRemove<T>
where
    T: Clone + 'static,
    M: Mutator<T>,
{
    #[no_coverage]
    fn revert(
        self,
        _mutator: &VecMutator<T, M>,
        value: &mut Vec<T>,
        _cache: &mut <VecMutator<T, M> as Mutator<Vec<T>>>::Cache,
    ) {
        value.insert(self.idx, self.element);
    }
}

impl<T, M> Mutation<Vec<T>, VecMutator<T, M>> for Remove
where
    T: Clone + 'static,
    M: Mutator<T>,
{
    type RandomStep = RemoveStep;
    type Step = RemoveStep;
    type Concrete<'a> = ConcreteRemove;
    type Revert = RevertRemove<T>;

    #[no_coverage]
    fn default_random_step(&self, mutator: &VecMutator<T, M>, value: &Vec<T>) -> Option<Self::RandomStep> {
        if mutator.m.max_complexity() == 0. {
            return None;
        }
        if value.len() <= *mutator.len_range.start() {
            None
        } else {
            Some(RemoveStep {
                idx: mutator.rng.usize(..value.len()),
            })
        }
    }
    #[no_coverage]
    fn random<'a>(
        _mutator: &VecMutator<T, M>,
        _value: &Vec<T>,
        _cache: &<VecMutator<T, M> as Mutator<Vec<T>>>::Cache,
        random_step: &Self::RandomStep,
        _max_cplx: f64,
    ) -> Self::Concrete<'a> {
        ConcreteRemove { idx: random_step.idx }
    }
    #[no_coverage]
    fn default_step(
        &self,
        mutator: &VecMutator<T, M>,
        value: &Vec<T>,
        _cache: &<VecMutator<T, M> as Mutator<Vec<T>>>::Cache,
    ) -> Option<Self::Step> {
        if mutator.m.max_complexity() == 0. {
            return None;
        }
        if value.len() <= *mutator.len_range.start() {
            None
        } else {
            Some(RemoveStep { idx: 0 })
        }
    }
    #[no_coverage]
    fn from_step<'a>(
        _mutator: &VecMutator<T, M>,
        value: &Vec<T>,
        _cache: &<VecMutator<T, M> as Mutator<Vec<T>>>::Cache,
        step: &'a mut Self::Step,
        _subvalue_provider: &dyn SubValueProvider,
        _max_cplx: f64,
    ) -> Option<Self::Concrete<'a>> {
        if step.idx < value.len() {
            let x = ConcreteRemove { idx: step.idx };
            step.idx += 1;
            Some(x)
        } else {
            None
        }
    }
    #[no_coverage]
    fn apply<'a>(
        mutation: Self::Concrete<'a>,
        mutator: &VecMutator<T, M>,
        value: &mut Vec<T>,
        cache: &mut <VecMutator<T, M> as Mutator<Vec<T>>>::Cache,
        _subvalue_provider: &dyn SubValueProvider,
        _max_cplx: f64,
    ) -> (Self::Revert, f64) {
        let removed = value.remove(mutation.idx);
        let removed_cplx = mutator.m.complexity(&removed, &cache.inner[mutation.idx]);
        let new_cplx = mutator.complexity_from_inner(cache.sum_cplx - removed_cplx, value.len());
        (
            RevertRemove {
                idx: mutation.idx,
                element: removed,
            },
            new_cplx,
        )
    }
}