More performant lookup for line number

This commit is contained in:
Deukhoofd 2019-07-28 12:14:10 +02:00
parent 2b248101d5
commit faa9300132
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 8 additions and 19 deletions

View File

@ -40,30 +40,19 @@ Diagnostic *DiagnosticsHolder::GetDiagnosticAt(int position) {
} }
size_t DiagnosticsHolder::GetLineFromPosition(size_t i) { size_t DiagnosticsHolder::GetLineFromPosition(size_t i) {
size_t topLimit = _lineStarts.size() - 1;
size_t bottomLimit = 0; size_t bottomLimit = 0;
while (true){ size_t topLimit = _lineStarts.size() - 1;
if (bottomLimit == topLimit){ while (bottomLimit <= topLimit){
return bottomLimit; size_t index = bottomLimit + (topLimit - bottomLimit) / 2;
} auto pos = _lineStarts[index];
size_t half = bottomLimit + ((topLimit - bottomLimit) / 2); if (pos == i) return index;
size_t pos = _lineStarts[half];
size_t length = _lineLength[half];
if (pos < i && pos + length > i){
return half;
}
if (pos > i){ if (pos > i){
topLimit = half; topLimit = index - 1;
} else if (pos < i){
if (bottomLimit == half){
bottomLimit = half + 1;
} else{ } else{
bottomLimit = half; bottomLimit = index + 1;
}
} else{
return half;
} }
} }
return bottomLimit - 1;
} }
std::string SeverityToString(DiagnosticSeverity s){ std::string SeverityToString(DiagnosticSeverity s){