diff --git a/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.h b/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.h index 02a8117d3..a6c4eebf3 100644 --- a/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.h +++ b/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.h @@ -238,6 +238,23 @@ blockType:(YapDatabaseViewBlockType)blockType __attribute((deprecated("Use method findRangeInGroup:using: instead"))); +/** + * This method uses a binary search algorithm to find an item within the view that matches the given criteria. + * + * It works similarly to findRangeInGroup:using:, but immediately returns once a single match has been found. + * This makes it more efficient when you only care about the existence of a match, + * or you know there will never be more than a single match. + * + * See the documentation for findRangeInGroup:using: for more information. + * @see findRangeInGroup:using: + * + * @return + * If found, the index of the first match discovered. + * That is, an item where the find block returned NSOrderedSame. + * If not found, returns NSNotFound. +**/ +- (NSUInteger)findFirstMatchInGroup:(NSString *)group using:(YapDatabaseViewFind *)find; + #pragma mark Enumerating /** diff --git a/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.m b/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.m index d2a4f6c8d..415227623 100644 --- a/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.m +++ b/YapDatabase/Extensions/Views/YapDatabaseViewTransaction.m @@ -4311,7 +4311,7 @@ - (NSString *)versionTag /** * See header file for extensive documentation for this method. **/ -- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find +- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find quitAfterOne:(BOOL)quitAfterOne { if (group == nil || find == NULL) { @@ -4462,6 +4462,11 @@ - (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find return NSMakeRange(NSNotFound, 0); } + if (quitAfterOne) + { + return NSMakeRange(mMid, 1); + } + // Find start of range NSUInteger sMin = mMin; @@ -4507,6 +4512,14 @@ - (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find return NSMakeRange(sMin, (eMax - sMin)); } +/** + * See header file for extensive documentation for this method. +**/ +- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find +{ + return [self findRangeInGroup:group using:find quitAfterOne:NO]; +} + /** * This method is deprecated. * Use findRangeInGroup:using: instead. @@ -4518,6 +4531,28 @@ - (NSRange)findRangeInGroup:(NSString *)group return [self findRangeInGroup:group using:[YapDatabaseViewFind withBlock:block blockType:blockType]]; } +/** + * This method uses a binary search algorithm to find an item within the view that matches the given criteria. + * + * It works similarly to findRangeInGroup:using:, but immediately returns once a single match has been found. + * This makes it more efficient when you only care about the existence of a match, + * or you know there will never be more than a single match. + * + * See the documentation for findRangeInGroup:using: for more information. + * @see findRangeInGroup:using: + * + * @return + * If found, the index of the first match discovered. + * That is, an item where the find block returned NSOrderedSame. + * If not found, returns NSNotFound. +**/ +- (NSUInteger)findFirstMatchInGroup:(NSString *)group using:(YapDatabaseViewFind *)find +{ + NSRange range = [self findRangeInGroup:group using:find quitAfterOne:YES]; + + return range.location; +} + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #pragma mark Public API - Enumerating ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////