private static int IndexFirstPalindromeOfLength(int l, ArrayHashComputer <T> a, ArrayHashComputer <T> aReversed) { Debug.Assert(a.Length == aReversed.Length); if (l <= 1) { return(0); } for (int start = 0; start < (a.Length - l + 1); ++start) { int end = start + l / 2 - 1; var aHash = a.Hash(start, end); int reverStart = a.Length - (start + l); var aReverseHash = aReversed.Hash(reverStart, reverStart + l / 2 - 1); if (aHash == aReverseHash) { return(start); } } return(-1); }
/// <summary> /// finds the index of first sub array of length 'subArrayLength' that can be found both in 'this and 'other' array in o (this.Length) time /// if 'subArrayLength == 0', it will return 0 (the first occurence of an empty string is at index 0) /// </summary> /// <param name="other"></param> /// <param name="subArrayLength">the exact length of the sub array to find</param> /// <returns>index of the sub array in the 'this' array, or -1 if no such array of length 'subArrayLength' exists</returns> public int FirstIndexOfSameSubArrayWithLength(ArrayHashComputer <T> other, int subArrayLength) { if (subArrayLength <= 0) { return(0); } var hashInOtherArray = new HashSet <long>(); for (var startIndex = 0; startIndex <= other.Length - subArrayLength; ++startIndex) { hashInOtherArray.Add(other.Hash(startIndex, startIndex + subArrayLength - 1)); } for (var startIndex = 0; startIndex <= Length - subArrayLength; ++startIndex) { if (hashInOtherArray.Contains(Hash(startIndex, startIndex + subArrayLength - 1))) { return(startIndex); } } return(-1); //there is no common array of length 'subArrayLength' in 'this' and 'other' array }