private PriorityData(long id, string displayName, string iconName) { ID = id; DisplayName = displayName; bitmapImage = WazeraUtils.GetResource(iconName); Priorities.Add(this); PriorityMap.Add(ID, this); }
private bool TryLoadPriorityMap() { try { PriorityMap.Clear(); Log.InfoV($"Reading {Options.PriorityFile}..."); using (TextFieldParser p = new TextFieldParser(Options.PriorityFile)) { p.CommentTokens = new string[] { "#" }; p.SetDelimiters(new string[] { "," }); while (!p.EndOfData) { bool hasPriority = false; bool hasIndex = false; int priority = -1; int index = -1; long line = p.LineNumber; string[] fields = p.ReadFields(); if (fields.Length >= 2) { hasPriority = int.TryParse(fields[0], out priority); hasIndex = int.TryParse(fields[1], out index); } if (!hasPriority || !hasIndex || index < 0) { Log.Info($"Warning: {Path.GetFileName(Options.PriorityFile)}:{line}: Line is malformatted."); continue; } if (index >= MaxCapacity) { Log.Info($"Warning: {Path.GetFileName(Options.PriorityFile)}:{line}: Index exceeds maximum value of {MaxCapacity - 1}."); continue; } if (!PriorityMap.ContainsKey(priority)) { PriorityMap[priority] = new List <int>(); } PriorityMap[priority].Add(index); } } return(true); } catch (Exception e) { if (e is IOException || e is UnauthorizedAccessException || e is SecurityException) { Result = ExitCode.BadIO; Log.Exception(e); return(false); } throw; } }
private void GeneratePriorityMap() { PriorityMap.Clear(); IEnumerable <int> carGenIndices = Enumerable.Range(0, MaxCapacity).OrderBy(e => RandGen.Next()); int i = 0; foreach (int index in carGenIndices) { PriorityMap[i++] = new List <int>() { index }; } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void shouldReplaceIfBetter() internal virtual void ShouldReplaceIfBetter() { // GIVEN PriorityMap <int, int, double> map = PriorityMap.WithSelfKeyNaturalOrder(); map.Put(1, 2d); // WHEN bool putResult = map.Put(1, 1.5d); // THEN assertTrue(putResult); Entry <int, double> top = map.Pop(); assertNull(map.Peek()); assertEquals(1, top.Entity); assertEquals(1.5d, top.Priority, 0.00001); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void inCaseSaveAllPrioritiesShouldHandleNewEntryWithBetterPrio() internal virtual void InCaseSaveAllPrioritiesShouldHandleNewEntryWithBetterPrio() { // GIVEN int first = 1; int second = 2; PriorityMap <int, int, double> map = PriorityMap.WithSelfKeyNaturalOrder(false, false); // WHEN assertTrue(map.Put(first, 3d)); assertTrue(map.Put(second, 2d)); assertTrue(map.Put(first, 1d)); // THEN AssertEntry(map.Pop(), first, 1d); AssertEntry(map.Pop(), second, 2d); AssertEntry(map.Pop(), first, 3d); assertNull(map.Peek()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void testIt() internal virtual void TestIt() { PriorityMap <int, int, double> map = PriorityMap.WithSelfKeyNaturalOrder(); map.Put(0, 5d); map.Put(1, 4d); map.Put(1, 4d); map.Put(1, 3d); AssertEntry(map.Pop(), 1, 3d); AssertEntry(map.Pop(), 0, 5d); assertNull(map.Pop()); int start = 0; int a = 1; int b = 2; int c = 3; int d = 4; int e = 6; int f = 7; int y = 8; int x = 9; map.Put(start, 0d); map.Put(a, 1d); // get start // get a map.Put(x, 10d); map.Put(b, 2d); // get b map.Put(x, 9d); map.Put(c, 3d); // get c map.Put(x, 8d); map.Put(x, 6d); map.Put(d, 4d); // get d map.Put(x, 7d); map.Put(e, 5d); // get e map.Put(x, 6d); map.Put(f, 7d); // get x map.Put(y, 8d); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test void shouldKeepAllPrioritiesIfToldTo() internal virtual void ShouldKeepAllPrioritiesIfToldTo() { // GIVEN int entity = 5; PriorityMap <int, int, double> map = PriorityMap.WithSelfKeyNaturalOrder(false, false); assertTrue(map.Put(entity, 3d)); assertTrue(map.Put(entity, 2d)); // WHEN assertTrue(map.Put(entity, 5d)); assertTrue(map.Put(entity, 4d)); // THEN AssertEntry(map.Pop(), entity, 2d); AssertEntry(map.Pop(), entity, 3d); AssertEntry(map.Pop(), entity, 4d); AssertEntry(map.Pop(), entity, 5d); }
private static async Task <bool> DistributeTheNiceness( Options options ) { var selected = PriorityMap.TryGetValue(options.Niceness, out var priorityClass) ? priorityClass : throw new ArgumentException( $"Unable to set priority to {options.Niceness}. Try a value from 19 (lowest) to -19"); var error = false; var ids = await options.ResolveAllProcessIds(); if (ids.Length == 0) { options.Logger.Log($"No matching process ids found"); } foreach (var id in ids) { error |= TrySetPriority(id, selected, options); } return(error); }
internal void InitializeInstanceFields() { Queue = new PriorityMap <TraversalBranch, Node, P>(Converter, outerInstance.interest.Comparator(), outerInstance.interest.StopAfterLowestCost()); }
private void MergeCarGens <TSaveData>() where TSaveData : SaveData, new() { List <TSaveData> sourceSaves = new List <TSaveData>(); List <ICarGenerator> differingCarGens = new List <ICarGenerator>(); CarGenComparer cgComparer = new CarGenComparer(); // Load priority map if (HasUserProvidedPriorityMap) { if (!TryLoadPriorityMap()) { return; } } else { GeneratePriorityMap(); } // Load target file if (!TryOpenSaveData(Options.TargetFile, out TSaveData targetSave)) { return; } // Load source files and find differing car generators foreach (string sourcePath in Options.SourceFiles) { if (!TryOpenSaveData(sourcePath, out TSaveData sourceSave)) { return; } int numDifferingThisSave = 0; for (int i = 0; i < MaxCapacity; i++) { ICarGenerator tgt = (targetSave as ISaveData).CarGenerators[i]; ICarGenerator src = (sourceSave as ISaveData).CarGenerators[i]; if (src.Model != 0 && !cgComparer.Equals(src, tgt)) { Log.InfoF($"Difference found in slot {i}."); differingCarGens.Add(src); numDifferingThisSave++; } } Log.Info($"Found {numDifferingThisSave} differing car generator{Pluralize(numDifferingThisSave)} in {Path.GetFileName(sourcePath)}."); } int numDiffering = differingCarGens.Count; // Reduce the priority map to a list of car generator indices representing the replacement order. // First, sort the map in ascending order by key (priority) and exclude negatives. Next, flatten // all values (list of car generator indices) into a single list of integers, randomizing each // sub-list along the way. List <int> replacementOrder = PriorityMap .OrderBy(pair => pair.Key).Where(pair => pair.Key > -1) .SelectMany(pair => pair.Value.OrderBy(i => RandGen.Next())) .ToList(); // Merge! ISaveData target = (targetSave as ISaveData); ICarGeneratorData targetCarGens = target.CarGenerators; int numReplaced = 0; foreach (int cgIndex in replacementOrder) { if (numReplaced >= numDiffering) { break; } ICarGenerator cg = differingCarGens[numReplaced++]; if (CheckForCollision(targetCarGens, cg, out int idx, out double dist)) { Log.Error($"Collision found: Slot = {idx}; Location = {targetCarGens[idx].Position}; Distance = {dist:0.###}"); Result = ExitCode.Error; return; } targetCarGens[cgIndex] = cg; Log.InfoV($"Wrote slot {cgIndex}: Enabled = {cg.Enabled}; Model = {cg.Model}; Location = {cg.Position}"); } Log.Info($"Merged {numReplaced} car generator{Pluralize(numReplaced)}."); UpdateCarGenMetadata(targetCarGens); if (SetTitle) { targetSave.Name = Options.Title; Log.Info($"Title set to: {targetSave.Name}"); } targetSave.TimeLastSaved = DateTime.Now; if (!TryWriteSaveData(OutputFilePath, targetSave)) { return; } Result = ExitCode.Success; }