示例#1
0
文件: STTests.cs 项目: zzhi/Algs4Net
        public void STTest1()
        {
            // testing Get/Put semantics
            ST <string, int> st = new ST <string, int>();

            Assert.IsTrue(st.IsEmpty);

            string[] keys = { "to", "be", "or", "not", "to", "be", "is", "quest" };
            for (int i = 0; i < keys.Length; i++)
            {
                st.Put(keys[i], i);
            }
            Assert.IsTrue(!(st.IsEmpty) && (st.Count == 6));

            string key = "not";

            Assert.IsTrue(st.Contains(key));
            st.Delete(key);
            Assert.IsFalse(st.Contains(key));
            Assert.IsNull(st.Get(key));

            object value = st.Get("is");

            Assert.AreEqual(6, value);

            value = st.Get("world");
            Assert.IsNull(value);

            int dummy = (int)st.Get("hello"); // generate null exception
        }
示例#2
0
文件: STTests.cs 项目: zzhi/Algs4Net
        public void STTest2()
        {
            // testing indexer semantics
            ST <int, string> st = new ST <int, string>();

            st[99] = null;
            Assert.AreEqual(st.Count, 0);
            st[3] = "abc";
            st[2] = "cde";
            Assert.AreEqual(st[2], "cde");
            st[99] = null;
            st[2]  = null;
            Assert.AreEqual(st.Count, 1);
            st.Delete(3);
            Assert.AreEqual(st.Count, 0);
            try
            {
                string dummyS = st[99];
                Assert.IsNull(dummyS);
            }
            catch (Exception)
            {
                Assert.Fail("it is possible to look up an empty table");
            }

            st[4] = "def";
            Assert.IsNull(st[99]);
            Assert.IsNull(st[3]);
            Assert.IsNull(st[2]);
            st[3] = "101";
            int oldCount = st.Count;

            // delete non-existing key does nothing
            st.Delete(123456);
            Assert.AreEqual(oldCount, st.Count);

            ST <int, int> st2 = new ST <int, int>();

            st2[3]  = 22;
            st2[2]  = 33;
            st2[99] = 44;
            st2[2]  = 55;
            st2.Delete(3);
            Assert.IsFalse(st2.Contains(3));
            st2[3] = 101;
            Assert.AreEqual(101, st2[3]);
            st2.Delete(99);
            int dummy = st2[99]; // generate null exception
        }
示例#3
0
        /// <summary>
        /// Initializes a graph from a file using the specified delimiter.
        /// Each line in the file contains
        /// the name of a vertex, followed by a list of the names
        /// of the vertices adjacent to that vertex, separated by the delimiter.
        /// </summary>
        /// <param name="lines">array of string lines</param>
        /// <param name="delimiter">delimiter the delimiter between fields</param>
        public SymbolGraph(IList <string> lines, char delimiter)
        {
            _st = new ST <string, Integer>();

            // First pass builds the index by reading strings to associate
            // distinct strings with an index
            // while (in.hasNextLine()) {
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in a)
                {
                    if (!_st.Contains(word))
                    {
                        _st.Put(word, _st.Size());
                    }
                }
            }

            Console.WriteLine("Done reading");

            // inverted index to get string keys in an aray
            _keys = new string[_st.Size()];
            foreach (var name in _st.Keys())
            {
                _keys[_st.Get(name)] = name;
            }

            // second pass builds the graph by connecting first vertex on each
            // line to all others
            G = new Graph(_st.Size());
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                int v = _st.Get(a[0]);
                for (var i = 1; i < a.Length; i++)
                {
                    int w = _st.Get(a[i]);
                    G.AddEdge(v, w);
                }
            }
        }
示例#4
0
        private readonly ST<string, Integer> _st; // string -> index

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a graph from a file using the specified delimiter.
        /// Each line in the file contains
        /// the name of a vertex, followed by a list of the names
        /// of the vertices adjacent to that vertex, separated by the delimiter.
        /// </summary>
        /// <param name="lines">array of string lines</param>
        /// <param name="delimiter">delimiter the delimiter between fields</param>
        public SymbolGraph(IList<string> lines, char delimiter)
        {
            _st = new ST<string, Integer>();

            // First pass builds the index by reading strings to associate
            // distinct strings with an index
            // while (in.hasNextLine()) {
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in a)
                {
                    if (!_st.Contains(word))
                        _st.Put(word, _st.Size());
                }
            }

            Console.WriteLine("Done reading");

            // inverted index to get string keys in an aray
            _keys = new string[_st.Size()];
            foreach (var name in _st.Keys())
            {
                _keys[_st.Get(name)] = name;
            }

            // second pass builds the graph by connecting first vertex on each
            // line to all others
            G = new Graph(_st.Size());
            foreach (var line in lines)
            {
                var a = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                int v = _st.Get(a[0]);
                for (var i = 1; i < a.Length; i++)
                {
                    int w = _st.Get(a[i]);
                    G.AddEdge(v, w);
                }
            }
        }
示例#5
0
        // This method simply uses the helper method "contains" with some input validation
        // to return a true or false value.
        // Decided to stop looking here as Contains() does not care about word beginning or ending,
        // EX will return true for "Fox" in "Foxtrot".
        public static bool StringContains2(string little, string big)
        {
            // input validation
            if (string.IsNullOrEmpty(little) || string.IsNullOrEmpty(big))
            {
                return(false);
            }

            //String placeholders
            string SS; // Search String
            string ST; // Search Target


            // checks size of the strings together, assigns the smaller string to the search string.
            // if strings equal size, then comparison is made no need to go any further.
            if (little.Length == big.Length)
            {
                if (little == big)
                {
                    return(true);
                }
                return(false);
            }
            if (little.Length < big.Length)
            {
                SS = little.ToUpper();
                ST = big.ToUpper();
            }
            else
            {
                SS = big.ToUpper();
                ST = little.ToUpper();
            }

            return(ST.Contains(SS));
        }
示例#6
0
 /// <summary>
 /// Does the digraph contain the vertex named <tt>s</tt>?
 /// </summary>
 /// <param name="s">s the name of a vertex</param>
 /// <returns><tt>true</tt> if <tt>s</tt> is the name of a vertex, and <tt>false</tt> otherwise</returns>
 public bool Contains(string s)
 {
     return(_st.Contains(s));
 }