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 topLimit = _lineStarts.size() - 1;
size_t bottomLimit = 0;
while (true){
if (bottomLimit == topLimit){
return bottomLimit;
}
size_t half = bottomLimit + ((topLimit - bottomLimit) / 2);
size_t pos = _lineStarts[half];
size_t length = _lineLength[half];
if (pos < i && pos + length > i){
return half;
}
size_t topLimit = _lineStarts.size() - 1;
while (bottomLimit <= topLimit){
size_t index = bottomLimit + (topLimit - bottomLimit) / 2;
auto pos = _lineStarts[index];
if (pos == i) return index;
if (pos > i){
topLimit = half;
} else if (pos < i){
if (bottomLimit == half){
bottomLimit = half + 1;
} else{
bottomLimit = half;
}
topLimit = index - 1;
} else{
return half;
bottomLimit = index + 1;
}
}
return bottomLimit - 1;
}
std::string SeverityToString(DiagnosticSeverity s){