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
use super::{genes::gene::Gene, phenotype::Phenotype};

pub struct Population<G: Gene<G, A>, A> {
    pub individuals: Vec<Phenotype<G, A>>,
    pub is_sorted: bool,
}

impl<G: Gene<G, A>, A> Population<G, A> {
    pub fn get(&self, index: usize) -> &Phenotype<G, A> {
        self.individuals.get(index).expect("Index out of bounds")
    }

    pub fn get_mut(&mut self, index: usize) -> &mut Phenotype<G, A> {
        self.is_sorted = false;
        self.individuals
            .get_mut(index)
            .expect("Index out of bounds")
    }

    pub fn set(&mut self, index: usize, individual: Phenotype<G, A>) {
        self.individuals[index] = individual;
        self.is_sorted = false;
    }

    pub fn iter(&self) -> std::slice::Iter<Phenotype<G, A>> {
        self.individuals.iter()
    }

    pub fn iter_mut(&mut self) -> std::slice::IterMut<Phenotype<G, A>> {
        self.is_sorted = false;
        self.individuals.iter_mut()
    }

    pub fn len(&self) -> usize {
        self.individuals.len()
    }

    pub fn sort_by<F>(&mut self, f: F)
    where
        F: FnMut(&Phenotype<G, A>, &Phenotype<G, A>) -> std::cmp::Ordering,
    {
        if self.is_sorted {
            return;
        }

        self.individuals.sort_by(f);
        self.is_sorted = true;
    }

    pub fn from_vec(individuals: Vec<Phenotype<G, A>>) -> Self {
        Population {
            individuals,
            is_sorted: false,
        }
    }

    pub fn from_func<F>(size: usize, f: F) -> Self
    where
        F: Fn() -> Phenotype<G, A>,
    {
        let mut individuals = Vec::with_capacity(size);
        for _ in 0..size {
            individuals.push(f());
        }

        Population {
            individuals,
            is_sorted: false,
        }
    }
}

impl<G: Gene<G, A>, A> IntoIterator for Population<G, A> {

    type Item = Phenotype<G, A>;
    type IntoIter = std::vec::IntoIter<Phenotype<G, A>>;

    fn into_iter(self) -> Self::IntoIter {
        self.individuals.into_iter()
    }
}

impl<G: Gene<G, A>, A> FromIterator<Phenotype<G, A>> for Population<G, A> {
    fn from_iter<I: IntoIterator<Item = Phenotype<G, A>>>(iter: I) -> Self {
        let individuals = iter.into_iter().collect();
        Population {
            individuals,
            is_sorted: false,
        }
    }
}

impl<G: Gene<G, A>, A> Clone for Population<G, A> {
    fn clone(&self) -> Self {
        Population {
            individuals: self.individuals.clone(),
            is_sorted: self.is_sorted,
        }
    }
}

impl<G: Gene<G, A> + std::fmt::Debug, A> std::fmt::Debug for Population<G, A> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[")?;
        for individual in &self.individuals {
            write!(f, "{:?},\n ", individual)?;
        }
        write!(f, "]")
    }
}