use cache to store char-to-column mapping
This is faster by O(n) to O(n²) compared to the previous algorithm that would re-compute the column for each call.
This commit is contained in:
17
line.py
17
line.py
@@ -1,3 +1,5 @@
|
||||
from typing import Dict
|
||||
|
||||
import constants
|
||||
|
||||
|
||||
@@ -6,6 +8,7 @@ class Line:
|
||||
self._byte_offset = byte_offset
|
||||
self._byte_end = byte_end
|
||||
self._line = line
|
||||
self._char_to_column_cache = self._cache_char_to_column()
|
||||
|
||||
def byte_offset(self) -> int:
|
||||
return self._byte_offset
|
||||
@@ -63,17 +66,25 @@ class Line:
|
||||
|
||||
return result
|
||||
|
||||
# todo this method is slow
|
||||
def char_to_column(self, char_in_line: int) -> int:
|
||||
if not char_in_line in self._char_to_column_cache:
|
||||
# print("%d in %s" % (char_in_line, self._char_to_column_cache))
|
||||
return -1
|
||||
return self._char_to_column_cache[char_in_line]
|
||||
|
||||
def _cache_char_to_column(self) -> Dict[int, int]:
|
||||
char_to_column_cache = {}
|
||||
result = 0
|
||||
i = 0
|
||||
while i < char_in_line:
|
||||
char_to_column_cache[0] = 0
|
||||
while i < len(self._line):
|
||||
if i < len(self._line) and self._line[i] == "\t":
|
||||
result = result + constants.tab_width - result % constants.tab_width
|
||||
else:
|
||||
result = result + 1
|
||||
i = i + 1
|
||||
return result
|
||||
char_to_column_cache[i] = result
|
||||
return char_to_column_cache
|
||||
|
||||
def includes_byte(self, byte: int) -> bool:
|
||||
return self._byte_offset <= byte <= self._byte_end
|
||||
|
||||
Reference in New Issue
Block a user