Skip to content

Commit 7f5aaf8

Browse files
committed
Issue#1537: Modified get_token_indexes_between_indexes to use binary search instead of linear search.
1 parent 2789048 commit 7f5aaf8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

vsg/token_map.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ def get_token_indexes(self, oToken, bCopy=False):
2222
return []
2323

2424
def get_token_indexes_between_indexes(self, oToken, iStart, iEnd):
25-
lReturn = []
2625
lIndexes = self.get_token_indexes(oToken)
27-
for iIndex in lIndexes:
28-
if iIndex > iStart and iIndex < iEnd:
29-
lReturn.append(iIndex)
30-
return lReturn
26+
if not lIndexes:
27+
return []
28+
# Use a binary search to get the first index > the start.
29+
iLowIndex = bisect.bisect_right(lIndexes, iStart) # first index > iStart
30+
# Use a binary search to get the last index < the end.
31+
iHighIndex = bisect.bisect_left(lIndexes, iEnd) # first index >= iEnd
32+
return lIndexes[iLowIndex:iHighIndex]
3133

3234
def get_line_number_of_index(self, iIndex):
3335
iLine = bisect.bisect_left(self.dMap["parser"]["carriage_return"], iIndex) + 1

0 commit comments

Comments
 (0)