public void Clear(long stime, long etime) { if (this.Count == 0 || stime > etime || stime > this.LastTime || etime < this.FirstTime) { return; } int sIndex = General.BinarySearch(Time, 0, Time.Count - 1, stime, true); if (sIndex < 0) { sIndex++; } else if (Time[sIndex] != stime) { sIndex++; if (sIndex > Time.Count - 1) { return; } } int eIndex = General.BinarySearch(Time, 0, Time.Count - 1, etime, true); if (eIndex == -1) { eIndex = Time.Count - 1; } if (sIndex > eIndex) { return; } Clear(sIndex, eIndex); }
//private void FillGap(bool isFillGap, long endTime, ref int numAddedElement) //{ // if (this.Count > 0 && isFillGap) // { // while (endTime > this.LastTime + this.Interval) // { // this.Add(this.LastTime + this.Interval, // this.Open.LastOrDefault(), this.High.LastOrDefault(), // this.Low.LastOrDefault(), this.Close.LastOrDefault(), 0); // ++numAddedElement; // } // } //} #endregion //return index whose timestamp <= time public int FindIndexForGivenTime(long time, bool isReturnJustSmallerElement = false) { if (this.Count <= 0) { return(-1); } if (time < this.FirstTime || time > this.LastTime) { return(-1); } return(General.BinarySearch(Time, 0, Time.Count - 1, time, isReturnJustSmallerElement)); }
public virtual IQuoteCapture Extract(long stime, long etime) { var q = new QuoteCapture(this.Symbol); if (this.Count == 0 || stime > etime || stime > this.LastTime || etime < this.FirstTime) { return(q); } int sIndex = General.BinarySearch(Time, 0, Time.Count - 1, stime, true); if (sIndex < 0) { sIndex++; } else if (Time[sIndex] != stime) { sIndex++; if (sIndex > Time.Count - 1) { return(q); } } int eIndex = General.BinarySearch(Time, 0, Time.Count - 1, etime, true); if (eIndex == -1) { eIndex = Time.Count - 1; } if (sIndex > eIndex) { return(q); } return(Extract(sIndex, eIndex)); }
public void TestBinarySearch() { Console.WriteLine(DateTime.UtcNow.GetIso8601WeekOfYear()); //good weather var list = new List <int> { 1, 3, 6, 9, 10 }; Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 6, false), 2); Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 5, false), -1); Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 0, false), -1); Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 12, false), -1); Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 6, true), 2); Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 5, true), 1); Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 0, true), -1); Assert.AreEqual(General.BinarySearch(list, 0, list.Count - 1, 12, true), -1); //bad weather Assert.AreEqual(General.BinarySearch(null, 0, list.Count - 1, 3, true), -1); Assert.AreEqual(General.BinarySearch(list, -1, list.Count - 1, 3, true), -1); Assert.AreEqual(General.BinarySearch(list, -1, list.Count, 3, true), -1); }