/// <summary>Gets a copy of this instance.</summary> /// <returns></returns> public Similars Copy() { Similars copySmls = new Similars(Count); Array.Copy(_numbers, copySmls._numbers, Count); return(copySmls); }
/// <summary>Gets inizializeted similar words for given seed word.</summary> /// <param name="seedNumber">Serial number of seed word.</param> /// <returns>The total nubmer of filled similars.</returns> /// <remarks>In Turbo mode getting Similars are saved and returned from memory by requery.</remarks> public static Similars GetSimilars(int seedNumber) { if (_MismatchCount == 0) { return(null); } _Word = Words.GetWord(seedNumber); int sum = 0; if (_IsTurbo) { Similars smls = _ArrSimilars[seedNumber]; if (smls == null) { // create and fill Similars _ArrSimilars[seedNumber] = smls = new Similars(_Capacity); Fill_recurs(0, _MismatchCount, seedNumber, smls._numbers, ref sum, true); } return(smls); } else { Fill_recurs(0, _MismatchCount, seedNumber, _Similars._numbers, ref sum, true); return(_Similars); } }
/// <summary>Initializes static members.</summary> /// <param name="mismatchCount">The number of acceptable mismatches</param> /// <param name="isTurbo">True if Turbo mode is defined.</param> /// <param name="isDifferentWordLength">True if word's length is differ with previous session.</param> public static void Init(byte mismatchCount, bool isTurbo, bool isDifferentWordLength) { _Capacity = GetCapacity(Words.WordLength, mismatchCount); bool isDiffer = mismatchCount != _MismatchCount || isDifferentWordLength; if (_IsTurbo = isTurbo) { // save filling Similars in case of the same MismatchCount & WordLength _Similars = null; if (isDiffer || _ArrSimilars == null) { _ArrSimilars = new Similars[Words.Count]; } } else { _ArrSimilars = null; if (isDiffer || _Similars == null) { _Similars = new Similars(_Capacity); } } _MismatchCount = mismatchCount; _DepthOfClastering = DoC_FACTOR * Words.WordLength; }
/// <summary>Reports the shrinked (without zero Frequency) Similars of the specified pattern.</summary> /// <param name="seedNumber">The number of the seed-word.</param> /// <returns>If Similars have zero-frequency items, the shrinked (without zero Frequency) Similars; otherwise initial Similars.</returns> public Similars GetSimilars(int seedNumber) { Similars smls = Similars.GetSimilars(seedNumber); bool isNotCopied = true; // We have to remove from the total Similars all the numbers with zero Frequency int j; bool isFound = true; for (int i = 0; i < smls.Count; i++) { // scan throw the whole collection to check for real existing similars for (j = 0; j < Count; j++) { if (isFound = this[j].SeedNumber == smls[i]) { break; } } if (!isFound) { // if zero-frequence element is finded, Similars should be copied // because it change its Count by shrinking if (isNotCopied) { smls = smls.Copy(); isNotCopied = false; } smls[i] = Similars.UndefNumber; } } return(isNotCopied ? smls : smls.Shrink()); }
/// <summary>Initialezas static member.</summary> /// <param name="wordLength">The length of word.</param> /// <param name="mismatchCount">The number of acceptable mismatches</param> public static void Init(byte wordLength, byte mismatchCount) { _wordLength = wordLength; _wordLastIndex = (byte)(wordLength - 1); _count = (int)Math.Pow(Chars.Count, wordLength); Similars.Init(mismatchCount); // inizialise words _wordsTrain = new byte[_count * wordLength]; SetRank_recurs(0, 1); }
/// <summary>Initialezas static member.</summary> /// <param name="wordLength">The length of word.</param> /// <param name="mismatchCount">The number of acceptable mismatches</param> /// <param name="isTurbo">True if Turbo mode is defined.</param> public static void Init(byte wordLength, byte mismatchCount, bool isTurbo) { bool isDifferentLength = _wordLength != wordLength; if (isDifferentLength) { _wordLength = wordLength; _wordLastIndex = (byte)(wordLength - 1); _count = GetCount(wordLength); } Similars.Init(mismatchCount, isTurbo, isDifferentLength); // _wordLength & _count are used in that method, so it could be the last }
/// <summary> /// Fills referenced array by the indexes of similar words of given seed word an returns number of indexes. /// </summary> /// <param name="words">Words sequence.</param> /// <param name="wrdIndex">The index of the seed word in the words sequence.</param> /// <param name="smlIndexes">Array of indexes of similar words.</param> /// <returns></returns> public static int Fill(byte[] words, int wrdIndex, ref Similars smlIndexes) { bool[] lockedLetters = new bool[Words.WordLength]; // array of signs indicated wich letter's position is locked int currSmlIndex = 0; if (_MismatchCount > 0) { FillUnder(ref words, wrdIndex, ref lockedLetters, 0, _MismatchCount, ref smlIndexes, ref currSmlIndex); } return(smlIndexes._count = currSmlIndex); }
/// <summary> /// Fills referenced array by the numbers of similar words for given seed word and returns number of similars. /// </summary> /// <param name="word">Seed word.</param> /// <param name="smlIndexes">External uninitialised array of numbers of similar words.</param> /// <returns>The total nubmer of filled similars.</returns> public static int Fill(byte[] word, ref Similars smlIndexes) { _word = word; _smlNumbers = smlIndexes; _currSmlIndex = 0; if (_MismatchCount > 0) { FillUnder_recurs(0, _MismatchCount); } return(smlIndexes._count = _currSmlIndex); }
///// <summary>Removes all the elements with the zero Frequency from the instance.</summary> ///// <remarks>Sorts the collection by Frequency.</remarks> //void Shrink () //{ // if (_isShrinked == true) // return; // Sort(); // //System.Console.Beep(400, 100); // // start from the end because zero Frequencies are much more less then unzero // for (int i = Count-1; i >= 0; i--) // //if (this[i].Freq > 0) // if (this[i] > 0) // { // Array.Resize(ref _coll, _insCount = i+1); // break; // } // _isShrinked = true; //} #endregion #region Statistic methods /// <summary>Reports the shrinked (without zero Frequency) Similars of the specified unsorted pattern.</summary> /// <param name="seedNumber">The number of the seed-word in the collection.</param> /// <returns>If Similars have zero-frequency items, the shrinked (without zero Frequency) Similars; otherwise initial Similars.</returns> public Similars GetSimilars(int seedNumber) { Similars smls = Similars.GetSimilars(seedNumber); bool isNotCopied = true; // We have to remove from the total Similars all the numbers with zero Frequency // this is a variant used in ScanWindows // we may use direct indexing to check zero-frequency seeds for (int i = 0; i < smls.Count; i++) { if (this[smls[i]] == 0) { // if zero-frequence element is finded, Similars should be copied // because it change its Count by shrinking if (isNotCopied) { smls = smls.Copy(); isNotCopied = false; } smls[i] = Similars.UndefNumber; } } return(isNotCopied ? smls : smls.Shrink()); }
/// <summary>Adds to referenced array the indexes of similar words of given seed word.</summary> /// <param name="words">Words sequence.</param> /// <param name="wrdIndex">The index of the seed word in the word's sequence.</param> /// <param name="lockedLetters">Array of signs indicated wich letter's position is locked on this step.</param> /// <param name="startPos">Started letter's position in seed word at wich search begins</param> /// <param name="mismatchCnt">Number of mismathes.</param> /// <param name="smlIndexes">Array of indexes of similar words.</param> /// <param name="currSmlIndex">Current writing position in array of indexes of similar words.</param> static void FillUnder(ref byte[] words, int wrdIndex, ref bool[] lockedLetters, int startPos, int mismatchCnt, ref Similars smlIndexes, ref int currSmlIndex) { byte j, originLetter, currLetter; int ind, i; mismatchCnt--; // reduce count of mismatches because of processing current mismatch's level for (i = startPos; i < Words.WordLength; i++) { if (!lockedLetters[i]) // pass locked letter { ind = i + wrdIndex; originLetter = words[ind]; // save original letter wich would be changed // substitute other letters for (j = 0; j < Chars.Count; j++) { //currLetter = Chars.GetCodeByIndex(j); currLetter = j; if (currLetter != originLetter) { words[ind] = currLetter; // substitute other letter smlIndexes[currSmlIndex++] = Words.GetNumber(words, wrdIndex); // save words index if (mismatchCnt > 0) // lock letter and pass down { lockedLetters[i] = true; // lock letter FillUnder(ref words, wrdIndex, ref lockedLetters, i + 1, mismatchCnt, ref smlIndexes, ref currSmlIndex); lockedLetters[i] = false; // unlock letter } } } words[ind] = originLetter; // restore original letter } } mismatchCnt++; // restore count of mismatches }
/// <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); }