[Test] public void Sort() { int[] testArray = Enumerable.Range(0, 10).ToArray(); RawList <int> intList = new RawList <int>(); // Sorting an empty array is a no-op, but entirely valid. No exceptions expected. intList.Sort(); // Insert the reversed data intList.AddRange(testArray.Reverse().ToArray()); CollectionAssert.AreEqual(testArray.Reverse(), intList); // Sort it and check if its equal to the original data intList.Sort(); CollectionAssert.AreEqual(testArray, intList); }
/// <summary> /// Locks all pairs which involve other characters. /// </summary> public void LockCharacterPairs() { //If this character is colliding with another character, there's a significant danger of the characters //changing the same collision pair handlers. Rather than infect every character system with micro-locks, //we lock the entirety of a character update. foreach (var pair in characterBody.CollisionInformation.Pairs) { //Is this a pair with another character? var other = pair.BroadPhaseOverlap.EntryA == characterBody.CollisionInformation ? pair.BroadPhaseOverlap.EntryB : pair.BroadPhaseOverlap.EntryA; var otherCharacter = other.Tag as ICharacterTag; if (otherCharacter != null) { involvedCharacters.Add(otherCharacter); } } if (involvedCharacters.Count > 0) { //If there were any other characters, we also need to lock ourselves! involvedCharacters.Add((ICharacterTag)characterBody.CollisionInformation.Tag); //However, the characters cannot be locked willy-nilly. There needs to be some defined order in which pairs are locked to avoid deadlocking. involvedCharacters.Sort(comparer); for (int i = 0; i < involvedCharacters.Count; ++i) { Monitor.Enter(involvedCharacters[i]); } } }
public EpisodeSelectSection() { JsonParser jsonParser = new JsonParser(); IImageCodec imageCodec = ImageCodec.GetRead(ImageCodec.FormatPng); foreach (string episode in DirectoryOp.GetDirectories(PathOp.Combine(DualityApp.DataDirectory, "Episodes"))) { string pathAbsolute = PathOp.Combine(episode, ".res"); if (FileOp.Exists(pathAbsolute)) { Episode json; using (Stream s = DualityApp.SystemBackend.FileSystem.OpenFile(pathAbsolute, FileAccessMode.Read)) { json = jsonParser.Parse <Episode>(s); } json.Token = PathOp.GetFileName(episode); if (!DirectoryOp.Exists(PathOp.Combine(episode, json.FirstLevel))) { continue; } EpisodeEntry entry; entry.Episode = json; if (json.PreviousEpisode != null) { int time = Preferences.Get <int>("EpisodeEnd_Time_" + json.PreviousEpisode); entry.IsAvailable = (time > 0); } else { entry.IsAvailable = true; } entry.CanContinue = Preferences.Get <byte[]>("EpisodeContinue_Misc_" + entry.Episode.Token) != null; string logoPath = PathOp.Combine(episode, ".png"); if (FileOp.Exists(logoPath)) { PixelData pixelData; using (Stream s = FileOp.Open(logoPath, FileAccessMode.Read)) { pixelData = imageCodec.Read(s); } Texture texture = new Texture(new Pixmap(pixelData), TextureSizeMode.NonPowerOfTwo); entry.Logo = new Material(DrawTechnique.Alpha, texture); } else { entry.Logo = null; } episodes.Add(entry); } } episodes.Sort((x, y) => x.Episode.Position.CompareTo(y.Episode.Position)); }
[Test] public void Sort() { int[] testArray = Enumerable.Range(0, 10).ToArray(); RawList <int> intList = new RawList <int>(); intList.AddRange(testArray.Reverse().ToArray()); CollectionAssert.AreEqual(testArray.Reverse(), intList); intList.Sort(); CollectionAssert.AreEqual(testArray, intList); }
protected RawList<ImportInputAssignment> SelectImporter(AssetImportEnvironment env) { if (!env.IsPrepareStep) throw new ArgumentException( "The specified import environment must be configured as a preparation environment.", "env"); // Find an importer to handle some or all of the unhandled input files RawList<ImportInputAssignment> candidateMapping = new RawList<ImportInputAssignment>(); foreach (IAssetImporter importer in AssetManager.Importers) { env.ResetAcquiredData(); try { importer.PrepareImport(env); } catch (Exception ex) { Log.Editor.WriteError("An error occurred in the preparation step of '{1}': {0}", Log.Exception(ex), Log.Type(importer.GetType())); continue; } if (env.HandledInput.Any()) { candidateMapping.Add(new ImportInputAssignment { Importer = importer, HandledInput = env.HandledInput.ToArray(), ExpectedOutput = env.Output.ToArray() }); } } // Sort candidate mapping from most files to least files, so we can solve the biggest conflicts first candidateMapping.Sort((a, b) => b.HandledInput.Length - a.HandledInput.Length); // Determine if multiple importers intend to handle the same files and resolve conflicts List<int> conflictingIndices = new List<int>(); List<string> conflictingFiles = new List<string>(); for (int mainIndex = 0; mainIndex < candidateMapping.Count; mainIndex++) { ImportInputAssignment assignment = candidateMapping[mainIndex]; // Find all conflicts related to this assignment conflictingIndices.Clear(); conflictingFiles.Clear(); for (int secondIndex = 0; secondIndex < candidateMapping.Count; secondIndex++) { if (secondIndex == mainIndex) continue; ImportInputAssignment conflictAssignment = candidateMapping[secondIndex]; IEnumerable<string> mainFiles = assignment.HandledInput.Select(item => item.Path); IEnumerable<string> secondFiles = conflictAssignment.HandledInput.Select(item => item.Path); string[] conflicts = mainFiles.Intersect(secondFiles).ToArray(); if (conflicts.Length > 0) { if (conflictingIndices.Count == 0) conflictingIndices.Add(mainIndex); conflictingIndices.Add(secondIndex); conflictingFiles.AddRange(conflicts); } } // Resolve conflicts with this assignment if (conflictingIndices.Count > 0) { // Determine which importer to prefer for this conflict ImportInputAssignment[] conflictingAssignments = conflictingIndices.Select(i => candidateMapping[i]).ToArray(); int keepIndex = this.ResolveMappingConflict(conflictingAssignments); // If we somehow decided that none of the options is viable, abort the operation if (keepIndex == -1) { candidateMapping.Clear(); return candidateMapping; } // Sort indices to remove in declining order and remove their mappings conflictingIndices.Remove(keepIndex); conflictingIndices.Sort((a, b) => b - a); foreach (int index in conflictingIndices) { candidateMapping.RemoveAt(index); } // Start over with the conflict search mainIndex = -1; continue; } } return candidateMapping; }
/// <summary> /// Sorts the involved entities according to their hashcode to allow non-batched multithreading to avoid deadlocks. /// </summary> protected internal void SortInvolvedEntities() { numberOfInvolvedEntities = involvedEntities.Count; involvedEntities.Sort(comparer); }
[Test] public void Sort() { int[] testArray = Enumerable.Range(0, 10).ToArray(); RawList<int> intList = new RawList<int>(); // Sorting an empty array is a no-op, but entirely valid. No exceptions expected. intList.Sort(); // Insert the reversed data intList.AddRange(testArray.Reverse().ToArray()); CollectionAssert.AreEqual(testArray.Reverse(), intList); // Sort it and check if its equal to the original data intList.Sort(); CollectionAssert.AreEqual(testArray, intList); }
public void Sort() { int[] testArray = Enumerable.Range(0, 10).ToArray(); RawList<int> intList = new RawList<int>(); intList.AddRange(testArray.Reverse().ToArray()); CollectionAssert.AreEqual(testArray.Reverse(), intList); intList.Sort(); CollectionAssert.AreEqual(testArray, intList); }
protected RawList <ImportInputAssignment> SelectImporter(AssetImportEnvironment env) { if (!env.IsPrepareStep) { throw new ArgumentException( "The specified import environment must be configured as a preparation environment.", "env"); } // Find an importer to handle some or all of the unhandled input files RawList <ImportInputAssignment> candidateMapping = new RawList <ImportInputAssignment>(); foreach (IAssetImporter importer in AssetManager.Importers) { env.ResetAcquiredData(); try { importer.PrepareImport(env); } catch (Exception ex) { Log.Editor.WriteError("An error occurred in the preparation step of '{1}': {0}", Log.Exception(ex), Log.Type(importer.GetType())); continue; } if (env.HandledInput.Any()) { candidateMapping.Add(new ImportInputAssignment { Importer = importer, HandledInput = env.HandledInput.ToArray(), ExpectedOutput = env.Output.ToArray() }); } } // Sort candidate mapping from most files to least files, so we can solve the biggest conflicts first candidateMapping.Sort((a, b) => b.HandledInput.Length - a.HandledInput.Length); // Determine if multiple importers intend to handle the same files and resolve conflicts List <int> conflictingIndices = new List <int>(); List <string> conflictingFiles = new List <string>(); for (int mainIndex = 0; mainIndex < candidateMapping.Count; mainIndex++) { ImportInputAssignment assignment = candidateMapping[mainIndex]; // Find all conflicts related to this assignment conflictingIndices.Clear(); conflictingFiles.Clear(); for (int secondIndex = 0; secondIndex < candidateMapping.Count; secondIndex++) { if (secondIndex == mainIndex) { continue; } ImportInputAssignment conflictAssignment = candidateMapping[secondIndex]; IEnumerable <string> mainFiles = assignment.HandledInput.Select(item => item.Path); IEnumerable <string> secondFiles = conflictAssignment.HandledInput.Select(item => item.Path); string[] conflicts = mainFiles.Intersect(secondFiles).ToArray(); if (conflicts.Length > 0) { if (conflictingIndices.Count == 0) { conflictingIndices.Add(mainIndex); } conflictingIndices.Add(secondIndex); conflictingFiles.AddRange(conflicts); } } // Resolve conflicts with this assignment if (conflictingIndices.Count > 0) { // Determine which importer to prefer for this conflict ImportInputAssignment[] conflictingAssignments = conflictingIndices.Select(i => candidateMapping[i]).ToArray(); int keepIndex = this.ResolveMappingConflict(conflictingAssignments); // If we somehow decided that none of the options is viable, abort the operation if (keepIndex == -1) { candidateMapping.Clear(); return(candidateMapping); } // Sort indices to remove in declining order and remove their mappings conflictingIndices.Remove(keepIndex); conflictingIndices.Sort((a, b) => b - a); foreach (int index in conflictingIndices) { candidateMapping.RemoveAt(index); } // Start over with the conflict search mainIndex = -1; continue; } } return(candidateMapping); }
public void Sort(int index, int count, IComparer <T> comparer) { RawList.Sort(index, count, comparer); }