예제 #1
0
        /// <summary>FOR COMPATIBILITY ONLY. AVOID IF POSSIBLE.</summary>
        public System.Collections.Generic.IEnumerator <T> GetEnumerator()
        {
            IStack <Link <Node, int, bool> > forks = new StackLinked <Link <Node, int, bool> >();

            forks.Push(new Link <Node, int, bool>(this._root, 0, false));
            while (forks.Count > 0)
            {
                Link <Node, int, bool> link = forks.Pop();
                if (link._1 == null)
                {
                    continue;
                }
                else if (!link._3)
                {
                    link._3 = true;
                    forks.Push(link);
                    Node child = link._1.Children[link._2];
                    forks.Push(new Link <Node, int, bool>(child, 0, false));
                }
                else if (link._2 < link._1.ItemCount)
                {
                    yield return(link._1.Items[link._2]);

                    link._2++;
                    link._3 = false;
                    forks.Push(link);
                }
            }
        }
예제 #2
0
파일: Trie.cs 프로젝트: Thaina/Towel
        /// <summary>Tries to add a value to the trie.</summary>
        /// <param name="stepper">The relative keys of the value.</param>
        /// <param name="exception">The exception that occurred if the add failed.</param>
        /// <returns>True if the value was added or false if not.</returns>
        public bool TryAdd(Stepper <T> stepper, out Exception exception)
        {
            if (stepper is null)
            {
                exception = new ArgumentNullException(nameof(stepper));
                return(false);
            }
            IStack <Node> stack = new StackLinked <Node>();
            Node          node  = null;

            stepper(key =>
            {
                MapHashLinked <Node, T> map = node is null
                                        ? _map
                                        : node.Map;
                if (map.Contains(key))
                {
                    node = map[key];
                }
                else
                {
                    Node temp = new Node(Equate, Hash);
                    map[key]  = temp;
                    node      = temp;
                }
                stack.Push(node);
            });
            if (node is null)
            {
                exception = new ArgumentException(nameof(stepper), "Stepper was empty.");
                return(false);
            }
            else if (node.IsLeaf)
            {
                exception = new ArgumentException(nameof(stepper), "Attempted to add an already existing item.");
                return(false);
            }
            else
            {
                node.IsLeaf = true;
                stack.Stepper(n => n.Count++);
                _count++;
                exception = null;
                return(true);
            }
        }
예제 #3
0
파일: AvlTree.cs 프로젝트: Thaina/Towel
        /// <summary>Gets the enumerator for this instance.</summary>
        /// <returns>An enumerator to iterate through the data structure.</returns>
        public IEnumerator <T> GetEnumerator()
        {
            IStack <Node> forks   = new StackLinked <Node>();
            Node          current = _root;

            while (current != null || forks.Count > 0)
            {
                if (current != null)
                {
                    forks.Push(current);
                    current = current.LeftChild;
                }
                else if (forks.Count > 0)
                {
                    current = forks.Pop();
                    yield return(current.Value);

                    current = current.RightChild;
                }
            }
        }