Example #1
0
 private void ProperAccess_Click(object sender, EventArgs e)
 {
     if (stackCollection.Any())
     {
         var ret = stackCollection.Pop();
         MessageBox.Show(ret.ToString());
     }
     else
     {
         MessageBox.Show("Collection is empty");
     }
 }
Example #2
0
        public virtual void IterateTree(Predicate <INfNode <T> > searchBy, Func <INfNode <T>, INfNode <T> > doSomething)
        {
            var callStack = new System.Collections.Generic.Stack <INfNode <T> >();
            //start at top
            var ivItem = (INfNode <T>) this;

            while (ivItem != null)
            {
                var nextItem = ivItem.NextItem();
                if (nextItem != null)
                {
                    //detect if some parent item is also a child item
                    if (ReferenceEquals(nextItem, ivItem) || callStack.Any(v => ReferenceEquals(v, nextItem)))
                    {
                        nextItem = null;
                    }
                    else
                    {
                        callStack.Push(ivItem);
                    }
                }

                ivItem = nextItem;

                if (ivItem == null)
                {
                    if (callStack.Count <= 0)
                    {
                        break;
                    }
                    ivItem = callStack.Pop();
                    continue;
                }
                //search for whatever by such-and-such
                if (!searchBy(ivItem))
                {
                    continue;
                }

                //having a match then do such-and-such
                var parent    = callStack.Peek();
                var ivItemIdx = (parent?.CurrentIndex ?? 0) - 1;
                if (parent == null || ivItemIdx < 0 || ivItemIdx >= parent.Count)
                {
                    ivItem = doSomething(ivItem);
                    continue;
                }
                parent.Items[ivItemIdx] = doSomething(ivItem);
            }
        }