c# - Overlapping Ranges Check for Overlapping -
i have list of ranges , find out if overlap.
i have following code. not seem working. there easier way or way works :)
thanks in advance advice.
public partial class form1 : form { public form1() { initializecomponent(); } private ilist<range> rangelist; private void form1_load(object sender, eventargs e) { rangelist.add(new range{fromnumber = 0, tonumber = 100}); rangelist.add(new range { fromnumber = 101, tonumber = 200 }); // range should on lap , throw exception rangelist.add(new range { fromnumber = 199, tonumber = 300 }); } private bool rangesoverlap() { var biglist = new list<list<int>>(); foreach (var range in this.rangelist) { biglist.add(new list<int> { range.fromnumber , range.tonumber }); } ienumerable<ienumerable<int>> lists = biglist; return lists .where(c => c != null && c.any()) .aggregate(enumerable.intersect) .tolist().count > 0; } } public class range { public int fromnumber { get; set; } public int tonumber { get; set; } }
first merge numbers , check generated list in sorted order:
rangelist .orderby(p => p.fromnumber) .select(p => new[] { p.fromnumber, p.tonumber }) .selectmany(p => p) .aggregate((p, q) => q >= p ? q : int.maxvalue) == int.maxvalue
Comments
Post a Comment