@@ -105,45 +105,6 @@ impl Pattern {
105105 }
106106 }
107107
108- /// Clears all pulses from a pattern
109- ///
110- /// # Examples
111- ///
112- /// ```
113- /// use rhythms::Pattern;
114- /// let mut pattern = Pattern::new(4, 2, 0);
115- /// assert_eq!([true, false, true, false], pattern.as_slice());
116- /// pattern.clear();
117- /// assert_eq!([false, false, false, false], pattern.as_slice());
118- /// ```
119- pub fn clear ( & mut self ) {
120- self . steps . clear ( ) ;
121- for _ in 0 ..self . length {
122- self . steps . push ( false ) ;
123- }
124- }
125-
126- /// Resize the current pattern. If length is
127- ///
128- /// # Arguments
129- ///
130- /// * `length` - Total number of steps
131- ///
132- /// # Examples
133- ///
134- /// ```
135- /// use rhythms::Pattern;
136- /// let mut pattern = Pattern::with_length(1);
137- /// assert_eq!([false], pattern.as_slice());
138- /// pattern.resize(4);
139- /// assert_eq!(4, pattern.len());
140- /// assert_eq!([false, false, false, false], pattern.as_slice());
141- /// ```
142- pub fn resize ( & mut self , length : usize ) {
143- self . steps . resize ( length, false ) ;
144- self . length = length;
145- }
146-
147108 /// Updates the current pattern with a evenly distributed number of pulses, using an
148109 /// abstraction based on Bjorklund's Euclidean algorithm.
149110 ///
@@ -218,17 +179,84 @@ impl Pattern {
218179 }
219180 }
220181
221- /// Returns a boolean slice reprensenting the pattern
182+ /// Clears all pulses from a pattern
222183 ///
223184 /// # Examples
224185 ///
225186 /// ```
226187 /// use rhythms::Pattern;
227- /// let pattern = Pattern::new(4, 2, 1);
228- /// assert_eq!([false, true, false, true], pattern.as_slice());
188+ /// let mut pattern = Pattern::new(4, 2, 0);
189+ /// assert_eq!([true, false, true, false], pattern.as_slice());
190+ /// pattern.clear();
191+ /// assert_eq!([false, false, false, false], pattern.as_slice());
229192 /// ```
230- pub fn as_slice ( & self ) -> & [ bool ] {
231- self . steps . as_slice ( )
193+ pub fn clear ( & mut self ) {
194+ self . steps . clear ( ) ;
195+ for _ in 0 ..self . length {
196+ self . steps . push ( false ) ;
197+ }
198+ }
199+
200+ /// Resize the current pattern. If length is
201+ ///
202+ /// # Arguments
203+ ///
204+ /// * `length` - Total number of steps
205+ ///
206+ /// # Examples
207+ ///
208+ /// ```
209+ /// use rhythms::Pattern;
210+ /// let mut pattern = Pattern::with_length(1);
211+ /// assert_eq!([false], pattern.as_slice());
212+ /// pattern.resize(4);
213+ /// assert_eq!(4, pattern.len());
214+ /// assert_eq!([false, false, false, false], pattern.as_slice());
215+ /// ```
216+ pub fn resize ( & mut self , length : usize ) {
217+ self . steps . resize ( length, false ) ;
218+ self . length = length;
219+ }
220+
221+ /// Moves the pattern cursor to the first step
222+ ///
223+ /// # Examples
224+ ///
225+ /// ```
226+ /// use rhythms::Pattern;
227+ /// let mut pattern = Pattern::new(4, 2, 0);
228+ /// assert_eq!([true, false, true, false], pattern.as_slice());
229+ /// assert_eq!(Some(true), pattern.next());
230+ /// pattern.reset();
231+ /// assert_eq!(Some(true), pattern.next());
232+ /// ```
233+ pub fn reset ( & mut self ) {
234+ self . move_cursor ( 0 ) ;
235+ }
236+
237+ /// Moves the pattern cursor to a given step. If step overflows, it will move to the last step
238+ ///
239+ /// # Arguments
240+ ///
241+ /// * `step` - Step identifiyer. Range starts at 0
242+ ///
243+ /// # Examples
244+ ///
245+ /// ```
246+ /// use rhythms::Pattern;
247+ /// let mut pattern = Pattern::new(4, 2, 0);
248+ /// assert_eq!([true, false, true, false], pattern.as_slice());
249+ /// assert_eq!(Some(true), pattern.next());
250+ /// assert_eq!(Some(false), pattern.next());
251+ /// pattern.move_cursor(1);
252+ /// assert_eq!(Some(false), pattern.next());
253+ /// ```
254+ pub fn move_cursor ( & mut self , step : usize ) {
255+ self . cursor = if self . is_in_range ( step) {
256+ step
257+ } else {
258+ self . last_index ( )
259+ } ;
232260 }
233261
234262 /// Returns the state of a step
@@ -267,6 +295,19 @@ impl Pattern {
267295 self . steps . len ( )
268296 }
269297
298+ /// Returns a boolean slice reprensenting the pattern
299+ ///
300+ /// # Examples
301+ ///
302+ /// ```
303+ /// use rhythms::Pattern;
304+ /// let pattern = Pattern::new(4, 2, 1);
305+ /// assert_eq!([false, true, false, true], pattern.as_slice());
306+ /// ```
307+ pub fn as_slice ( & self ) -> & [ bool ] {
308+ self . steps . as_slice ( )
309+ }
310+
270311 /// Returns the next step in a pattern. If the end of the pattern is reached, it resets
271312 /// the cursor and will return the first step
272313 ///
@@ -291,47 +332,6 @@ impl Pattern {
291332 step
292333 }
293334
294- /// Resets the pattern cursor to the first step
295- ///
296- /// # Examples
297- ///
298- /// ```
299- /// use rhythms::Pattern;
300- /// let mut pattern = Pattern::new(4, 2, 0);
301- /// assert_eq!([true, false, true, false], pattern.as_slice());
302- /// assert_eq!(Some(true), pattern.next());
303- /// pattern.reset();
304- /// assert_eq!(Some(true), pattern.next());
305- /// ```
306- pub fn reset ( & mut self ) {
307- self . move_cursor ( 0 ) ;
308- }
309-
310- /// Moves the pattern cursor to step. If step overflows, it will move to the last step
311- ///
312- /// # Arguments
313- ///
314- /// * `step` - Step identifiyer. Range starts at 0
315- ///
316- /// # Examples
317- ///
318- /// ```
319- /// use rhythms::Pattern;
320- /// let mut pattern = Pattern::new(4, 2, 0);
321- /// assert_eq!([true, false, true, false], pattern.as_slice());
322- /// assert_eq!(Some(true), pattern.next());
323- /// assert_eq!(Some(false), pattern.next());
324- /// pattern.move_cursor(1);
325- /// assert_eq!(Some(false), pattern.next());
326- /// ```
327- pub fn move_cursor ( & mut self , step : usize ) {
328- self . cursor = if self . is_in_range ( step) {
329- step
330- } else {
331- self . last_index ( )
332- } ;
333- }
334-
335335 fn is_in_range ( & self , step : usize ) -> bool {
336336 step < self . len ( )
337337 }
0 commit comments