Insert() public méthode

public Insert ( IComparable key, object value ) : void
key IComparable
value object
Résultat void
Exemple #1
0
        static void Main()
        {
            SkipList skipList = new SkipList();

            int[] fibs = { 1, 1, 2, 3, 5, 8, 13, 21 };

            foreach (var fib in fibs)
            {
                skipList.Insert(fib);
            }

            Console.WriteLine(skipList.Contains(8));

            if (skipList.Remove(8))
            {
                Console.WriteLine(skipList.Contains(8));
            }
            else
            {
                Console.WriteLine("Remove failed!");
            }

            foreach (var skipValue in skipList.Enumerate())
            {
                Console.WriteLine("SkipList enumeration: {0}", skipValue);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            SkipList <int> list   = new SkipList <int>();
            Random         random = new Random(42);

            for (int i = 0; i < 10; i++)
            {
                list.Insert(random.Next(0, 100));
            }

            int[] array = list.Test();

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(array[i].ToString());
            }

            list.Delete(12);
            list.Delete(14);
            list.Delete(16);
            list.Delete(17);
            list.Delete(26);
            list.Delete(51);
            list.Delete(52);
            list.Delete(66);
            list.Delete(72);
            list.Delete(76);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var random = new Random();

            var list = new SkipList();

            list.Insert(49);
            list.Print();

            for (int i = 0; i < 5; i++)
            {
                var val = random.Next(0, 99);
                Console.WriteLine($"Inserting {val}...");
                list.Insert(val);
                list.Print();
            }

            Console.WriteLine("List contains '8': " + list.Search(8));
            Console.WriteLine("List contains '49': " + list.Search(49));
            list.Delete(49);
            Console.WriteLine("List contains '49': " + list.Search(49));

            list.Print();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            /* We will test the skip list with many keys. */
            int n = 50000;

            /* Prepare the keys and the values. Make sure the keys are unique. */
            Console.WriteLine("Preparing {0} random unique numbers...", n);
            int[] uniqueNumbers = new int[n];
            Int32[] keys = new Int32[n];
            object[] values = new object[n];
            Random r = new Random();
            bool unique = true;
            int x = 0;
            for (int i=0; i<n; i++)
            {
                /* Each number we generate has to be unique. So after we generate a new
                 * number, we compare it with all previously generated numbers and if it is
                 * now unique then we regenerate it. */
                do
                {
                    x = r.Next();
                    unique = true;
                    for (int j=0; j<i; j++)
                    {
                        if (uniqueNumbers[j] == x)
                        {
                            unique = false;
                            break;
                        }
                    }
                } while (!unique);
                uniqueNumbers[i] = x;

                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}", x);

                keys[i] = Int32.Parse(sb.ToString());
                values[i] = sb.ToString();

                Console.Write("\r{0}", i);
            }
            Console.WriteLine("\n{0} random numbers generated.", n);

            /* ------------------------------------------------
             * Test and measure execution time for skip list.
             * ------------------------------------------------
             */
            SkipList slist = new SkipList();

            DateTime start = DateTime.Now;

            /* Make insertions. */
            for (int i=0; i<n; i++)
                slist.Insert(keys[i], values[i]);

            /* Make searches. */
            for (int i=0; i<n; i++)
            {
                object obj = slist.Find(keys[i]);
                if (obj == null)
                    throw new Exception("Null value for key ->"+keys[i]+"<-");

                string stringValue = (string)obj;
                if (!stringValue.Equals(values[i]))
                    throw new Exception("Wrong value for key ->"+keys[i]+"<-");
            }

            /* Make removals. */
            for (int i=0; i<n; i++)
                slist.Remove(keys[i]);

            DateTime stop = DateTime.Now;

            Console.WriteLine("SkipList execution time: "+ (stop-start));

            /* ------------------------------------------------
             * Test and measure execution time for standard Hashtable.
             * ------------------------------------------------
             */
            SortedList stdSortedList = new SortedList();

            start = DateTime.Now;

            /* Make insertions. */
            for (int i=0; i<n; i++)
                stdSortedList.Add(keys[i], values[i]);

            /* Make searches. */
            for (int i=0; i<n; i++)
            {
                object obj = stdSortedList[keys[i]];
                if (obj == null)
                    throw new Exception("Null value for key ->"+keys[i]+"<-");

                string stringValue = (string)obj;
                if (!stringValue.Equals(values[i]))
                    throw new Exception("Wrong value for key ->"+keys[i]+"<-");
            }

            /* Make removals. */
            for (int i=0; i<n; i++)
                stdSortedList.Remove(keys[i]);

            stop = DateTime.Now;

            Console.WriteLine("Standard SortedList execution time: "+ (stop-start));
        }