public virtual void OnDeserialization(Object sender)
        {
            if (siInfo == null)
            {
                return; //Somebody had a dependency on this Dictionary and fixed us up before the ObjectManager got to it.
            }

            int realVersion = siInfo.GetInt32(VersionName);
            int count       = siInfo.GetInt32(CountName);

            if (count != 0)
            {
                T[] array = (T[])siInfo.GetValue(ValuesName, typeof(T[]));

                if (array == null)
                {
                    throw new SerializationException("SR.Serialization_MissingValues");
                }
                for (int i = 0; i < array.Length; i++)
                {
                    AddLast(array[i]);
                }
            }
            else
            {
                head = null;
            }

            version = realVersion;
            siInfo  = null;
        }
 public void AddAfter(SuperLinkedListNode <T> node, SuperLinkedListNode <T> newNode)
 {
     ValidateNode(node);
     ValidateNewNode(newNode);
     InternalInsertNodeBefore(node.next, newNode);
     newNode.list = this;
 }
        public SuperLinkedListNode <T> AddAfter(SuperLinkedListNode <T> node, SuperLinkedList <T> newLinkedList)
        {
            if (newLinkedList.Count <= 0)
            {
                return(node);
            }

            SuperLinkedListNode <T> cur = node;

            foreach (T t in newLinkedList)
            {
                cur = this.AddAfter(cur, t);
            }

            return(cur);

            //SuperLinkedListNode<T> nodeNext = node.Next;
            //SuperLinkedListNode<T> newLast = newLinkedList.Last;

            //node.next = newLinkedList.First;
            //newLinkedList.First.prev = node;

            //if (nodeNext != null)
            //{
            //    newLast.next = nodeNext;
            //    nodeNext.prev = newLast;
            //}
            //else
            //{
            //    newLast.next = head;
            //    head.prev = newLast;
            //}

            //count += newLinkedList.Count;
        }
 public SuperLinkedListNode<T> AddAfter(SuperLinkedListNode<T> node, T value)
 {
     ValidateNode(node);
     SuperLinkedListNode<T> result = new SuperLinkedListNode<T>(node.list, value);
     InternalInsertNodeBefore(node.next, result);
     return result;
 }
        public SuperLinkedListNode <T> Find(T value)
        {
            SuperLinkedListNode <T> node = head;
            EqualityComparer <T>    c    = EqualityComparer <T> .Default;

            if (node != null)
            {
                if (value != null)
                {
                    do
                    {
                        if (c.Equals(node.item, value))
                        {
                            return(node);
                        }
                        node = node.next;
                    } while (node != head);
                }
                else
                {
                    do
                    {
                        if (node.item == null)
                        {
                            return(node);
                        }
                        node = node.next;
                    } while (node != head);
                }
            }
            return(null);
        }
        public SuperLinkedListNode<T> FindLast(T value)
        {
            if (head == null) return null;

            SuperLinkedListNode<T> last = head.prev;
            SuperLinkedListNode<T> node = last;
            EqualityComparer<T> c = EqualityComparer<T>.Default;
            if (node != null)
            {
                if (value != null)
                {
                    do
                    {
                        if (c.Equals(node.item, value))
                        {
                            return node;
                        }

                        node = node.prev;
                    } while (node != last);
                }
                else
                {
                    do
                    {
                        if (node.item == null)
                        {
                            return node;
                        }
                        node = node.prev;
                    } while (node != last);
                }
            }
            return null;
        }
        public void CopyTo(T[] array, int index)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            if (index < 0 || index > array.Length)
            {
                throw new ArgumentOutOfRangeException("index", index.ToString());
            }

            if (array.Length - index < Count)
            {
                throw new ArgumentException("SR.Arg_InsufficientSpace");
            }

            SuperLinkedListNode <T> node = head;

            if (node != null)
            {
                do
                {
                    array[index++] = node.item;
                    node           = node.next;
                } while (node != head);
            }
        }
 private void InternalInsertNodeBefore(SuperLinkedListNode <T> node, SuperLinkedListNode <T> newNode)
 {
     newNode.next   = node;
     newNode.prev   = node.prev;
     node.prev.next = newNode;
     node.prev      = newNode;
     version++;
     count++;
 }
Example #9
0
 internal Enumerator(StreamingContext context)
 {
     //siInfo = info;
     list    = null;
     version = 0;
     node    = null;
     current = default(T);
     index   = 0;
 }
 internal Enumerator(SuperLinkedList <T> list)
 {
     this.list = list;
     version   = list.version;
     node      = list.head;
     current   = default(T);
     index     = 0;
     siInfo    = null;
 }
 internal Enumerator(SerializationInfo info, StreamingContext context)
 {
     siInfo  = info;
     list    = null;
     version = 0;
     node    = null;
     current = default(T);
     index   = 0;
 }
 private void InternalInsertNodeToEmptyList(SuperLinkedListNode <T> newNode)
 {
     Debug.Assert(head == null && count == 0, "SuperLinkedList must be empty when this method is called!");
     newNode.next = newNode;
     newNode.prev = newNode;
     head         = newNode;
     version++;
     count++;
 }
 public bool Remove(T value)
 {
     SuperLinkedListNode<T> node = Find(value);
     if (node != null)
     {
         InternalRemoveNode(node);
         return true;
     }
     return false;
 }
 public SuperLinkedListNode<T> AddBefore(SuperLinkedListNode<T> node, T value)
 {
     ValidateNode(node);
     SuperLinkedListNode<T> result = new SuperLinkedListNode<T>(node.list, value);
     InternalInsertNodeBefore(node, result);
     if (node == head)
     {
         head = result;
     }
     return result;
 }
            void System.Collections.IEnumerator.Reset()
            {
                if (version != list.version)
                {
                    throw new InvalidOperationException("SR.InvalidOperation_EnumFailedVersion");
                }

                current = default(T);
                node    = list.head;
                index   = 0;
            }
 public void AddBefore(SuperLinkedListNode <T> node, SuperLinkedListNode <T> newNode)
 {
     ValidateNode(node);
     ValidateNewNode(newNode);
     InternalInsertNodeBefore(node, newNode);
     newNode.list = this;
     if (node == head)
     {
         head = newNode;
     }
 }
        internal void ValidateNewNode(SuperLinkedListNode <T> node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.list != null)
            {
                throw new InvalidOperationException("SR.LinkedListNodeIsAttached");
            }
        }
        internal void ValidateNode(SuperLinkedListNode <T> node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.list != this)
            {
                throw new InvalidOperationException("SR.ExternalLinkedListNode");
            }
        }
 public SuperLinkedListNode<T> AddLast(T value)
 {
     SuperLinkedListNode<T> result = new SuperLinkedListNode<T>(this, value);
     if (head == null)
     {
         InternalInsertNodeToEmptyList(result);
     }
     else
     {
         InternalInsertNodeBefore(head, result);
     }
     return result;
 }
        public void AddLast(SuperLinkedListNode <T> node)
        {
            ValidateNewNode(node);

            if (head == null)
            {
                InternalInsertNodeToEmptyList(node);
            }
            else
            {
                InternalInsertNodeBefore(head, node);
            }
            node.list = this;
        }
        public void Clear()
        {
            SuperLinkedListNode<T> current = head;
            while (current != null)
            {
                SuperLinkedListNode<T> temp = current;
                current = current.Next;   // use Next the instead of "next", otherwise it will loop forever 
                temp.Invalidate();
            }

            head = null;
            count = 0;
            version++;
        }
            void IDeserializationCallback.OnDeserialization(Object sender)
            {
                if (list != null)
                {
                    return; //Somebody had a dependency on this Dictionary and fixed us up before the ObjectManager got to it.
                }

                if (siInfo == null)
                {
                    throw new SerializationException("SR.Serialization_InvalidOnDeser");
                }

                list    = (SuperLinkedList <T>)siInfo.GetValue(LinkedListName, typeof(SuperLinkedList <T>));
                version = siInfo.GetInt32(VersionName);
                current = (T)siInfo.GetValue(CurrentValueName, typeof(T));
                index   = siInfo.GetInt32(IndexName);

                if (list.siInfo != null)
                {
                    list.OnDeserialization(sender);
                }

                if (index == list.Count + 1)
                {  // end of enumeration
                    node = null;
                }
                else
                {
                    node = list.First;
                    // We don't care if we can point to the correct node if the LinkedList was changed
                    // MoveNext will throw upon next call and Current has the correct value.
                    if (node != null && index != 0)
                    {
                        for (int i = 0; i < index; i++)
                        {
                            node = node.next;
                        }
                        if (node == list.First)
                        {
                            node = null;
                        }
                    }
                }
                siInfo = null;
            }
            public bool MoveNext()
            {
                if (version != list.version)
                {
                    throw new InvalidOperationException("SR.InvalidOperation_EnumFailedVersion");
                }

                if (node == null)
                {
                    index = list.Count + 1;
                    return(false);
                }

                ++index;
                current = node.item;
                node    = node.next;
                if (node == list.head)
                {
                    node = null;
                }
                return(true);
            }
 internal void InternalRemoveNode(SuperLinkedListNode <T> node)
 {
     Debug.Assert(node.list == this, "Deleting the node from another list!");
     Debug.Assert(head != null, "This method shouldn't be called on empty list!");
     if (node.next == node)
     {
         Debug.Assert(count == 1 && head == node, "this should only be true for a list with only one node");
         head = null;
     }
     else
     {
         node.next.prev = node.prev;
         node.prev.next = node.next;
         if (head == node)
         {
             head = node.next;
         }
     }
     node.Invalidate();
     count--;
     version++;
 }
 internal void Invalidate()
 {
     list = null;
     next = null;
     prev = null;
 }
 public void Remove(SuperLinkedListNode <T> node)
 {
     ValidateNode(node);
     InternalRemoveNode(node);
 }
        void System.Collections.ICollection.CopyTo(Array array, int index)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            if (array.Rank != 1)
            {
                throw new ArgumentException("SR.Arg_MultiRank");
            }

            if (array.GetLowerBound(0) != 0)
            {
                throw new ArgumentException("SR.Arg_NonZeroLowerBound");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", index.ToString());
            }

            if (array.Length - index < Count)
            {
                throw new ArgumentException("SR.Arg_InsufficientSpace");
            }

            T[] tArray = array as T[];
            if (tArray != null)
            {
                CopyTo(tArray, index);
            }
            else
            {
                //
                // Catch the obvious case assignment will fail.
                // We can found all possible problems by doing the check though.
                // For example, if the element type of the Array is derived from T,
                // we can't figure out if we can successfully copy the element beforehand.
                //
                Type targetType = array.GetType().GetElementType();
                Type sourceType = typeof(T);
                if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))
                {
                    throw new ArgumentException("SR.Invalid_Array_Type");
                }

                object[] objects = array as object[];
                if (objects == null)
                {
                    throw new ArgumentException("SR.Invalid_Array_Type");
                }
                SuperLinkedListNode <T> node = head;
                try
                {
                    if (node != null)
                    {
                        do
                        {
                            objects[index++] = node.item;
                            node             = node.next;
                        } while (node != head);
                    }
                }
                catch (ArrayTypeMismatchException)
                {
                    throw new ArgumentException("SR.Invalid_Array_Type");
                }
            }
        }