/// <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); } } }
internal StackLinked(StackLinked <T> stack) { if (stack._top is not null) { _top = new Node(value: stack._top.Value); Node?a = stack._top.Down; Node?b = _top; while (a is not null) { b.Down = new Node(value: a.Value); b = b.Down; a = a.Down; } _count = stack._count; } }
/// <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); } }
/// <summary>Creates a shallow clone of this data structure.</summary> /// <returns>A shallow clone of this data structure.</returns> public StackLinked<T> Clone() { StackLinked<T> clone = new StackLinked<T>(); if (_count == 0) { return clone; } Node copying = _top; Node cloneTop = new Node(_top.Value, null); Node cloning = cloneTop; while (!(copying is null)) { copying = copying.Down; cloning.Down = new Node(copying.Value, null); cloning = cloning.Down; } clone._top = cloneTop; return clone; }
/// <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; } } }