public int FindIndex(int startIndex, int count, Predicate <T> match) { if ((uint)startIndex > (uint)_size) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } if (count < 0 || startIndex > _size - count) { ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count(); } if (match == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.Ensures(Contract.Result <int>() >= -1); Contract.Ensures(Contract.Result <int>() < startIndex + count); Contract.EndContractBlock(); int endIndex = startIndex + count; for (int i = startIndex; i < endIndex; i++) { // ReSharper disable once PossibleNullReferenceException if (match(_items[i])) { return(i); } } return(-1); }
// Returns the index of the first occurrence of a given value in a range of // this list. The list is searched forwards, starting at index // index and upto count number of elements. The // elements of the list are compared to the given value using the // Object.Equals method. // // This method uses the Array.IndexOf method to perform the // search. // public int IndexOf(T item, int index, int count) { if (index > _size) { ThrowHelper.ThrowArgumentOutOfRange_IndexException(); } if (count < 0 || index > _size - count) { ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count(); } Contract.Ensures(Contract.Result <int>() >= -1); Contract.Ensures(Contract.Result <int>() < Count); Contract.EndContractBlock(); return(Array.IndexOf(_items, item, index, count)); }
public int FindLastIndex(int startIndex, int count, Predicate <T> match) { if (match == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.Ensures(Contract.Result <int>() >= -1); Contract.Ensures(Contract.Result <int>() <= startIndex); Contract.EndContractBlock(); if (_size == 0) { // Special case for 0 length List if (startIndex != -1) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } } else { // Make sure we're not out of range if ((uint)startIndex >= (uint)_size) { ThrowHelper.ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index(); } } // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0. if (count < 0 || startIndex - count + 1 < 0) { ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count(); } int endIndex = startIndex - count; for (int i = startIndex; i > endIndex; i--) { // ReSharper disable once PossibleNullReferenceException if (match(_items[i])) { return(i); } } return(-1); }