Example #1
0
            /// <summary>
            /// Splits a SOM name in the individual parts.
            /// </summary>
            /// <param name="name">the full SOM name</param>
            /// <returns>the split name</returns>
            public static Stack2 SplitParts(string name)
            {
                while (name.StartsWith("."))
                {
                    name = name.Substring(1);
                }
                Stack2 parts = new Stack2();
                int    last  = 0;
                int    pos   = 0;
                string part;

                while (true)
                {
                    pos = last;
                    while (true)
                    {
                        pos = name.IndexOf(".", pos, StringComparison.Ordinal);
                        if (pos < 0)
                        {
                            break;
                        }
                        if (name[pos - 1] == '\\')
                        {
                            ++pos;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (pos < 0)
                    {
                        break;
                    }
                    part = name.Substring(last, pos - last);
                    if (!part.EndsWith("]"))
                    {
                        part += "[0]";
                    }
                    parts.Add(part);
                    last = pos + 1;
                }
                part = name.Substring(last);
                if (!part.EndsWith("]"))
                {
                    part += "[0]";
                }
                parts.Add(part);
                return(parts);
            }
        public void Peek_StackWithObjects_ReturnObjectOnTopOfTheStack()
        {
            //Arrange
            var stack = new Stack2 <string>();

            stack.Push("a");
            stack.Push("b");
            stack.Push("c");

            //Act
            var result = stack.Peek();

            //Assert
            Assert.That(result, Is.EqualTo("c"));
        }
        public void Pop_StackWithAFewObjects_RemoveObjectFromStack()
        {
            //Arrange
            var stack = new Stack2 <string>();

            stack.Push("a");
            stack.Push("b");
            stack.Push("c");

            //Act
            var result = stack.Pop();

            //Assert
            Assert.That(stack.Count, Is.EqualTo(2));
        }
        public void Peek_StackWithObjects_DoesNotRemoveObjectOnTopOfTheStack()
        {
            //Arrange
            var stack = new Stack2 <string>();

            stack.Push("a");
            stack.Push("b");
            stack.Push("c");

            //Act
            var result = stack.Peek();

            //Assert
            Assert.That(stack.Count, Is.EqualTo(3));
        }
Example #5
0
            /**
             * Inserts a new <CODE>Node</CODE> that will match the short name.
             * @param n the datasets top <CODE>Node</CODE>
             * @param shortName the short name
             * @return the new <CODE>Node</CODE> of the inserted name
             */
            public XmlNode InsertNode(XmlNode n, String shortName)
            {
                Stack2 <string> stack = SplitParts(shortName);
                XmlDocument     doc   = n.OwnerDocument;
                XmlNode         n2    = null;

                n = n.FirstChild;
                while (n.NodeType != XmlNodeType.Element)
                {
                    n = n.NextSibling;
                }
                for (int k = 0; k < stack.Count; ++k)
                {
                    String part = stack[k];
                    int    idx  = part.LastIndexOf('[');
                    String name = part.Substring(0, idx);
                    idx = int.Parse(part.Substring(idx + 1, part.Length - idx - 2));
                    int found = -1;
                    for (n2 = n.FirstChild; n2 != null; n2 = n2.NextSibling)
                    {
                        if (n2.NodeType == XmlNodeType.Element)
                        {
                            String s = EscapeSom(n2.LocalName);
                            if (s.Equals(name))
                            {
                                ++found;
                                if (found == idx)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    for (; found < idx; ++found)
                    {
                        n2 = doc.CreateElement(name);
                        n2 = n.AppendChild(n2);
                        XmlNode attr = doc.CreateNode(XmlNodeType.Attribute, "dataNode", XFA_DATA_SCHEMA);
                        attr.Value = "dataGroup";
                        n2.Attributes.SetNamedItem(attr);
                    }
                    n = n2;
                }
                InverseSearchAdd(inverseSearch, stack, shortName);
                name2Node[shortName] = n2;
                order.Add(shortName);
                return(n2);
            }
        public void Pop_StackWithAFewObjects_ReturnObjectOnTheTop()
        {
            //Arrange
            var stack = new Stack2 <string>();

            stack.Push("a");
            stack.Push("b");
            stack.Push("c");

            //Act
            var result = stack.Pop();

            //Assert
            Assert.That(result, Is.EqualTo("c"));
            //pre uistenie sa ze test funguje spravne je treba po jeho napisani a uspesnom zbehnuti zmenit business logiku s predpokladom ze by mal test zlyhat pre overenie
        }
Example #7
0
 /// <summary>
 /// Add a new item to the "rear" of the queue, which is actually the bottom of the primary stack
 /// </summary>
 /// <param name="val">Value to be enqueued</param>
 public void Enqueue(string val)
 {
     if (Stack1.Top == null)
     {
         Stack1.Push(val);
         Front = Stack1.Top;
     }
     else
     {
         while (Stack1.Top != null)
         {
             Stack2.Push(Stack1.Pop());
         }
         Stack1.Push(val);
         while (Stack2.Top != null)
         {
             Stack1.Push(Stack2.Pop());
         }
         Front = Stack1.Top;
     }
 }
Example #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("DFS最短步數與路徑搜尋,使用堆疊");
            Console.WriteLine("輸入檔案名稱(跟執行檔不同資料夾請輸入絕對路徑)\nEx: maze.txt、maze1.txt、maze2.txt");
            path = Console.ReadLine();

            text = GetFileText(path);

            map = Get2DMap(text, " ", "", new string[] { "\r\n" });
            vis = new int[map.Length, map[0].Length];

            stack = new Stack2();

            Console.WriteLine($"路徑:{path}");
            Console.WriteLine($"讀入檔案\n{text}");
            Console.WriteLine("讀入陣列");
            for (int r = 0; r < map.Length; r++)
            {
                for (int c = 0; c < map[r].Length; c++)
                {
                    Console.Write(map[r][c]);
                }
                Console.Write("\n");
            }

            Console.WriteLine("\n輸入起始位置、終點位置(以空白分隔)EX:0 0 2 3");
            temp = Console.ReadLine().Split(' ');
            sx   = int.Parse(temp[0]); sy = int.Parse(temp[1]);
            ex   = int.Parse(temp[2]); ey = int.Parse(temp[3]);

            // 加入起始位置
            vis[sy, sx] = 1;
            stack.push(new note(sx, sy, stack.top, 0));
            dfs(sx, sy, 0);

            Console.WriteLine($"最短步數:{min}");

            Console.ReadKey();
        }
Example #9
0
            /**
             * Adds a SOM name to the search node chain.
             * @param inverseSearch the start point
             * @param stack the stack with the separeted SOM parts
             * @param unstack the full name
             */
            public static void InverseSearchAdd(Dictionary <String, InverseStore> inverseSearch, Stack2 <string> stack, String unstack)
            {
                String       last = stack.Peek();
                InverseStore store;

                inverseSearch.TryGetValue(last, out store);
                if (store == null)
                {
                    store = new InverseStore();
                    inverseSearch[last] = store;
                }
                for (int k = stack.Count - 2; k >= 0; --k)
                {
                    last = stack[k];
                    InverseStore store2;
                    int          idx = store.part.IndexOf(last);
                    if (idx < 0)
                    {
                        store.part.Add(last);
                        store2 = new InverseStore();
                        store.follow.Add(store2);
                    }
                    else
                    {
                        store2 = (InverseStore)store.follow[idx];
                    }
                    store = store2;
                }
                store.part.Add("");
                store.follow.Add(unstack);
            }
Example #10
0
 /**
 * Creates a new instance from the datasets node. This expects
 * not the datasets but the data node that comes below.
 * @param n the datasets node
 */
 public Xml2SomDatasets(XmlNode n)
 {
     order = new ArrayList();
     name2Node = new Hashtable();
     stack = new Stack2();
     anform = 0;
     inverseSearch = new Hashtable();
     ProcessDatasetsInternal(n);
 }
Example #11
0
 /**
 * Splits a SOM name in the individual parts.
 * @param name the full SOM name
 * @return the split name
 */
 public static Stack2 SplitParts(String name)
 {
     while (name.StartsWith("."))
         name = name.Substring(1);
     Stack2 parts = new Stack2();
     int last = 0;
     int pos = 0;
     String part;
     while (true) {
         pos = last;
         while (true) {
             pos = name.IndexOf('.', pos);
             if (pos < 0)
                 break;
             if (name[pos - 1] == '\\')
                 ++pos;
             else
                 break;
         }
         if (pos < 0)
             break;
         part = name.Substring(last, pos - last);
         if (!part.EndsWith("]"))
             part += "[0]";
         parts.Add(part);
         last = pos + 1;
     }
     part = name.Substring(last);
     if (!part.EndsWith("]"))
         part += "[0]";
     parts.Add(part);
     return parts;
 }
Example #12
0
 /**
 * Adds a SOM name to the search node chain.
 * @param inverseSearch the start point
 * @param stack the stack with the separeted SOM parts
 * @param unstack the full name
 */
 public static void InverseSearchAdd(Hashtable inverseSearch, Stack2 stack, String unstack)
 {
     String last = (String)stack.Peek();
     InverseStore store = (InverseStore)inverseSearch[last];
     if (store == null) {
         store = new InverseStore();
         inverseSearch[last] = store;
     }
     for (int k = stack.Count - 2; k >= 0; --k) {
         last = (String)stack[k];
         InverseStore store2;
         int idx = store.part.IndexOf(last);
         if (idx < 0) {
             store.part.Add(last);
             store2 = new InverseStore();
             store.follow.Add(store2);
         }
         else
             store2 = (InverseStore)store.follow[idx];
         store = store2;
     }
     store.part.Add("");
     store.follow.Add(unstack);
 }
Example #13
0
 /**
 * Creates a new instance from the datasets node.
 * @param n the template node
 */
 public Xml2SomTemplate(XmlNode n)
 {
     order = new ArrayList();
     name2Node = new Hashtable();
     stack = new Stack2();
     anform = 0;
     templateLevel = 0;
     inverseSearch = new Hashtable();
     ProcessTemplate(n, null);
 }
        public void Peek_EmptyStack_ThrowInvalidOperationException()
        {
            var stack = new Stack2 <string>();

            Assert.That(() => stack.Peek(), Throws.InvalidOperationException);
        }
Example #15
0
        public void Test_Empty()
        {
            var stack = new Stack2();

            Assert.AreEqual(true, stack.Empty());
        }
        public void Count_EmptyStack_ReturnZero()
        {
            var stack = new Stack2 <string>();

            Assert.That(stack.Count, Is.EqualTo(0));
        }
        public void Push_ArgIsNull_ThrowArgNullException()
        {
            var stack = new Stack2 <string>();

            Assert.That(() => stack.Push(null), Throws.Exception.TypeOf <ArgumentNullException>());
        }