public (int begin, int end) Find(string query) { var p = new StringView(query); int l = LowerBound(p, 0, CharacterCount); int u = UpperBound(p, 0, CharacterCount); return(l, u); }
private int LowerBound(StringView pattern, int start, int end) { while (start < end) { int mid = start + (end - start) / 2; var suffix = GetSuffix(SA[mid], pattern.Length); if (Algorithm.LexicographicalCompare(suffix, pattern)) { start = mid + 1; } else { end = mid; } } return(start); }
public static bool LexicographicalCompare(StringView a, StringView b) { int first1 = a.Offset; int first2 = b.Offset; int last1 = a.Offset + a.Length; int last2 = b.Offset + b.Length; while (first1 != last1) { if (first2 == last2 || b.AtAbsolute(first2) < a.AtAbsolute(first1)) { return(false); } if (a.AtAbsolute(first1) < b.AtAbsolute(first2)) { return(true); } ++first1; ++first2; } return(first2 != last2); }