Skip to content

Commit

Permalink
Adding optimized find method (YapDatabaseView) in case one only needs…
Browse files Browse the repository at this point in the history
… the first match.
  • Loading branch information
robbiehanson committed Aug 1, 2015
1 parent e36e3c7 commit 47413fa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
17 changes: 17 additions & 0 deletions YapDatabase/Extensions/Views/YapDatabaseViewTransaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
37 changes: 36 additions & 1 deletion YapDatabase/Extensions/Views/YapDatabaseViewTransaction.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 47413fa

Please sign in to comment.