Ejemplo n.º 1
0
        public void ListOfNumbersWithIncreasingDigits_WithNumbers0To2And3Digits_ReturnsCorrectResult()
        {
            // Arrange
            var characters = new List <char> {
                '0', '1', '2'
            };
            var numberOfDigits = 3;
            var expectedResult = new List <string>
            {
                "000",
                "001",
                "002",
                "011",
                "012",
                "022",
                "111",
                "112",
                "122",
                "222"
            };

            // Act
            var result = PermutationHelper.ListOfNumbersWithIncreasingDigits(numberOfDigits, characters);

            // Assert
            result.Should().BeEquivalentTo(expectedResult);
        }
Ejemplo n.º 2
0
        public void NumberOfPermutations_For022_Returns3()
        {
            // Arrange
            var number         = "022";
            var expectedResult = 3;

            // Act
            var result = PermutationHelper.NumberOfPermutations(number);

            // Assert
            result.Should().Be(expectedResult);
        }
    public static void Test()
    {
        List <int> test = new List <int>();

        for (int i = 0; i < 4; ++i)
        {
            test.Add(i + 1);
        }
        PermutationHelper <int> permutationHelper = new PermutationHelper <int>(test);
        int count = 0;

        foreach (List <int> oneSet in permutationHelper)
        {
            foreach (var iter in oneSet)
            {
                Console.Write(iter + ",");
            }
            count += 1;
            Console.WriteLine();
        }

        Console.WriteLine("total " + count);
        //Console.ReadLine();
    }
Ejemplo n.º 4
0
        private async Task <bool> CheckAndUpdateAsync(CancellationToken ct)
        {
            if (Hashes.Items.Count == 0)
            {
                return(false);
            }

            int[] singlePermutation = new int[Hashes.Items.Count];
            for (int i = 0; i < Hashes.Items.Count; i++)
            {
                singlePermutation[i] = i;
            }

            IEnumerable <IEnumerable <int> > permutations;

            if (DontChangeHashOrder.IsChecked.Value)
            {
                permutations = new List <IEnumerable <int> >(1);
                List <IEnumerable <int> > single = permutations as List <IEnumerable <int> >;
                single.Add(singlePermutation);
            }
            else
            {
                permutations = PermutationHelper.GetPermutations <int>(singlePermutation, singlePermutation.Length);
            }
            // split the permuations into chunks that we run sequenctially to avoid out of memory exception when trying to
            // run too many permutations in parallel
            var chunks = PermutationHelper.Split <IEnumerable <int> >(permutations, 1024);

            bool          foundMatch = false;
            List <string> results    = new List <string>();

            // check localities (0..4) and Locality Indicator (locality as bitfield) (1, 2, 4, 8, 16)
            int[] startValues = new int[] { 0, 1, 2, 3, 4, 8, 16 };
            foreach (int startValue in startValues)
            {
                foreach (var chunk in chunks)
                {
                    IEnumerable <Task <List <string> > > computePermutatioQuery =
                        from permutation in chunk select CheckAndUpdateAsync((string)ListOfAlgorithms.SelectedItem, startValue, Hashes.Items.Cast <string>().ToArray <string>(), permutation.ToArray <int>(), ExpectedResultHash.Text.Trim());

                    List <Task <List <string> > > computePermutation = computePermutatioQuery.ToList();

                    while (computePermutation.Count > 0)
                    {
                        Task <List <string> > finishedPermutation = await Task.WhenAny(computePermutation);

                        computePermutation.Remove(finishedPermutation);
                        results    = await finishedPermutation;
                        foundMatch = results.Count > 0;
                        if (foundMatch || ct.IsCancellationRequested)
                        {
                            // cancel the rest
                            cts.Cancel();
                            break;
                        }
                    }
                    if (foundMatch || ct.IsCancellationRequested)
                    {
                        break;
                    }
                }
                if (foundMatch || ct.IsCancellationRequested)
                {
                    break;
                }
            }

            if (foundMatch)
            {
                foreach (string result in results)
                {
                    ResultHashes.Items.Add(result);
                }
            }
            return(foundMatch);
        }