public RangeSet Intersection(RangeSet rs2) { RangeSet rOut = new RangeSet(); RangeSet rs1 = this; int i1 = 0; int i2 = 0; while (i1 < rs1.RangeCount && i2 < rs2.RangeCount) { Range r1 = rs1.ranges[i1]; Range r2 = rs2.ranges[i2]; ulong olo = Math.Max(r1.lo, r2.lo); ulong ohi = Math.Min(r1.hi, r2.hi); // if there is overlap in this section then we emit it if (olo < ohi) { rOut.AddRange(olo, ohi); } if (r1.hi < r2.hi) { i1++; } else { i2++; } } return(rOut); }
public RangeSet Union(RangeSet rs2) { RangeSet rOut = new RangeSet(); RangeSet rs1 = this; int i1 = 0; int i2 = 0; // try to add them in order because appending is the fastest while (i1 < rs1.RangeCount && i2 < rs2.RangeCount) { Range r1 = rs1.ranges[i1]; Range r2 = rs2.ranges[i2]; if (r1.hi < r2.hi) { rOut.AddRange(r1.lo, r1.hi); i1++; } else { rOut.AddRange(r2.lo, r2.hi); i2++; } } // now add whatever is left, we could have just done this in the first place but we wanted them in order while (i1 < rs1.RangeCount) { Range r1 = rs1.ranges[i1]; rOut.AddRange(r1.lo, r1.hi); i1++; } while (i2 < rs2.RangeCount) { Range r2 = rs2.ranges[i2]; rOut.AddRange(r2.lo, r2.hi); i2++; } return(rOut); }