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
//! Register structure.

use ndarray::Array1;

/// Register used to convert between names and indices.
pub struct Register {
    /// Species names.
    names: Array1<String>,
}

impl Register {
    /// Construct a new instance.
    #[inline]
    #[must_use]
    pub fn new(mut names: Vec<String>) -> Self {
        debug_assert!(!names.is_empty());

        names.sort();
        names.dedup();

        Self {
            names: Array1::from(names),
        }
    }

    /// Determine the index of a given name.
    #[inline]
    #[must_use]
    pub fn index(&self, name: &str) -> usize {
        self.names.iter().position(|n| n == name).unwrap()
    }

    /// Determine the .
    #[inline]
    #[must_use]
    pub fn name(&self, index: usize) -> &str {
        &self.names[index]
    }

    /// Find the total number of species in the register.
    #[inline]
    #[must_use]
    pub fn total(&self) -> usize {
        self.names.len()
    }
}