Ejemplo n.º 1
0
        public void RemoveTest()
        {
            var arr = UnsafeList.Allocate <int>(10);

            for (int i = 1; i <= 10; i++)
            {
                UnsafeList.Add(arr, i);
            }

            Assert.AreEqual(10, UnsafeList.GetCount(arr));

            UnsafeList.RemoveAt(arr, 4); //Remove number 5
            Assert.AreEqual(9, UnsafeList.GetCount(arr));

            int offs = 0;

            for (int i = 1; i < 10; i++)
            {
                if (i == 5)
                {
                    offs++;         //Skip previously removed 5
                }
                var num = UnsafeList.Get <int>(arr, i - 1);
                Assert.AreEqual(i + offs, num);
            }
        }
Ejemplo n.º 2
0
        public void FreeRange(int offset, int length)
        {
            if (offset < 0)
            {
                throw new ArgumentException($"{nameof(offset)} cannot be negative", nameof(offset));
            }
            if (length < 0)
            {
                throw new ArgumentException($"{nameof(length)} cannot be negative", nameof(length));
            }
            if (length == 0)
            {
                return;
            }

            var found = FindSectionByOffset(offset, out var sectionIndex);

            if (!found)
            {
                throw new IndexOutOfRangeException("Could not find section for given offset");
            }

            if (IsSectionFree(sectionIndex))
            {
                throw new IndexOutOfRangeException("Cannot free section that's already free");
            }

            if (GetSectionLength(sectionIndex) < length)
            {
                throw new IndexOutOfRangeException("Free section at offset is not large enough");
            }

            SetRange(sectionIndex, offset, length, desiredFree: true);
            if (sections.Length > 0 &&
                IsSectionFree(sections.Length - 1))
            {
                sections.RemoveAt(sections.Length - 1);
            }
        }
Ejemplo n.º 3
0
        internal void AllocateIndexRange(int index, int range)
        {
            if ((index + range) > indexToID.Length)
            {
                indexToID.Resize((index + range), NativeArrayOptions.ClearMemory);
            }

            int idInternal, generation;

            // TODO: should make it possible to allocate ids in a range as well, for cache locality
            if (freeIDs.Length >= range)
            {
                var childIndex = index;
                while (range > 0)
                {
                    var freeID = freeIDs.Length - 1;
                    idInternal = freeIDs[freeID];
                    freeIDs.RemoveAt(freeID);

                    generation            = idToIndex[idInternal].generation + 1;
                    idToIndex[idInternal] = new IndexLookup {
                        index = childIndex, generation = generation
                    };

                    indexToID[childIndex] = idInternal + 1;

                    range--;
                    childIndex++;
                }
            }

            if (range <= 0)
            {
                return;
            }

            generation = 1;
            idInternal = idToIndex.Length;
            idToIndex.Resize((idInternal + range), NativeArrayOptions.ClearMemory);

            for (int childIndex = index, lastID = (idInternal + range); idInternal < lastID; idInternal++, childIndex++)
            {
                indexToID[childIndex] = idInternal + 1;
                idToIndex[idInternal] = new IndexLookup {
                    index = childIndex, generation = generation
                };
            }
        }
Ejemplo n.º 4
0
        internal void AddUnmanagedSystemToUpdateList(SystemHandleUntyped sysHandle)
        {
            CheckCreated();

            if (-1 != UnmanagedSystemIndex(sysHandle))
            {
                int index = m_UnmanagedSystemsToRemove.IndexOf(sysHandle);
                if (-1 != index)
                {
                    m_UnmanagedSystemsToRemove.RemoveAt(index);
                }
                return;
            }

            m_UnmanagedSystemsToUpdate.Add(sysHandle);
            m_systemSortDirty = true;
        }
Ejemplo n.º 5
0
        private void RemovePending()
        {
            if (m_systemsToRemove.Count > 0)
            {
                foreach (var sys in m_systemsToRemove)
                {
                    m_systemsToUpdate.Remove(sys);
                }

                m_systemsToRemove.Clear();
            }

            for (int i = 0; i < m_UnmanagedSystemsToRemove.Length; ++i)
            {
                var sysHandle = m_UnmanagedSystemsToRemove[i];
                m_UnmanagedSystemsToUpdate.RemoveAt(m_UnmanagedSystemsToUpdate.IndexOf(sysHandle));
            }

            m_UnmanagedSystemsToRemove.Clear();
        }
        internal void AddUnmanagedSystemToUpdateList(SystemHandleUntyped sysHandle)
        {
            CheckCreated();

            if (-1 != UnmanagedSystemIndex(sysHandle))
            {
                int index = m_UnmanagedSystemsToRemove.IndexOf(sysHandle);
                if (-1 != index)
                {
                    m_UnmanagedSystemsToRemove.RemoveAt(index);
                }
                return;
            }

            if (UseLegacySortOrder)
            {
                throw new InvalidOperationException("ISystemBase systems are not compatible with legacy sort order. Set UseLegacySortOrder to false to use ISystemBase systems.");
            }

            m_UnmanagedSystemsToUpdate.Add(sysHandle);
            m_systemSortDirty = true;
        }
Ejemplo n.º 7
0
 public void RemoteAt(int index)
 {
     UnsafeList.RemoveAt(m_inner, index);
 }
Ejemplo n.º 8
0
        internal static void Split2DPolygonAlongOriginXAxis(ref UnsafeList <SegmentVertex> polygonVerticesList, ref UnsafeList <int> polygonVerticesSegments, int defaultSegment = 0)
        {
            const float kEpsilon = CSGConstants.kFatPlaneWidthEpsilon;

            for (int r = polygonVerticesSegments.Length - 1; r >= 0; r--)
            {
                var start = r == 0 ? 0 : polygonVerticesSegments[r - 1];
                var end   = polygonVerticesSegments[r];

                var positiveSide = 0;
                var negativeSide = 0;
                for (int i = start; i < end; i++)
                {
                    var x = polygonVerticesList[i].position.x;
                    if (x < -kEpsilon)
                    {
                        negativeSide++; if (positiveSide > 0)
                        {
                            break;
                        }
                    }
                    if (x > kEpsilon)
                    {
                        positiveSide++; if (negativeSide > 0)
                        {
                            break;
                        }
                    }
                }
                if (negativeSide == 0 ||
                    positiveSide == 0)
                {
                    return;
                }

                using (var polygon = new NativeList <SegmentVertex>(Allocator.Temp))
                {
                    for (int a = end - 1, b = start; b < end; a = b, b++)
                    {
                        var point_a = polygonVerticesList[a];
                        var point_b = polygonVerticesList[b];

                        var x_a = point_a.position.x;
                        var y_a = point_a.position.y;

                        var x_b = point_b.position.x;
                        var y_b = point_b.position.y;

                        if (!(x_a <= kEpsilon && x_b <= kEpsilon) &&
                            !(x_a >= -kEpsilon && x_b >= -kEpsilon))
                        {
                            // *   .
                            //  \  .
                            //   \ .
                            //    \.
                            //     *
                            //     .\
                            //     . \
                            //     .  \
                            //     .   *

                            if (x_b < x_a)
                            {
                                var t = x_a; x_a = x_b; x_b = t; t = y_a; y_a = y_b; y_b = t;
                            }

                            var x_s = (x_a - x_b);
                            var y_s = (y_a - y_b);

                            var intersection = new float2(0, y_b - (y_s * (x_b / x_s)));
                            polygon.Add(new SegmentVertex {
                                position = intersection, segmentIndex = defaultSegment
                            });
                        }
                        polygon.Add(point_b);
                    }

                    polygonVerticesList.RemoveRange(start, (end - start));
                    polygonVerticesSegments.RemoveAt(r);
                    var delta = end - start;
                    for (; r < polygonVerticesSegments.Length; r++)
                    {
                        polygonVerticesSegments[r] -= delta;
                    }

                    const float kAxisCenter = 0.0f;

                    // positive side polygon
                    for (int i = 0; i < polygon.Length; i++)
                    {
                        var v = polygon[i];
                        var p = v.position;
                        if (p.x < -kEpsilon)
                        {
                            continue;
                        }
                        if (p.x > kEpsilon)
                        {
                            polygonVerticesList.Add(v);
                        }
                        else
                        {
                            polygonVerticesList.Add(new SegmentVertex {
                                position = new float2(kAxisCenter, p.y), segmentIndex = defaultSegment
                            });
                        }
                    }
                    polygonVerticesSegments.Add(polygonVerticesList.Length);


                    // negative side polygon (reversed)
                    for (int i = polygon.Length - 1; i >= 0; i--)
                    {
                        var v = polygon[i];
                        var p = v.position;
                        if (p.x > kEpsilon)
                        {
                            continue;
                        }
                        if (p.x < -kEpsilon)
                        {
                            polygonVerticesList.Add(v);
                        }
                        else
                        {
                            polygonVerticesList.Add(new SegmentVertex {
                                position = new float2(-kAxisCenter, p.y), segmentIndex = defaultSegment
                            });
                        }
                    }
                    polygonVerticesSegments.Add(polygonVerticesList.Length);
                }
            }
        }