public PublishNugetPackagesStep CompileNugetPackages() { var nugetExePath = Path.Combine(solutionSystemInfo.FolderPath, @".nuget\NuGet.exe"); var nuspecFiles = nugetPackageResultCollection.NugetPackages.Select(x => x.ProjectFile).ToArray(); var nugetCompilePath = Path.Combine(solutionSystemInfo.FolderPath, "Nuget.Packages"); Directory.CreateDirectory(nugetCompilePath); var nuspecArguments = nuspecFiles .Select(nuspecFile => $"pack -Prop Configuration=Release -IncludeReferencedProjects \"{nuspecFile.FullName}\"") .ToArray(); foreach (var nuspecArgument in nuspecArguments.AsParallel()) { ProcessHelper.Start(nugetExePath, nuspecArgument, nugetCompilePath, false); } var updatedPackages = nugetPackageResultCollection.NugetPackages.Select(x => x.Title).ToArray(); var nugetPushArguments = new DirectoryInfo(nugetCompilePath) .GetFiles() .Where(n => n.Name.EndsWith($"{buildAndRevision.Build}.{buildAndRevision.Revision}.nupkg") && updatedPackages.Any(x => n.Name.StartsWith(x))) .Select(nuspecFile => $"push {nuspecFile.FullName}"); foreach (var nuspecArgument in nugetPushArguments.AsParallel()) { ProcessHelper.Start(nugetExePath, nuspecArgument, nugetCompilePath, false); } return(new PublishNugetPackagesStep(solutionSystemInfo, nugetPackageResultCollection, buildAndRevision)); }
private static async Task Main(string[] args) { var options = Parser.Default.ParseArguments <CommandLineArgs>(args).MapResult(x => x, errors => { foreach (var error in errors) { Console.Error.Write(error); } return(null); }); if (options == null) { Environment.Exit(1); } options.ValidateOrExit(); var files = new DirectoryInfo(options.SourcePath) .GetFiles() .Where(x => x.Name != "30_search.html") .ToArray(); var parallelFiles = files.AsParallel(); if (!Directory.Exists(options.OutPath)) { Directory.CreateDirectory(options.OutPath); } var startTime = DateTime.Now; var failed = new List <string>(); Console.WriteLine($"Converting {files.Length:N0}x files in \"{options.SourcePath}\" to \"{options.OutPath}\""); var tasks = parallelFiles.Select(async fileInfo => { try { var html = await File.ReadAllTextAsync(fileInfo.FullName); html = UnityCleaner.CleanupDocument(html); var str = HtmlConverter.ToMarkDown(html); var outPath = Path.Combine(options.OutPath, fileInfo.Name.Replace(".html", ".md")); await File.WriteAllTextAsync(outPath, str); } catch (Exception) { failed.Add(fileInfo.FullName); } }); await Task.WhenAll(tasks); var time = DateTime.Now - startTime; Console.WriteLine( $"Converted {files.Length:N0}x in {time.TotalSeconds:F}s. Successful: {files.Length - failed.Count:N0} | Failed {failed.Count:N0}x"); }
public void d4j_FileList() { int counter = 1; var allSources = new DirectoryInfo(@"C:\Research\Localization\Original\SEERS\2_bug_localization_data\d4j_data\code_corpus\preprocessed").GetFiles().ToList(); allSources.AsParallel().ForAll(fileInfo => { Console.WriteLine(counter++ + " of " + allSources.Count); var bugName = fileInfo.Name.Substring(5).Replace(".json.prep", ""); var systemName = fileInfo.Name.Substring(0, 4); var infos = File.ReadAllLines(fileInfo.FullName).ToList().Select(info => { var json = JsonConvert.DeserializeObject <D4JSource>(info); return(new { FileName = json.qname.SplitWith(":")[0], Text = json.text }); }) .GroupBy(x => x.FileName) .ToList() .Select((x, index) => new { Index = index + 1, FileName = x.Key, Text = string.Join(" ", x.Select(y => y.Text)) }) .ToList(); var dirPath = @"C:\Research\Localization\Process\Seers\d4j\"; if (systemName == "Lang") { dirPath += @"lang\"; } else if (systemName == "Math") { dirPath += @"math\"; } else if (systemName == "Time") { dirPath += @"time\"; } else { throw new Exception(); } dirPath += @"Issues\" + bugName + @"\"; File.WriteAllLines(dirPath + @"\FileList.txt", infos.Select(x => x.Index + "," + x.FileName)); var sourceDirPath = dirPath + @"Source\"; if (Directory.Exists(sourceDirPath)) { Directory.Delete(sourceDirPath, true); Thread.Sleep(100); } Directory.CreateDirectory(sourceDirPath); foreach (var info in infos) { File.WriteAllLines(sourceDirPath + info.Index + ".txt", info.Text.SplitWith(" ")); } }); }
private static void Converto8S() { if (String.IsNullOrWhiteSpace(BuildDirectory)) { throw new UserMessageException("Need to set -d to a real directory fool!"); } var convertDirectory = Path.Combine(BuildDirectory, "Conversion"); var sets = new DirectoryInfo(BuildDirectory).GetFiles("*.o8s", SearchOption.TopDirectoryOnly); //foreach (var sfile in sets) //{ sets.AsParallel().ForAll(sfile => { var set = Set.SetFromFile(sfile.FullName); set.UnpackBase = convertDirectory; set.ExtractImages(); set.ExtractSetXML(); }); //} }
public List <ModDetail> LoadModsFromDirectoryAsync(string pathToDirectory) { List <ModDetail> result = new List <ModDetail>(); try { var paths = new DirectoryInfo(pathToDirectory).GetFiles().Where(x => x.Extension.ToLowerInvariant() == ".zip" || x.Extension.ToLowerInvariant() == ".disabled"); count = paths.Count(); var moddetails = paths.AsParallel().Select(x => LoadModFromZip(x.FullName)); result.AddRange(moddetails.ToArray()); } catch (Exception ex) { log.ErrorException("Error while loadings mods.", ex); } return(result); }
public List <string> GetAllFileNames() { var fileInfos = new DirectoryInfo(folder).GetFiles(searchPattern, SearchOption.AllDirectories); return(fileInfos.AsParallel().Select(fi => fi.Name).ToList()); }
private static void MeasuringAverageAlgorithmSolvingTime() { const int REPEATS = 2; System.Console.WriteLine("Measuring average algorithm solving time:"); System.Console.WriteLine(""); FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask); var solutions1 = from file in files.AsParallel() select new { intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName), fileName = file.FullName }; var solutions2 = (from solution in solutions1 group solution by solution.intermediate_solution.Solution.Type into g select(from obj in g orderby obj.fileName select obj).ToArray()).ToArray(); foreach (var sols_gr in solutions2) { var sols = SudokuSolutionNode.FilterByOptions(sols_gr.Select(gr => gr.intermediate_solution.Solution).ToList()); if (sols_gr.Length == 20) { if (SudokuOptions.Current.IncludeBoxes) { sols = sols.Take(10).ToList(); } else { sols = sols.Skip(10).Take(10).ToList(); } } var sols_gr2 = (from gr in sols_gr where sols.Contains(gr.intermediate_solution.Solution) select gr).ToList(); System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); foreach (var solution in sols_gr2) { SudokuIntermediateSolution intermediate_solution = solution.intermediate_solution; bool bError = false; for (int i = 0; i < REPEATS; i++) { for (int j = 0; j < 4; j++) { if (!intermediate_solution.Test(false)) { bError = true; } intermediate_solution = intermediate_solution.Rotate(); } } if (bError) { System.Console.WriteLine("Can't solve: {0}", solution.fileName); } } stopwatch.Stop(); double time = stopwatch.ElapsedMilliseconds * 1.0 / sols_gr.Length / REPEATS / 4; s_algorithmToTime[sols_gr.First().intermediate_solution.Solution.Type] = time; System.Console.WriteLine("{0} ({1}): {2} [ms]", sols_gr.First().intermediate_solution.Solution.Type, sols_gr.Length, time.ToString("G4")); } }
private void btnLinq_Click(object sender, RoutedEventArgs e) { // 2867 IEnumerable <FileInfo> list = new DirectoryInfo (@"c:\windows\system32").GetFiles("*.*"); // Where var soloDll1 = from f in list where filtroPerDll(f) select f; var soloDll2 = list.Where(filtroPerDll); // list.Count() > 0 if (list != null && list.Any()) { } var filtroDaConfig = list .Where(f => f.Extension == ConfigurationManager.AppSettings["Extension"]) .ToList(); // Ne esiste almeno uno che....è più grande di 50K? bool esiste50K = list.Any(f => f.Length > 50 * 1024); FileInfo file50K = list.FirstOrDefault(f => f.Length > 50000 * 1024); bool esistonoTutti = list .All(f => f.CreationTime.Year < 2015 && !f.IsReadOnly); var xyz = list //.Where(f => f.CreationTime.Year < 2015) //.Where(f => !f.IsReadOnly) .Where(f => f.Extension == ".exe") .Average(f => f.Length) .ToString() + " bytes"; int soloNascosti1 = list .Count(f => f.Attributes == FileAttributes.Hidden); int soloNascosti2 = list .Where(f => f.Attributes == FileAttributes.Hidden) .Count(); FileInfo fi = list.ElementAtOrDefault(50000); var fileOrdinati = list .Where(f => f.Name.Contains("p")) .ToList() .OrderBy(f => f.Name) .ThenBy(f => f.Length) .ThenByDescending(f => f.DirectoryName); int elementPerPage = 20; var primaPagina = list.Skip(40) .Take(elementPerPage) .ToList(); var toolbar = list.TakeWhile(f => f.Extension != ".ini") .Select(f => new TemplateButton { Etichetta = f.Name, Colore = "Red", Big = f.Length > 500000 }) .ToList(); List <IGrouping <string, FileInfo> > raggruppati = list.GroupBy(f => f.Extension).ToList(); foreach (IGrouping <string, FileInfo> item in raggruppati) { // Key contiene l'estensione string ext = item.Key; foreach (FileInfo file in item) { } } var token = new CancellationTokenSource(); // PLINQ var ricerca = list.AsParallel() .WithDegreeOfParallelism(2) .WithCancellation(token.Token) .Where(f => f.Name.Contains("ll")) .ToList(); token.Cancel(); this.toolbar.ItemsSource = toolbar; if (file50K != null) { } foreach (var item in soloDll2) { } // Metodi di materializzazione (anche delle liste) // ToList() // FirstOrDefault() // First() // LastOrDefault() // Last() // SingleOrDefault() // Single() // Any() }
public Task RefreshDatabase(Action <string> logger, Action <double> progress) { return(Task.Run(() => { logger($"Updating database {cxstring} from MIDI files in {jukebox.Settings.MIDIPath}"); var files = new DirectoryInfo(jukebox.Settings.MIDIPath) .EnumerateFiles("*.mid", SearchOption.AllDirectories) .ToList(); var total = (float)files.Count; logger($"Processing {total:#,##0} MIDI files"); // use the top-level folder name as the Genre // pre-pend the immediate folder name in the name // use the file name as the name // capture the file path too var re1 = new Regex(@"[\\\'\/\>\<;:\|\*?\@\=\^\!\`\~\#\u0000\u0001\u0003\u0004]", RegexOptions.IgnoreCase | RegexOptions.Compiled); var re2 = new Regex(@"[\s_-]+|\.$", RegexOptions.IgnoreCase | RegexOptions.Compiled); var reIgnore = new Regex(@"^(untitled|generated|played|written|words|\(brt\)|Copyright|http|www\.|e?mail|piano|acoustic|pedal|edition|sequenced|music\s+by|for\s+general|by\s+|words\s+|from\s+|arranged\s+|sung\s+|composed|dedicated|kmidi|melody|seq|track|this\s+and|1800S|midi\s+out|\S+\.com|\S+\.org|All Rights Reserved|with|when|just|Bdca426|dont|know|some|what|like|this|tk10|youre|Bwv001|Unnamed|comments|have|will|thing|come|v0100|midisource)", RegexOptions.IgnoreCase | RegexOptions.Compiled); var counter = 0; var sw = Stopwatch.StartNew(); using var db = new LiteRepository(cxstring); files.AsParallel().ForAll(file => { var subpath = file.FullName.Substring(jukebox.Settings.MIDIPath.Length + 1); var library = subpath.Contains("\\") ? subpath.Substring(0, subpath.IndexOf("\\")) : subpath; var name = Path.GetFileNameWithoutExtension(file.Name); var tags = new List <string> { Path.GetFileName(file.DirectoryName), library }; var tracksCount = 0; var duration = 0; try { using var str = file.OpenRead(); var music = MidiMusic.Read(str); duration = music.GetTotalPlayTimeMilliseconds(); var events = music.Tracks .SelectMany(track => track.Messages) .Select(msg => msg.Event) .ToList(); tracksCount = music.Tracks.Count; tags.AddRange(events .Where(e => e.EventType == MidiEvent.Meta && (e.Msb == MidiMetaType.TrackName || e.Msb == MidiMetaType.Text || e.Msb == MidiMetaType.InstrumentName)) .Select(e => new string(Encoding.ASCII.GetChars(e.ExtraData)).Trim()) .Select(m => re1.Replace(m, string.Empty)) .Select(m => re2.Replace(m, " ").Trim()) .Where(m => !reIgnore.IsMatch(m)) ); } catch (Exception ex) { logger($"Error on {file.FullName}: {ex.Message}"); return; } // add/update the tune var tune = db.Query <Tune>() .Where(t => t.Filepath.Equals(subpath, StringComparison.CurrentCultureIgnoreCase)) .FirstOrDefault() ?? new Tune(); tune.Name = name; tune.Library = library; tune.Filepath = subpath; tune.Tracks = tracksCount; tune.Durationms = duration; tune.Tags = tags .Where(m => !string.IsNullOrWhiteSpace(m)) .Where(m => m.Length > 3) .Select(m => toTitleCase(m)) .OrderBy(m => m) .Distinct() .ToList(); if (tune.Filepath != null) { db.Upsert(tune); } Interlocked.Increment(ref counter); if (counter % 500 == 0 || counter == total) { var rate = counter / (float)sw.ElapsedMilliseconds; // = items per ms var msleft = (total - counter) / rate; // = ms var eta = DateTime.Now.AddMilliseconds(msleft); logger($"{(total - counter):#,##0} = {(counter / total):P0} eta: {eta:h:mm:ss} {subpath} => {library} / {name}"); progress(counter / total); } }); logger("Shrinking Database..."); db.Database.Rebuild(); jukebox.Tunes = db.Query <Tune>().ToList(); logger("Database refresh complete"); })); }
public void Test_Solutions_Unique() { ProgressIndicator pi = new ProgressIndicator(System.Reflection.MethodBase.GetCurrentMethod().Name); FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask); var solutions1 = (from file in files.AsParallel() select new { intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName), fileName = file.FullName, }).ToArray(); solutions1.ForEach(example => Assert.IsNotNull(example.intermediate_solution, example.fileName)); var solutions2 = (from sol in solutions1.AsParallel() select new[] { new { intermediate_solution = sol.intermediate_solution, fileName = sol.fileName, rotate = 0 }, new { intermediate_solution = sol.intermediate_solution.Rotate(), fileName = sol.fileName, rotate = 1 }, new { intermediate_solution = sol.intermediate_solution.Rotate().Rotate(), fileName = sol.fileName, rotate = 2 }, new { intermediate_solution = sol.intermediate_solution.Rotate().Rotate().Rotate(), fileName = sol.fileName, rotate = 3 } }).SelectMany().ToArray(); var solutions_gr = (from obj in solutions2 group obj by obj.intermediate_solution.Solution.Type into gr select(from obj in gr orderby obj.fileName select obj).ToArray()).ToArray(); solutions_gr = (from gr in solutions_gr select(gr.Length == 80) ? new[] { gr.Take(40).ToArray(), gr.Skip(40).Take(40).ToArray() } : new[] { gr }).SelectMany(gr => gr.ToArray()).ToArray(); ConcurrentBag <string> not_uniques = new ConcurrentBag <string>(); ConcurrentCounter counter = new ConcurrentCounter(); int count = solutions_gr.Sum(gr => gr.Length); Parallel.ForEach(solutions_gr, (solution_gr) => { var sols = solution_gr; foreach (var solution1 in sols) { foreach (var solution2 in sols.Except(solution1)) { if (sols.IndexOf(solution1) >= sols.IndexOf(solution2)) { continue; } if ((solution1.fileName == solution2.fileName)) { continue; } if (solution1.intermediate_solution.Equals(solution2.intermediate_solution)) { not_uniques.Add("are the same"); not_uniques.Add("solution1; " + solution1.rotate + " - " + solution1.fileName); not_uniques.Add("solution2; " + solution2.rotate + " - " + solution2.fileName); } } counter.Increment(); pi.AddLine((counter.Value * 100 / count).ToString()); } }); if (not_uniques.Count != 0) { foreach (var line in not_uniques) { TestContext.WriteLine(line); } Assert.Fail(); } }
public void Test_Solutions_Solvable() { SudokuOptions.Current.ShowAllSolutions = false; SudokuOptions.Current.IncludeBoxes = true; FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask); ProgressIndicator pi = new ProgressIndicator(System.Reflection.MethodBase.GetCurrentMethod().Name); var solutions1 = (from file in files.AsParallel() select new { intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName), fileName = file.FullName }).ToArray(); var solutions2 = (from sol in solutions1.AsParallel() select new[] { new { intermediate_solution = sol.intermediate_solution, fileName = sol.fileName, rotate = 0 }, new { intermediate_solution = sol.intermediate_solution.Rotate(), fileName = sol.fileName, rotate = 1 }, new { intermediate_solution = sol.intermediate_solution.Rotate().Rotate(), fileName = sol.fileName, rotate = 2 }, new { intermediate_solution = sol.intermediate_solution.Rotate().Rotate().Rotate(), fileName = sol.fileName, rotate = 3 } }).SelectMany().ToArray(); var solutions3 = (from sol in solutions2 group sol by sol.intermediate_solution.Solution.Type into gr select gr.ToArray()).ToArray(); ConcurrentBag <string> unsolvable = new ConcurrentBag <string>(); int count = solutions3.Sum(gr => gr.Count()); ConcurrentCounter counter = new ConcurrentCounter(); Parallel.ForEach(ChunkPartitioner.Create(solutions3.SelectMany(), 10), (solution) => { if (!solution.intermediate_solution.Test(true)) { unsolvable.Add("unsolvable; rotate_" + solution.rotate + "; " + solution.fileName); } counter.Increment(); pi.AddLine((counter.Value * 100 / count).ToString()); }); if (unsolvable.Count > 0) { foreach (var file in unsolvable) { TestContext.WriteLine(file); } Assert.Fail(); } }
public void Test_Solutions_Quantity() { SudokuOptions.Current.ShowAllSolutions = false; SudokuOptions.Current.IncludeBoxes = true; ProgressIndicator pi = new ProgressIndicator(System.Reflection.MethodBase.GetCurrentMethod().Name); FileInfo[] files = new DirectoryInfo(Directories.Solutions).GetFiles(FileExtensions.XmlZipMask); var solutions = (from file in files.AsParallel() select new { intermediate_solution = SudokuIntermediateSolution.LoadFromFile(file.FullName), fileName = file.FullName, }).ToArray(); solutions.ForEach(example => Assert.IsNotNull(example.intermediate_solution, example.fileName)); var solutions_gr = (from obj in solutions group obj by obj.intermediate_solution.Solution.Type into gr select(from obj in gr orderby obj.fileName select obj).ToArray()).ToArray(); ConcurrentBag <string> bad_quantities = new ConcurrentBag <string>(); Parallel.ForEach(solutions_gr, (solution_gr) => { pi.AddLine((solutions_gr.IndexOf(solution_gr) * 100 / solutions_gr.Count()).ToString()); if (new[] { SudokuSolutionType.XWing, SudokuSolutionType.JellyFish, SudokuSolutionType.SwordFish }.Contains( solution_gr.First().intermediate_solution.Solution.Type)) { if (solution_gr.Count() < 20) { bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - less then 20"); } else if (solution_gr.Count() > 20) { bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - more then 20"); } else { SudokuOptions.Current.IncludeBoxes = false; var gr1 = (from obj in solution_gr.Take(10) select obj.intermediate_solution.Solution).ToList(); int c1 = SudokuSolutionNode.FilterByOptions(gr1).Count(); if (c1 == 0) { bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - solutions from 1 to 10 doesn't contains any boxes"); } if (c1 == 10) { bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - solutions from 1 to 10 all contains boxes"); } SudokuOptions.Current.IncludeBoxes = false; var gr2 = (from obj in solution_gr.Skip(10).Take(10) select obj.intermediate_solution.Solution).ToList(); int c2 = SudokuSolutionNode.FilterByOptions(gr2).Count(); if (c2 != 10) { bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - solutions from 11 to 20 some contains boxes"); } } } else { if (solution_gr.Count() < 10) { bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - less then 10"); } if (solution_gr.Count() > 10) { bad_quantities.Add(solution_gr.First().intermediate_solution.Solution.Type + " - more then 10"); } } }); if (solutions_gr.Count() != Enum.GetValues(typeof(SudokuSolutionType)).Length) { bad_quantities.Add("Not all algorithms was tested"); } if (bad_quantities.Count != 0) { foreach (var line in bad_quantities) { TestContext.WriteLine(line); } } }