@@ -76,26 +76,20 @@ public int resolvePosition(int currentRow, ArrayView matchedLabels, int searchSt
7676 checkArgument (currentRow >= patternStart && currentRow < patternStart + matchedLabels .length (), "current row is out of bounds of the match" );
7777
7878 int relativePosition ;
79+ int patternEndInclusive = running ? currentRow - patternStart : matchedLabels .length () - 1 ;
7980 if (last ) {
80- int start ;
81- if (running ) {
82- start = currentRow - patternStart ;
83- }
84- else {
85- start = matchedLabels .length () - 1 ;
86- }
87- relativePosition = findLastAndBackwards (start , matchedLabels );
81+ relativePosition = findLastAndBackwards (patternEndInclusive , matchedLabels );
8882 }
8983 else {
90- relativePosition = findFirstAndForward (matchedLabels );
84+ relativePosition = findFirstAndForward (patternEndInclusive , matchedLabels );
9185 }
9286 return adjustPosition (relativePosition , patternStart , searchStart , searchEnd );
9387 }
9488
9589 // LAST(A.price, 3): find the last occurrence of label "A" and go 3 occurrences backwards
96- private int findLastAndBackwards (int searchStart , ArrayView matchedLabels )
90+ private int findLastAndBackwards (int patternEndInclusive , ArrayView matchedLabels )
9791 {
98- int position = searchStart + 1 ;
92+ int position = patternEndInclusive + 1 ;
9993 int found = 0 ;
10094 while (found <= logicalOffset && position > 0 ) {
10195 position --;
@@ -110,11 +104,11 @@ private int findLastAndBackwards(int searchStart, ArrayView matchedLabels)
110104 }
111105
112106 // FIRST(A.price, 3): find the first occurrence of label "A" and go 3 occurrences forward
113- private int findFirstAndForward (ArrayView matchedLabels )
107+ private int findFirstAndForward (int patternEndInclusive , ArrayView matchedLabels )
114108 {
115109 int position = -1 ;
116110 int found = 0 ;
117- while (found <= logicalOffset && position < matchedLabels . length () - 1 ) {
111+ while (found <= logicalOffset && position < patternEndInclusive ) {
118112 position ++;
119113 if (labels .isEmpty () || labels .contains (matchedLabels .get (position ))) {
120114 found ++;
0 commit comments