Пример #1
0
        public static void Test11()
        {
            uint m = n, a = 1, z = n;

            SimpleDepartment[] deps = new SimpleDepartment[n];
            for (uint i = 0; i < n - 1; ++i)
            {
                deps[i] = new SimpleDepartment(i + 1, 0, i + 2);
            }
            deps[n - 1] = new SimpleDepartment(m, 0, 1);
            Departments dps         = new Departments(n, m, a, z, deps);

            bool[]     isInfExpected  = new bool[n];
            bool[]     isVisited      = new bool[n];
            uint[][][] stampsExpected = new uint[n][][];
            for (uint i = 0; i < n - 1; ++i)
            {
                isInfExpected[i]     = false;
                isVisited[i]         = true;
                stampsExpected[i]    = new uint[1][];
                stampsExpected[i][0] = new uint[i + 1];
                for (uint j = 0; j < i + 1; ++j)
                {
                    stampsExpected[i][0][j] = j + 1;
                }
            }
            isInfExpected[n - 1]  = false;
            isVisited[n - 1]      = true;
            stampsExpected[n - 1] = new uint[0][];
            isTest11Correct       = true;
            var    pnow = DateTime.Now;
            Thread t1   = StartTheThread(0, n / 2, isInfExpected, isVisited, stampsExpected, dps);
            Thread t2   = StartTheThread(n / 2, n, isInfExpected, isVisited, stampsExpected, dps);

            t1.Join();
            t2.Join();
            var duration = DateTime.Now - pnow;

            Console.WriteLine("Query time:");
            Console.WriteLine(duration);
            Console.WriteLine("Is correct:");
            Console.WriteLine(isTest11Correct);
        }
Пример #2
0
        public static void Test10()
        {
            uint m = n, a = 1, z = n;

            SimpleDepartment[] deps = new SimpleDepartment[n];
            for (uint i = 0; i < n - 1; ++i)
            {
                deps[i] = new SimpleDepartment(i + 1, 0, i + 2);
            }
            deps[n - 1] = new SimpleDepartment(m, 0, 1);
            Departments dps         = new Departments(n, m, a, z, deps);

            bool[]     isInfExpected  = new bool[n];
            bool[]     isVisited      = new bool[n];
            uint[][][] stampsExpected = new uint[n][][];
            for (uint i = 0; i < n - 1; ++i)
            {
                isInfExpected[i]     = false;
                isVisited[i]         = true;
                stampsExpected[i]    = new uint[1][];
                stampsExpected[i][0] = new uint[i + 1];
                for (uint j = 0; j < i + 1; ++j)
                {
                    stampsExpected[i][0][j] = j + 1;
                }
            }
            isInfExpected[n - 1]  = false;
            isVisited[n - 1]      = true;
            stampsExpected[n - 1] = new uint[0][];
            var  pnow     = DateTime.Now;
            bool tmp      = MyTestHelper(isInfExpected, isVisited, stampsExpected, dps, n);
            var  duration = DateTime.Now - pnow;

            Console.WriteLine("Query time:");
            Console.WriteLine(duration);
            Console.WriteLine("Is correct:");
            Console.WriteLine(tmp);
        }
Пример #3
0
        private void moveFront()
        {
            uint depNum = A;

            while (true)
            {
                if (depNum == Z)
                {
                    zVisited = true;
                    break;
                }
                SimpleDepartment dep  = departments[depNum];
                bool             way1 = false;
                if (dep.ruleType == RuleType.unconditional ||
                    (dep.ruleType == RuleType.conditional &&
                     stamps.Contains(dep.S)))
                {
                    if (dep.I != SimpleDepartment.noActions)
                    {
                        if (stamps.Add(dep.I))
                        {
                            controlSum += dep.I;
                        }
                    }
                    if (dep.J != SimpleDepartment.noActions)
                    {
                        if (stamps.Remove(dep.J))
                        {
                            controlSum -= dep.J;
                        }
                    }
                    way1 = true;
                }
                else
                {
                    if (dep.T != SimpleDepartment.noActions)
                    {
                        if (stamps.Add(dep.T))
                        {
                            controlSum += dep.T;
                        }
                    }
                    if (dep.R != SimpleDepartment.noActions)
                    {
                        if (stamps.Remove(dep.R))
                        {
                            controlSum -= dep.R;
                        }
                    }
                }
                int tmp = checkInf(depNum);
                if (tmp != 0)
                {
                    moveBack(depNum, tmp);
                    break;
                }
                movingStack.Push(depNum);
                SortedSet <uint> stampsCopy = new SortedSet <uint>();
                stampsCopy.UnionWith(stamps);
                requests[depNum].AddLast(new KeyValuePair <uint, SortedSet <uint> >(controlSum, stampsCopy));
                if (way1)
                {
                    depNum = dep.K;
                }
                else
                {
                    depNum = dep.P;
                }
            }
        }
Пример #4
0
 /// <summary>
 /// Initializes instance of Departments class
 /// </summary>
 /// <param name="n">Number of deparments.</param>
 /// <param name="m">Number of stamps.</param>
 /// <param name="a">Id of the start department.</param>
 /// <param name="z">Id of the finish department.</param>
 /// <param name="departments"></param>
 public Departments(uint n, uint m, uint a, uint z, SimpleDepartment[] departments)
 {
     if (n == 0)
     {
         throw new DepartmentException("Incorrect number of departments.");
     }
     if (m == 0)
     {
         throw new DepartmentException("Incorrect number of stamps.");
     }
     if (a == 0 || a > n)
     {
         throw new DepartmentException("Invalid starting department number.");
     }
     if (z == 0 || z > n)
     {
         throw new DepartmentException("Invalid ending department number.");
     }
     if (departments.Length != n)
     {
         throw new DepartmentException("Invalid department array size.");
     }
     for (int i = 0; i < n; ++i)
     {
         SimpleDepartment dep = departments[i];
         if (dep.I > m)
         {
             throw new DepartmentException($"departments[{i}].I > m.");
         }
         if (dep.J > m)
         {
             throw new DepartmentException($"departments[{i}].J > m.");
         }
         if (dep.K > n || dep.K == 0)
         {
             throw new DepartmentException($"Index at departments[{i}].K out of range.");
         }
         if (dep.ruleType == RuleType.conditional)
         {
             if (dep.S > n || dep.S == 0)
             {
                 throw new DepartmentException($"Index at departments[{i}].S out of range.");
             }
             if (dep.T > m)
             {
                 throw new DepartmentException($"departments[{i}].P > m.");
             }
             if (dep.R > m)
             {
                 throw new DepartmentException($"departments[{i}].T > m.");
             }
             if (dep.P > n || dep.P == 0)
             {
                 throw new DepartmentException($"Index at departments[{i}].R out of range.");
             }
         }
     }
     N = n;
     M = m;
     A = a;
     Z = z;
     this.departments = new SimpleDepartment[N + 1];
     departments.CopyTo(this.departments, 1);
     movingStack = new Stack <uint>();
     CalculateRequests();
 }