User:Inthar/Code: Difference between revisions

Inthar (talk | contribs)
Inthar (talk | contribs)
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:
}
}


// Assumes that equiv : T x T -> bool is an equivalence relation.
/// 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:
}
}


// Is the scale word s = s(x, y, z) abstractly mv3?
/// Computes the maximum variety of a ternary scale word.
pub fn maximum_variety(s: &str) -> usize {
pub fn maximum_variety(scale: &str) -> usize {
     let mut result = 0;
     let mut result = 0;
let floor_half: usize = s.len()/2;
let floor_half: usize = scale.len()/2;
for l in 1..(floor_half+1) {
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 b in 0..(s.len()) {
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(s, b, l);
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(s: &str) -> (usize, usize) {
pub fn max_variety_and_balance(scale: &str) -> (usize, usize) {
     if s.len() <= 1 {
     if scale.len() <= 1 {
         (s.len(), 0)
         (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 = s.len()/2;
    let floor_half: usize = scale.len()/2;
    for l in 1..(floor_half+1) {
    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 b in 0..s.len() {
    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(s, b, l);
    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.