Example #1
0
        //TODO: implement the zoekmodus hash check, the zoekmodus termination, the c# lock
        static void Main(string[] args)
        {
            string readLine = Console.ReadLine();
            string[] splitLine = readLine.Split();

            int lockType = Int32.Parse(splitLine[0]);       //0 for homemade lock, 1 for c# lock
            int lower = Int32.Parse(splitLine[1]);          //inclusive lower bound
            int upper = Int32.Parse(splitLine[2]);          //exclusive upper bound
            int modulus = Int32.Parse(splitLine[3]);        //modulus for the m-test
            int p = Int32.Parse(splitLine[4]);              //amount of threads the program should start
            int u = Int32.Parse(splitLine[5]);              //what mode should the program run, 0, count, 1, list, 2, search
            string h = splitLine[6];                        //the hash used in search mode

            //Create the lock to be used by the program. The lock instance also creates an instance of ProgramModus inside itself to write away ansers
            customLock programLock;
            if (lockType == 0)
                programLock = new tasLock(u);
            else
                programLock = new cSharpLock(u);

            //create a list of threads here
            Thread[] ts = new Thread[p];

            //The maximum amount of threads we can make based on the lower and upper bounds
            int maxThreads = Math.Min(upper - lower, p);
            int leftover;
            //if p is larger than the bounds, leftover will screw up the assigning of numbers
            if(p < upper - lower)
                leftover = (upper - lower) % p;
            else
                leftover = -1;
            //used to divide the remainder of (upper - lower) % p
            int lastUpper = lower;

            for(int t = 0; t < maxThreads; t++)
            {
                //Calculate the range of numbers a thread has to handle
                int segmentReach = Math.Max(1, (upper - lower) / p);
                int[] segmentBounds = new int[2];

                segmentBounds[0] = lastUpper;

                if (maxThreads - (t + 1) < leftover)
                    segmentBounds[1] = lower + (t + 1) * (segmentReach) + 1;
                else
                    segmentBounds[1] = lower + (t + 1) * segmentReach;

                lastUpper = segmentBounds[1];

                //create thread t
                DoeMProef dmp = new DoeMProef();
                dmp.segment = segmentBounds;  //the bounds the thread has to handle
                dmp.programLock = programLock;
                dmp.mod = modulus;

                //Create the right method, doMP for lijst and tel, doHashProef for zoek
                ThreadStart tDelegate;
                if (u == 2)
                {
                    dmp.hash = h;
                    tDelegate = new ThreadStart(dmp.doHashProef);
                }
                else
                    tDelegate = new ThreadStart(dmp.doMP);

                ts[t] = new Thread(tDelegate);

                //this terminates the threads when the program closes
                ts[t].IsBackground = true;
            }

            //start threads
            //for(int t = 0; t < p; t++)
            for(int t = 0; t < maxThreads; t++ )
            {
                ts[t].Start();
            }

            //join threads
            //for (int t = 0; t < p; t++)
            for (int t = 0; t < maxThreads; t++)
            {
                ts[t].Join();
            }

            //print answer if the zoek modus hasn't found anything
            //if (u == 2 && accountToBlock == -1)
                //Console.WriteLine(-1);
            PrintAnswer(u);
            Console.ReadLine();
        }
Example #2
0
        //TODO: implement the zoekmodus hash check, the zoekmodus termination, the c# lock

        static void Main(string[] args)
        {
            string readLine = Console.ReadLine();

            string[] splitLine = readLine.Split();

            int    lockType = Int32.Parse(splitLine[0]);    //0 for homemade lock, 1 for c# lock
            int    lower    = Int32.Parse(splitLine[1]);    //inclusive lower bound
            int    upper    = Int32.Parse(splitLine[2]);    //exclusive upper bound
            int    modulus  = Int32.Parse(splitLine[3]);    //modulus for the m-test
            int    p        = Int32.Parse(splitLine[4]);    //amount of threads the program should start
            int    u        = Int32.Parse(splitLine[5]);    //what mode should the program run, 0, count, 1, list, 2, search
            string h        = splitLine[6];                 //the hash used in search mode

            //Create the lock to be used by the program. The lock instance also creates an instance of ProgramModus inside itself to write away ansers
            customLock programLock;

            if (lockType == 0)
            {
                programLock = new tasLock(u);
            }
            else
            {
                programLock = new cSharpLock(u);
            }


            //create a list of threads here
            Thread[] ts = new Thread[p];

            //The maximum amount of threads we can make based on the lower and upper bounds
            int maxThreads = Math.Min(upper - lower, p);
            int leftover;

            //if p is larger than the bounds, leftover will screw up the assigning of numbers
            if (p < upper - lower)
            {
                leftover = (upper - lower) % p;
            }
            else
            {
                leftover = -1;
            }
            //used to divide the remainder of (upper - lower) % p
            int lastUpper = lower;

            for (int t = 0; t < maxThreads; t++)
            {
                //Calculate the range of numbers a thread has to handle
                int   segmentReach  = Math.Max(1, (upper - lower) / p);
                int[] segmentBounds = new int[2];

                segmentBounds[0] = lastUpper;

                if (maxThreads - (t + 1) < leftover)
                {
                    segmentBounds[1] = lower + (t + 1) * (segmentReach) + 1;
                }
                else
                {
                    segmentBounds[1] = lower + (t + 1) * segmentReach;
                }

                lastUpper = segmentBounds[1];

                //create thread t
                DoeMProef dmp = new DoeMProef();
                dmp.segment     = segmentBounds; //the bounds the thread has to handle
                dmp.programLock = programLock;
                dmp.mod         = modulus;

                //Create the right method, doMP for lijst and tel, doHashProef for zoek
                ThreadStart tDelegate;
                if (u == 2)
                {
                    dmp.hash  = h;
                    tDelegate = new ThreadStart(dmp.doHashProef);
                }
                else
                {
                    tDelegate = new ThreadStart(dmp.doMP);
                }


                ts[t] = new Thread(tDelegate);

                //this terminates the threads when the program closes
                ts[t].IsBackground = true;
            }

            //start threads
            //for(int t = 0; t < p; t++)
            for (int t = 0; t < maxThreads; t++)
            {
                ts[t].Start();
            }

            //join threads
            //for (int t = 0; t < p; t++)
            for (int t = 0; t < maxThreads; t++)
            {
                ts[t].Join();
            }

            //print answer if the zoek modus hasn't found anything
            //if (u == 2 && accountToBlock == -1)
            //Console.WriteLine(-1);
            PrintAnswer(u);
            Console.ReadLine();
        }