public static bool Exist(Period p) { foreach (int i in periodList.Keys) // 'i' would have existing period values { if (i > p.periodValue) break; if ((p.periodValue % i) == 0) { foreach (int j in periodList[i].Keys) // j would have existing stpos { if (j > p.stPos) break; // Nov 5 2007 - Commented and replaced line below to accomodate periodicity within a subsection of series // Nov 25 2008 - Added the last check on first line to check that the supplied string is the substring of an existing periodic occurence to qualify as redundant //if (j % i == p.stPos % i || (j+periodList[i][j].length-1) >= p.stPos) if ((periodList[i][j].length >= p.length && periodList[i][j].endPos >= p.stPos) && Program.s.Substring(j,periodList[i][j].length).StartsWith(Program.s.Substring(p.stPos, p.length)) && (j % i == p.stPos % i || (j + periodList[i][j].length - 1) >= p.stPos)) { return true; } } } } return false; /* if (!periodList.ContainsKey(p.periodValue)) { if (PeriodValueExist(p)) { return true; } } return false; */ }
public static void Add(int periodVal, Period period) { if (periodList.ContainsKey(periodVal)) // check for stpos redundancy { SortedList<int, Period> innerList = periodList[period.periodValue]; List<int> keysToRemove = new List<int>(); foreach (int sp in innerList.Keys) { if (sp <= period.stPos) continue; /** April 03 2008, 7:25 pm Adding another condition that would check that the new period not only start earlier but also ends later * */ // Nov 25 2008 - Added the second check on second line to check that the string to be removed is the substring of new periodic occurence to qualify as redundant if ((sp % period.periodValue == period.stPos % period.periodValue) && Program.s.Substring(period.stPos, period.length).StartsWith(Program.s.Substring(sp, periodList[periodVal][sp].length)) && (period.endPos >= periodList[period.periodValue][sp].endPos)) { keysToRemove.Add(sp); //innerList.Remove(sp); } } for (int i = 0; i < keysToRemove.Count; i++) innerList.Remove(keysToRemove[i]); innerList.Add(period.stPos, period); } else // check for period value redundancy { List<int> keysToRemove = new List<int>(); foreach (int p in periodList.Keys) { if (p <= periodVal) continue; if (p % period.periodValue == 0) { if (periodList[p].ContainsKey(period.stPos) && periodList[p][period.stPos].length <= period.length) { periodList[p].Remove(period.stPos); if (periodList[p].Count == 0) { keysToRemove.Add(p); //periodList.Remove(p); } } } } for (int i = 0; i < keysToRemove.Count; i++) periodList.Remove(keysToRemove[i]); SortedList<int, Period> innerList = new SortedList<int, Period>(); innerList.Add(period.stPos, period); periodList.Add(periodVal, innerList); } }
public bool IsPeriodExist(Period pin) { PeriodExistCounter++; foreach (Period p in periods) { if (p.periodValue == pin.periodValue) { if (p.stPos == pin.stPos) { //if (p.symbolString.Equals(pin.symbolString)) if (p.length == pin.length) { return true; } //else if (p.symbolString.Length > pin.symbolString.Length) if (p.length > pin.length) { return true; } } //else if (p.symbolString.Equals(pin.symbolString) && ((pin.stPos % pin.periodValue) == (p.stPos % pin.periodValue))) else if (((T.Substring(p.fci, p.length)).Equals(T.Substring(pin.fci, pin.length))) && ((pin.stPos % pin.periodValue) == (p.stPos % pin.periodValue))) { if (pin.stPos < p.stPos) { p.stPos = pin.stPos; p.foundPosCount = pin.foundPosCount; p.threshold = pin.threshold; //p.symbolString = pin.symbolString; p.fci = pin.fci; p.length = pin.length; } return true; } } } return false; }
public void CalculatePeriodImp(Edge e) { if (e.value > Program.maxStrLen || e.value < Program.minStrLen) return; OccurNode current = e.st; /* *********** Extracting data for complexity improvement experiments ************** * Date: 10th Sep 2008, 5:54 AM */ //if (e.value < 35) //{ for (int i = 0; i < e.len; i++) { ovl.Add(current.value); current = current.next; } current = e.st; Program.CalculatePeriod(ovl.ToArray(), e.value); ovl.Clear(); return; //} /************ End extracting data for complexity improvement experiments ************/ // code to find the last value of occur_vector - 2nd nov 2007 OccurNode last = e.st; for (int k = 1; k < e.len; k++) { last = last.next; } int lastOccurValue = last.value; // end code to find the last value of occur_vector - 2nd nov 2007 int preDiffValue = -5; // introduced to check the repetitive stPos periods like 0,3,6,9 calculatePeriodCounter++; for (int i = 1; i < e.len; i++) { outerLoopCounterBeforeCheck++; //int diffValue = occur[i + 1] - occur[i]; int diffValue = current.next.value - current.value; /******* Modifying temporarily to check periodicity of 3 if (diffValue < 5 || diffValue < e.value || diffValue == 1 || diffValue > (0.33 * T.Length) || current.value > (0.5 * T.Length)) //*********** End Modifying to check 3 *********/ /* replacing with line below for packet data if (diffValue < 5 || diffValue < e.value || diffValue > (0.33 * T.Length) || current.value > (0.5 * T.Length) || diffValue == preDiffValue) */ if (diffValue < 2 || diffValue < e.value || diffValue > 20 || diffValue == preDiffValue) { diffCounter++; current = current.next; continue; } Period p = new Period(); p.periodValue = diffValue; p.stPos = current.value; p.endPos = lastOccurValue; // introduced to find periodicity in segments - 2nd nov 2007 p.fci = p.stPos; p.length = e.value; preDiffValue = diffValue; ///*****commenting temporarily - 5th nov 2007 ********* if (PeriodCollection.Exist(p)) { periodColExistCounter++; current = current.next; continue; } //****** end commenting ********/ outerLoopCounterAfterCheck++; /*********** not required after new code aug 27 int modRes = p.stPos % p.periodValue; */ p.foundPosCount = 0; /******** NEW CODE Aug 27 ************/ int A, C = 0; double B = 0, sumPerVal = 0; int preSubCurValue = -5; //int preStPos = p.stPos; int currStPos = p.stPos; /***** END NEW CODE Aug 27 *********/ OccurNode subCurrent = current; for (int j = i; j <= e.len; j++) { innerLoopCounter++; ///******** NEW CODE Aug 27 *********** //A = subCurrent.value - p.stPos; A = subCurrent.value - currStPos; B = Math.Round((double)A / p.periodValue); C = A - (p.periodValue * (int)B); if (C >= (-1 * tolWin) && C <= tolWin) { //if (Math.Round((double)((preSubCurValue - p.stPos) / p.periodValue)) != B) if (Math.Round((double)((preSubCurValue - currStPos) / p.periodValue)) != B) // if it is a valid periodic occurence { preSubCurValue = subCurrent.value; //preStPos = currStPos; // new lines currStPos = subCurrent.value; // new lines p.foundPosCount++; sumPerVal += (p.periodValue + C); // new lines } } //***** END NEW CODE Aug 27 *********/ /**************** commenting the original one if (modRes == subCurrent.value % p.periodValue) p.foundPosCount++; ********** end comment ***/ ///***** introducing dmax and mu concept - Nov 4 2007 if (j != i && j < e.len && // if its atleast the 2nd occurence or at maximum 2nd last occurence (subCurrent.next.value - subCurrent.value) >= (dmax /* * p.periodValue*/) && // if difference between this and previous occurence is greater than dmax times period value (currStPos - p.stPos) >= (minLengthOfSegment * (T.Length - 1)/*p.periodValue*/)) // if the length of to be segment is greater than minLengthOfSegment times period value //(subCurrent.value - p.stPos) >= (minLengthOfSegment * p.periodValue)) // if the length of to be segment is greater than minLengthOfSegment times period value { /*if (currStPos == subCurrent.value) // if this occurence was match { p.endPos = subCurrent.value; } else { p.endPos = currStPos; // set to the last matched position } */ p.endPos = currStPos; // set to the last matched position break; } //****** end dmax and mu code - Nov 4 2007 **********/ subCurrent = subCurrent.next; } double y = 0; /*** commented and replaced with the lines below to avoid > 1 conf using avgPeriodValue instead of periodValue if (((T.Length - 1 - p.stPos) % p.periodValue) >= e.value) y = 1; else y = 0; double th1 = p.foundPosCount / Math.Floor(((double)(T.Length - 1 - p.stPos) / p.periodValue) + y); */ ///*** used p.avgPeriodValue instead of p.periodValue in threshold (conf) calculation to avoid th > 1 /// also added periodicity for segments code by replacing T.length - 1 with lastOccurValue + p.length p.avgPeriodValue = (sumPerVal - p.periodValue) / (p.foundPosCount - 1); /************ changed lastOccurValue with p.endPos in the following lines *********** if (((lastOccurValue + p.length - p.stPos) % ((int)Math.Round(p.avgPeriodValue))) >= e.value) y = 1; else y = 0; double th1 = p.foundPosCount / Math.Floor(((double)(lastOccurValue + p.length - p.stPos) / p.avgPeriodValue) + y); *********end changed lastOccurValue with p.endPos in the above lines *****************/ if (((p.endPos + p.length - p.stPos) % ((int)Math.Round(p.avgPeriodValue))) >= e.value) y = 1; else y = 0; double th1 = p.foundPosCount / Math.Floor(((double)(p.endPos + p.length - p.stPos) / p.avgPeriodValue) + y); //*/ p.threshold = th1; if (p.threshold >= this.minThreshold) //&& (!IsPeriodExist(p))) { AddPeriodCounter++; //p.avgPeriodValue = (sumPerVal-p.periodValue) / (p.foundPosCount-1); // moving this to upper lines PeriodCollection.Add(p.periodValue, p); //break; //periods.Add(p); } current = current.next; } }
public static bool PeriodValueExist(Period p) { foreach (int i in periodList.Keys) // 'i' would have existing period values { if (i > p.periodValue) break; if ((p.periodValue % i) == 0) { foreach (int j in periodList[i].Keys) // j would have existing stpos { if (j > p.stPos) break; if (j % i == p.stPos % i) { return true; } } } } return false; }