public static bool Push <T>(UnsafeRingBuffer *ring, T item) where T : unmanaged
        {
            if (ring->_count == ring->_items.Length)
            {
                if (ring->_overwrite)
                {
                    ring->_tail  = (ring->_tail + 1) % ring->_items.Length;
                    ring->_count = (ring->_count - 1);
                }
                else
                {
                    return(false);
                }
            }

            // store value at head
            *(T *)UnsafeBuffer.Element(ring->_items.Ptr, ring->_head, ring->_items.Stride) = item;

            // move head pointer forward
            ring->_head = (ring->_head + 1) % ring->_items.Length;

            // add count
            ring->_count += 1;

            // success!
            return(true);
        }
Beispiel #2
0
        public static bool Pop <T>(UnsafeRingBuffer *ring, out T value) where T : unmanaged
        {
            UDebug.Assert(ring != null);
            UDebug.Assert(ring->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == ring->_typeHandle);

            var count = ring->_count;

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

            var head  = ring->_head;
            var items = ring->_items;

            // grab result
            value = *items.Element <T>(head);

            // decrement count and head index
            ring->_count--;
            MoveNext(items.Length, ref ring->_head);

            return(true);
        }
Beispiel #3
0
        public static bool Push <T>(UnsafeRingBuffer *ring, T item) where T : unmanaged
        {
            UDebug.Assert(ring != null);
            UDebug.Assert(ring->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == ring->_typeHandle);

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

            if (count == items.Length)
            {
                if (ring->_overwrite == 1)
                {
                    MoveNext(items.Length, ref ring->_head);
                    ring->_count--;
                }
                else
                {
                    return(false);
                }
            }

            var tail = ring->_tail;

            *items.Element <T>(tail) = item;

            ring->_count++;
            MoveNext(items.Length, ref ring->_tail);

            return(true);
        }
Beispiel #4
0
        private static void SplitRingBuffer(UnsafeRingBuffer *q)
        {
            //Wrap tail back to 0
            for (int i = 0; i < 5; i++)
            {
                UnsafeRingBuffer.Push(q, 111);
            }

            //First half
            for (int i = 0; i < 5; i++)
            {
                UnsafeRingBuffer.Push(q, i);
            }

            //Move head by 5
            for (int i = 0; i < 5; i++)
            {
                UnsafeRingBuffer.Pop <int>(q, out _);
            }

            //Second half (head and tail are now both 5)
            for (int i = 5; i < 10; i++)
            {
                UnsafeRingBuffer.Push(q, i);
            }

            //Circular buffer now "ends" in the middle of the underlying array
        }
Beispiel #5
0
        public static UnsafeList.Enumerator <T> GetEnumerator <T>(UnsafeRingBuffer *ringbuffer) where T : unmanaged
        {
            UDebug.Assert(ringbuffer != null);
            UDebug.Assert(ringbuffer->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == ringbuffer->_typeHandle);

            return(new UnsafeList.Enumerator <T>(ringbuffer->_items, ringbuffer->_head, ringbuffer->_count));
        }
Beispiel #6
0
        public static void Clear(UnsafeRingBuffer *ring)
        {
            UDebug.Assert(ring != null);
            UDebug.Assert(ring->_items.Ptr != null);

            ring->_tail  = 0;
            ring->_head  = 0;
            ring->_count = 0;
        }
        public static void Clear(UnsafeRingBuffer *ring)
        {
            Assert.Check(ring != null);
            Assert.Check(ring->_items.Ptr != null);

            ring->_tail  = 0;
            ring->_head  = 0;
            ring->_count = 0;
        }
        public static void Free(UnsafeRingBuffer *ring)
        {
            Assert.Check(ring != null);

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

            // release ring memory
            AllocHelper.Free(ring);
        }
        public static T *GetPtr <T>(UnsafeRingBuffer *ring, int index) where T : unmanaged
        {
            // cast to uint trick, which eliminates < 0 check
            if ((uint)index >= (uint)ring->_count)
            {
                throw new IndexOutOfRangeException();
            }

            return((T *)UnsafeBuffer.Element(ring->_items.Ptr, (ring->_tail + index) % ring->_items.Length, ring->_items.Stride));
        }
        public static void Set <T>(UnsafeRingBuffer *ring, int index, T value) where T : unmanaged
        {
            // cast to uint trick, which eliminates < 0 check
            if ((uint)index >= (uint)ring->_count)
            {
                throw new IndexOutOfRangeException();
            }

            // assign element
            *(T *)UnsafeBuffer.Element(ring->_items.Ptr, (ring->_tail + index) % ring->_items.Length, ring->_items.Stride) = value;
        }
Beispiel #11
0
        public static T *GetPtr <T>(UnsafeRingBuffer *ring, int index) where T : unmanaged
        {
            UDebug.Assert(ring != null);
            UDebug.Assert(ring->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == ring->_typeHandle);

            // cast to uint trick, which eliminates < 0 check
            if ((uint)index >= (uint)ring->_count)
            {
                throw new IndexOutOfRangeException(ThrowHelper.ArgumentOutOfRange_Index);
            }
            return(ring->_items.Element <T>((ring->_tail + index) % ring->_items.Length));
        }
Beispiel #12
0
        public static void Free(UnsafeRingBuffer *ring)
        {
            if (ring == null)
            {
                return;
            }

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

            // release ring memory
            Memory.Free(ring);
        }
Beispiel #13
0
        public static bool Peek <T>(UnsafeRingBuffer *ring, out T value) where T : unmanaged
        {
            UDebug.Assert(ring != null);
            UDebug.Assert(ring->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == ring->_typeHandle);

            if (ring->_count == 0)
            {
                value = default;
                return(false);
            }

            value = *ring->_items.Element <T>(ring->_head);
            return(true);
        }
        public static bool Pop <T>(UnsafeRingBuffer *ring, out T value) where T : unmanaged
        {
            Assert.Check(ring != null);
            Assert.Check(ring->_items.Ptr != null);

            if (ring->_count == 0)
            {
                value = default;
                return(false);
            }

            // copy item from tail
            value = *(T *)UnsafeBuffer.Element(ring->_items.Ptr, ring->_tail, ring->_items.Stride);

            // move tail forward and decrement count
            ring->_tail  = (ring->_tail + 1) % ring->_items.Length;
            ring->_count = (ring->_count - 1);
            return(true);
        }
Beispiel #15
0
        public static bool Contains <T>(UnsafeRingBuffer *ringbuffer, T item) where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(ringbuffer != null);
            UDebug.Assert(ringbuffer->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == ringbuffer->_typeHandle);

            int count = ringbuffer->_count;
            int head  = ringbuffer->_head;
            int tail  = ringbuffer->_tail;

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

            if (head < tail)
            {
                return(UnsafeBuffer.IndexOf(ringbuffer->_items, item, head, count) > -1);
            }

            return(UnsafeBuffer.IndexOf(ringbuffer->_items, item, head, ringbuffer->_items.Length - head) > -1 ||
                   UnsafeBuffer.IndexOf(ringbuffer->_items, item, 0, tail) > -1);
        }
Beispiel #16
0
        public static void CopyTo <T>(UnsafeRingBuffer *ringbuffer, 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(ringbuffer != null);
            UDebug.Assert(ringbuffer->_items.Ptr != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == ringbuffer->_typeHandle);


            int numToCopy = ringbuffer->_count;

            if (numToCopy == 0)
            {
                return;
            }

            int bufferLength = ringbuffer->_items.Length;
            int head         = ringbuffer->_head;

            int firstPart = Math.Min(bufferLength - head, numToCopy);

            UnsafeBuffer.CopyTo <T>(ringbuffer->_items, head, destination, destinationIndex, firstPart);
            numToCopy -= firstPart;
            if (numToCopy > 0)
            {
                UnsafeBuffer.CopyTo <T>(ringbuffer->_items, 0, destination, destinationIndex + bufferLength - head, numToCopy);
            }
        }
Beispiel #17
0
 public static T Get <T>(UnsafeRingBuffer *ring, int index) where T : unmanaged
 {
     return(*GetPtr <T>(ring, index));
 }
 public static int Capacity(UnsafeRingBuffer *ring)
 {
     Assert.Check(ring != null);
     Assert.Check(ring->_items.Ptr != null);
     return(ring->_items.Length);
 }
Beispiel #19
0
 public static ref T GetRef <T>(UnsafeRingBuffer *ring, int index) where T : unmanaged
 {
     return(ref *GetPtr <T>(ring, index));
 }
 public static int Count(UnsafeRingBuffer *ring)
 {
     Assert.Check(ring != null);
     Assert.Check(ring->_items.Ptr != null);
     return(ring->_count);
 }
 public static bool IsFull(UnsafeRingBuffer *ring)
 {
     Assert.Check(ring != null);
     Assert.Check(ring->_items.Ptr != null);
     return(ring->_count == ring->_items.Length);
 }
 public NativeRingBuffer(int capacity)
 {
     m_inner = UnsafeRingBuffer.Allocate <T>(capacity);
 }
Beispiel #23
0
 public static int GetCount(UnsafeRingBuffer *ring)
 {
     UDebug.Assert(ring != null);
     UDebug.Assert(ring->_items.Ptr != null);
     return(ring->_count);
 }
Beispiel #24
0
 public static int GetCapacity(UnsafeRingBuffer *ring)
 {
     UDebug.Assert(ring != null);
     UDebug.Assert(ring->_items.Ptr != null);
     return(ring->_items.Length);
 }
 public static UnsafeList.Iterator <T> GetIterator <T>(UnsafeRingBuffer *buffer) where T : unmanaged
 {
     return(new UnsafeList.Iterator <T>(buffer->_items, buffer->_tail, buffer->_count));
 }
 public NativeRingBuffer(int capacity, bool overwrite)
 {
     m_inner = UnsafeRingBuffer.Allocate <T>(capacity, overwrite);
 }
Beispiel #27
0
 public static bool IsFull(UnsafeRingBuffer *ring)
 {
     UDebug.Assert(ring != null);
     UDebug.Assert(ring->_items.Ptr != null);
     return(ring->_count == ring->_items.Length);
 }