Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove use of NSPredicate, more cached information

  • Loading branch information...
commit 06a1c5fa691b03d48c3c195ad1f2411d53743749 1 parent 78f6ad5
@davedelong authored
View
5 DDMathParser/DDMathStringToken.m
@@ -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;
@@ -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;
View
1  DDMathParser/_DDOperatorInfo.h
@@ -27,5 +27,6 @@
+ (NSArray *)allOperators;
+ (NSArray *)infosForOperator:(DDOperator)operator;
++ (NSArray *)infosForOperatorToken:(NSString *)token;
@end
View
40 DDMathParser/_DDOperatorInfo.m
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.