示例#1
0
        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);
        }
示例#2
0
        public static void Push <K, V>(UnsafeHeapMin *heap, K key, V val)
            where K : unmanaged, IComparable <K>
            where V : unmanaged
        {
            if (heap->_count == heap->_items.Length)
            {
                if (heap->_items.Dynamic == 1)
                {
                    ExpandHeap(heap);
                }
                else
                {
                    throw new InvalidOperationException(HEAP_FULL);
                }
            }

            // index we're bubbling up from
            var bubbleIndex = heap->_count;

            // assign new key/val to it
            SetKeyVal(heap, bubbleIndex, key, val);

            while (bubbleIndex != 1)
            {
                var parentIndex    = bubbleIndex / 2;
                var parentIndexKey = *(K *)UnsafeBuffer.Element(heap->_items.Ptr, parentIndex, heap->_items.Stride);
                if (parentIndexKey.CompareTo(key) > 0)
                {
                    GetKeyVal(heap, parentIndex, out K parentKey, out V parentVal);
                    SetKeyVal(heap, bubbleIndex, parentKey, parentVal);
                    SetKeyVal(heap, parentIndex, key, val);

                    bubbleIndex = parentIndex;
                }
                else
                {
                    break;
                }
            }

            heap->_count = heap->_count + 1;
        }
示例#3
0
        public static void Add <T>(UnsafeList *list, T item) where T : unmanaged
        {
            Assert.Check(list != null);

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

            // fast path
            if (count < items.Length)
            {
                // set element
                *(T *)UnsafeBuffer.Element(items.Ptr, count, items.Stride) = item;

                // increment count
                list->_count = count + 1;

                return;
            }

            if (list->_items.Dynamic == false)
            {
                throw new InvalidOperationException(LIST_FULL);
            }

            // double capacity, make sure that if length is 0 then we set capacity to at least 2
            SetCapacity(list, Math.Max(2, items.Length * 2));

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

            // this has to hold now
            Assert.Check(count < items.Length);

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

            // increment count
            list->_count = count + 1;
        }
示例#4
0
        public static T Dequeue <T>(UnsafeQueue *queue) where T : unmanaged
        {
            Assert.Check(queue != null);
            Assert.Check(queue->_items.Ptr != null);

            var count = queue->_count;

            if (count == 0)
            {
                throw new InvalidOperationException(QUEUE_EMPTY);
            }

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

            // grab result
            T result = *(T *)UnsafeBuffer.Element(items.Ptr, head, items.Stride);

            // decrement count and tail index
            queue->_count = (count - 1);
            queue->_head  = (head + 1) % items.Length;
            return(result);
        }
 public static Entry *GetEntry(UnsafeHashCollection *collection, int index)
 {
     return((Entry *)UnsafeBuffer.Element(collection->Entries.Ptr, index, collection->Entries.Stride));
 }
示例#6
0
 static K Key <K>(UnsafeHeapMin *heap, int index) where K : unmanaged
 {
     return(*(K *)UnsafeBuffer.Element(heap->_items.Ptr, index, heap->_items.Stride));
 }