Example #1
0
 internal LayoutCache(GUILayoutUtility.LayoutCache other)
 {
     this.id           = other.id;
     this.topLevel     = other.topLevel;
     this.layoutGroups = other.layoutGroups;
     this.windows      = other.windows;
 }
Example #2
0
    static void Main(string[] args)
    {
        // create an instance from the derived type
        EventStack <int> eStack = new EventStack <int>();
        // upcast to the base generic class
        GenericStack <int> gSTack = eStack;

        // use a lambda expression to register with the event
        eStack.PoppedEvent += (sender, eventArg) => {
            Console.WriteLine("Popped Event Invoked");
        };

        // push some data into the stack
        eStack.Push(1);
        eStack.Push(2);
        eStack.Push(3);

        // pop the data back out of the stack
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Popped Value: {0}", eStack.Pop());
        }

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Example #3
0
        public void Top_WhenStackIsEmpty_ShouldReturnStacIsEmptyMessage()
        {
            //Act
            var stack = new GenericStack <int>(1);

            //Assert
            Assert.AreEqual(default, stack.Top());
Example #4
0
        private static GenericStack GetScrollStack()
        {
            if (m_scrollViewStatesInfo == null)
            {
                if (typeof(GUI).GetProperty("scrollViewStates", ReflectionHelpers.CommonFlags) is PropertyInfo scrollStatesInfo)
                {
                    m_scrollViewStatesInfo = scrollStatesInfo;
                }
                else if (typeof(GUI).GetProperty("s_ScrollViewStates", ReflectionHelpers.CommonFlags) is PropertyInfo s_scrollStatesInfo)
                {
                    m_scrollViewStatesInfo = s_scrollStatesInfo;
                }
            }

            if (m_scrollViewStatesInfo?.GetValue(null, null) is GenericStack stack)
            {
                m_scrollStack = stack;
            }
            else
            {
                m_scrollStack = new GenericStack();
            }

            return(m_scrollStack);
        }
        // There is a trick applied with -ve numbers, make sure tree nodes do not have -ve number anytime.
        // when both left and right subtree traversal is complete then popped node will have -ve value, so time to print
        private static void PostOrderIterative(TreeNode <int> node)
        {
            var stack = new GenericStack <TreeNode <int> >(50); // taking a fail safe size

            while (node != null || !stack.IsEmpty())
            {
                if (node != null)
                {
                    stack.Push(node);
                    node = node.Left;
                }
                else
                {
                    node = stack.Pop();
                    if (node.Value > 0)
                    {
                        node.Value = node.Value * -1;
                        stack.Push(node);
                        node = node.Right;
                    }
                    else
                    {
                        Console.Write("({0}), ", node.Value * -1);
                        node = null;
                    }
                }
            }
        }
 internal LayoutCache()
 {
     this.topLevel     = new GUILayoutGroup();
     this.layoutGroups = new GenericStack();
     this.windows      = new GUILayoutGroup();
     this.layoutGroups.Push(this.topLevel);
 }
Example #7
0
        [Test] public void testPushPop()
        {
            GenericStack <string>         stringStack     = new GenericStack <string>();
            GenericStack <List <string> > stringListStack = new GenericStack <List <string> >();

            List <string> vect = new List <string>();

            vect.Add("a string in a vector 1");
            vect.Add("another string 2");

            stringStack.Push("value 1");
            stringListStack.Push(vect);

            stringStack.Push("value 2");
            stringListStack.Push(null);

            Assert.AreEqual(2, stringStack.Count);
            Assert.AreEqual(2, stringListStack.Count);

            Assert.AreEqual("value 2", stringStack.Pop());
            Assert.AreEqual(1, stringStack.Count);
            Assert.AreEqual("value 1", stringStack.Pop());
            Assert.AreEqual(0, stringStack.Count);

            Assert.Null(stringListStack.Pop());
            Assert.AreEqual(vect, stringListStack.Pop());

            Assert.Null(stringStack.Pop());
            Assert.AreEqual(0, stringStack.Count);
        }
Example #8
0
        [Test] public void testShove()
        {
            GenericStack <string> stringStack = new GenericStack <string>();

            stringStack.Shove("value 1", 0);
            Assert.AreEqual(1, stringStack.Count);
            Assert.AreEqual("value 1", stringStack.DeepPeek(0));
            Assert.AreEqual("value 1", stringStack.Peek(0));
            stringStack.Shove("value 4", 0);
            Assert.AreEqual(2, stringStack.Count);
            Assert.AreEqual("value 1", stringStack.DeepPeek(0));
            Assert.AreEqual("value 4", stringStack.Peek(0));
            stringStack.Pop();

            stringStack.Shove("value 2", 1);
            Assert.AreEqual(2, stringStack.Count);
            // Assert.AreEqual("value 2", stringStack.DeepPeek(0));
            // Assert.AreEqual("value 1", stringStack.DeepPeek(1));
            // Assert.AreEqual("[value 1 value 2]", stringStack.ToString());
            Assert.AreEqual("value 2", stringStack.DeepPeek(1));
            Assert.AreEqual("value 1", stringStack.DeepPeek(0));

            Assert.AreEqual(2, stringStack.Count);
            stringStack.Shove("value 3", 1);
            Assert.AreEqual(3, stringStack.Count);
            // Assert.AreEqual("[value 1 value 3 value 2]", stringStack.ToString());
            Assert.AreEqual("value 2", stringStack.DeepPeek(2));
            Assert.AreEqual("value 3", stringStack.DeepPeek(1));
            Assert.AreEqual("value 1", stringStack.DeepPeek(0));
            Assert.AreEqual("value 2", stringStack.Peek(0));
            Assert.AreEqual("value 3", stringStack.Peek(1));
            Assert.AreEqual("value 1", stringStack.Peek(2));
        }
Example #9
0
        static void Main(string[] args)
        {
            ObjectStack aObjectStack = new ObjectStack();
            aObjectStack.Push("ABCD");
            aObjectStack.Push(10);
            int firstItem = (int) aObjectStack.Pop();
            int lastItem = (int) aObjectStack.Pop(); // Boom...

            IntegerStack aIntegerStack = new IntegerStack();
            aIntegerStack.Push(12);
            //aIntegerStack.Push("ABCD"); //Compile error: good.
            // But for others data type you have to write seperate stacks ...;=((

            GenericStack<int> intGenericStack = new GenericStack<int>();
            intGenericStack.Push(1212);
            //intGenericStack.Push("ABCD"); //Compile error

            GenericStack<string> stringGenericStack = new GenericStack<string>(); //Wow. Can configure the type
            stringGenericStack.Push("ABCD");
            //stringGenericStack.Push(2323);

            GenericStack<Student> studentStack = new GenericStack<Student>(); //..Great it can be used for user defined type
            Student student1 = new Student("001", "Pinki");
            Student student2 = new Student("003", "Pavel");

            studentStack.Push(student1);
            studentStack.Push(student2);
        }
Example #10
0
        [Test] public void testEquals()
        {
            GenericStack <string>         stringStack     = new GenericStack <string>();
            GenericStack <string>         stringStack2    = new GenericStack <string>();
            GenericStack <List <string> > stringListStack = new GenericStack <List <string> >();

            // System.out.println("stringStack type is " + stringStack.getClass());
            // XXX What!?
            // Assert.True(stringStack == (stringListStack)); // see note in equals
            // Assert.True(stringStack == (stringStack2));

            Assert.AreEqual(stringStack.GetHashCode(), stringStack2.GetHashCode());
            // Assert.AreEqual(stringStack.GetHashCode(), stringListStack.GetHashCode()); // see note in equals
            // XXX C# doesn't do type erasure so this doesn't work anymore. Thankfully.
            Assert.AreNotEqual(stringStack.GetHashCode(), stringListStack.GetHashCode());

            stringStack.Push("value 1");
            Assert.False(stringStack == (stringStack2));
            // Assert.False(stringStack == (stringListStack));
            Assert.False(stringStack.GetHashCode() == stringStack2.GetHashCode());

            stringStack2.Push("value 1");
            Assert.True(stringStack.Equals(stringStack2));

            Assert.AreEqual(stringStack.GetHashCode(), stringStack2.GetHashCode());
        }
Example #11
0
 public LayoutCacheState(LayoutCache cache)
 {
     id           = cache.id;
     topLevel     = cache.topLevel;
     layoutGroups = cache.layoutGroups;
     windows      = cache.windows;
 }
 internal LayoutCache(LayoutCache other)
 {
     id           = other.id;
     topLevel     = other.topLevel;
     layoutGroups = other.layoutGroups;
     windows      = other.windows;
 }
Example #13
0
 internal void CopyState(LayoutCacheState other)
 {
     id           = other.id;
     topLevel     = other.topLevel;
     layoutGroups = other.layoutGroups;
     windows      = other.windows;
 }
    static void Main(string[] args)
    {
        GenericStack <int> intStack = new GenericStack <int>();

        intStack.Push(2);
        intStack.Push(4);
        intStack.Push(8);

        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Pop value: {0}", intStack.Pop());
        }

        GenericStack <string> stringStack = new GenericStack <string>();

        stringStack.Push("C#");
        stringStack.Push("to");
        stringStack.Push("Introduction");

        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Pop value: {0}", stringStack.Pop());
        }

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Example #15
0
        public int[] CheckAddElementToQueue(int[] array, int element)
        {
            var stack = new GenericStack <int>(array);

            stack.Push(element);
            return(stack.ToArray());
        }
Example #16
0
        public void CheckClearStack(int[] array)
        {
            var stack = new GenericStack <int>(array);

            stack.Clear();
            Assert.That(stack.ToArray, Is.EqualTo(new int[] { }));
        }
 public void Clear()
 {
     // clear the stack
     ActorStack = new GenericStack <PMGActor>();
     IntStack   = new GenericStack <int>();
     TimerStack = new GenericStack <Stopwatch>();
 }
 internal LayoutCache(GUILayoutUtility.LayoutCache other)
 {
     this.topLevel     = new GUILayoutGroup();
     this.layoutGroups = new GenericStack();
     this.windows      = new GUILayoutGroup();
     this.topLevel     = other.topLevel;
     this.layoutGroups = other.layoutGroups;
     this.windows      = other.windows;
 }
Example #19
0
        [Test] public void TestTop()
        {
            GenericStack <string> stringStack = new GenericStack <string>();

            stringStack.Push("Hi");
            Assert.AreEqual("Hi", stringStack.Top());
            stringStack.Pop();
            Assert.AreEqual(null, stringStack.Top());
        }
        public void TestGenericStackPeek()
        {
            GenericStack<string> stack = new GenericStack<string>();
            stack.Push("1. John");
            stack.Push("2. Nicholas");
            stack.Push("3. Mary");
            stack.Push("4. George");

            Assert.AreEqual("4. George", stack.Peek());
        }
Example #21
0
        static void GenericTest()
        {
            GenericStack <string, int> gs = new GenericStack <string, int>();

            gs.DisplayTypesofTemplates();

            GenericStack <Students, ListStudents> gs2 = new GenericStack <Students, ListStudents>();

            gs2.DisplayTypesofTemplates();
        }
Example #22
0
        private static void Contravariance()
        {
            GenericStack<Employee> empGenericStack = new GenericStack<Employee>(5);

            IGenericStackContravariant<Manager> mGS = empGenericStack;
            mGS.Push(new Manager());

            //IGenericStackPush<Manager> mGenericStack = empGenericStack;
            //mGenericStack.Push(new Manager());
        }
    /// Test the generic stack
    public static void TestGenericStack()
    {
        // Create stack
        int size = 10;
        GenericStack <double> stack  = new GenericStack <double>(size);
        GenericStack <string> stack2 = new GenericStack <string>(size);

        // Push elements on the stack
        try
        {
            for (int i = 0; i <= size; i++)
            {
                stack.Push(i);
                Console.WriteLine("Push: {0}", i);
            }
        }
        catch (ApplicationException ex)
        {
            Console.WriteLine("Error while pushing values on the stack: {0}", ex.Message);
        }

        // Pop elements from the stack
        double total = 0.0;

        try
        {
            for (int i = 0; i <= size + 5; i++)
            {
                // Note, no casting needed.
                double value = stack.Pop();
                total += value;
                Console.WriteLine("Pop: {0}", value);
            }
        }
        catch (ApplicationException ex)
        {
            Console.WriteLine("Error while poping values from the stack: {0}", ex.Message);
        }

        Console.WriteLine("Total: {0}", total);

        // Using Generic methods
        int sz1 = 10; int sz2 = 6;
        GenericStack <double> stackA = new GenericStack <double>(sz1);
        GenericStack <double> stackB = new GenericStack <double>(sz2);

        GenericMethod.Swap <GenericStack <double> >(ref stackA, ref stackB);
        Console.WriteLine("Sizes of stacks: {0} {1}", stackA.Size(), stackB.Size());

        // Swap 2 doubles
        double d1 = 1.2; double d2 = 3.0;

        GenericMethod.Swap <double>(ref d1, ref d2);
        Console.WriteLine("Sizes of stacks: {0} {1}", d1, d2);
    }
Example #24
0
        public void Push_WhenGivenOneInt_ShoudReturn1()
        {
            //Act
            var stack = new GenericStack <int>(1);

            stack.Push(5);
            var expected = 1;

            //Assert
            Assert.AreEqual(expected, stack.Size());
        }
Example #25
0
        public void TestGenericStackPeek()
        {
            GenericStack <string> stack = new GenericStack <string>();

            stack.Push("1. John");
            stack.Push("2. Nicholas");
            stack.Push("3. Mary");
            stack.Push("4. George");

            Assert.AreEqual("4. George", stack.Peek());
        }
    static void Main(string[] args)
    {
        GenericStack <string> stringStack = new GenericStack <string>();

        // push in an item of data
        stringStack.Push("Introduction to C#");

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Example #27
0
        public void Top_WhenStackGetsOneValue_ShouldReturnValue()
        {
            //Act
            var stack = new GenericStack <int>(1);

            stack.Push(1);
            var expected = 1;

            //Assert
            Assert.AreEqual(expected, stack.Top());
        }
Example #28
0
 static void Main(string[] args)
 {
     GenericStack<int> exampleIntStack = new GenericStack<int>();
     Console.WriteLine(exampleIntStack.StackEmpty());
     exampleIntStack.Push(25);
     exampleIntStack.Push(26);
     Console.WriteLine(exampleIntStack.StackEmpty());
     Console.WriteLine(exampleIntStack.Pop());
     Console.WriteLine(exampleIntStack.Pop());
     Console.WriteLine(exampleIntStack.StackEmpty());
 }
        public void TestGenericStackContains()
        {
            GenericStack<string> stack = new GenericStack<string>();
            stack.Push("1. John");
            stack.Push("2. Nicholas");
            stack.Push("3. Mary");
            stack.Push("4. George");
            stack.Push("5. Michael");

            Assert.IsTrue(stack.Contains("5. Michael"));
        }
Example #30
0
        public void Pop_WhenStackIsEmpty_ShouldReturnIndexOutOfRangeException()
        {
            //Act
            var stack = new GenericStack <int>(1);

            stack.Pop();

            //Assert
            Assert.Throws <IndexOutOfRangeException>(
                delegate { throw new IndexOutOfRangeException(); });
        }
Example #31
0
        [Test] public void testPeek()
        {
            GenericStack <string> stringStack = new GenericStack <string>();

            stringStack.Push("value 1");
            stringStack.Push("value 2");

            Assert.AreEqual("value 2", stringStack.Peek(0)); // deepest stack
            Assert.AreEqual(2, stringStack.Count);
            Assert.AreEqual("value 2", stringStack.Top());
            Assert.AreEqual("value 1", stringStack.Peek(1));
        }
Example #32
0
        public void TestGenericStackContains()
        {
            GenericStack <string> stack = new GenericStack <string>();

            stack.Push("1. John");
            stack.Push("2. Nicholas");
            stack.Push("3. Mary");
            stack.Push("4. George");
            stack.Push("5. Michael");

            Assert.IsTrue(stack.Contains("5. Michael"));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="postfix"></param>
        public static string evaluatePostfixExpression(string postfix)
        {
            char[]   postfixChars = postfix.ToCharArray();
            string[] tokens       = postfix.Split();
            //LinkedList<string> tokens = parseArithmeticExpression(postfixChars);
            GenericStack <string> parsingStack = new GenericStack <string>();

            //1. Scan the Postfix string from left to right.
            for (int i = 0; i < tokens.Length - 1; i++)
            {
                //2. If the scannned character is an operand, add it to the stack.
                if (isOperand(tokens[i]))
                {
                    parsingStack.push(tokens[i]);
                }
                else
                {
                    //3. If the scanned character is an Operator, then we store the top most element of the stack(topStack) in a variable temp.
                    //   Pop the stack. Now evaluate topStack(Operator)temp. Let the result of this operation be retVal.
                    //   Pop the stack and Push retVal into the stack.
                    double operand1 = Double.Parse(parsingStack.pop());
                    double operand2 = Double.Parse(parsingStack.pop());
                    string result   = "";
                    switch (tokens[i])
                    {
                    case "*":
                        result = (operand2 * operand1) + "";
                        break;

                    case "/":
                        result = (operand2 / operand1) + "";
                        break;

                    case "+":
                        result = (operand2 + operand1) + "";
                        break;

                    case "-":
                        result = (operand2 - operand1) + "";
                        break;

                    default:
                        Console.WriteLine("Error during Postfix Evaluation, unrecognized operator: " + tokens[i]);
                        result = "";
                        break;
                    }
                    parsingStack.push(result);
                }
            }
            //5. After all characters are scanned, we will have only one element in the stack.Return topStack.
            return(parsingStack.pop());
        }
Example #34
0
        private static void CoVariance()
        {
            GenericStack<Employee> empGenericStack = new GenericStack<Employee>(5);

            IGenericStackCovariant<Person> personGenericStack = empGenericStack;
            Person p = personGenericStack.Pop();

            //Employee e = new Employee();
            //Person p = e;

            //IGenericStackPop<Person> personGenericStack = empGenericStack;
            //Person p = personGenericStack.Pop();
        }
    static void Main(string[] args)
    {
        GenericStack <int> intStack = new GenericStack <int>();

        GenericStack <string> stringStack = new GenericStack <string>();

        Console.WriteLine("Int type: {0}", intStack.GetType());
        Console.WriteLine("String type: {0}", stringStack.GetType());

        // wait for input before exiting
        Console.WriteLine("Press enter to finish");
        Console.ReadLine();
    }
Example #36
0
        [Test] public void TestReverseIndex()
        {
            GenericStack <string> stringStack = new GenericStack <string>();

            stringStack.Push("1");
            stringStack.Push("2");
            stringStack.Push("3");

            Assert.AreEqual(2, stringStack.ReverseIndex(0));
            Assert.AreEqual(1, stringStack.ReverseIndex(1));
            Assert.AreEqual(0, stringStack.ReverseIndex(2));
            Assert.AreEqual(0, stringStack.ReverseIndex(5));
            Assert.AreEqual(2, stringStack.ReverseIndex(-20));
        }
        public void TestGenericStackClear()
        {
            GenericStack<string> stack = new GenericStack<string>();
            stack.Push("1. John");
            stack.Push("2. Nicholas");
            stack.Push("3. Mary");
            stack.Push("4. George");
            stack.Push("5. Michael");

            Assert.AreEqual(5, stack.Count);

            stack.Clear();

            Assert.AreEqual(0, stack.Count);
        }
Example #38
0
		public MessageBox() : base()
		{
			_frame = new MessageBoxFrame(Orientation.Vertical);
			Control = _frame;
			
			_bodyStack = new Stack(Orientation.Horizontal);
			_frame.AddChild(_bodyStack);
			_messageLabel = new Label();
			_bodyStack.AddChild(_messageLabel);
			
			_buttonStack = new GenericStack<Button>(Orientation.Horizontal);
			_frame.AddChild(_buttonStack);
			
			GrayScene = true;
			CloseOnOutsideClick = false;
		}
 static void Main(string[] args)
 {
     GenericStack<int> stack = new GenericStack<int>();
     Console.WriteLine(stack.Pop());
     Console.WriteLine(stack.Peek());
     int[] array = new int[] { 1, 3, 5, 6, 7, 8, 9, 11, 13 };
     for(int i=0;i<array.Length;i++)
     {
         stack.Push(array[i]);
     }
     Console.WriteLine(stack.Pop());
     Console.WriteLine(stack.Peek());
     Console.WriteLine(stack.Contains(8));
     stack.Clear();
     Console.WriteLine(stack.Pop());
 }
Example #40
0
        static void GenericExplanation()
        {
            IntegerStack iStack = new IntegerStack(5);

               iStack.Push(0);
               iStack.Push(100);
               iStack.Push(1000);

               int element = iStack.Pop();
               Console.WriteLine(element);

               element = iStack.Pop();
               Console.WriteLine(element);

               element = iStack.Pop();
               Console.WriteLine(element);

               GeneralPurposeStack gStack = new GeneralPurposeStack(5);

               gStack.Push("ABCD");
               gStack.Push("jj");
               gStack.Push("kk");

               string element1 = (string) gStack.Pop();

            GenericStack<int> genericIntStack = new GenericStack<int>(5);

            genericIntStack.Push(0);
            genericIntStack.Push(1);

            int element22 = genericIntStack.Pop();
            Console.WriteLine(element);

            element = genericIntStack.Pop();
            Console.WriteLine(element);

            GenericStack<Person> pStack = new GenericStack<Person>(5);

            pStack.Push(new Person { FirstName = "ABCD" });
            pStack.Push(new Person { FirstName = "DEFG" });

            Person element122 = pStack.Pop();
            Console.WriteLine(element122.FirstName);

            element122 = pStack.Pop();
            Console.WriteLine(element122.FirstName);
        }
        public static void Main(string[] args)
        {
            GenericPair<int, string> pairche = new GenericPair<int, string>(5, "asd");
            GenericPair<int, string> pairche2 = new GenericPair<int, string>(5, "asd");
            Console.WriteLine("{0} equals {1} : {2}\n", pairche, pairche2, pairche.Equals(pairche2));

            GenericStack<int> stackche = new GenericStack<int>();
            stackche.Push(1);
            stackche.Push(2);
            stackche.Push(3);
            stackche.Push(4);
            Console.WriteLine(stackche.Peek());  // 4
            Console.WriteLine(stackche.Pop());   // 4
            Console.WriteLine(stackche.Peek());  // 3
            Console.WriteLine("The stack contains 2 : {0}", stackche.Contains(2));
            Console.WriteLine("The stack contains 7 : {0}", stackche.Contains(7));
            stackche.Clear();
            Console.WriteLine("Stack was cleared.");
            Console.WriteLine("The stack contains 2 : {0}\n", stackche.Contains(2));
            //Console.WriteLine(stackche.Peek()); // throws InvalidOperationException;

            GenericDequeue<int> dequeueche = new GenericDequeue<int>();
            dequeueche.AddToEnd(3);     // {3}
            dequeueche.AddToEnd(2);     // {3, 2}
            dequeueche.AddToFront(4);   // {4, 3, 2}
            dequeueche.AddToEnd(1);     // {4, 3, 2, 1}
            dequeueche.AddToFront(5);   // {5, 4, 3, 2, 1}
            Console.WriteLine(dequeueche.PeekFromEnd());   // 1
            Console.WriteLine(dequeueche.PeekFromFront()); // 5
            Console.WriteLine("Dequeue contains 5: {0}", dequeueche.Contains(5));
            dequeueche.RemoveFromEnd();   // {5, 4, 3, 2}
            dequeueche.RemoveFromFront(); // {4, 3, 2}
            Console.WriteLine("Dequeue contains 5: {0}", dequeueche.Contains(5));
            Console.WriteLine(dequeueche.PeekFromEnd());   // 2
            Console.WriteLine(dequeueche.PeekFromFront()); // 4
            dequeueche.Clear();
            //Console.WriteLine(dequeuche.PeekFromEnd()); // throws InvalidOperationException;
        }
        public void SortbyModel()
        {
            GenericStack<ProtocolDroid> ProtocolStack = new GenericStack<ProtocolDroid>();
            GenericStack<UtilityDroid> UtilityStack = new GenericStack<UtilityDroid>();
            GenericStack<JanitorDroid> JanitorStack = new GenericStack<JanitorDroid>();
            GenericStack<AstromechDroid> AstromechStack = new GenericStack<AstromechDroid>();

            GenericQueue<Droid> DroidsQueue = new GenericQueue<Droid>();

            foreach (IDroid droid in droidCollection)
            {
                if (droid is ProtocolDroid)
                {
                    ProtocolStack.Push((ProtocolDroid)droid);
                }
                else if (droid is UtilityDroid)
                {
                    UtilityStack.Push((UtilityDroid)droid);
                }
                else if (droid is JanitorDroid)
                {
                    JanitorStack.Push((JanitorDroid)droid);
                }
                else if (droid is AstromechDroid)
                {
                    AstromechStack.Push((AstromechDroid)droid);
                }
            }

            while (ProtocolStack != null)
            {
                DroidsQueue.Enqueue(ProtocolStack.Pop());
            }
            while (UtilityStack != null)
            {
                DroidsQueue.Enqueue(UtilityStack.Pop());
            }
            while (JanitorStack != null)
            {
                DroidsQueue.Enqueue(JanitorStack.Pop());
            }
            while (AstromechStack != null)
            {
                DroidsQueue.Enqueue(AstromechStack.Pop());

            }

            while (DroidsQueue != null)
            {
                int i =0;
                droidCollection[i] = DroidsQueue.Dequeue();
                i++;
            }
        }
 internal LayoutCache()
 {
     this.topLevel = new GUILayoutGroup();
     this.layoutGroups = new GenericStack();
     this.windows = new GUILayoutGroup();
     this.layoutGroups.Push(this.topLevel);
 }
        public void TestGenericStackPop_ReferenceType()
        {
            GenericStack<int[]> stack = new GenericStack<int[]>();
            stack.Push(new int[] { 1, 1, 1 });
            stack.Push(new int[] { 2, 2, 2 });
            stack.Push(new int[] { 3, 3, 3 });
            stack.Push(new int[] { 4, 4, 4 });
            stack.Push(new int[] { 5, 5, 5 });
            stack.Push(new int[] { 6, 6, 6 });


            Assert.AreEqual(6, stack.Count);

            CollectionAssert.AreEqual(new int[] { 6, 6, 6 }, stack.Pop());
        }
        public void SortByModel()
        {
            //Create generic stacks for each model type
            GenericStack<Droid> protocolStack = new GenericStack<Droid>();
            GenericStack<Droid> utilityStack = new GenericStack<Droid>();
            GenericStack<Droid> janitorStack = new GenericStack<Droid>();
            GenericStack<Droid> astromechStack = new GenericStack<Droid>();

            //Create new generic queue
            GenericQueue<Droid> droidQueue = new GenericQueue<Droid>();

            foreach (Droid droid in droidArray)
            {
                if (droid != null)
                {
                    switch (droid.GetModel())
                    {
                        case "Protocol":
                            protocolStack.Push(droid);
                            break;
                        case "Utility":
                            utilityStack.Push(droid);
                            break;
                        case "Janitor":
                            janitorStack.Push(droid);
                            break;
                        case "Astromech":
                            astromechStack.Push(droid);
                            break;
                        default:
                            break;
                    }
                }
            }

            //Get droids from each stack and enqueue them
            while (astromechStack.IsNotEmpty())
            {
                droidQueue.Enqueue(astromechStack.Pop());
            }
            while (janitorStack.IsNotEmpty())
            {
                droidQueue.Enqueue(janitorStack.Pop());
            }
            while (utilityStack.IsNotEmpty())
            {
                droidQueue.Enqueue(utilityStack.Pop());
            }
            while (protocolStack.IsNotEmpty())
            {
                droidQueue.Enqueue(protocolStack.Pop());
            }

            int i = 0;
            while (droidQueue.IsNotEmpty())
            {
                droidArray[i] = droidQueue.Dequeue();
                i++;
            }
        }
Example #46
0
        static void Main(string[] args)
        {
            Console.WriteLine("---Generic Stack Test---");

            GenericStack<int> stack = new GenericStack<int>(0);

            stack.Push(1);
            stack.Push(2);
            stack.Push(3);

            while(!stack.IsEmpty())
            {
                Console.WriteLine(stack.Contains(2));
                Console.WriteLine(stack.Peek());
                Console.WriteLine(stack.Pop());
            }

            Console.WriteLine("---Generic Dequeue Test---");

            GenericDequeue<int> dequeue = new GenericDequeue<int>(0);

            dequeue.AddToEnd(3);
            dequeue.AddToEnd(4);
            dequeue.AddToFront(2);
            dequeue.AddToFront(1);

            while (!dequeue.IsEmpty())
            {
                Console.WriteLine(dequeue.Contains(3));
                Console.WriteLine(dequeue.PeekFromEnd());
                Console.WriteLine(dequeue.RemoveFromEnd());
                Console.WriteLine(dequeue.Contains(3));
                Console.WriteLine(dequeue.PeekFromFront());
                Console.WriteLine(dequeue.RemoveFromFront());
            }

            Console.WriteLine("---Lotto Game Test---");

            LottoGame<int, string> lottoGame = new LottoGame<int, string>(new Combination<int, string>(1, 2, 3, "a", "b", "c"));

            var comb1 = new Combination<int, string>(5, 7, 9, "we", "asd", "rgd");
            var comb2 = new Combination<int, string>(5, 7, 8, "we", "asd", "rgd");
            var comb3 = new Combination<int, string>(5, 7, 9, "we", "asd", "rgd");
            var comb4 = new Combination<int, string>(1, 7, 9, "we", "asd", "rgd");
            var comb5 = new Combination<int, string>(5, 2, 9, "we", "b", "rgd");

            if (lottoGame.AddUserCombination(comb1)) Console.WriteLine("Added combination {0}", comb1);
            else Console.WriteLine("Combination {0} already exists!", comb1);
            if (lottoGame.AddUserCombination(comb2)) Console.WriteLine("Added combination {0}", comb2);
            else Console.WriteLine("Combination {0} already exists!", comb2);
            if (lottoGame.AddUserCombination(comb3)) Console.WriteLine("Added combination {0}", comb3);
            else Console.WriteLine("Combination {0} already exists!", comb3);
            if (lottoGame.AddUserCombination(comb4)) Console.WriteLine("Added combination {0}", comb4);
            else Console.WriteLine("Combination {0} already exists!", comb4);
            if (lottoGame.AddUserCombination(comb5)) Console.WriteLine("Added combination {0}", comb5);
            else Console.WriteLine("Combination {0} already exists!", comb5);

            Console.WriteLine();

            var lottoResult = lottoGame.Validate();
            if (lottoResult.IsWinning) Console.WriteLine("This lotto game has a winner with {0} matching values!", lottoResult.MatchedNumbersCount);
            else Console.WriteLine("There is no winner in this lotto game!");

            Console.ReadKey();
        }
 public void TestGenericStackConstructor()
 {
     GenericStack<string> stack = new GenericStack<string>();
     Assert.AreEqual(0, stack.Count);
 }
        /// <summary>
        /// public method to Sort the droids into categories using a modified bucket sort
        /// </summary>
        public void SortIntoCategories()
        {
            //Create a generic stack for each type of droid, and pass in the droid type as the generic that will
            //come through on the stack class as T.
            GenericStack<ProtocolDroid> protocolStack = new GenericStack<ProtocolDroid>();
            GenericStack<UtilityDroid> utilityStack = new GenericStack<UtilityDroid>();
            GenericStack<JanitorDroid> janitorStack = new GenericStack<JanitorDroid>();
            GenericStack<AstromechDroid> astromechStack = new GenericStack<AstromechDroid>();

            //Create a queue to hold the droids as we pop them off the stack.
            GenericQueue<IDroid> categorizedDroidQueue = new GenericQueue<IDroid>();

            //For each IDroid in the droidCollection
            foreach (IDroid droid in this.droidCollection)
            {
                //if the droid is not null we want to process it. If it is null we will go to the else
                if (droid != null)
                {
                    //The testing of the droids must occur in this order. It must be done in the order of
                    //most specific droid to least specific.

                    //If we were to test a droid that IS of type Astromech against Utility BEFORE we test against
                    //Astromech, it would pass and be put into the Utility stack and not the Astromech. That is why it
                    //is important to test from most specific to least.

                    //If the droid is an Astromech, push it on the astromech stack
                    if (droid is AstromechDroid)
                    {
                        astromechStack.Push((AstromechDroid)droid);
                    }
                    //Else if it is a JanitorDroid, push it on the janitor stack
                    else if (droid is JanitorDroid)
                    {
                        janitorStack.Push((JanitorDroid)droid);
                    }
                    //Do for Utility
                    else if (droid is UtilityDroid)
                    {
                        utilityStack.Push((UtilityDroid)droid);
                    }
                    //Do for Protocol
                    else if (droid is ProtocolDroid)
                    {
                        protocolStack.Push((ProtocolDroid)droid);
                    }
                }
                //The droid we are trying to consider is null, break out of the loop.
                else
                {
                    break;
                }
            }

            //Now that the droids are all in thier respective stacks we can do the work
            //of poping them off of the stacks and adding them to the queue.
            //It is required that they be popped off from each stack in this order so that they have
            //the correct order going into the queue.

            //This is a primer pop. It gets the first droid off the stack, which could be null if the stack is empty
            AstromechDroid currentAstromechDroid = astromechStack.Pop();
            //While the droid that is popped off is not null
            while (currentAstromechDroid != null)
            {
                //Add the popped droid to the queue.
                categorizedDroidQueue.Enqueue(currentAstromechDroid);
                //Pop off the next droid for the loop test
                currentAstromechDroid = astromechStack.Pop();
            }

            //See above method for Astromech. It is the same except for Janitor
            JanitorDroid currentJanitorDroid = janitorStack.Pop();
            while (currentJanitorDroid != null)
            {
                categorizedDroidQueue.Enqueue(currentJanitorDroid);
                currentJanitorDroid = janitorStack.Pop();
            }

            //See above method for Astromech. It is the same except for Utility
            UtilityDroid currentUtilityDroid = utilityStack.Pop();
            while (currentUtilityDroid != null)
            {
                categorizedDroidQueue.Enqueue(currentUtilityDroid);
                currentUtilityDroid = utilityStack.Pop();
            }

            //See above method for Astromech. It is the same except for Protocol
            ProtocolDroid currentProtocolDroid = protocolStack.Pop();
            while (currentProtocolDroid != null)
            {
                categorizedDroidQueue.Enqueue(currentProtocolDroid);
                currentProtocolDroid = protocolStack.Pop();
            }

            //Now that the droids have all been removed from the stacks and put into the queue
            //we need to dequeue them all and put them back into the original array.

            //Set a int counter to 0.
            int counter = 0;

            //This is a primer dequeue that will get the first droid out of the queue.
            IDroid iDroid = categorizedDroidQueue.Dequeue();
            //While the dequeued droid is not null.
            while (iDroid != null)
            {
                //Add the droid to the droid collection using the int counter as the index
                this.droidCollection[counter] = iDroid;
                //increment the counter
                counter++;
                //dequeue the next droid off the queue so it can be used in the while condition
                iDroid = categorizedDroidQueue.Dequeue();
            }

            //set the lenght of the collection to the value of the counter. It should be the same, but in case it changed.
            this.lengthOfCollection = counter;
        }
 internal LayoutCache(GUILayoutUtility.LayoutCache other)
 {
   this.topLevel = other.topLevel;
   this.layoutGroups = other.layoutGroups;
   this.windows = other.windows;
 }
        //method to organize droids by type
        public bool Organize()
        {
            GenericStack<AstromechDroid> astromechStack = new GenericStack<AstromechDroid>();
            GenericStack<JanitorDroid> janitorStack = new GenericStack<JanitorDroid>();
            GenericStack<UtilityDroid> utilityStack = new GenericStack<UtilityDroid>();
            GenericStack<ProtocolDroid> protocolStack = new GenericStack<ProtocolDroid>();

            GenericQueue<IDroid> droidQueue = new GenericQueue<IDroid>();

            //Add droids to appropriate stack types
            for (int i = 0; i < lengthOfCollection; i++)
            {
                try
                {
                    astromechStack.Add((AstromechDroid)droidCollection[i]);
                }
                catch
                {
                    try
                    {
                        janitorStack.Add((JanitorDroid)droidCollection[i]);
                    }
                    catch
                    {
                        try
                        {
                            utilityStack.Add((UtilityDroid)droidCollection[i]);
                        }
                        catch
                        {
                            try
                            {
                                protocolStack.Add((ProtocolDroid)droidCollection[i]);
                            }
                            catch
                            {
                                return false;
                            }
                        }
                    }
                }
            }

            //Add droids in order to the queue
            while (astromechStack.Head != null)
            {
                droidQueue.Add((IDroid)astromechStack.Pop());
            }

            while (janitorStack.Head != null)
            {
                droidQueue.Add((IDroid)janitorStack.Pop());
            }

            while (utilityStack.Head != null)
            {
                droidQueue.Add((IDroid)utilityStack.Pop());
            }

            while (protocolStack.Head != null)
            {
                droidQueue.Add((IDroid)protocolStack.Pop());
            }

            //Dequeue droids back into the array
            for (int i = 0; droidQueue.Tail != null; i++)
            {
                droidCollection[i] = (IDroid)droidQueue.Dequeue();
            }

                return true;
        }
Example #51
0
		static GUI()
		{
			GUI.scrollStepSize = 10f;
			GUI.boxHash = "Box".GetHashCode();
			GUI.repeatButtonHash = "repeatButton".GetHashCode();
			GUI.toggleHash = "Toggle".GetHashCode();
			GUI.buttonGridHash = "ButtonGrid".GetHashCode();
			GUI.sliderHash = "Slider".GetHashCode();
			GUI.beginGroupHash = "BeginGroup".GetHashCode();
			GUI.scrollviewHash = "scrollView".GetHashCode();
			GUI.s_ScrollViewStates = new GenericStack();
			GUI.nextScrollStepTime = DateTime.Now;
		}
        public void SortModel()
        {
            GenericStack<UtilityDroid> UtilityDroidStack = new GenericStack<UtilityDroid>();
            GenericStack<AstromechDroid> AstromechDroidStack = new GenericStack<AstromechDroid>();
            GenericStack<ProtocolDroid> ProtocolDroidStack = new GenericStack<ProtocolDroid>();
            GenericStack<JanitorDroid> JanitorDroidStack = new GenericStack<JanitorDroid>();

            foreach(IDroid droids in droidCollection)
            {
                if(droids is UtilityDroid)
                {
                    UtilityDroidStack.Add((UtilityDroid)droids);
                }
                else if (droids is AstromechDroid)
                {
                    AstromechDroidStack.Add((AstromechDroid)droids);
                }
                else if (droids is ProtocolDroid)
                {
                    ProtocolDroidStack.Add((ProtocolDroid)droids);
                }
                else if (droids is JanitorDroid)
                {
                    JanitorDroidStack.Add((JanitorDroid)droids);
                }

                //*****************************************************************************************************************PROBLEM***********************************************************************************************************************************8*************************************************
                //Error	4	Argument 1: cannot convert from 'cis237assignment4.GenericStack<cis237assignment4.UtilityDroid>' to 'cis237assignment4.IDroid'	c:\users\jason\cis237\cis237assignment4\cis237assignment4\droidcollection.cs	174	23	cis237assignment4
                //Error	3	The best overloaded method match for 'cis237assignment4.GenericQueue<cis237assignment4.IDroid>.Add(cis237assignment4.IDroid)' has some invalid arguments	c:\users\jason\cis237\cis237assignment4\cis237assignment4\droidcollection.cs	175	13	cis237assignment4
                /************************************************************************************************************************************************************************************************************************************************/

            }
            GenericQueue<IDroid> Queue = new GenericQueue<IDroid>();
            //Queue.Add(AstromechDroidStack);
            //Queue.Add(UtilityDroidStack);
        }
 internal LayoutCache(GUILayoutUtility.LayoutCache other)
 {
     this.topLevel = new GUILayoutGroup();
     this.layoutGroups = new GenericStack();
     this.windows = new GUILayoutGroup();
     this.topLevel = other.topLevel;
     this.layoutGroups = other.layoutGroups;
     this.windows = other.windows;
 }
        public void CategorizeByModel()
        {
            //Method to categorize droids in the order of Astromech, Janitor, Utility, Protocol
            GenericStack<IDroid> protocolStack = new GenericStack<IDroid>();
            GenericStack<IDroid> utilityStack = new GenericStack<IDroid>();
            GenericStack<IDroid> janitorStack = new GenericStack<IDroid>();
            GenericStack<IDroid> astromechStack = new GenericStack<IDroid>();

            foreach (IDroid droid in droidCollection)
            {
                if (droid != null)
                {
                    switch (droid.GetModel())
                    {   //Go through the droidCollection and separate the droids into stacks by Model
                        case "PROTOCOL":
                            protocolStack.Add(droid);
                            break;
                        case "UTILITY":
                            utilityStack.Add(droid);
                            break;
                        case "JANITOR":
                            janitorStack.Add(droid);
                            break;
                        case "ASTROMECH":
                            astromechStack.Add(droid);
                            break;
                        default:
                            break;
                    }
                }
            }

            //Create the queue and add the stacks to it in the desired order
            GenericQueue<IDroid> droidQueue = new GenericQueue<IDroid>();
            droidQueue.AddStack(astromechStack);
            droidQueue.AddStack(janitorStack);
            droidQueue.AddStack(utilityStack);
            droidQueue.AddStack(protocolStack);

            droidCollection = new IDroid[droidQueue.Depth];

            for (int i = 0; i < droidQueue.Depth; i++)
            {
                droidCollection[i] = (IDroid)droidQueue.Retrieve(i + 1).Droid;
            }
        }