protected virtual int BinarySearchForNearest(int val, int begin, int end) { int mid = (begin + end) / 2; int midval = array.Get(mid); if (mid == end) { return(midval >= val ? mid : -1); } if (midval < val) { // Find number equal or greater than the target. if (array.Get(mid + 1) >= val) { return(mid + 1); } return(BinarySearchForNearest(val, mid + 1, end)); } else { // Find number equal or greater than the target. if (midval == val) { return(mid); } return(BinarySearchForNearest(val, begin, mid)); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldWorkOnSingleChunk() public virtual void ShouldWorkOnSingleChunk() { // GIVEN int defaultValue = 0; IntArray array = NumberArrayFactory_Fields.AutoWithoutPagecache.newDynamicIntArray(10, defaultValue); array.Set(4, 5); // WHEN assertEquals(5L, array.Get(4)); assertEquals(defaultValue, array.Get(12)); array.Set(7, 1324); assertEquals(1324L, array.Get(7)); }
public new bool Equals(object obj) { if (this == obj) { return(true); } if (!(obj is IntArray)) { return(false); } IntArray array = (IntArray)obj; int size = GetSize(); if (array.GetSize() != size) { return(false); } for (int i = 0; i < size; i++) { if (Get(i) != array.Get(i)) { return(false); } } return(true); }
public void Remove(IntArray indices) { int size = indices.GetSize(); for (int i = 0; i < size; i++) { Remove(indices.Get(i)); } }
public void Add(IntArray indices) { int size = indices.GetSize(); for (int i = 0; i < size; i++) { Add(indices.Get(i)); } }
/// <summary> /// Appends all the elements in the specified array at the /// end of this array. /// </summary> /// <param name="array"></param> public void Add(IntArray array) { int arraySize = array.GetSize(); EnsureCapacity(size + arraySize); for (int i = 0; i < arraySize; i++) { data[size++] = array.Get(i); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldChunksAsNeeded() public virtual void ShouldChunksAsNeeded() { // GIVEN IntArray array = NumberArrayFactory_Fields.AutoWithoutPagecache.newDynamicIntArray(10, 0); // WHEN long index = 243; int value = 5485748; array.Set(index, value); // THEN assertEquals(value, array.Get(index)); }
///<summary>Binary search</summary> ///<param name="val"> </param> ///<param name="begin"> </param> ///<param name="end"> </param> ///<returns> index greater than or equal to the target. -1 if the target is out of range. </returns> protected internal virtual int BinarySearchForNearest(int val, int begin, int end) { int mid = (begin + end) / 2; if (mid == end || (baseList.Get(mid) <= val && baseList.Get(mid + 1) > val)) { return(mid); } else if (baseList.Get(mid) < val) { return(BinarySearchForNearest(val, mid + 1, end)); } else { return(BinarySearchForNearest(val, begin, mid)); } }
/// <summary> /// Constructs an IntSet using the specified array. /// <para> /// NOTE: The array should not have any negative elements. /// </para> /// </summary> /// <param name="array"></param> /// <returns></returns> public static IntSet CreateIntSet(IntArray array) { if (array is IntSet) { return((IntSet)array); } int size = array.GetSize(); BitSetIntSet bitset = new BitSetIntSet(size); for (int i = 0; i < size; i++) { bitset.Set(array.Get(i)); } return(bitset); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldPickFirstAvailableCandidateIntArray() public virtual void ShouldPickFirstAvailableCandidateIntArray() { // GIVEN FailureMonitor monitor = new FailureMonitor(); NumberArrayFactory factory = new NumberArrayFactory_Auto(monitor, NumberArrayFactory.HEAP); // WHEN IntArray array = factory.NewIntArray(KILO, -1); array.Set(KILO - 10, 12345); // THEN assertTrue(array is HeapIntArray); assertEquals(12345, array.Get(KILO - 10)); assertEquals(NumberArrayFactory.HEAP, monitor.SuccessfulFactory); assertFalse(monitor.AttemptedAllocationFailures.GetEnumerator().hasNext()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldPickFirstAvailableCandidateIntArrayWhenSomeThrowNativeMemoryAllocationRefusedError() public virtual void ShouldPickFirstAvailableCandidateIntArrayWhenSomeThrowNativeMemoryAllocationRefusedError() { // GIVEN NumberArrayFactory lowMemoryFactory = mock(typeof(NumberArrayFactory)); doThrow(typeof(NativeMemoryAllocationRefusedError)).when(lowMemoryFactory).newIntArray(anyLong(), anyInt(), anyLong()); NumberArrayFactory factory = new NumberArrayFactory_Auto(NO_MONITOR, lowMemoryFactory, NumberArrayFactory.HEAP); // WHEN IntArray array = factory.NewIntArray(KILO, -1); array.Set(KILO - 10, 12345); // THEN verify(lowMemoryFactory, times(1)).newIntArray(KILO, -1, 0); assertTrue(array is HeapIntArray); assertEquals(12345, array.Get(KILO - 10)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @TestFactory Stream<org.junit.jupiter.api.DynamicTest> shouldHandleSomeRandomSetAndGet() internal virtual Stream <DynamicTest> ShouldHandleSomeRandomSetAndGet() { // GIVEN ThrowingConsumer <NumberArrayFactory> arrayFactoryConsumer = factory => { int length = _random.Next(100_000) + 100; int defaultValue = _random.Next(2) - 1; // 0 or -1 using (IntArray array = factory.newIntArray(length, defaultValue)) { int[] expected = new int[length]; Arrays.Fill(expected, defaultValue); // WHEN int operations = _random.Next(1_000) + 10; for (int i = 0; i < operations; i++) { // THEN int index = _random.Next(length); int value = _random.Next(); switch (_random.Next(3)) { case 0: // set array.Set(index, value); expected[index] = value; break; case 1: // get assertEquals(expected[index], array.Get(index), "Seed:" + _seed); break; default: // swap int toIndex = _random.Next(length); array.Swap(index, toIndex); Swap(expected, index, toIndex); break; } } } }; return(stream(ArrayFactories(), NumberArrayFactoryName, arrayFactoryConsumer)); }
public int Next() { return(array.Get(index++)); }
/// <summary> /// Create a convex hull from the given array of points. /// The count must be in the range [3, Settings.maxPolygonVertices]. /// This method takes an arraypool for pooling /// </summary> /// <warning>the points may be re-ordered, even if they form a convex polygon</warning> /// <warning>collinear points are handled but not removed. Collinear points may lead to poor stacking behavior.</warning> public void Set(Vec2[] verts, int num, Vec2Array vecPool, IntArray intPool) { Debug.Assert(3 <= num && num <= Settings.MAX_POLYGON_VERTICES); if (num < 3) { SetAsBox(1.0f, 1.0f); return; } int n = MathUtils.Min(num, Settings.MAX_POLYGON_VERTICES); // Copy the vertices into a local buffer Vec2[] ps = (vecPool != null) ? vecPool.Get(n) : new Vec2[n]; for (int i = 0; i < n; ++i) { ps[i] = verts[i]; } // Create the convex hull using the Gift wrapping algorithm // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm // Find the right most point on the hull int i0 = 0; float x0 = ps[0].X; for (int i = 1; i < num; ++i) { float x = ps[i].X; if (x > x0 || (x == x0 && ps[i].Y < ps[i0].Y)) { i0 = i; x0 = x; } } int[] hull = (intPool != null) ? intPool.Get(Settings.MAX_POLYGON_VERTICES) : new int[Settings.MAX_POLYGON_VERTICES]; int m = 0; int ih = i0; while (true) { hull[m] = ih; int ie = 0; for (int j = 1; j < n; ++j) { if (ie == ih) { ie = j; continue; } Vec2 r = pool1.Set(ps[ie]).SubLocal(ps[hull[m]]); Vec2 v = pool2.Set(ps[j]).SubLocal(ps[hull[m]]); float c = Vec2.Cross(r, v); if (c < 0.0f) { ie = j; } // Collinearity check if (c == 0.0f && v.LengthSquared() > r.LengthSquared()) { ie = j; } } ++m; ih = ie; if (ie == i0) { break; } } VertexCount = m; // Copy vertices. for (int i = 0; i < VertexCount; ++i) { if (Vertices[i] == null) { Vertices[i] = new Vec2(); } Vertices[i].Set(ps[hull[i]]); } Vec2 edge = pool1; // Compute normals. Ensure the edges have non-zero length. for (int i = 0; i < VertexCount; ++i) { int i1 = i; int i2 = i + 1 < VertexCount ? i + 1 : 0; edge.Set(Vertices[i2]).SubLocal(Vertices[i1]); Debug.Assert(edge.LengthSquared() > Settings.EPSILON * Settings.EPSILON); Vec2.CrossToOutUnsafe(edge, 1f, Normals[i]); Normals[i].Normalize(); } // Compute the polygon centroid. ComputeCentroidToOut(Vertices, VertexCount, Centroid); }