Skip to content

Commit e36e3c7

Browse files
committed
Converting [YapDatabaseView findRangeInGroup:usingBlock:blockType:] to modern syntax. (no more explicit blockType parameter)
1 parent 4bc3065 commit e36e3c7

File tree

5 files changed

+305
-59
lines changed

5 files changed

+305
-59
lines changed

YapDatabase/Extensions/Views/Internal/YapDatabaseViewPrivate.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ static NSString *const changeset_key_changes = @"changes";
5656
#pragma mark -
5757
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5858

59+
@interface YapDatabaseViewFind ()
60+
61+
+ (instancetype)withBlock:(YapDatabaseViewFindBlock)block blockType:(YapDatabaseViewBlockType)blockType;
62+
63+
@end
64+
65+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
66+
#pragma mark -
67+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68+
5969
@interface YapDatabaseView () {
6070
@protected
6171

YapDatabase/Extensions/Views/YapDatabaseViewTransaction.h

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,6 @@
144144

145145
#pragma mark Finding
146146

147-
typedef id YapDatabaseViewFindBlock; // One of the YapDatabaseViewFindX types below.
148-
149-
typedef NSComparisonResult (^YapDatabaseViewFindWithKeyBlock) \
150-
(NSString *collection, NSString *key);
151-
typedef NSComparisonResult (^YapDatabaseViewFindWithObjectBlock) \
152-
(NSString *collection, NSString *key, id object);
153-
typedef NSComparisonResult (^YapDatabaseViewFindWithMetadataBlock) \
154-
(NSString *collection, NSString *key, id metadata);
155-
typedef NSComparisonResult (^YapDatabaseViewFindWithRowBlock) \
156-
(NSString *collection, NSString *key, id object, id metadata);
157-
158147
/**
159148
* This method uses a binary search algorithm to find a range of items within the view that match the given criteria.
160149
* For example:
@@ -234,20 +223,20 @@ typedef NSComparisonResult (^YapDatabaseViewFindWithRowBlock) \
234223
* @param group
235224
* The group within the view to search.
236225
*
237-
* @param block
238-
* One of the YapDatabaseViewFindWithXBlock types.
239-
*
240-
* @param blockType
241-
* The proper YapDatabaseViewBlockTypeWithX type that matches the given block.
226+
* @param find
227+
* Instance of YapDatabaseViewFind. (See YapDatabaseViewTypes.h)
242228
*
243229
* @return
244230
* If found, the range that matches the items within the desired range.
245231
* That is, is these items were passed to the given block, the block would return NSOrderedSame.
246232
* If not found, returns NSMakeRange(NSNotFound, 0).
247233
**/
234+
- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find;
235+
248236
- (NSRange)findRangeInGroup:(NSString *)group
249237
usingBlock:(YapDatabaseViewFindBlock)block
250-
blockType:(YapDatabaseViewBlockType)blockType;
238+
blockType:(YapDatabaseViewBlockType)blockType
239+
__attribute((deprecated("Use method findRangeInGroup:using: instead")));
251240

252241
#pragma mark Enumerating
253242

YapDatabase/Extensions/Views/YapDatabaseViewTransaction.m

Lines changed: 81 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4311,16 +4311,9 @@ - (NSString *)versionTag
43114311
/**
43124312
* See header file for extensive documentation for this method.
43134313
**/
4314-
- (NSRange)findRangeInGroup:(NSString *)group
4315-
usingBlock:(YapDatabaseViewFindBlock)block
4316-
blockType:(YapDatabaseViewBlockType)blockType
4314+
- (NSRange)findRangeInGroup:(NSString *)group using:(YapDatabaseViewFind *)find
43174315
{
4318-
BOOL invalidBlockType = blockType != YapDatabaseViewBlockTypeWithKey &&
4319-
blockType != YapDatabaseViewBlockTypeWithObject &&
4320-
blockType != YapDatabaseViewBlockTypeWithMetadata &&
4321-
blockType != YapDatabaseViewBlockTypeWithRow;
4322-
4323-
if (group == nil || block == NULL || invalidBlockType)
4316+
if (group == nil || find == NULL)
43244317
{
43254318
return NSMakeRange(NSNotFound, 0);
43264319
}
@@ -4338,9 +4331,11 @@ - (NSRange)findRangeInGroup:(NSString *)group
43384331
return NSMakeRange(NSNotFound, 0);
43394332
}
43404333

4341-
NSComparisonResult (^compare)(NSUInteger) = ^NSComparisonResult (NSUInteger index){
4342-
4343-
int64_t rowid = 0;
4334+
// Helper block:
4335+
//
4336+
// Finds the rowid for a given index (within the view.group).
4337+
4338+
int64_t (^findRowid)(NSUInteger) = ^int64_t (NSUInteger index){
43444339

43454340
NSUInteger pageOffset = 0;
43464341
for (YapDatabaseViewPageMetadata *pageMetadata in pagesMetadataForGroup)
@@ -4349,59 +4344,92 @@ - (NSRange)findRangeInGroup:(NSString *)group
43494344
{
43504345
YapDatabaseViewPage *page = [self pageForPageKey:pageMetadata->pageKey];
43514346

4352-
rowid = [page rowidAtIndex:(index - pageOffset)];
4353-
break;
4347+
return [page rowidAtIndex:(index - pageOffset)];
43544348
}
43554349
else
43564350
{
43574351
pageOffset += pageMetadata->count;
43584352
}
43594353
}
43604354

4361-
if (blockType == YapDatabaseViewBlockTypeWithKey)
4355+
NSAssert(NO, @"index(%lu) not found !!!", (unsigned long)index);
4356+
return (int64_t)0;
4357+
};
4358+
4359+
// Helper block:
4360+
//
4361+
// Executes the findBlock against the row represented by the given index (within the view.group).
4362+
4363+
NSComparisonResult (^compare)(NSUInteger);
4364+
4365+
switch (find.findBlockType)
4366+
{
4367+
case YapDatabaseViewBlockTypeWithKey :
43624368
{
43634369
__unsafe_unretained YapDatabaseViewFindWithKeyBlock findBlock =
4364-
(YapDatabaseViewFindWithKeyBlock)block;
4365-
4366-
YapCollectionKey *ck = [databaseTransaction collectionKeyForRowid:rowid];
4370+
(YapDatabaseViewFindWithKeyBlock)find.findBlock;
43674371

4368-
return findBlock(ck.collection, ck.key);
4372+
compare = ^NSComparisonResult (NSUInteger index){
4373+
4374+
int64_t rowid = findRowid(index);
4375+
4376+
YapCollectionKey *ck = [databaseTransaction collectionKeyForRowid:rowid];
4377+
4378+
return findBlock(ck.collection, ck.key);
4379+
};
43694380
}
4370-
else if (blockType == YapDatabaseViewBlockTypeWithObject)
4381+
case YapDatabaseViewBlockTypeWithObject :
43714382
{
43724383
__unsafe_unretained YapDatabaseViewFindWithObjectBlock findBlock =
4373-
(YapDatabaseViewFindWithObjectBlock)block;
4374-
4375-
YapCollectionKey *ck = nil;
4376-
id object = nil;
4377-
[databaseTransaction getCollectionKey:&ck object:&object forRowid:rowid];
4384+
(YapDatabaseViewFindWithObjectBlock)find.findBlock;
43784385

4379-
return findBlock(ck.collection, ck.key, object);
4386+
compare = ^NSComparisonResult (NSUInteger index){
4387+
4388+
int64_t rowid = findRowid(index);
4389+
4390+
YapCollectionKey *ck = nil;
4391+
id object = nil;
4392+
[databaseTransaction getCollectionKey:&ck object:&object forRowid:rowid];
4393+
4394+
return findBlock(ck.collection, ck.key, object);
4395+
};
43804396
}
4381-
else if (blockType == YapDatabaseViewBlockTypeWithMetadata)
4397+
case YapDatabaseViewBlockTypeWithMetadata :
43824398
{
43834399
__unsafe_unretained YapDatabaseViewFindWithMetadataBlock findBlock =
4384-
(YapDatabaseViewFindWithMetadataBlock)block;
4385-
4386-
YapCollectionKey *ck = nil;
4387-
id metadata = nil;
4388-
[databaseTransaction getCollectionKey:&ck metadata:&metadata forRowid:rowid];
4400+
(YapDatabaseViewFindWithMetadataBlock)find.findBlock;
43894401

4390-
return findBlock(ck.collection, ck.key, metadata);
4402+
compare = ^NSComparisonResult (NSUInteger index){
4403+
4404+
int64_t rowid = findRowid(index);
4405+
4406+
YapCollectionKey *ck = nil;
4407+
id metadata = nil;
4408+
[databaseTransaction getCollectionKey:&ck metadata:&metadata forRowid:rowid];
4409+
4410+
return findBlock(ck.collection, ck.key, metadata);
4411+
};
43914412
}
4392-
else
4413+
default :
43934414
{
43944415
__unsafe_unretained YapDatabaseViewFindWithRowBlock findBlock =
4395-
(YapDatabaseViewFindWithRowBlock)block;
4396-
4397-
YapCollectionKey *ck = nil;
4398-
id object = nil;
4399-
id metadata = nil;
4400-
[databaseTransaction getCollectionKey:&ck object:&object metadata:&metadata forRowid:rowid];
4416+
(YapDatabaseViewFindWithRowBlock)find.findBlock;
44014417

4402-
return findBlock(ck.collection, ck.key, object, metadata);
4418+
compare = ^NSComparisonResult (NSUInteger index){
4419+
4420+
int64_t rowid = findRowid(index);
4421+
4422+
YapCollectionKey *ck = nil;
4423+
id object = nil;
4424+
id metadata = nil;
4425+
[databaseTransaction getCollectionKey:&ck object:&object metadata:&metadata forRowid:rowid];
4426+
4427+
return findBlock(ck.collection, ck.key, object, metadata);
4428+
};
44034429
}
4404-
};
4430+
4431+
} // end switch (blockType)
4432+
44054433

44064434
NSUInteger loopCount = 0;
44074435

@@ -4479,6 +4507,17 @@ - (NSRange)findRangeInGroup:(NSString *)group
44794507
return NSMakeRange(sMin, (eMax - sMin));
44804508
}
44814509

4510+
/**
4511+
* This method is deprecated.
4512+
* Use findRangeInGroup:using: instead.
4513+
**/
4514+
- (NSRange)findRangeInGroup:(NSString *)group
4515+
usingBlock:(YapDatabaseViewFindBlock)block
4516+
blockType:(YapDatabaseViewBlockType)blockType
4517+
{
4518+
return [self findRangeInGroup:group using:[YapDatabaseViewFind withBlock:block blockType:blockType]];
4519+
}
4520+
44824521
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
44834522
#pragma mark Public API - Enumerating
44844523
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

YapDatabase/Extensions/Views/YapDatabaseViewTypes.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ typedef NSString* (^YapDatabaseViewGroupingWithRowBlock) \
5151

5252
@end
5353

54+
#pragma mark -
55+
5456
/**
5557
* The sorting block handles sorting of objects within their group.
5658
*
@@ -130,3 +132,78 @@ typedef NSComparisonResult (^YapDatabaseViewSortingWithRowBlock)
130132
@property (nonatomic, assign, readonly) YapDatabaseViewBlockType sortingBlockType;
131133

132134
@end
135+
136+
#pragma mark -
137+
138+
/**
139+
* A find block is used to efficiently find items within a view.
140+
* It allows you to perform a binary search on the pre-sorted items within a view.
141+
*
142+
* The return values from the YapDatabaseViewFindBlock have the following meaning:
143+
*
144+
* - NSOrderedAscending : The given row (block parameters) is less than the range I'm looking for.
145+
* That is, the row would have a smaller index within the view than would the range I seek.
146+
*
147+
* - NSOrderedDecending : The given row (block parameters) is greater than the range I'm looking for.
148+
* That is, the row would have a greater index within the view than would the range I seek.
149+
*
150+
* - NSOrderedSame : The given row (block parameters) is within the range I'm looking for.
151+
*
152+
* Keep in mind 2 things:
153+
*
154+
* #1 : This method can only be used if you need to find items according to their sort order.
155+
* That is, according to how the items are sorted via the view's sortingBlock.
156+
* Attempting to use this method in any other manner makes no sense.
157+
*
158+
* #2 : The findBlock that you pass needs to be setup in the same manner as the view's sortingBlock.
159+
* That is, the following rules must be followed, or the results will be incorrect:
160+
*
161+
* For example, say you have a view like this, looking for the following range of 3 items:
162+
* myView = @[ A, B, C, D, E, F, G ]
163+
* ^^^^^^^
164+
* sortingBlock(A, B) => NSOrderedAscending
165+
* findBlock(A) => NSOrderedAscending
166+
*
167+
* sortingBlock(E, D) => NSOrderedDescending
168+
* findBlock(E) => NSOrderedDescending
169+
*
170+
* findBlock(B) => NSOrderedSame
171+
* findBlock(C) => NSOrderedSame
172+
* findBlock(D) => NSOrderedSame
173+
*
174+
* In other words, you can't sort one way in the sortingBlock, and "sort" another way in the findBlock.
175+
* Another way to think about it is in terms of how the Apple docs define the NSOrdered enums:
176+
*
177+
* NSOrderedAscending : The left operand is smaller than the right operand.
178+
* NSOrderedDescending : The left operand is greater than the right operand.
179+
*
180+
* For the findBlock, the "left operand" is the row that is passed,
181+
* and the "right operand" is the desired range.
182+
*
183+
* And NSOrderedSame means: "the passed row is within the range I'm looking for".
184+
**/
185+
@interface YapDatabaseViewFind : NSObject
186+
187+
typedef id YapDatabaseViewFindBlock; // One of the YapDatabaseViewFindX types below.
188+
189+
typedef NSComparisonResult (^YapDatabaseViewFindWithKeyBlock) \
190+
(NSString *collection, NSString *key);
191+
192+
typedef NSComparisonResult (^YapDatabaseViewFindWithObjectBlock) \
193+
(NSString *collection, NSString *key, id object);
194+
195+
typedef NSComparisonResult (^YapDatabaseViewFindWithMetadataBlock) \
196+
(NSString *collection, NSString *key, id metadata);
197+
198+
typedef NSComparisonResult (^YapDatabaseViewFindWithRowBlock) \
199+
(NSString *collection, NSString *key, id object, id metadata);
200+
201+
+ (instancetype)withKeyBlock:(YapDatabaseViewFindWithKeyBlock)findBlock;
202+
+ (instancetype)withObjectBlock:(YapDatabaseViewFindWithObjectBlock)findBlock;
203+
+ (instancetype)withMetadataBlock:(YapDatabaseViewFindWithMetadataBlock)findBlock;
204+
+ (instancetype)withRowBlock:(YapDatabaseViewFindWithRowBlock)findBlock;
205+
206+
@property (nonatomic, strong, readonly) YapDatabaseViewFindBlock findBlock;
207+
@property (nonatomic, assign, readonly) YapDatabaseViewBlockType findBlockType;
208+
209+
@end

0 commit comments

Comments
 (0)