Skip to content

Commit

Permalink
Remove use of NSPredicate, more cached information
Browse files Browse the repository at this point in the history
  • Loading branch information
davedelong committed Oct 10, 2011
1 parent 78f6ad5 commit 06a1c5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
5 changes: 2 additions & 3 deletions DDMathParser/DDMathStringToken.m
Expand Up @@ -31,8 +31,7 @@ - (id) initWithToken:(NSString *)t type:(DDTokenType)type {
operatorType = DDOperatorInvalid;

if (tokenType == DDTokenTypeOperator) {
NSArray *operators = [_DDOperatorInfo allOperators];
NSArray *matching = [operators filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"token = %@", t]];
NSArray *matching = [_DDOperatorInfo infosForOperatorToken:t];
if ([matching count] == 0) {
DD_RELEASE(self);
return nil;
Expand Down Expand Up @@ -104,7 +103,7 @@ - (void)resolveToOperator:(DDOperator)operator {
DD_RELEASE(operatorInfo);
operatorInfo = nil;

NSArray *matching = [[_DDOperatorInfo allOperators] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"operator = %d", operator]];
NSArray *matching = [_DDOperatorInfo infosForOperator:operator];
if ([matching count] != 1) { return; }

ambiguous = NO;
Expand Down
1 change: 1 addition & 0 deletions DDMathParser/_DDOperatorInfo.h
Expand Up @@ -27,5 +27,6 @@

+ (NSArray *)allOperators;
+ (NSArray *)infosForOperator:(DDOperator)operator;
+ (NSArray *)infosForOperatorToken:(NSString *)token;

@end
40 changes: 39 additions & 1 deletion DDMathParser/_DDOperatorInfo.m
Expand Up @@ -35,7 +35,45 @@ + (id)infoForOperator:(DDOperator)operator arity:(DDOperatorArity)arity preceden
}

+ (NSArray *)infosForOperator:(DDOperator)operator {
return [[self allOperators] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"operator = %d", operator]];
static dispatch_once_t onceToken;
static NSMutableDictionary *_operatorLookup = nil;
dispatch_once(&onceToken, ^{
_operatorLookup = [[NSMutableDictionary alloc] init];

NSArray *operators = [self allOperators];
for (_DDOperatorInfo *info in operators) {
DDOperator op = [info operator];
NSNumber *key = [NSNumber numberWithInt:op];

NSMutableArray *value = [_operatorLookup objectForKey:key];
if (value == nil) {
value = [NSMutableArray array];
[_operatorLookup setObject:value forKey:key];
}
[value addObject:info];
}
});
return [_operatorLookup objectForKey:[NSNumber numberWithInt:operator]];
}

+ (NSArray *)infosForOperatorToken:(NSString *)token {
static dispatch_once_t onceToken;
static NSMutableDictionary *_operatorLookup = nil;
dispatch_once(&onceToken, ^{
_operatorLookup = [[NSMutableDictionary alloc] init];

NSArray *operators = [self allOperators];
for (_DDOperatorInfo *info in operators) {

NSMutableArray *value = [_operatorLookup objectForKey:[info token]];
if (value == nil) {
value = [NSMutableArray array];
[_operatorLookup setObject:value forKey:[info token]];
}
[value addObject:info];
}
});
return [_operatorLookup objectForKey:token];
}

#if !DD_HAS_ARC
Expand Down

0 comments on commit 06a1c5f

Please sign in to comment.