| 209 | } |
| 210 | |
| 211 | Int MSSpectrum::findNearest(MSSpectrum::CoordinateType mz, MSSpectrum::CoordinateType tolerance_left, |
| 212 | MSSpectrum::CoordinateType tolerance_right) const |
| 213 | { |
| 214 | if (ContainerType::empty()) |
| 215 | { |
| 216 | return -1; |
| 217 | } |
| 218 | // do a binary search for nearest peak first |
| 219 | Size i = findNearest(mz); |
| 220 | |
| 221 | const double nearest_mz = this->operator[](i).getMZ(); |
| 222 | |
| 223 | if (nearest_mz < mz) |
| 224 | { |
| 225 | if (nearest_mz >= mz - tolerance_left) |
| 226 | { |
| 227 | return i; // success: nearest peak is in left tolerance window |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | if (i == this->size() - 1) |
| 232 | { |
| 233 | return -1; // we are at the last peak which is too far left |
| 234 | } |
| 235 | // Nearest peak is too far left so there can't be a closer peak in the left window. |
| 236 | // There still might be a peak to the right of mz that falls in the right window |
| 237 | ++i; // now we are at a peak exactly on or to the right of mz |
| 238 | const double next_mz = this->operator[](i).getMZ(); |
| 239 | if (next_mz <= mz + tolerance_right) |
| 240 | { |
| 241 | return i; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | if (nearest_mz <= mz + tolerance_right) |
| 248 | { |
| 249 | return i; // success: nearest peak is in right tolerance window |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | if (i == 0) |
| 254 | { |
| 255 | return -1; // we are at the first peak which is too far right |
| 256 | } |
| 257 | --i; // now we are at a peak exactly on or to the right of mz |
| 258 | const double next_mz = this->operator[](i).getMZ(); |
| 259 | if (next_mz >= mz - tolerance_left) |
| 260 | { |
| 261 | return i; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // neither in the left nor the right tolerance window |
| 267 | return -1; |
| 268 | } |
no test coverage detected