public void Constructor_IEnumerable(EnumerableType enumerableType, int listLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements) { _ = listLength; _ = numberOfMatchingElements; SCG.IEnumerable <T> enumerable = CreateEnumerable(enumerableType, null, enumerableLength, 0, numberOfDuplicateElements); List <T> list = new List <T>(enumerable); List <T> expected = enumerable.ToList(); Assert.Equal(enumerableLength, list.Count); //"Number of items in list do not match the number of items given." for (int i = 0; i < enumerableLength; i++) { Assert.Equal(expected[i], list[i]); //"Expected object in item array to be the same as in the list" } Assert.False(((SCG.IList <T>)list).IsReadOnly); //"List should not be readonly" }
public static bool UnsequenceEqual <T>(this SCG.IEnumerable <T> first, SCG.IEnumerable <T> second, SCG.IEqualityComparer <T> comparer = null) { #region Code Contracts // first remains unchanged Ensures(first == null || first.IsSameSequenceAs(OldValue(first.ToList()))); // second remains unchanged Ensures(second == null || second.IsSameSequenceAs(OldValue(second.ToList()))); #endregion if (ReferenceEquals(first, second)) { return(true); } if (first == null || second == null) { return(false); } var firstArray = first.ToArray(); var secondArray = second.ToArray(); if (firstArray.Length != secondArray.Length) { return(false); } // Use default comparer if none is supplied if (comparer == null) { comparer = SCG.EqualityComparer <T> .Default; } // Sort based on hash code Comparison <T> hashCodeComparison = (x, y) => comparer.GetHashCode(x).CompareTo(comparer.GetHashCode(y)); Array.Sort(firstArray, hashCodeComparison); Array.Sort(secondArray, hashCodeComparison); for (var i = 0; i < firstArray.Length; i++) { var found = false; var firstElement = firstArray[i]; for (var j = i; j < secondArray.Length; j++) { var secondElement = secondArray[j]; if (hashCodeComparison(firstElement, secondElement) != 0) { break; } if (comparer.Equals(firstElement, secondElement)) { secondArray.Swap(i, j); // Continue with next element in first array found = true; break; } } if (!found) { return(false); } } return(true); }
public static bool ContainsRange <T>(this SCG.IEnumerable <T> first, SCG.IEnumerable <T> second, SCG.IEqualityComparer <T> equalityComparer = null) { #region Code Contracts // Argument must be non-null Requires(first != null, ArgumentMustBeNonNull); // Argument must be non-null Requires(second != null, ArgumentMustBeNonNull); // first remains unchanged Ensures(first == null || first.IsSameSequenceAs(OldValue(first.ToList()))); // second remains unchanged Ensures(second == null || second.IsSameSequenceAs(OldValue(second.ToList()))); #endregion var firstArray = first.ToArray(); var secondArray = second.ToArray(); if (firstArray.Length < secondArray.Length) { return(false); } // Use default comparer if none is supplied if (equalityComparer == null) { equalityComparer = SCG.EqualityComparer <T> .Default; } // Sort based on hash code Comparison <T> hashCodeComparison = (x, y) => equalityComparer.GetHashCode(x).CompareTo(equalityComparer.GetHashCode(y)); Array.Sort(firstArray, hashCodeComparison); Array.Sort(secondArray, hashCodeComparison); for (var j = 0; j < secondArray.Length; j++) { var found = false; var secondElement = secondArray[j]; for (var i = j; i < firstArray.Length; i++) { var firstElement = firstArray[i]; var comparison = hashCodeComparison(firstElement, secondElement); // Equal doesn't exist if (comparison > 0) { break; } if (comparison == 0 && equalityComparer.Equals(firstElement, secondElement)) { firstArray.Swap(i, j); // TODO: the hash codes are not necessarily ordered after swapping the items found = true; break; } } if (!found) { return(false); } // Invariant: all items up to and including j are equal pairwise in the two arrays Assume(ForAll(0, j + 1, i => equalityComparer.Equals(firstArray[i], secondArray[i]))); } return(true); }
private void worldLoad() { processJobAdds(); /** * Load the world tiles around the camera and their objects */ int tileX = Mathf.FloorToInt(telaraWorldCamPos.x / 256.0f); int tileY = Mathf.FloorToInt(telaraWorldCamPos.z / 256.0f); int[][] v = { new int[] { -1, 1 }, new int[] { 0, 1 }, new int[] { 1, 1 }, new int[] { -1, 0 }, new int[] { 0, 0 }, new int[] { 1, 0 }, new int[] { -1, -1 }, new int[] { 0, -1 }, new int[] { 1, -1 }, }; int range = ProgramSettings.get("TERRAIN_VIS", 10); submitCDRJob(tileX, tileY); for (int txx = tileX - range; txx <= tileX + range; txx++) { for (int txy = tileY - range; txy <= tileY + range; txy++) { submitCDRJob(txx, txy); } } processCDRQueue(); if (availThreads() > 0) { Vector3 camPos = cameraWorldCamPos; //getWorldCamPos(); float[] camPosF = new float[] { camPos.x, camPos.z }; lock (terraintree) { lock (postree) { KdTreeNode <float, SCG.List <NifLoadJob> >[] tercandidates = this.terraintree.RadialSearch(camPosF, Math.Max(256, ProgramSettings.get("TERRAIN_VIS", 10) * 256), 200); KdTreeNode <float, SCG.List <NifLoadJob> >[] candidates = this.postree.RadialSearch(camPosF, ProgramSettings.get("OBJECT_VISIBLE", 500), 200); SCG.IEnumerable <NifLoadJob> terjobs = tercandidates.SelectMany(e => e.Value); // always have a terrain job running if (terjobs.Count() > 0) { lock (camPlaneLock) { terjobs = terjobs.OrderBy(n => Vector3.Distance(n.parentPos, camPos)); } SCG.List <NifLoadJob> jobs = terjobs.ToList(); lock (terrainRunningList) { if (terrainRunningList.Count <= 1) { startJob(jobs[0], terrainRunningList, tercandidates); } } } lock (terrainRunningList) { tListCountEstimate = terrainRunningList.Count; } foreach (KdTreeNode <float, SCG.List <NifLoadJob> > n in tercandidates) { if (n.Value.Count == 0) { terraintree.RemoveAt(n.Point); } } if (availThreads() > 0) { oListCountEstimate = objectRunningList.Count; SCG.IEnumerable <NifLoadJob> otherjobs = candidates.SelectMany(e => e.Value); lock (camPlaneLock) { otherjobs = otherjobs.OrderBy(n => !TestPlanesAABB(camPlanes, n.parentPos)).ThenBy(n => Vector3.Distance(n.parentPos, camPos)); } foreach (NifLoadJob job in otherjobs) { if (availThreads() > 0) { lock (objectRunningList) { startJob(job, objectRunningList, candidates); } } else { break; } } foreach (KdTreeNode <float, SCG.List <NifLoadJob> > n in candidates) { if (n.Value.Count == 0) { postree.RemoveAt(n.Point); } } } } } } }