bool MergeRight(AddressRange range, int position) { if (position == Ranges.Count) { position--; } if (position >= 0 && position < Ranges.Count) { AddressRange rightRange = Ranges[position]; if (rightRange.Contains(range.End)) { Ranges[position] = new AddressRange(Math.Min(range.Start, rightRange.Start), rightRange.End); return(true); } else if (range.Contains(rightRange)) { Ranges[position] = range; return(true); } else if (rightRange.Contains(range.Start)) { Ranges[position] = new AddressRange(rightRange.Start, Math.Max(range.End, rightRange.End)); return(true); } } return(false); }
bool MergeLeft(AddressRange range, int position) { if (position > 0) { position--; } if (Ranges.Count > position && position >= 0) { AddressRange leftRange = Ranges[position]; if (leftRange.Contains(range.Start)) { Ranges[position] = new AddressRange(leftRange.Start, Math.Max(leftRange.End, range.End)); return(true); } else if (leftRange.End + 1 == range.Start) { Ranges[position] = new AddressRange(leftRange.Start, range.End); return(true); } else if (leftRange.Start - 1 == range.End) { Ranges[position] = new AddressRange(range.Start, leftRange.End); return(true); } } return(false); }
internal void Remove(AddressRange item) { if (Ranges.Count == 0) { return; } for (int i = item.Start; i <= item.End; i++) { var addressRange = new AddressRange(i, i); int index = Ranges.BinarySearch(addressRange, new RangeComparer()); if (index < 0) { index = Math.Max((~index) - 1, 0); AddressRange range = Ranges[index]; if (addressRange.Start < range.Start || addressRange.Start > range.End) { continue; } if (addressRange.Start == range.Start) { Ranges[index] = new AddressRange(range.Start + 1, range.End); } else if (addressRange.End == range.End) { Ranges[index] = new AddressRange(range.Start, range.End - 1); } else { Ranges[index] = new AddressRange(range.Start, addressRange.Start - 1); Ranges.Insert(index + 1, new AddressRange(addressRange.Start + 1, range.End)); } } else { AddressRange range = Ranges[index]; if (range.Contains(addressRange)) { if (range.Start == range.End) { Ranges.RemoveAt(index); } else { Ranges[index] = new AddressRange(range.Start + 1, range.End); } } } } }
internal bool Contains(AddressRange range) { int index = Ranges.BinarySearch(range, new RangeComparer()); // The start of this range is smaller than the start of any range in the list if (index == -1) { return(false); } // An element in the collection has the same 'Start' as 'range' if (index >= 0) { return(range.End <= Ranges[index].End); } index = ~index; AddressRange r = Ranges[index - 1]; return(r.Contains(range)); }