private static void WriteEntries(StreamWriter writer, IEnumerable <RantDictionaryEntry> entries, bool diffmark = false) { var root = new RantDictionaryTableClassDirective("root"); // we create the tree of class directives first - then we fill it var classes = entries.Where(x => GetClassesForExport(x).Any()).Select(x => GetClassesForExport(x).ToArray()).ToList(); if (classes.Any()) { CreateNestedClassDirectives(root, classes); } // now that we have a tree of class directives, let's populate it foreach (var entry in entries) { if (!GetClassesForExport(entry).Any()) { root.Entries.Add(entry); continue; } root.FindDirectiveForClasses(GetClassesForExport(entry).ToArray())?.Entries.Add(entry); } root.Prune(); root.Render(writer, -1, diffmark); }
// thank you stack exchange // http://programmers.stackexchange.com/q/267495/161632 private static void CreateNestedClassDirectives(RantDictionaryTableClassDirective parent, List <string[]> entries) { while (true) { var classCounts = new Dictionary <string, int>(); foreach (var x in entries) { int count; for (int i = 0; i < x.Length; i++) { classCounts[x[i]] = classCounts.TryGetValue(x[i], out count) ? count + 1 : 1; } } // do this again with children of this class string bestClass = classCounts.OrderByDescending(x => x.Value).First().Key; if (classCounts[bestClass] <= 3) { return; } var bestDirective = new RantDictionaryTableClassDirective(bestClass); bestDirective.Parent = parent; var childEntries = entries.Where(x => x.Contains(bestClass) && x.Length > 1).Select(x => { // remove bestClass from array return(x.Where(y => y != bestClass).ToArray()); }).ToList(); if (childEntries.Any()) { CreateNestedClassDirectives(bestDirective, childEntries); } parent.Children[bestClass] = bestDirective; // for things that aren't children of this class var otherEntries = entries.Where(x => !x.Contains(bestClass) && x.Length > 0).ToList(); if (otherEntries.Any()) { entries = otherEntries; continue; } break; } }
private static void WriteEntries(StreamWriter writer, IEnumerable<RantDictionaryEntry> entries, bool diffmark = false) { var root = new RantDictionaryTableClassDirective("root"); // we create the tree of class directives first - then we fill it var classes = entries.Where(x => GetClassesForExport(x).Any()).Select(x => GetClassesForExport(x).ToArray()).ToList(); if (classes.Any()) CreateNestedClassDirectives(root, classes); // now that we have a tree of class directives, let's populate it foreach (var entry in entries) { if (!GetClassesForExport(entry).Any()) { root.Entries.Add(entry); continue; } root.FindDirectiveForClasses(GetClassesForExport(entry).ToArray())?.Entries.Add(entry); } root.Prune(); root.Render(writer, -1, diffmark); }
// thank you stack exchange // http://programmers.stackexchange.com/q/267495/161632 private static void CreateNestedClassDirectives(RantDictionaryTableClassDirective parent, List<string[]> entries) { while (true) { var classCounts = new Dictionary<string, int>(); foreach (var x in entries) { int count; for (int i = 0; i < x.Length; i++) classCounts[x[i]] = classCounts.TryGetValue(x[i], out count) ? count + 1 : 1; } // do this again with children of this class string bestClass = classCounts.OrderByDescending(x => x.Value).First().Key; if (classCounts[bestClass] <= 3) return; var bestDirective = new RantDictionaryTableClassDirective(bestClass); bestDirective.Parent = parent; var childEntries = entries.Where(x => x.Contains(bestClass) && x.Length > 1).Select(x => { // remove bestClass from array return x.Where(y => y != bestClass).ToArray(); }).ToList(); if (childEntries.Any()) CreateNestedClassDirectives(bestDirective, childEntries); parent.Children[bestClass] = bestDirective; // for things that aren't children of this class var otherEntries = entries.Where(x => !x.Contains(bestClass) && x.Length > 0).ToList(); if (otherEntries.Any()) { entries = otherEntries; continue; } break; } }