static void Main(string[] args)
        {
            var    n           = 8000;
            var    multiplyBy2 = 5;
            var    repeatTimes = 5;
            double lastTime    = -1;

            Console.WriteLine("n\ttime\tratio");
            for (var i = 0; i < multiplyBy2; i++)
            {
                Console.Write(n + "\t");
                long timeSum = 0;
                for (var j = 0; j < repeatTimes; j++)
                {
                    var st = new SequentialSearchST <string, int>();
                    var sw = Stopwatch.StartNew();
                    FrequencyCounter.MostFrequentlyWord("tale.txt", n, 0, st);
                    sw.Stop();
                    timeSum += sw.ElapsedMilliseconds;
                }
                timeSum /= repeatTimes;
                Console.Write(timeSum + "\t");
                if (lastTime < 0)
                {
                    Console.WriteLine("--");
                }
                else
                {
                    Console.WriteLine(timeSum / lastTime);
                }
                lastTime = timeSum;
                n       *= 2;
            }
        }
Example #2
0
        public GraphBase(int v)
        {
            #region node char to int mapping

            NodeFromCharToIntMapping = new SequentialSearchST<char, int?>();
            NodeFromCharToIntMapping.Put('A', 0);
            NodeFromCharToIntMapping.Put('B', 1);
            NodeFromCharToIntMapping.Put('C', 2);
            NodeFromCharToIntMapping.Put('D', 3);
            NodeFromCharToIntMapping.Put('E', 4);
            NodeFromCharToIntMapping.Put('F', 5);
            NodeFromCharToIntMapping.Put('G', 6);
            NodeFromCharToIntMapping.Put('H', 7);
            NodeFromCharToIntMapping.Put('I', 8);
            NodeFromCharToIntMapping.Put('J', 9);
            NodeFromCharToIntMapping.Put('K', 10);
            NodeFromCharToIntMapping.Put('L', 11);

            #endregion

            _V = v;
            _E = 0;
            Al = new LinkedList<Edge>[v];
            for (var i = 0; i < v; i++)
            {
                Al[i] = new LinkedList<Edge>();
            }
        }
        public void Delete_Performance_Test()
        {
            int size = 9000;
            var st = new SequentialSearchST<int, string>();
            for (int i = 0; i < size; i++)
            {
                st.Put(i, i.ToString());
            }

            using (new PerformanceWatch(new CDriveLocationLogger(), "recursive"))
            {
                for (int i = 0; i < size; i++)
                {
                    st.Delete_Recursive(i);
                }
            }

            var st2 = new SequentialSearchST<int, string>();
            for (int i = 0; i < size; i++)
            {
                st2.Put(i, i.ToString());
            }

            using (new PerformanceWatch(new CDriveLocationLogger(), "non recursive"))
            {
                for (int i = 0; i < size; i++)
                {
                    st2.Delete(i);
                }
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            // 事实上指的是最近一次访问的键,而非访问最频繁的。

            var sr   = new StreamReader(File.OpenRead("tale.txt"));
            var data = sr.ReadToEnd().Split(new char[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var repeatTimes = 1;

            Console.WriteLine("BinarySearchST");
            Console.WriteLine("Origin\tCached\tRatio");
            long bstTimes = 0, bstcTimes = 0;

            var bst  = new BinarySearchST <string, int>();
            var bstc = new BinarySearchSTCached <string, int>();

            bstTimes  += SearchCompare.Time(bst, data);
            bstcTimes += SearchCompare.Time(bstc, data);
            Console.WriteLine(bstTimes / repeatTimes + "\t" + bstcTimes / repeatTimes + "\t" + (double)bstTimes / bstcTimes);

            Console.WriteLine("SequentialSearchST");
            Console.WriteLine("Origin\tCached\tRatio");
            long sstTimes = 0, sstcTimes = 0;

            var sst  = new SequentialSearchST <string, int>();
            var sstc = new SequentialSearchSTCached <string, int>();

            sstTimes  += SearchCompare.Time(sst, data);
            sstcTimes += SearchCompare.Time(sstc, data);
            Console.WriteLine(sstTimes + "\t" + sstcTimes + "\t" + (double)sstTimes / sstcTimes);
        }
Example #5
0
        private SequentialSearchST[] st = null; //散列数组

        public SeqarateChainHashST()
        {
            this.M = 97;//M越大,每个链表节点越少。97是一个平衡的数字
            st = new SequentialSearchST[M];
            for (int i = 0; i < st.Length; i++)
                st[i] = new SequentialSearchST();
        }
        public void Delete_Olny_One_Node_List_Test()
        {
            var st = new SequentialSearchST<string, string>();
            st.Put("a", "0");

            st.Delete("a");
            Assert.AreEqual(0, st.Size());
        }
Example #7
0
        public void SequentialSearchStFrequency()
        {
            var st     = new SequentialSearchST <string, int>();
            var result = FrequencyCounter(1, st);

            Assert.AreEqual("of", result.maxWord);
            Assert.AreEqual(10, result.maxCount);
        }
 public SeparateChainingHashST(int size)
 {
     this.stSize = size;
     st          = new SequentialSearchST <TKey, TValue> [size];
     for (int i = 0; i < stSize; i++)
     {
         st[i] = new SequentialSearchST <TKey, TValue>();
     }
 }
Example #9
0
 public SeparateChainingHashST(int m)
 {
     this.m = m;
     st     = new SequentialSearchST <Key, Value> [m];
     for (int i = 0; i < m; i++)
     {
         st[i] = new SequentialSearchST <Key, Value>();
     }
 }
Example #10
0
 public SeparaterHashST(int M)
 {
     //创建m条链表
     this.m = M;
     st     = new SequentialSearchST <Key, Value> [M];
     for (int i = 0; i < m; i++)
     {
         st[i] = new SequentialSearchST <Key, Value>();
     }
 }
Example #11
0
        public ChainHashSet(int capacity)
        {
            Capacity = capacity;
            _chains  = new SequentialSearchST <TKey, TValue> [capacity];

            for (int i = 0; i < capacity; i++)
            {
                _chains[i] = new SequentialSearchST <TKey, TValue>();
            }
        }
Example #12
0
    public void SequentialTest()
    {
        SequentialSearchST <string, int> st = new SequentialSearchST <string, int>();

        for (int i = 0; i < v.Length; i++)
        {
            st.Put(k[i], v[i]);
        }
        st.DisPlay();
        st.Delete("b");
        st.DisPlay();
    }
Example #13
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyST.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in       = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();


            var st = new SequentialSearchST <string, string>();


            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var key   = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key, value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(st.Get(item));
            }
            Console.ReadLine();
        }
Example #14
0
        public void SequentialSearchSTTest2()
        {
            // testing indexer semantics
            SequentialSearchST <int, string> st = new SequentialSearchST <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);

            SequentialSearchST <int, int> st2 = new SequentialSearchST <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
        }
Example #15
0
        static void Main(string[] args)
        {
            // 官方实现见:https://algs4.cs.princeton.edu/31elementary/SequentialSearchST.java.html
            var input = "S E A R C H E X A M P L E".Split(' ');
            var st    = new SequentialSearchST <string, int>();

            for (var i = 0; i < input.Length; i++)
            {
                st.Put(input[i], i);
            }
            foreach (var s in st.Keys())
            {
                Console.WriteLine(s + " " + st.Get(s));
            }
        }
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            var fieName = string.Empty;
            switch (fileNumber)
            {
                case "1":
                    fieName = "tinyST.txt";
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();

            var st = new SequentialSearchST<string,string>();

            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                var key = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key,value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(st.Get(item));
            }
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            var n = 100000;

            Console.WriteLine("n=" + n);
            var sst = new SequentialSearchST <string, int> [4];
            var bst = new BinarySearchST <string, int> [4];

            for (var i = 0; i < 4; i++)
            {
                bst[i] = new BinarySearchST <string, int>();
                sst[i] = new SequentialSearchST <string, int>();
            }
            Console.WriteLine("BinarySearch");
            Test(bst, n);
            Console.WriteLine();
            Console.WriteLine("SequentialSearch");
            Test(sst, n);
        }
Example #18
0
        public void SequentialSearchSTTest1()
        {
            // testing Get/Put semantics
            SequentialSearchST <string, int> st = new SequentialSearchST <string, int>();

            Assert.IsTrue(st.IsEmpty);

            // making sure we can delete all from ST
            st.Put("asd", 355);
            st.Put("dsd", 25);
            st.Put("esd", 15);
            st.Delete("esd");
            st.Delete("dsd");
            st.Delete("asd");
            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
        }
Example #19
0
        static void Main(string[] args)
        {
            ST <string, string> st;

            st = new SequentialSearchST <string, string>();
            st.Put("a", "1");
            st.Put("b", "2");
            int size = st.Size();

            st.Delete("a");
            size = st.Size();

            SortedST <string, string> sortedSt;

            sortedSt = new BST <string, string>();
            sortedSt.Put("r", "1");
            sortedSt.Put("b", "2");
            sortedSt.Put("h", "3");
            sortedSt.Put("t", "4");
            sortedSt.Delete("b");

            st = new SeparateChainingHashST <string, string>();
            st.Put("f", "1");
            st.Put("a", "3");
            st.Put("d", "4");
            st.Put("h", "9");
            st.Put("k", "8");
            var v = st.Get("h");

            st = new LinearProbingHashST <string, string>();
            st.Put("f", "1");
            st.Put("a", "3");
            st.Put("d", "4");
            st.Put("h", "9");
            st.Put("u", "8");
            v = st.Get("u");

            Console.ReadKey();
        }
Example #20
0
        public SymbolGraph(string stream, char sp)
        {
            st = new SequentialSearchST <string, Int32>();
            string[] a = stream.Split('\n');

            for (int i = 0; i < a.Length; i++)
            {
                string[] b = a[i].Split(sp);
                for (int j = 0; j < b.Length; j++)
                {
                    if (!st.contains(b[j]))
                    {
                        st.put(b[j], st.size() + 1); // avoid key 0
                        UnityEngine.Debug.Log($"{st.size()}:{b[j]}");
                    }
                }
            }

            keys = new string[st.size()];

            foreach (string name in st.allkeys())
            {
                keys[st.get(name) - 1] = name;
                UnityEngine.Debug.Log($"{st.get(name)-1}:{name}");
            }

            G = new Graph(st.size());

            for (int i = 0; i < a.Length; i++)
            {
                string[] b = a[i].Split(sp);
                int      v = st.get(b[0]) - 1;
                for (int j = 1; j < b.Length; j++)
                {
                    G.addEdge(v, st.get(b[j]) - 1);
                }
            }
        }
Example #21
0
        public void TestSequentialSearchST()
        {
            SequentialSearchST<string, int> st = new SequentialSearchST<string, int>();

            int amounts = 20;
            string[] strs = new string[amounts];
            Random rand = new Random();
            for (int i = 0; i < amounts; i++)
            {
                strs[i] = Convert.ToChar(rand.Next(97, 122)).ToString();
                st.Put(strs[i], i);
            }
            StringBuilder sb0 = new StringBuilder();
            foreach (var str in strs)
                sb0.Append(str + " ");
            Debug.WriteLine(sb0.ToString());

            StringBuilder sb = new StringBuilder();
            foreach (var item in st.Keys())
                sb.Append(st.Get(item) + " ");
            Debug.WriteLine(sb.ToString());

            Assert.AreEqual(strs.Distinct().Count(), st.Keys().Count());
        }
        private SequentialSearchST<string, int?> CreateSTWithNullableValueType()
        {
            var st = new SequentialSearchST<string, int?>();
            st.Put("a", 0);
            st.Put("b", 1);
            st.Put("c", 2);

            return st;
        }
        private SequentialSearchST<string, string> CreateST()
        {
            var st = new SequentialSearchST<string, string>();
            st.Put("a", "0");
            st.Put("b", "1");
            st.Put("c", "2");

            return st;
        }
 public void SequentialSearchST_Test()
 {
     var st = new SequentialSearchST<string, int?>();
 }
 public void SequentialSearchST_Not_Nullable_Exception_Test()
 {
     var st = new SequentialSearchST<string, int>();
 }
        public void IsEmpty_True_Test()
        {
            var st = new SequentialSearchST<string, string>();

            Assert.IsTrue(st.IsEmpty());
        }