Skip to content

Commit 47413fa

Browse files
committed
Adding optimized find method (YapDatabaseView) in case one only needs the first match.
1 parent e36e3c7 commit 47413fa

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

YapDatabase/Extensions/Views/YapDatabaseViewTransaction.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,23 @@
238238
blockType:(YapDatabaseViewBlockType)blockType
239239
__attribute((deprecated("Use method findRangeInGroup:using: instead")));
240240

241+
/**
242+
* This method uses a binary search algorithm to find an item within the view that matches the given criteria.
243+
*
244+
* It works similarly to findRangeInGroup:using:, but immediately returns once a single match has been found.
245+
* This makes it more efficient when you only care about the existence of a match,
246+
* or you know there will never be more than a single match.
247+
*
248+
* See the documentation for findRangeInGroup:using: for more information.
249+
* @see findRangeInGroup:using:
250+
*
251+
* @return
252+
* If found, the index of the first match discovered.
253+
* That is, an item where the find block returned NSOrderedSame.
254+
* If not found, returns NSNotFound.
255+
**/
256+
- (NSUInteger)findFirstMatchInGroup:(NSString *)group using:(YapDatabaseViewFind *)find;
257+
241258
#pragma mark Enumerating
242259

243260
/**

YapDatabase/Extensions/Views/YapDatabaseViewTransaction.m

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4311,7 +4311,7 @@ - (NSString *)versionTag
43114311
/**
43124312
* See header file for extensive documentation for this method.
43134313
**/
4314-
- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find
4314+
- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find quitAfterOne:(BOOL)quitAfterOne
43154315
{
43164316
if (group == nil || find == NULL)
43174317
{
@@ -4462,6 +4462,11 @@ - (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find
44624462
return NSMakeRange(NSNotFound, 0);
44634463
}
44644464

4465+
if (quitAfterOne)
4466+
{
4467+
return NSMakeRange(mMid, 1);
4468+
}
4469+
44654470
// Find start of range
44664471

44674472
NSUInteger sMin = mMin;
@@ -4507,6 +4512,14 @@ - (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find
45074512
return NSMakeRange(sMin, (eMax - sMin));
45084513
}
45094514

4515+
/**
4516+
* See header file for extensive documentation for this method.
4517+
**/
4518+
- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find
4519+
{
4520+
return [self findRangeInGroup:group using:find quitAfterOne:NO];
4521+
}
4522+
45104523
/**
45114524
* This method is deprecated.
45124525
* Use findRangeInGroup:using: instead.
@@ -4518,6 +4531,28 @@ - (NSRange)findRangeInGroup:(NSString *)group
45184531
return [self findRangeInGroup:group using:[YapDatabaseViewFind withBlock:block blockType:blockType]];
45194532
}
45204533

4534+
/**
4535+
* This method uses a binary search algorithm to find an item within the view that matches the given criteria.
4536+
*
4537+
* It works similarly to findRangeInGroup:using:, but immediately returns once a single match has been found.
4538+
* This makes it more efficient when you only care about the existence of a match,
4539+
* or you know there will never be more than a single match.
4540+
*
4541+
* See the documentation for findRangeInGroup:using: for more information.
4542+
* @see findRangeInGroup:using:
4543+
*
4544+
* @return
4545+
* If found, the index of the first match discovered.
4546+
* That is, an item where the find block returned NSOrderedSame.
4547+
* If not found, returns NSNotFound.
4548+
**/
4549+
- (NSUInteger)findFirstMatchInGroup:(NSString *)group using:(YapDatabaseViewFind *)find
4550+
{
4551+
NSRange range = [self findRangeInGroup:group using:find quitAfterOne:YES];
4552+
4553+
return range.location;
4554+
}
4555+
45214556
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45224557
#pragma mark Public API - Enumerating
45234558
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)