User:Inthar/Code: Difference between revisions
| Line 656: | Line 656: | ||
type EquivalenceRelation<T> = fn(&T, &T) -> bool; | type EquivalenceRelation<T> = fn(&T, &T) -> bool; | ||
// Treating `scale` as a cyclic string (that is, "scale[i] == scale[i % scale.len()]"), | /// Treating `scale` as a cyclic string (that is, "scale[i] == scale[i % scale.len()]"), | ||
// take a slice of length `slice_length` from `basepoint`; assumes `slice_length` <= `scale`.len() | /// take a slice of length `slice_length` from `basepoint`; assumes `slice_length` <= `scale`.len() | ||
fn slice_cyclic_string(scale: &str, basepoint: usize, slice_length: usize) -> String { | fn slice_cyclic_string(scale: &str, basepoint: usize, slice_length: usize) -> String { | ||
let arr = scale; | let arr = scale; | ||
| Line 684: | Line 684: | ||
} | } | ||
// | /// Assuming that "`equiv` : T x T -> bool" is an equivalence relation, | ||
/// return the equivalence class representatives `set` under `equiv`. | |||
pub fn equivalence_class_representatives<T>(set: Vec<T>, equiv: EquivalenceRelation<T>) -> Vec<T> | pub fn equivalence_class_representatives<T>(set: Vec<T>, equiv: EquivalenceRelation<T>) -> Vec<T> | ||
where T : Clone + PartialEq { | where T : Clone + PartialEq { | ||
| Line 744: | Line 745: | ||
} | } | ||
// | /// Computes the maximum variety of a ternary scale word. | ||
pub fn maximum_variety( | pub fn maximum_variety(scale: &str) -> usize { | ||
let mut result = 0; | let mut result = 0; | ||
let floor_half: usize = | let floor_half: usize = scale.len()/2; | ||
for | for slice_length in 1..(floor_half+1) { | ||
let mut sizes: BTreeSet<(usize, usize, usize)> = BTreeSet::new(); | let mut sizes: BTreeSet<(usize, usize, usize)> = BTreeSet::new(); | ||
for | for basepoint in 0..(scale.len()) { | ||
let mut size: (usize, usize, usize) = (0, 0, 0); | let mut size: (usize, usize, usize) = (0, 0, 0); | ||
let sl = &slice_cyclic_string( | let sl = &slice_cyclic_string(scale, basepoint, slice_length); | ||
let chars_in_sl = sl.chars(); | let chars_in_sl = sl.chars(); | ||
for ch in chars_in_sl { | for ch in chars_in_sl { | ||
| Line 769: | Line 770: | ||
} | } | ||
// Return the balance of `s` = `s`(x, y, z), defined as max { | |w|_{x_i} - |w'|_{x_i} | : x_i is a letter of `s` and k = len(w) = len(w') }. | /// Return the balance of `s` = `s`(x, y, z), defined as max { | |w|_{x_i} - |w'|_{x_i} | : x_i is a letter of `s` and k = len(w) = len(w') }. | ||
pub fn balance(scale: &str) -> usize { | pub fn balance(scale: &str) -> usize { | ||
if scale.len() <= 1 { | if scale.len() <= 1 { | ||
| Line 807: | Line 808: | ||
} | } | ||
// Function for computing two properties, maximum variety and balance, while executing the subroutine to extract words only once at a time for both properties. | /// Function for computing two properties, maximum variety and balance, while executing the subroutine to extract words only once at a time for both properties. | ||
pub fn max_variety_and_balance( | pub fn max_variety_and_balance(scale: &str) -> (usize, usize) { | ||
if | if scale.len() <= 1 { | ||
( | (scale.len(), 0) | ||
} else { | } else { | ||
let mut mv_result = 0; | let mut mv_result = 0; | ||
let mut balance_result = 0; | let mut balance_result = 0; | ||
let (mut min_x, mut max_x, mut min_y, mut max_y, mut min_z, mut max_z) = (usize::MAX, 0, usize::MAX, 0, usize::MAX, 0); | let (mut min_x, mut max_x, mut min_y, mut max_y, mut min_z, mut max_z) = (usize::MAX, 0, usize::MAX, 0, usize::MAX, 0); | ||
let floor_half: usize = | let floor_half: usize = scale.len()/2; | ||
for | for slice_length in 1..(floor_half+1) { | ||
let mut sizes: BTreeSet<(usize, usize, usize)> = BTreeSet::new(); | let mut sizes: BTreeSet<(usize, usize, usize)> = BTreeSet::new(); | ||
for | for basepoint in 0..scale.len() { | ||
let mut size: (usize, usize, usize) = (0, 0, 0); | let mut size: (usize, usize, usize) = (0, 0, 0); | ||
let sl = &slice_cyclic_string( | let sl = &slice_cyclic_string(scale, basepoint, slice_length); | ||
let chars_in_sl = sl.chars(); | let chars_in_sl = sl.chars(); | ||
for ch in chars_in_sl { | for ch in chars_in_sl { | ||
| Line 846: | Line 847: | ||
} | } | ||
} | } | ||
// Return a Christoffel word with `a` x's and `b` y's. | // Return a Christoffel word with `a` x's and `b` y's. | ||