示例#1
0
        /// <summary>
        /// Processes all of the tasks in the list.
        /// </summary>
        public void Process()
        {
            TaskListNode last    = null;
            var          current = _first;

            PreProcess();

            // Loop through the nodes until we hit null, indicating the end of the list
            while (current != null)
            {
                // Perform the func and get if we need to remove the node
                var remove = ProcessItem(current.Value);

                if (remove)
                {
                    // Remove the node
                    if (last != null)
                    {
                        // Skip over the node by setting the previous node's Next to the current node's Next
                        last.Next = current.Next;
                    }
                    else
                    {
                        // No previous node, so just set the next node as the head, bumping the current node into nothingness
                        _first = current.Next;
                    }

                    // Clear the value of the node to ensure we don't hold onto any references, then push the node
                    // back into the pool so it can be reused
                    current.Value = default(T);

                    var tmp = current;
                    current = current.Next;

                    tmp.Next = null;
                    _pool.Push(tmp);
                }
                else
                {
                    // No nodes removed, so just set the last node to the current
                    last    = current;
                    current = current.Next;
                }
            }

            PostProcess();
        }
示例#2
0
        /// <summary>
        /// Adds a new item to this <see cref="TaskList{T}"/> at the head of the list.
        /// </summary>
        /// <param name="value">The value of the node to add.</param>
        public void Add(T value)
        {
            // Get the node object
            TaskListNode newHead;

            if (_pool.Count > 0)
            {
                newHead = _pool.Pop();
            }
            else
            {
                newHead = new TaskListNode();
            }

            // Set the node's values
            newHead.Value = value;
            newHead.Next  = _first;

            // Set as the new head
            _first = newHead;
        }