| 175 | self.offsets.sort() |
| 176 | |
| 177 | def find_offset(self, offset, lower_bound=None): |
| 178 | # Find the largest mapped offset <= the search offset |
| 179 | lo = 0 |
| 180 | hi = len(self.offsets) |
| 181 | |
| 182 | while lo < hi: |
| 183 | mid = (lo + hi) // 2 |
| 184 | if self.offsets[mid] > offset: |
| 185 | hi = mid |
| 186 | else: |
| 187 | lo = mid + 1 |
| 188 | if lo == 0: |
| 189 | return None |
| 190 | # If lower bound is given, return the offset only if the offset is equal to |
| 191 | # or greater than the lower bound |
| 192 | if lower_bound: |
| 193 | if self.offsets[lo - 1] >= lower_bound: |
| 194 | return self.offsets[lo - 1] |
| 195 | else: |
| 196 | return None |
| 197 | else: |
| 198 | return self.offsets[lo - 1] |
| 199 | |
| 200 | def lookup(self, offset, lower_bound=None): |
| 201 | nearest = self.find_offset(offset, lower_bound) |