public void test2() { IntervalList list1 = new IntervalList(); list1.Add(1); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(6); list1.Add(7); list1.Add(8); list1.Add(9); list1.Add(11); IntervalList list2 = new IntervalList(); list2.Add(3); list2.Add(4); list2.Add(5); list2.Add(7); list2.Add(8); var result = Utilities.Objects.IntersectSorted(list2, list1, null); }
public void mergetest() { IntervalList list1 = new IntervalList(); list1.Intervals = new List <Interval>() { new Interval(10, 12), new Interval(20, 50), new Interval(60, 70), new Interval(71, 80), new Interval(100, 120), new Interval(180, 210) }; IntervalList list2 = new IntervalList(); list2.Intervals = new List <Interval>() { new Interval(11, 15), new Interval(16, 20), new Interval(21, 25), new Interval(30, 130), new Interval(200, 202), new Interval(205, 208) }; IntervalList result = Utilities.Objects.MergeSorted_New(list1, list2, null); }
public void Test() { IntervalList list = new IntervalList(); list.Add(3); list.Add(4); list.Add(1); list.Add(4); list.Add(10); list.Add(-1); list.Add(9); list.Add(7); list.Add(11); list.Add(4); list.Add(21); list.Add(22); list.Add(24); list.Add(23); list.Add(22); list.Add(24); list.Add(18); list.Add(2); list.Add(1); var ix1 = list[4]; var ix2 = list[0]; var ix3 = list[1]; var ix4 = list[3]; var ix5 = list[list.Count - 1]; var ix6 = list[list.Count - 2]; Interval sx = null; var ix = 2; list.SetNextInterval(ref sx, ref ix); var s = list.GetString(); }
public static IntervalList GetIntervals(IEnumerable <int> items) { var result = new IntervalList(); foreach (var item in items) { result.Add(item); } return(result); }
public IntervalList Copy() { var il = new IntervalList(); foreach (var i in this.Intervals) { il.Intervals.Add(i.Copy()); } return(il); }
public void AddRange(IntervalList values) { var merged = Utilities.Objects.MergeSorted_New(this, values, null); //merged.Normalize(); this.Clear(); this.Intervals = merged.Intervals; //foreach (var interval in merged.Intervals) //{ // this.Intervals.Add(interval); //} //_Count = -1; }
public void Normalize() { var ic1 = this.Intervals.Count; if (this.Intervals.Count > 1) { var il = new IntervalList(); foreach (var i in this.AsEnumerable()) { il.Add(i); } this.Intervals = il.Intervals; } _Count = -1; if (this.Intervals.Count != ic1) { } //foreach (var interval in this.Intervals) //{ // il.AddInterval(interval); //} }
public void Testb() { IntervalList list1 = new IntervalList(); list1.AddInterval(new Interval(0, 16286069)); IntervalList list2 = new IntervalList(); list2.AddInterval(new Interval(48639, 204038)); list2.AddInterval(new Interval(212919, 354998)); list2.AddInterval(new Interval(363879, 505958)); list2.AddInterval(new Interval(514839, 656918)); list2.AddInterval(new Interval(665799, 807878)); list2.AddInterval(new Interval(816759, 958838)); list2.AddInterval(new Interval(967719, 1109798)); list2.AddInterval(new Interval(1122739, 2744818)); list2.AddInterval(new Interval(2768499, 4390578)); list2.AddInterval(new Interval(4402419, 6024498)); list2.AddInterval(new Interval(6036339, 7658418)); list2.AddInterval(new Interval(7682099, 9304178)); list2.AddInterval(new Interval(9316019, 10938098)); list2.AddInterval(new Interval(10949939, 12572018)); list2.AddInterval(new Interval(12595699, 14217778)); list2.AddInterval(new Interval(14229619, 15851698)); list2.AddInterval(new Interval(15863539, 15996738)); list2.AddInterval(new Interval(16132899, 16133306)); list2.AddInterval(new Interval(16133331, 16133738)); list2.AddInterval(new Interval(16133763, 16134170)); list2.AddInterval(new Interval(16134195, 16137674)); list2.AddInterval(new Interval(16137699, 16138106)); list2.AddInterval(new Interval(16138131, 16138538)); list2.AddInterval(new Interval(16138563, 16140866)); list2.AddInterval(new Interval(16141851, 16157690)); var x = Utilities.Objects.SortedExcept(list1, list2, null); }
public static IntervalList IntersectSorted(IntervalList sequence1, IntervalList sequence2, IComparer <int> comparer) { var result = new IntervalList(); var steps = 0; //var ds = DateTime.Now; var s1 = sequence1.Intervals.Count; var s2 = sequence2.Intervals.Count; var smaller = s1 < s2 ? sequence1 : sequence2; var bigger = smaller == sequence1 ? sequence2 : sequence1; var sc = smaller.Intervals.Count; var bc = bigger.Intervals.Count; var bix = 0; var six = 0; Interval s = null; Interval b = null; var bchanged = true; var schanged = true; while (six < sc && bix < bc) { steps++; if (schanged) { s = smaller.Intervals[six]; schanged = false; } if (bchanged) { if (bc > 10000 && sc < 100) { bix = bigger.SearchByStartIndexBefore(s, bix); } b = bigger.Intervals[bix]; bchanged = false; } var r = s.Compare(b); if (r == 0) { //result.Intervals.Add(s.Intersect(b)); result.AddIntervalToEnd(s.Intersect(b)); if (s.End > b.End) { bix++; bchanged = true; } else { six++; schanged = true; } } if (r < 0) { six++; schanged = true; } if (r > 0) { bix++; bchanged = true; } } //var de = DateTime.Now; //Utilities.Logger.WriteToFile(string.Format("IntersectSorted({0},{1}):{4} in {2} steps, {3} ms ", sequence1, sequence2, steps, de.Subtract(ds).TotalMilliseconds, result.Count)); return(result); }
public static IntervalList MergeSorted(IntervalList sequence1, IntervalList sequence2, IComparer <int> comparer) { var result = new IntervalList(); var smaller = sequence1.Intervals.Count < sequence2.Intervals.Count ? sequence1 : sequence2; var bigger = smaller == sequence1 ? sequence2 : sequence1; var bix = 0; var six = 0; if (IsTheSame(sequence1, sequence2)) { result.Intervals.AddRange(sequence1.Intervals); return(result); } var sc = smaller.Intervals.Count; var bc = bigger.Intervals.Count; Interval s = smaller.Intervals.FirstOrDefault(); Interval b = bigger.Intervals.FirstOrDefault(); int r = 0; while (s != null || b != null) { if (s != null && b != null) { r = s.Compare(b); } else { r = s == null ? 1 : -1; } if (r == 0) { var interval = s.Merge(b); result.AddInterval(interval); if (interval.End >= b.End) { bigger.SetNextInterval(ref b, ref bix); } if (interval.End >= s.End) { smaller.SetNextInterval(ref s, ref six); } } if (r < 0) { result.AddInterval(s); smaller.SetNextInterval(ref s, ref six); } if (r > 0) { result.AddInterval(b); bigger.SetNextInterval(ref b, ref bix); } } return(result); }
public static IntervalList MergeSorted_New(IntervalList sequence1, IntervalList sequence2, IComparer <int> comparer) { var result = new IntervalList(); result.Intervals.Capacity = sequence1.Intervals.Count + sequence2.Intervals.Count; if (sequence1.Intervals.Count == 0) { result.Intervals.AddRange(sequence2.Intervals); return(result); } if (sequence2.Intervals.Count == 0) { result.Intervals.AddRange(sequence1.Intervals); return(result); } var smaller = sequence1.FirstInterval.Start < sequence2.FirstInterval.Start ? sequence1 : sequence2; var bigger = smaller == sequence1 ? sequence2 : sequence1; var s_c = smaller.Intervals.Count; var b_c = bigger.Intervals.Count; var start_ref_interval = smaller.FirstInterval; var end_ref_interval = smaller.LastInterval.Start < bigger.LastInterval.Start ? bigger.LastInterval : smaller.LastInterval; Interval current = start_ref_interval.Copy(); result.Intervals.Add(current); var ix_s = 1; var ix_b = 0; int r = 0; Interval next_s = null; Interval next_b = null; while (current != null) { //if (ix_s == s_c) //{ // result.Intervals.AddRange(bigger.Intervals.Skip(ix_b)); // break; //} //if (ix_b == b_c) //{ //} next_s = ix_s < s_c ? smaller.Intervals[ix_s] : null; next_b = ix_b < b_c ? bigger.Intervals[ix_b] : null; if (next_s == null && next_b == null) { break; } Interval next = null; if (next_b == null || (next_s != null && next_s.Start < next_b.Start)) { next = next_s; ix_s++; } else { next = next_b; ix_b++; } r = current.Compare(next); if (r < 0) { if (r == -1) { current.End = next.End; } else { var il = next.Copy(); result.Intervals.Add(il); current = il; } } if (r > 0) { } if (r == 0) { if (next.End > current.End) { current.End = next.End; } } } return(result); }
public static IntervalList SortedExcept(IntervalList sequence1, IntervalList sequence2, IComparer <int> comparer) { var result = new IntervalList(); var steps = 0; //var ds = DateTime.Now; var smaller = sequence1; var bigger = sequence2; var bix = 0; var six = 0; var sc = smaller.Intervals.Count; var bc = bigger.Intervals.Count; if (sc == 0) { return(result); } var s1 = smaller.FirstInterval == null ? 0 : smaller.FirstInterval.Start; var b1 = bigger.FirstInterval == null ? 0 : bigger.FirstInterval.Start; var se = smaller.LastInterval == null ? 0 : smaller.LastInterval.End; var be = bigger.LastInterval == null ? 0 : bigger.LastInterval.End; if (bc == 0 || se < b1 || be < s1) { return(smaller.Copy()); } var limit = 10; if (sc > limit) { var tsix = smaller.Intervals.BinarySearch(bigger.FirstInterval, IntervalList.startcomparer); if (tsix > sc) { six = sc - 1; } if (tsix < 0) { six = ~tsix; six = six > 0 ? six - 1 : six; } for (int i = 0; i < six; i++) { result.AddInterval(smaller.Intervals[i].Copy()); } } if (bc > limit) { var tbix = bigger.Intervals.BinarySearch(smaller.FirstInterval, IntervalList.startcomparer); if (tbix > bc) { bix = bc - 1; } if (tbix < 0) { bix = ~tbix; bix = bix > 0 ? bix - 1 : bix; } } Interval s = smaller.Intervals.FirstOrDefault(); Interval b = bigger.Intervals.FirstOrDefault(); Interval snext = six < sc ? smaller.Intervals[six] : s; Interval bnext = bix < bc ? bigger.Intervals[bix] : b; var evalb = true; while (snext != null) { steps++; s = snext; b = bnext; var r = b == null?-1: s.Compare(b); if (r == 0) { var except = s.Except(b); var hasany = except != null && except.Count > 0; if (!hasany) { six++; snext = six < sc ? smaller.Intervals[six] : null; continue; } else { if (except.Count == 2) { //result.Intervals.Add(except[0]); result.AddIntervalToEnd(except[0]); snext = except[1]; continue; } else { snext = except[0]; } s = snext; } if (s.End > b.End) { bix++; evalb = true; } else { // result.Intervals.Add(s); result.AddIntervalToEnd(s.Copy()); six++; snext = six < sc ? smaller.Intervals[six] : null; } } if (r < 0) { //result.Intervals.Add(s); result.AddIntervalToEnd(s.Copy()); six++; snext = six < sc ? smaller.Intervals[six] : null; } if (r > 0) { // if (bigger.Intervals.Count > 1000000) { } bix++; if (bc > 10000 && sc < 100) { bix = bigger.SearchByStartIndexBefore(s, bix); } evalb = true; } if (evalb) { evalb = false; bnext = bix < bc ? bigger.Intervals[bix] : null; } } //var de = DateTime.Now; //Utilities.Logger.WriteToFile(string.Format("SortedExcept({0},{1}):{4} in {2} steps, {3} ms ", sequence1, sequence2, steps, de.Subtract(ds).TotalMilliseconds, result.Count)); return(result); }
public void test4() { IntervalList list1 = new IntervalList(); list1.Add(-2); list1.Add(-1); list1.Add(1); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(6); list1.Add(7); list1.Add(9); list1.Add(11); list1.Add(12); list1.Add(20); list1.Add(21); IntervalList list2 = new IntervalList(); list2.Add(3); list2.Add(4); list2.Add(5); list2.Add(7); list2.Add(8); list2.Add(9); var list3 = new IntervalList(); list3.Intervals.Add(new Interval(0, 5)); list3.Intervals.Add(new Interval(7, 9)); list3.Intervals.Add(new Interval(11)); list3.Intervals.Add(new Interval(12)); var list4 = new IntervalList(); list4.Intervals.Add(new Interval(-1, 5)); list4.Intervals.Add(new Interval(6, 8)); var list5 = new IntervalList(); list5.Intervals.Add(new Interval(-1, 6)); list5.Intervals.Add(new Interval(7, 9)); var list6 = new IntervalList(); list6.Intervals.Add(new Interval(0, 5)); list6.Intervals.Add(new Interval(7, 9)); var list7 = new IntervalList(); list7.Intervals.Add(new Interval(0, 6)); list7.Intervals.Add(new Interval(11, 12)); var result = Utilities.Objects.MergeSorted(list1, list2, null); var result2 = Utilities.Objects.MergeSorted(list2, list1, null); var results = new List <IntervalList>(); results.Add(Utilities.Objects.MergeSorted(list3, list4, null)); results.Add(Utilities.Objects.MergeSorted(list3, list5, null)); results.Add(Utilities.Objects.MergeSorted(list3, list6, null)); results.Add(Utilities.Objects.MergeSorted(list3, list7, null)); }
public void test3() { IntervalList list1 = new IntervalList(); list1.Add(-2); list1.Add(-1); list1.Add(1); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(6); list1.Add(7); list1.Add(9); list1.Add(11); list1.Add(12); list1.Add(20); list1.Add(21); IntervalList list2 = new IntervalList(); list2.Add(3); list2.Add(4); list2.Add(5); list2.Add(7); list2.Add(8); list2.Add(9); var result = Utilities.Objects.SortedExcept(list1, list2, null); var result2 = Utilities.Objects.SortedExcept(list2, list1, null); var ls = new IntervalList(); ls.Intervals = new List <Interval>() { new Interval(6337, 6384) }; var lb = new IntervalList(); lb.Intervals = new List <Interval>() { new Interval(131, 147), new Interval(149, 169), new Interval(158, 165), new Interval(196, 198), new Interval(206, 206), new Interval(232, 232), new Interval(238, 240), new Interval(609, 672), new Interval(801, 928), new Interval(993, 1056), new Interval(1121, 1184), new Interval(1313, 1440), new Interval(1921, 2400), new Interval(2737, 3072), new Interval(3193, 3312), new Interval(3433, 3552), new Interval(3793, 4032), new Interval(4065, 4096), new Interval(4577, 5056), new Interval(5121, 5184), new Interval(5217, 5248), new Interval(5281, 5312), new Interval(5345, 5376), new Interval(5385, 5392), new Interval(5409, 5424), new Interval(5433, 5440), new Interval(5449, 5456), new Interval(5473, 5488), new Interval(5513, 5536), new Interval(5545, 5552), new Interval(5569, 5584), new Interval(5593, 5600), new Interval(5609, 5616), new Interval(5633, 5648), new Interval(5673, 5696), new Interval(5705, 5712), new Interval(5729, 5744), new Interval(5753, 5760), new Interval(5769, 5776), new Interval(5793, 5808), new Interval(5833, 5856), new Interval(5865, 5872), new Interval(5889, 5904), new Interval(5913, 5920), new Interval(5929, 5936), new Interval(5953, 5968), new Interval(5993, 6016), new Interval(6033, 6048), new Interval(6081, 6112), new Interval(6129, 6144), new Interval(6161, 6176), new Interval(6209, 6240), new Interval(6289, 6336), new Interval(6361, 6384), new Interval(6433, 6480), new Interval(6505, 6528), new Interval(6553, 6576), new Interval(6625, 6672), new Interval(6745, 6816), new Interval(6865, 6912), new Interval(7033, 7152), new Interval(7169, 7184), new Interval(7193, 7200), new Interval(7209, 7216), new Interval(7225, 7232), new Interval(7233, 7400), new Interval(7401, 7554), new Interval(7555, 12416), new Interval(12417, 13045), new Interval(13046, 13572), new Interval(13573, 22572), new Interval(29573, 34072), new Interval(37573, 39822), new Interval(52823, 109072), new Interval(109323, 109353), new Interval(109354, 109359), new Interval(109392, 110177), new Interval(110178, 110188), new Interval(110225, 110640), new Interval(110677, 110794), new Interval(110807, 110853), new Interval(110950, 111262), new Interval(111275, 111679), new Interval(111692, 111937), new Interval(111950, 111988), new Interval(118085, 118108), new Interval(118111, 118130), new Interval(118131, 118170), new Interval(118175, 118178), }; var result3 = Utilities.Objects.SortedExcept(ls, lb, null); }