Пример #1
0
        public static void RemoveLast(UnsafeLinkedList *llist)
        {
            UDebug.Assert(llist != null);

            var tail = llist->_tail;

            if (tail == null)
            {
                throw new InvalidOperationException(ThrowHelper.@InvalidOperation_EmptyLinkedList);
            }

            var node = llist->_head;

            // One item in the list
            if (node->_next == null)
            {
                llist->_head = llist->_tail = null;
                Memory.Free(node);
                llist->_count--;
                return;
            }

            // Grab second-last node.
            while (node->_next->_next != null)
            {
                node = node->_next;
            }

            Memory.Free(node->_next);
            node->_next = null;

            llist->_tail = node;
            llist->_count--;
            return;
        }
Пример #2
0
 private void FillList(UnsafeLinkedList *llist)
 {
     for (int i = 0; i < 10; i++)
     {
         UnsafeLinkedList.AddLast <int>(llist, i);
     }
 }
Пример #3
0
        internal static bool RemoveSlow <T>(UnsafeLinkedList *llist, T item)
            where T : unmanaged
        {
            UDebug.Assert(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            Node *prev = null;
            Node *node = llist->_head;

            var eq = EqualityComparer <T> .Default;

            while (node != null)
            {
                if (eq.Equals(item, *GetItemFromNode <T>(node)))
                {
                    llist->DeleteNode(node, prev);
                    return(true);
                }

                // Advance to next node and remember the prev node.
                prev = node;
                node = node->_next;
            }

            return(false);
        }
Пример #4
0
        public static void CopyTo <T>(UnsafeLinkedList *llist, 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(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            int numToCopy = llist->_count;

            if (numToCopy == 0)
            {
                return;
            }

            var dest       = (T *)destination;
            var enumerator = GetEnumerator <T>(llist);

            var index = 0;

            while (enumerator.MoveNext())
            {
                dest[destinationIndex + index] = enumerator.Current;
                index++;
            }
        }
Пример #5
0
        public static bool Remove(UnsafeLinkedList *llist, ref Node *node)
        {
            UDebug.Assert(llist != null);

            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            Node *prev = null;
            Node *head = llist->_head;

            while (head != null)
            {
                if (head == node)
                {
                    llist->DeleteNode(head, prev);
                    node = null;

                    return(true);
                }

                // Advance to next node and remember the prev node.
                prev = head;
                head = head->_next;
            }

            return(false);
        }
Пример #6
0
        public static Enumerator <T> GetEnumerator <T>(UnsafeLinkedList *llist)
            where T : unmanaged
        {
            UDebug.Assert(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            return(new Enumerator <T>(llist->_head));
        }
Пример #7
0
        public static bool Contains <T>(UnsafeLinkedList *llist, T item)
            where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            return(FindNode(llist, item) != null);
        }
Пример #8
0
        public static T GetFirst <T>(UnsafeLinkedList *llist)
            where T : unmanaged
        {
            UDebug.Assert(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            var head = llist->_head;

            return(head == null ? default : *GetItemFromNode <T>(head));
        }
Пример #9
0
        public static T GetLast <T>(UnsafeLinkedList *llist)
            where T : unmanaged
        {
            UDebug.Assert(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            var tail = llist->_tail;

            return(tail == null ? default : *GetItemFromNode <T>(tail));
        }
Пример #10
0
        public static void Clear(UnsafeLinkedList *llist)
        {
            UDebug.Assert(llist != null);

            for (Node *node = llist->_head; node != null; node = node->_next)
            {
                Memory.Free(node);
            }

            llist->_head  = null;
            llist->_tail  = null;
            llist->_count = 0;
        }
Пример #11
0
        public static Node *FindNode <T>(UnsafeLinkedList *llist, T item)
            where T : unmanaged, IEquatable <T>
        {
            for (Node *node = llist->_head; node != null; node = node->_next)
            {
                if (item.Equals(*GetItemFromNode <T>(node)))
                {
                    return(node);
                }
            }

            return(null);
        }
Пример #12
0
        public static void Free(UnsafeLinkedList *llist)
        {
            if (llist == null)
            {
                return;
            }

            Clear(llist);

            // clear memory
            *llist = default;

            // free list
            Memory.Free(llist);
        }
Пример #13
0
        public static void AddLast <T>(UnsafeLinkedList *llist, T item)
            where T : unmanaged
        {
            UDebug.Assert(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            var node = CreateNode(item, llist->_nodestride);

            if (llist->_head == null)
            {
                llist->_head = llist->_tail = node;
            }
            else
            {
                // Set the tail->next to this node and make node the new tail.
                llist->_tail->_next = node;
                llist->_tail        = node;
            }

            llist->_count++;
        }
Пример #14
0
        public static void RemoveFirst(UnsafeLinkedList *llist)
        {
            UDebug.Assert(llist != null);

            var head = llist->_head;

            if (head == null)
            {
                throw new InvalidOperationException(ThrowHelper.@InvalidOperation_EmptyLinkedList);
            }

            // One item in the list.
            if (head->_next == null)
            {
                llist->_head = llist->_tail = null;
            }

            llist->_head = head->_next;
            Memory.Free(head);

            llist->_count--;
        }
Пример #15
0
        public static bool Remove <T>(UnsafeLinkedList *llist, T item)
            where T : unmanaged, IEquatable <T>
        {
            UDebug.Assert(llist != null);
            UDebug.Assert(typeof(T).TypeHandle.Value == llist->_typeHandle);

            Node *prev = null;
            Node *node = llist->_head;

            while (node != null)
            {
                if (item.Equals(*GetItemFromNode <T>(node)))
                {
                    llist->DeleteNode(node, prev);
                    return(true);
                }

                // Advance to next node and remember the prev node.
                prev = node;
                node = node->_next;
            }

            return(false);
        }
Пример #16
0
        public static void AddAfter <T>(UnsafeLinkedList *llist, Node *previousNode, T item)
            where T : unmanaged
        {
            if (previousNode == null)
            {
                throw new ArgumentNullException(nameof(previousNode));
            }

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

            var node = CreateNode(item, llist->_nodestride);

            node->_next         = previousNode->_next;
            previousNode->_next = node;

            // If the next node is null, this is the tail.
            if (node->_next == null)
            {
                llist->_tail = node;
            }

            llist->_count++;
        }
Пример #17
0
 /// <summary>
 /// Constructor for <see cref="NativeLinkedList{T}"/>.
 /// </summary>
 /// <param name="create">Dummy value that does nothing.</param>
 public NativeLinkedList(bool create)
 {
     m_inner = UnsafeLinkedList.Allocate <T>();
 }
Пример #18
0
 public static int GetCount(UnsafeLinkedList *llist)
 {
     UDebug.Assert(llist != null);
     return(llist->_count);
 }