public static void CreateOrUpdateFileWithOneLine(VariableLetterGroup vlg, string path, Encoding encoding) { StreamWriter file = new StreamWriter(path, true, encoding); file.WriteLine(vlg.ToString()); file.Close(); }
private static VariableLetterGroup RankLetters(VariableLetterGroup letters) { VariableLetterGroup copy = new VariableLetterGroup(letters); VariableLetterGroup res = new VariableLetterGroup(); while (copy.Length() > 0) { res.Add(new Letter(copy[0].GetLetter())); int i = 1; int rank = 1; while (i < copy.Length()) { if (copy[i] == copy[0]) { res.Add(new Letter(copy[i].GetLetter(), rank)); copy.RemoveAt(i); rank++; } else { i++; } } copy.RemoveAt(0); } return(res); }
public static VariableLetterGroup operator +(VariableLetterGroup a, VariableLetterGroup b) { VariableLetterGroup c = new VariableLetterGroup(a); c.Add(b); return(c); }
// Interface public void Add(VariableLetterGroup value) { foreach (Letter v in value.GetLetters()) { _letters.Add(v); } }
public static IEnumerable <VariableLetterGroup> UniquePermutations(VariableLetterGroup letters) { VariableLetterGroup vlg = RankLetters(letters); foreach (VariableLetterGroup v in Permutations(vlg)) { VariableLetterGroup copy = new VariableLetterGroup(v); bool copy_is_consumed = false; while (!copy_is_consumed) { int i = 0; int j = 1; bool permutation_is_valid = true; while (j < copy.Length()) { if (copy[j] == copy[i]) { // we suppose the job is well done : unique rank for each identical letter if (copy[j].GetRank() > copy[i].GetRank()) { copy.RemoveAt(i); i = j - 1; } else { permutation_is_valid = false; break; } } else { j++; } } if (!permutation_is_valid) { break; } copy.RemoveAt(i); if (copy.Length() == 0) { copy_is_consumed = true; } } if (copy_is_consumed) { yield return(v); } } }
public static IEnumerable <VariableLetterGroup> AnalyzedPermutations <T>(VariableLetterGroup letters, GraphemeSet <T> graphemes) where T : Grapheme, new() { foreach (VariableLetterGroup vlg in Permutations(letters)) { if (vlg.IsValidAccordingToGraphemeSet <T>(graphemes)) { yield return(vlg); } } }
public IEnumerable <VariableLetterGroup> UniquePermutations() { VariableLetterGroup vlg = this.RankLetters(); foreach (VariableLetterGroup v in vlg.Permutations()) { if (v.IsOrderedAccordingToLetterRanks()) { yield return(v); } } }
public bool IsOrderedAccordingToLetterRanks() { VariableLetterGroup copy = new VariableLetterGroup(this); bool permutation_is_valid = false; while (!permutation_is_valid) { int i = 0; int j = 1; bool analyzed_letter_is_well_ordered = true; while (j < copy.Length()) { if (copy[j] == copy[i]) { // we suppose the job is well done : unique rank for each identical letter if (copy[j].GetRank() > copy[i].GetRank()) { copy.RemoveAt(i); i = j - 1; } else { analyzed_letter_is_well_ordered = false; break; } } else { j++; } } if (!analyzed_letter_is_well_ordered) { break; } copy.RemoveAt(i); if (copy.Length() == 0) { permutation_is_valid = true; } } return(permutation_is_valid); }
public static IEnumerable <VariableLetterGroup> Permutations(VariableLetterGroup letters) { int letters_length = letters.Length(); for (int i = 0; i < letters_length; i++) { if (letters_length == 1) { yield return(letters); } else { VariableLetterGroup res = new VariableLetterGroup(letters[i]); VariableLetterGroup vlg_to_permute = new VariableLetterGroup(letters); vlg_to_permute.RemoveAt(i); foreach (VariableLetterGroup vlg_permuted in Permutations(vlg_to_permute)) { yield return(res + vlg_permuted); } } } }
public IEnumerable <VariableLetterGroup> Permutations() { int letters_length = _letters.Count; for (int i = 0; i < letters_length; i++) { if (letters_length == 1) { yield return(this); } else { VariableLetterGroup res = new VariableLetterGroup(_letters[i]); VariableLetterGroup vlg_to_permute = new VariableLetterGroup(_letters); vlg_to_permute.RemoveAt(i); foreach (VariableLetterGroup vlg_permuted in vlg_to_permute.Permutations()) { yield return(res + vlg_permuted); } } } }