Ejemplo n.º 1
0
        static void Exercise1(IEnumerable <Tuple <ulong, int> > stream, Int32 l)
        {
            BigInteger sum = new BigInteger(0);

            var watch = Stopwatch.StartNew();

            foreach (Tuple <ulong, int> item in stream)
            {
                sum = HashFunctions.multiplyShift(item.Item1, l) + sum;
            }

            watch.Stop();

            Console.WriteLine("MultiplyShift sum:" + sum);
            Console.WriteLine("MultiplyShift elapsed:" + watch.ElapsedMilliseconds);

            sum = 0;

            watch.Restart();
            foreach (Tuple <ulong, int> item in stream)
            {
                sum = HashFunctions.multiplyMod(item.Item1, l) + sum;
            }
            watch.Stop();
            Console.WriteLine("MultiplyMod sum:" + sum);
            Console.WriteLine("MultiplyMod elapsed:" + watch.ElapsedMilliseconds);
        }
Ejemplo n.º 2
0
        // set(x, v): Skal sætte nøglen x til at have værdien v. Hvis x ikke allerede er i tabellen
        // så tilføjes den til tabellen med værdien v.
        public void set(UInt64 x, int v)
        {
            ulong hashVal;

            if (func == HashFuncType.mod)
            {
                hashVal = HashFunctions.multiplyMod(x, l);
            }
            else
            {
                hashVal = HashFunctions.multiplyShift(x, l);
            }



            if (hashT[(int)hashVal].Exists(keyValPair => keyValPair.Key.Equals((int)x)))
            {
                var index = hashT[(int)hashVal].FindIndex(keyValPair => keyValPair.Key.Equals((int)x));
                hashT[(int)hashVal][index].Value = v;
                // Console.WriteLine("Key found. Overwriting value");
            }
            else
            {
                // Console.WriteLine("Key wasn't found. Hashing k,v to hash table.");
                hashT[(int)hashVal].Add(new MutableKeyValuePair <int, int>((int)x, v));
            }
        }
Ejemplo n.º 3
0
        // increment(x, d): Skal lægge d til værdien tilhørende x. Hvis x ikke er i tabellen, skal
        // x tilføjes til tabellen med værdien d
        public void increment(UInt64 x, int d)
        {
            ulong hashVal;

            if (func == HashFuncType.mod)
            {
                hashVal = HashFunctions.multiplyMod(x, l);
            }
            else
            {
                hashVal = HashFunctions.multiplyShift(x, l);
            }


            if (hashT[(int)hashVal].Exists(keyValPair => keyValPair.Key.Equals((int)x)))
            {
                // Console.WriteLine("found");
                var index = hashT[(int)hashVal].FindIndex(keyValPair => keyValPair.Key.Equals((int)x));
                hashT[(int)hashVal][index].Value = hashT[(int)hashVal][index].Value + d;
            }
            else
            {
                // Console.WriteLine("not found");
                hashT[(int)hashVal].Add(new MutableKeyValuePair <int, int>((int)x, d));
            }
        }
Ejemplo n.º 4
0
        // get(x): Skal returnere den værdi, der tilhører nøglen x. Hvis x ikke er i tabellen skal der returneres 0.
        public int get(UInt64 x)
        {
            ulong hashVal;

            if (func == HashFuncType.mod)
            {
                hashVal = HashFunctions.multiplyMod(x, l);
            }
            else
            {
                hashVal = HashFunctions.multiplyShift(x, l);
            }


            if (hashT[(int)hashVal].Exists(keyValPair => keyValPair.Key.Equals((int)x)))
            {
                return(hashT[(int)hashVal].Find(keyValPair => keyValPair.Key.Equals((int)x)).Value);
            }
            return(0);
        }
Ejemplo n.º 5
0
        static void AnnouncementPart1(IEnumerable <Tuple <ulong, int> > stream)
        {
            Console.WriteLine("___ AnnouncementPart1 ____");
            // HERE WE RUN 3 EXPERIMENTS WITH l = 25, and m=16, m=128, m=1024.
            var epsilon = 0.001;
            //int t = (int) Math.Log2(8/(Math.Pow(epsilon, 2)));

            // EXPERIMENT 1, l = 25, m = 16, therefore t=4
            BigInteger sum   = new BigInteger(0);
            var        watch = Stopwatch.StartNew();

            foreach (Tuple <ulong, int> item in stream)
            {
                sum = HashFunctions.multiplyShift(item.Item1, 25) + sum;
            }

            watch.Stop();

            Console.WriteLine("[MultiplyShift]");
            Console.WriteLine("l = 25");
            Console.WriteLine("MultiplyShift sum:" + sum);
            Console.WriteLine("MultiplyShift elapsed:" + watch.ElapsedMilliseconds);
            Console.WriteLine();

            sum = 0;

            watch.Restart();
            foreach (Tuple <ulong, int> item in stream)
            {
                sum = HashFunctions.multiplyMod(item.Item1, 25) + sum;
            }
            watch.Stop();
            Console.WriteLine("[MultiplyMod]");
            Console.WriteLine("l = 25");
            Console.WriteLine("MultiplyMod sum:" + sum);
            Console.WriteLine("MultiplyMod elapsed:" + watch.ElapsedMilliseconds);
            Console.WriteLine();


            watch.Restart();
            double estimate = Algorithms.CountSketch(stream, 4);

            watch.Stop();
            Console.WriteLine("[CountSketch]\nl=25, m=16, t=4");
            Console.WriteLine("Estimate:" + estimate);
            Console.WriteLine("Elapsed time:" + watch.ElapsedMilliseconds);
            Console.WriteLine();

            watch.Restart();
            estimate = Algorithms.CountSketch(stream, 7);
            watch.Stop();
            Console.WriteLine("[CountSketch]\nl=25, m=128, t=7");
            Console.WriteLine("Estimate:" + estimate);
            Console.WriteLine("Elapsed time:" + watch.ElapsedMilliseconds);
            Console.WriteLine();

            watch.Restart();
            estimate = Algorithms.CountSketch(stream, 10);
            watch.Stop();
            Console.WriteLine("[CountSketch]\nl=25, m=1024, t=10");
            Console.WriteLine("Estimate:" + estimate);
            Console.WriteLine("Elapsed time:" + watch.ElapsedMilliseconds);
            Console.WriteLine();
        }