public static void CopyTo <T>(UnsafeStack *stack, void *destination, int destinationIndex) where T : unmanaged
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ThrowHelper.ArgumentOutOfRange_Index);
            }

            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == stack->_typeHandle);

            int numToCopy = stack->_count;

            if (numToCopy == 0)
            {
                return;
            }

            int srcIndex = 0;
            int stride   = stack->_items.Stride;
            int dstIndex = destinationIndex + numToCopy;

            while (srcIndex < numToCopy)
            {
                *(T *)((byte *)destination + (--dstIndex * stride)) = *stack->_items.Element <T>(srcIndex++);
            }
        }
        public static int GetCapacity(UnsafeStack *stack)
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);

            return(stack->_items.Length);
        }
        public static void Clear(UnsafeStack *stack)
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);

            stack->_count = 0;
        }
        public static void Push <T>(UnsafeStack *stack, T item) where T : unmanaged
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == stack->_typeHandle);

            var items = stack->_items;
            int count = stack->_count;

            if ((uint)count < (uint)items.Length)
            {
                *items.Element <T>(count) = item;
                stack->_count = count + 1;
            }
            else
            {
                if (items.Dynamic == 1)
                {
                    ResizeAndPush(stack, item);
                }
                else
                {
                    throw new InvalidOperationException(ThrowHelper.InvalidOperation_CollectionFull);
                }
            }
        }
        public static int GetCount(UnsafeStack *stack)
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);

            return(stack->_count);
        }
        public static bool TryPush <T>(UnsafeStack *stack, T item) where T : unmanaged
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == stack->_typeHandle);

            var items = stack->_items;
            int count = stack->_count;

            if ((uint)count < (uint)items.Length)
            {
                *items.Element <T>(count) = item;
                stack->_count = count + 1;

                return(true);
            }
            else
            {
                if (items.Dynamic == 1)
                {
                    ResizeAndPush(stack, item);
                    return(true);
                }
                return(false);
            }
        }
예제 #7
0
        public static void Push <T>(UnsafeStack *stack, T item) where T : unmanaged
        {
            Assert.Check(stack != null);

            var items = stack->_items;
            var count = stack->_count;

            if (count >= items.Length)
            {
                if (items.Dynamic)
                {
                    Expand(stack);

                    // re-assign items after expand
                    items = stack->_items;

                    // this has to hold now or something went wrong
                    Assert.Check(count < items.Length);
                }
                else
                {
                    throw new InvalidOperationException(STACK_FULL);
                }
            }

            // write element
            *(T *)UnsafeBuffer.Element(items.Ptr, count, items.Stride) = item;

            // increment size
            stack->_count = count + 1;
        }
        private static void ResizeAndPush <T>(UnsafeStack *stack, T item) where T : unmanaged
        {
            Expand(stack);

            *stack->_items.Element <T>(stack->_count) = item;
            stack->_count++;
        }
        public static Enumerator <T> GetEnumerator <T>(UnsafeStack *stack) where T : unmanaged
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == stack->_typeHandle);

            return(new Enumerator <T>(stack->_items, stack->_count));
        }
예제 #10
0
        public static bool TryPeek <T>(UnsafeStack *stack, out T item) where T : unmanaged
        {
            var ptr = Peek(stack);

            if (ptr == null)
            {
                item = default;
                return(false);
            }

            item = *(T *)ptr;
            return(true);
        }
예제 #11
0
        public static T Peek <T>(UnsafeStack *stack) where T : unmanaged
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == stack->_typeHandle);

            var count = stack->_count - 1;

            if ((uint)count >= (uint)stack->_items.Length)
            {
                throw new InvalidOperationException(ThrowHelper.InvalidOperation_EmptyStack);
            }

            return(*stack->_items.Element <T>(count));
        }
예제 #12
0
        public static bool Contains <T>(UnsafeStack *stack, T item) where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == stack->_typeHandle);

            int count = stack->_count;

            if (count == 0)
            {
                return(false);
            }

            return(UnsafeBuffer.LastIndexOf(stack->_items, item, count - 1, count) != -1);
        }
예제 #13
0
        static void *Peek(UnsafeStack *stack)
        {
            Assert.Check(stack != null);

            var count = stack->_count;

            if (count == 0)
            {
                return(null);
            }

            var items = stack->_items;

            return(UnsafeBuffer.Element(items.Ptr, count - 1, items.Stride));
        }
예제 #14
0
        public static void Free(UnsafeStack *stack)
        {
            Assert.Check(stack != null);

            // if this is a dynamic sized stack, we need to free the buffer by hand
            if (stack->_items.Dynamic)
            {
                UnsafeBuffer.Free(&stack->_items);
            }

            // clear stack memory just in case
            *stack = default;

            // free stack memory (if this is a fixed size stack, it frees the _items memory also)
            AllocHelper.Free(stack);
        }
예제 #15
0
        static void Expand(UnsafeStack *stack)
        {
            // new buffer for elements
            UnsafeBuffer newItems = default;

            // initialize to double size of existing one
            UnsafeBuffer.InitDynamic(&newItems, stack->_items.Length * 2, stack->_items.Stride);

            // copy memory over from previous items
            UnsafeBuffer.Copy(stack->_items, 0, newItems, 0, stack->_items.Length);

            // free old buffer
            UnsafeBuffer.Free(&stack->_items);

            // replace buffer with new
            stack->_items = newItems;
        }
예제 #16
0
        public static bool TryPeek <T>(UnsafeStack *stack, out T item) where T : unmanaged
        {
            UDebug.Assert(stack != null);
            UDebug.Assert(stack->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == stack->_typeHandle);

            var count = stack->_count - 1;

            if ((uint)count >= (uint)stack->_items.Length)
            {
                item = default;
                return(false);
            }

            item = *stack->_items.Element <T>(count);
            return(true);
        }
예제 #17
0
        public static bool TryPop <T>(UnsafeStack *stack, out T item) where T : unmanaged
        {
            Assert.Check(stack != null);

            var ptr = Peek(stack);

            if (ptr == null)
            {
                item = default;
                return(false);
            }

            // reduce count
            stack->_count = stack->_count - 1;

            // grab out item
            item = *(T *)ptr;
            return(true);
        }
예제 #18
0
        public static void Free(UnsafeStack *stack)
        {
            if (stack == null)
            {
                return;
            }

            // if this is a dynamic sized stack, we need to free the buffer by hand
            if (stack->_items.Dynamic == 1)
            {
                UnsafeBuffer.Free(&stack->_items);
            }

            // clear stack memory just in case
            *stack = default;

            // free stack memory (if this is a fixed size stack, it frees the _items memory also)
            Memory.Free(stack);
        }
예제 #19
0
 public static int Capacity(UnsafeStack *stack)
 {
     Assert.Check(stack != null);
     Assert.Check(stack->_items.Ptr != null);
     return(stack->_items.Length);
 }
예제 #20
0
 public static bool IsFixedSize(UnsafeStack *stack)
 {
     Assert.Check(stack != null);
     return(stack->_items.Dynamic == false);
 }
예제 #21
0
 public static void Clear(UnsafeStack *stack)
 {
     Assert.Check(stack != null);
     Assert.Check(stack->_items.Ptr != null);
     stack->_count = 0;
 }
예제 #22
0
 public static UnsafeList.Iterator <T> GetIterator <T>(UnsafeStack *stack) where T : unmanaged
 {
     return(new UnsafeList.Iterator <T>(stack->_items, 0, stack->_count));
 }
예제 #23
0
 public NativeStack(int capacity)
 {
     m_inner = UnsafeStack.Allocate <T>(capacity);
 }
예제 #24
0
 public NativeStack(int capacity, bool fixedSize)
 {
     m_inner = UnsafeStack.Allocate <T>(capacity, fixedSize);
 }
예제 #25
0
 public static int Count(UnsafeStack *stack)
 {
     Assert.Check(stack != null);
     Assert.Check(stack->_items.Ptr != null);
     return(stack->_count);
 }
예제 #26
0
        public static bool IsFixedSize(UnsafeStack *stack)
        {
            UDebug.Assert(stack != null);

            return(stack->_items.Dynamic == 0);
        }