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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Tool used to sequentialy switch between many options of same data.

/// Collection that contains several versions/options of data so you can switch between them in
/// sequential manner. It can be used to produce next data frame based on previous data frame.
///
/// # Example
/// ```
/// use psyche_utils::switch::Switch;
///
/// let mut switch = Switch::new(2, vec![1, 2, 4]);
/// if let Some((prev, next)) = switch.iterate() {
///     for i in 0..prev.len() {
///         // next frame item equals sum of two neighbors.
///         let start = i.max(1) - 1;
///         let end = (i + 2).min(prev.len());
///         next[i] = (start..end).map(|i| prev[i]).sum();
///     }
/// }
/// assert_eq!(switch.get().unwrap(), &vec![3, 7, 6]);
/// ```
pub struct Switch<T> {
    index: usize,
    options: Vec<T>,
}

impl<T> Switch<T> {
    /// Creates new switch with number of options and cloned value applied for each of them.
    ///
    /// # Arguments
    /// * `options` - Number of options that switch will hold.
    /// * `value` - Initial value applied for each of options.
    ///
    /// # Return
    /// Instance of switch.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let switch = Switch::new(2, vec![1, 2, 4]);
    /// ```
    pub fn new(options: usize, value: T) -> Self
    where
        T: Clone,
    {
        Self {
            index: 0,
            options: vec![value; options],
        }
    }

    /// Creates new switch with initial options values.
    ///
    /// # Note
    /// Make sure that your options values have same length if they are for example arrays or
    /// vectors or any other collection that needs to have same length across each iteration.
    ///
    /// # Arguments
    /// * `options` - Initial values applied for options.
    ///
    /// # Return
    /// Instance of switch.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let switch = Switch::with_options(vec![
    ///     vec![1, 2, 4],
    ///     vec![3, 7, 6],
    /// ]);
    /// ```
    pub fn with_options(options: Vec<T>) -> Self {
        Self { index: 0, options }
    }

    /// Gets currently active option index.
    ///
    /// # Return
    /// Index of currently active switch option.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let mut switch = Switch::new(2, 0);
    /// assert_eq!(switch.index(), 0);
    /// switch.switch();
    /// assert_eq!(switch.index(), 1);
    /// switch.switch();
    /// assert_eq!(switch.index(), 0);
    /// ```
    pub fn index(&self) -> usize {
        self.index
    }

    /// Gets number of options that holds.
    ///
    /// # Return
    /// Number of switch options.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let mut switch = Switch::new(2, 0);
    /// assert_eq!(switch.count(), 2);
    /// ```
    pub fn count(&self) -> usize {
        self.options.len()
    }

    /// Switches to next option.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let mut switch = Switch::with_options(vec![0, 1]);
    /// assert_eq!(*switch.get().unwrap(), 0);
    /// switch.switch();
    /// assert_eq!(*switch.get().unwrap(), 1);
    /// ```
    pub fn switch(&mut self) {
        if !self.options.is_empty() {
            self.index = (self.index + 1) % self.options.len();
        }
    }

    /// Switches to next option and returns pair of _previous_ and _next_ options.
    ///
    /// # Return
    /// Pair of _previous_ and _next_ options if holds more than one.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let mut switch = Switch::with_options(vec![0, 1]);
    /// assert_eq!(switch.iterate().unwrap(), (&0, &mut 1));
    /// ```
    pub fn iterate(&mut self) -> Option<(&T, &mut T)> {
        if !self.options.is_empty() {
            let prev = self.index;
            self.index = (self.index + 1) % self.options.len();
            let next = self.index;
            if prev != next {
                unsafe {
                    let prev_option_ptr = self.options.as_ptr().offset(prev as isize);
                    let next_option_ptr = self.options.as_mut_ptr().offset(next as isize);
                    return Some((
                        prev_option_ptr.as_ref().unwrap(),
                        next_option_ptr.as_mut().unwrap(),
                    ));
                }
            }
        }
        None
    }

    /// Gets currently active option if any.
    ///
    /// # Return
    /// Reference to currently active option.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let mut switch = Switch::with_options(vec![0, 1]);
    /// assert_eq!(switch.get().unwrap(), &0);
    /// ```
    pub fn get(&self) -> Option<&T> {
        if !self.options.is_empty() {
            Some(&self.options[self.index])
        } else {
            None
        }
    }

    /// Gets currently active option if any.
    ///
    /// # Return
    /// Mutable reference to currently active option.
    ///
    /// # Example
    /// ```
    /// use psyche_utils::switch::Switch;
    ///
    /// let mut switch = Switch::with_options(vec![0, 1]);
    /// assert_eq!(switch.get_mut().unwrap(), &mut 0);
    /// ```
    pub fn get_mut(&mut self) -> Option<&mut T> {
        if !self.options.is_empty() {
            Some(&mut self.options[self.index])
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_switch() {
        let mut switch = Switch::with_options(vec![1, 2]);
        assert_eq!(*switch.get().unwrap(), 1);
        switch.switch();
        assert_eq!(*switch.get().unwrap(), 2);
        switch.switch();
        assert_eq!(*switch.get().unwrap(), 1);
        switch.switch();
        assert_eq!(*switch.get().unwrap(), 2);
        assert_eq!(switch.iterate().unwrap(), (&2, &mut 1))
    }
}