예제 #1
0
        /// <summary>
        /// Gets the node at a logical index by walking the linked list.
        /// </summary>
        /// <param name="index">The logical index.</param>
        /// <remarks>
        /// The caller should make sure <paramref name="index"/> is less than this node's count.
        /// </remarks>
        public SingleLinkedNode <TSource> GetNode(int index)
        {
            Debug.Assert(index >= 0 && index < GetCount());

            SingleLinkedNode <TSource> node = this;

            for (; index > 0; index--)
            {
                node = node.Linked !;
                Debug.Assert(node != null);
            }

            return(node);
        }
예제 #2
0
            public AppendPrependN(IEnumerable <TSource> source, SingleLinkedNode <TSource>?prepended, SingleLinkedNode <TSource>?appended, int prependCount, int appendCount)
                : base(source)
            {
                Debug.Assert(prepended != null || appended != null);
                Debug.Assert(prependCount > 0 || appendCount > 0);
                Debug.Assert(prependCount + appendCount >= 2);
                Debug.Assert((prepended?.GetCount() ?? 0) == prependCount);
                Debug.Assert((appended?.GetCount() ?? 0) == appendCount);

                _prepended    = prepended;
                _appended     = appended;
                _prependCount = prependCount;
                _appendCount  = appendCount;
            }
예제 #3
0
        /// <summary>
        /// Returns an array that contains the items of this node's singly-linked list in reverse.
        /// </summary>
        /// <param name="count">The number of items in this node.</param>
        public TSource[] ToArray(int count)
        {
            Debug.Assert(count == GetCount());

            TSource[] array = new TSource[count];
            int       index = count;

            for (SingleLinkedNode <TSource>?node = this; node != null; node = node.Linked)
            {
                --index;
                array[index] = node.Item;
            }

            Debug.Assert(index == 0);
            return(array);
        }
예제 #4
0
            public override List <TSource> ToList()
            {
                int            count = GetCount(onlyIfCheap: true);
                List <TSource> list  = count == -1 ? new List <TSource>() : new List <TSource>(count);

                for (SingleLinkedNode <TSource>?node = _prepended; node != null; node = node.Linked)
                {
                    list.Add(node.Item);
                }

                list.AddRange(_source);

                if (_appended != null)
                {
                    list.AddRange(_appended.ToArray(_appendCount));
                }

                return(list);
            }
예제 #5
0
            public override bool MoveNext()
            {
                switch (_state)
                {
                case 1:
                    _node  = _prepended;
                    _state = 2;
                    goto case 2;

                case 2:
                    if (_node != null)
                    {
                        _current = _node.Item;
                        _node    = _node.Linked;
                        return(true);
                    }

                    GetSourceEnumerator();
                    _state = 3;
                    goto case 3;

                case 3:
                    if (LoadFromEnumerator())
                    {
                        return(true);
                    }

                    if (_appended == null)
                    {
                        return(false);
                    }

                    _enumerator = ((IEnumerable <TSource>)_appended.ToArray(_appendCount)).GetEnumerator();
                    _state      = 4;
                    goto case 4;

                case 4:
                    return(LoadFromEnumerator());
                }

                Dispose();
                return(false);
            }
예제 #6
0
            public override TSource[] ToArray()
            {
                int count = GetCount(onlyIfCheap: true);

                if (count == -1)
                {
                    return(LazyToArray());
                }

                TSource[] array = new TSource[count];
                int       index = 0;

                for (SingleLinkedNode <TSource>?node = _prepended; node != null; node = node.Linked)
                {
                    array[index] = node.Item;
                    ++index;
                }

                if (_source is ICollection <TSource> sourceCollection)
                {
                    sourceCollection.CopyTo(array, index);
                }
                else
                {
                    foreach (TSource item in _source)
                    {
                        array[index] = item;
                        ++index;
                    }
                }

                index = array.Length;
                for (SingleLinkedNode <TSource>?node = _appended; node != null; node = node.Linked)
                {
                    --index;
                    array[index] = node.Item;
                }

                return(array);
            }
예제 #7
0
 /// <summary>
 /// Constructs a node linked to the specified node.
 /// </summary>
 /// <param name="linked">The linked node.</param>
 /// <param name="item">The item to place in this node.</param>
 private SingleLinkedNode(SingleLinkedNode <TSource> linked, TSource item)
 {
     Debug.Assert(linked != null);
     Linked = linked;
     Item   = item;
 }