Exemple #1
0
        private IEnumerable <Solution> Cover(int k, SearchData searchData)
        {
            try
            {
                if (k == 0)
                {
                    RaiseStarted();
                }

                if (IsCancelled())
                {
                    RaiseCancelled();
                    yield break;
                }

                RaiseSearchStep(searchData.IterationCount, searchData.CurrentSolution.RowIndexes);
                searchData.IncrementIterationCount();

                if (MatrixIsEmpty(searchData.Root))
                {
                    if (searchData.CurrentSolution.RowIndexes.Any())
                    {
                        searchData.IncrementSolutionCount();
                        var solutionIndex = searchData.SolutionCount - 1;
                        var solution      = searchData.CurrentSolution;
                        RaiseSolutionFound(solution, solutionIndex);
                        yield return(solution);
                    }

                    yield break;
                }

                var c = ChooseColumnWithLeastRows(searchData.Root);
                CoverColumn(c);

                for (var r = c.Down; r != c; r = r.Down)
                {
                    if (IsCancelled())
                    {
                        RaiseCancelled();
                        yield break;
                    }

                    searchData.PushCurrentSolutionRowIndex(r.RowIndex);

                    for (var j = r.Right; j != r; j = j.Right)
                    {
                        CoverColumn(j.ListHeader);
                    }

                    var recursivelyFoundSolutions = Cover(k + 1, searchData);
                    foreach (var solution in recursivelyFoundSolutions)
                    {
                        yield return(solution);
                    }

                    for (var j = r.Left; j != r; j = j.Left)
                    {
                        UncoverColumn(j.ListHeader);
                    }

                    searchData.PopCurrentSolutionRowIndex();
                }

                UncoverColumn(c);
            }
            finally
            {
                if (k == 0)
                {
                    RaiseFinished();
                }
            }
        }