public DataSegment?GetSegmentOrNull(ModuleAddress address) { lock (sync) { // Binary search on start addresses, expecting not to find an exact match necessarily. // TODO: This shouldn't be necessary, surely int lowInc = 0; int highExc = segments.Count; while (lowInc < highExc) { int candidate = (lowInc + highExc) / 2; ModuleAddress candidateAddress = segments[candidate].Start; var comparison = candidateAddress.CompareTo(address); // Exact match! Great, can exit immediately. if (comparison == 0) { return(segments[candidate]); } else if (comparison < 0) { lowInc = candidate + 1; } else { highExc = candidate; } } // No exact match, but it's possible (likely!) that we found a match in "lowInc-1", with // a start address greater than the target, but which contains the target. if (lowInc > 0) { var segment = segments[lowInc - 1]; if (segment.Contains(address)) { return(segment); } } return(null); } }
public bool Contains(ModuleAddress other) => other.CompareTo(Start) >= 0 && other.CompareTo(End) < 0;