Example #1
0
        /// <summary>Initializes an instance of the Patterns.</summary>
        /// <param name="worker">The BackgroundWorker in wich Processing is run.</param>
        /// <param name="sequence">The 'names' sequence.</param>
        /// <param name="startIndex">The index in the inputArray at which scanning begins.</param>
        /// <param name="strictLength">The precise length of range of inputArray for scanning.</param>
        /// <param name="strictFreqs">The external array of strict frequencies; needed to avoid memory overallotment.</param>
        /// <param name="loopsPerStep">The amount of loops per one progress bar step; -1 if without progress bar steps.</param>
        /// <param name="maxFreqNumber">Return number of Pattern with max Frequence: if init value = -1, then no return.</param>
        /// <param name="isFirstCall">True if method is called first time in cycle; otherwise, false.</param>
        /// <param name="isDrawPlot">True if plot is drawing; otherwise, false.</param>
        /// <returns>Maximum frequency value in the instance; if init maxFreqNumber param = -1, then 0.</returns>
        int Init(
            BackgroundWorker worker, byte[] sequence,
            int startIndex, int strictLength, int[] strictFreqs, int loopsPerStep, ref int maxFreqNumber,
            bool isFirstCall, bool isDrawPlot)
        {
            #region Comments

            /*
             * Patterns are generated from inputArray sequence on 2 steps.
             *
             * Step 1: Initialization strict frequency
             * We create a new collection with the maximum possible length and an temporary array of strict frequencies.
             * Index of each element for both collections is equal to absolute word number,
             * so we scan through the input sequence and increase strict frequency in array directly by indexing.
             *
             * Step 2: Initialization Frequency
             * From each pattern in collection we added the Frequency of all his Similars.
             * In ALL POSSIBLE mode we search all the patterns, because their Similars may have Frequency > 0;
             * in FROM INPUT mode we skip zero-frequency patterns.
             *
             * In TURBO mode all the Similars are keeping in memory after first generation,
             * so we should go through the given Similars collection and adding thier Frequency directly by word number, equls to index in this instance.
             * In ORDINARY mode we receive the sum of Frequencies of all Similars through single call of Similars.SumUp().
             *
             * So we receive the frequencies with mismatches for all the Patterns in this instance.
             *
             * This is the maximum fastest method because of using only direct indexing.
             * We scan input sequence just once? and no searches through collection are required.
             */
            #endregion

            //System.Windows.Forms.MessageBox.Show(loopsPerStep.ToString());

            int i, maxFreq = 0;

            worker.ReportProgress(TreatSCANNING);
            OnPrgBarReseat();

            this.Clear();
            Array.Clear(strictFreqs, 0, strictFreqs.Length);

            //System.Windows.Forms.MessageBox.Show(startIndex.ToString("startIndex= 0  ") + strictLength.ToString("strictLength= 0  "));
            //*** Step_1: Look over all the patterns in a range of sequence
            for (i = 0; i <= strictLength; i++)
            {
                strictFreqs[Words.GetNumber(sequence, startIndex + i)]++;
            }

            //*** Step_2: expanding to mismatches
            int      k;
            Similars smls;
            //Pattern ptn;
            // compare each Pattern to all the Pattern's in the same collection with a glance of mismathes;
            // collection is still unsorted
            for (i = 0; i < Count; i++)
            {
                if (worker.CancellationPending)     // Canceled by user.
                {
                    throw new ApplicationException(string.Empty);
                }

                if (IsAllPossible || (strictFreqs[i] > 0))
                {
                    //ptn = this[i];
                    if (Similars.IsTurbo)
                    {
                        smls = Similars.GetSimilars(i);
                        for (k = 0; k < smls.Count; k++)
                        {
                            //ptn.Freq += strictFreqs[smls[k]];
                            this[i] += strictFreqs[smls[k]];
                        }
                    }
                    else
                    {
                        //ptn.Freq += Similars.SumUp(i, strictFreqs);
                        this[i] += Similars.SumUp(i, strictFreqs);
                    }

                    //ptn.WordNumber = i;             // initialize number
                    //ptn.Freq += strictFreqs[i];     // frequency itself
                    this[i] += strictFreqs[i];     // frequency itself
                    //this[i] = ptn;
                    // keep index of maximum Freq without sorting: needs only in case of Max Average Choice or Global task.
                    //if (maxFreqNumber >= 0 && ptn.Freq > maxFreq)
                    if (maxFreqNumber >= 0 && this[i] > maxFreq)
                    {
                        //maxFreq = ptn.Freq;
                        maxFreq       = this[i];
                        maxFreqNumber = i;
                    }
                }
                OnPrgBarIncreased(worker, loopsPerStep);
            }
            if (isDrawPlot)
            {
                // if it needs to keep sorting by name, we should clone collection for drawing,
                // because it will be sorted by Freq during drawing
                worker.ReportProgress(TreatSORTING);
                worker.ReportProgress(Convert.ToInt32(isFirstCall), Clone());
            }
            return(maxFreq);
        }