From faa9300132da782376788b885f248019c213200a Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 28 Jul 2019 12:14:10 +0200 Subject: [PATCH] More performant lookup for line number --- src/Diagnostics/DiagnosticsHolder.cpp | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/Diagnostics/DiagnosticsHolder.cpp b/src/Diagnostics/DiagnosticsHolder.cpp index 5e9e709..716a0c0 100644 --- a/src/Diagnostics/DiagnosticsHolder.cpp +++ b/src/Diagnostics/DiagnosticsHolder.cpp @@ -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){