public async Task <List <Chapter> > ChangeDbToNewSnapshotAsync( TextInfo text, TextType textType, ISet <int> chaptersToInclude, SortedList <int, IDocument <TextData> > dbChapterDocs, Dictionary <int, ChapterDelta> incomingChapters) { Debug.Assert(dbChapterDocs.All(chapter => chapter.Value.IsLoaded), "Docs must be loaded from the DB."); Debug.Assert(incomingChapters.All(incomingChapter => incomingChapter.Value.Delta != null), "Incoming chapter deltas cannot be null. Maybe DeltaUsxMapper.ToChapterDeltas() has a bug?"); var tasks = new List <Task>(); var chapters = new List <Chapter>(); foreach (KeyValuePair <int, ChapterDelta> incomingChapter in incomingChapters) { if (dbChapterDocs.TryGetValue(incomingChapter.Key, out IDocument <TextData> dbChapterDoc)) { Delta diffDelta = dbChapterDoc.Data.Diff(incomingChapter.Value.Delta); if (diffDelta.Ops.Count > 0) { tasks.Add(dbChapterDoc.SubmitOpAsync(diffDelta)); } dbChapterDocs.Remove(incomingChapter.Key); } else if (chaptersToInclude == null || chaptersToInclude.Contains(incomingChapter.Key)) { // Set database to content from Paratext. dbChapterDoc = GetTextDoc(text, incomingChapter.Key, textType); tasks.Add(dbChapterDoc.CreateAsync(new TextData(incomingChapter.Value.Delta))); } chapters.Add(new Chapter { Number = incomingChapter.Key, LastVerse = incomingChapter.Value.LastVerse, IsValid = incomingChapter.Value.IsValid }); } foreach (KeyValuePair <int, IDocument <TextData> > dbChapterDoc in dbChapterDocs) { tasks.Add(dbChapterDoc.Value.DeleteAsync()); } await Task.WhenAll(tasks); return(chapters); }
public MongoCollection GetCollection(string collectionName) { if (_collections.All(x => x.Key != collectionName)) { //verify this collection actually exists on database, otherwise return null var existingCollections = _database.GetCollectionNames().ToList(); if (existingCollections.Contains(collectionName)) { _collections.Add(collectionName, _database.GetCollection(collectionName)); } } var kvp = _collections.SingleOrDefault(x => x.Key == collectionName); if (kvp.Value != null) { return(kvp.Value); } return(null); }
private static void PrintAssignment(Alphametik alphametik, SortedList <char, int> assignment) { Console.WriteLine("The following assinment solves the alphametik: "); assignment.All(kvp => { Console.WriteLine("{0} -> {1}", kvp.Key, kvp.Value); return(true); }); string op = ""; switch (alphametik.Operator) { case Operator.Addition: op = "+"; break; case Operator.Subtraction: op = "-"; break; case Operator.Multiplication: op = "*"; break; case Operator.Division: op = "/"; break; } Console.Write(String.Join(" " + op + " ", alphametik.Words.Select((w) => Alphametik.Convert(w, assignment)))); Console.WriteLine(" = {0}", Alphametik.Convert(alphametik.Result, assignment)); }
public static List <StarData> FindPath(this List <StarData> stars, StarData source, StarData target, bool bestFirst = false) { SortedList <float, DijkstraStar> members = new SortedList <float, DijkstraStar> { { 0, new DijkstraStar { Star = source } } }; List <DijkstraStar> searched = new List <DijkstraStar>(); while (true) { var s = members.FirstOrDefault(m => !searched.Contains(m.Value)).Value; // Lowest cost unsearched node if (s == null) { return(null); // No vertices left unsearched } if (s.Star == target) // We found the path { Stack <DijkstraStar> path = new Stack <DijkstraStar>(); // Since we start at the end, use a LIFO collection path.Push(s); while (path.Peek().Parent != null) // Keep pushing until we reach the start, which has no parent { path.Push(path.Peek().Parent); } return(path.Select(dv => dv.Star).ToList()); } // foreach (var dijkstraStar in s.Star.Links.Select(i => stars[i]) // All adjacent stars // .Where(n => members.All(m => m.Value.Star != n)) // That are not members // .Select(n => new DijkstraStar {Parent = s, Star = n, Cost = s.Cost + (s.Star.Position - n.Position).magnitude})) // For each adjacent star (filter already visited stars unless heuristic is in use) foreach (var dijkstraStar in s.Star.Links.WhereSelectF(i => !bestFirst || members.All(m => m.Value.Star != stars[i]), // Cost is parent cost plus distance i => new DijkstraStar { Parent = s, Star = stars[i], Cost = s.Cost + (s.Star.Position - stars[i].Position).magnitude })) { // Add new member to list, sorted by cost plus optional heuristic members.Add(bestFirst ? dijkstraStar.Cost + (dijkstraStar.Star.Position - target.Position).magnitude : dijkstraStar.Cost, dijkstraStar); } searched.Add(s); } }