From 7d48d85dfbd332ff04d8d700f861580974beaeee Mon Sep 17 00:00:00 2001 From: Kim Wittenburg Date: Tue, 30 Sep 2014 22:17:42 +0200 Subject: [PATCH] Drastic Simplification of MPFunction subclass implementations --- MathPad/MPFunction.h | 29 +++++---- MathPad/MPFunction.m | 30 ++++----- MathPad/MPParenthesisFunction.m | 67 +++----------------- MathPad/MPPowerFunction.h | 17 +++++ MathPad/MPPowerFunction.m | 40 ++++++++++++ MathPad/MPSumFunction.m | 108 ++------------------------------ 6 files changed, 106 insertions(+), 185 deletions(-) create mode 100644 MathPad/MPPowerFunction.h create mode 100644 MathPad/MPPowerFunction.m diff --git a/MathPad/MPFunction.h b/MathPad/MPFunction.h index eccb0b4..def046e 100644 --- a/MathPad/MPFunction.h +++ b/MathPad/MPFunction.h @@ -11,12 +11,19 @@ #import "MPParseError.h" #import "MPTerm.h" +#define MPFunctionAccessorImplementation(Accessor, variableName) \ +- (void)set##Accessor:(MPExpression *)value \ +{ \ + variableName.parent = nil; \ + variableName = value; \ + variableName.parent = self; \ + [self didChangeChildAtIndex:[self indexOfChild:variableName]]; \ +} + @class MPFunction, MPExpression, MPRangePath; @interface MPFunction : NSObject -+ (NSString *)localizedFunctionName; // Override - #pragma mark Creation Methods - (instancetype)init; @@ -29,13 +36,14 @@ - (MPExpression *)rootExpression; - (NSIndexPath *)indexPath; -- (NSUInteger)numberOfChildren; // Override -- (MPExpression *)childAtIndex:(NSUInteger)index; // Override -- (void)setChild:(MPExpression *)child - atIndex:(NSUInteger)index; // Override +- (NSArray *)childrenAccessors; // Override -// May be overridden for performance improvements -- (NSArray *)children; // Indexes must equal the ones from the native methods +- (NSUInteger)numberOfChildren; +- (MPExpression *)childAtIndex:(NSUInteger)index; +- (void)setChild:(MPExpression *)child + atIndex:(NSUInteger)index; + +- (NSArray *)children; // Indexes must equal the ones from the other methods - (NSUInteger)indexOfChild:(MPExpression *)child; - (id)elementAtIndexPath:(NSIndexPath *)indexPath; @@ -47,15 +55,14 @@ #pragma mark Messages - (void)didChangeElementsInRangePath:(MPRangePath *)rangePath replacementLength:(NSUInteger)replacementLength; -- (void)didChangeChild:(MPExpression *)child; - (void)didChangeChildAtIndex:(NSUInteger)index; #pragma mark Working With Functions // - (BOOL)isEqualToFunction:(MPFunction *)aFunction; // Override - (NSString *)description; // Should be overridden -- (NSUInteger)hash;// Override +- (NSUInteger)hash; -- (id)copyWithZone:(NSZone *)zone; // Override +- (id)copyWithZone:(NSZone *)zone; @end diff --git a/MathPad/MPFunction.m b/MathPad/MPFunction.m index 7e28658..a36f335 100644 --- a/MathPad/MPFunction.m +++ b/MathPad/MPFunction.m @@ -24,6 +24,12 @@ { self = [super init]; if (self) { + for (NSString *key in self.childrenAccessors) { + MPExpression *emptyExpression = [[MPExpression alloc] init]; + emptyExpression.parent = self; + [self setValue:emptyExpression + forKey:key]; + } } return self; } @@ -42,17 +48,18 @@ - (NSUInteger)numberOfChildren { - return 0; + return self.childrenAccessors.count; } - (MPExpression *)childAtIndex:(NSUInteger)index { - return nil; + return [self valueForKey:self.childrenAccessors[index]]; } - (void)setChild:(MPExpression *)child atIndex:(NSUInteger)index { + [self setValue:child forKey:self.childrenAccessors[index]]; [self didChangeChildAtIndex:index]; } @@ -86,12 +93,6 @@ return [child elementAtIndexPath:[indexPath indexPathByRemovingFirstIndex]]; } -#pragma mark Evaluating Functions -- (MPTerm *)parseWithError:(MPParseError *__autoreleasing *)error -{ - return nil; -} - #pragma mark Notifications - (void)didChangeElementsInRangePath:(MPRangePath *)rangePath replacementLength:(NSUInteger)replacementLength @@ -102,11 +103,6 @@ replacementLength:replacementLength]; } -- (void)didChangeChild:(MPExpression *)child -{ - [self didChangeChildAtIndex:[self indexOfChild:child]]; -} - - (void)didChangeChildAtIndex:(NSUInteger)index { MPRangePath *path = [[MPRangePath alloc] initWithRange:NSMakeRange(index, 1)]; @@ -142,13 +138,19 @@ - (NSUInteger)hash { +#warning Unimplemented Method return 0; } #pragma mark - NSCopying - (id)copyWithZone:(NSZone *)zone { - return [[MPFunction allocWithZone:zone] init]; + id copy = [[self.class allocWithZone:zone] init]; + for (NSString *key in self.childrenAccessors) { + MPExpression *child = [[self valueForKey:key] copy]; + [copy setValue:child forKey:key]; + } + return copy; } #pragma mark - NSCoding diff --git a/MathPad/MPParenthesisFunction.m b/MathPad/MPParenthesisFunction.m index c1596fb..742c7cc 100644 --- a/MathPad/MPParenthesisFunction.m +++ b/MathPad/MPParenthesisFunction.m @@ -12,49 +12,18 @@ @implementation MPParenthesisFunction -+ (NSString *)localizedFunctionName -{ - return NSLocalizedString(@"Parenthesis", @"Name of Parenthesis Function"); -} +MPFunctionAccessorImplementation(Expression, _expression) -- (instancetype)init -{ - self = [super init]; - if (self) { - _expression = [[MPExpression alloc] init]; - _expression.parent = self; - } - return self; -} +//- (void)setExpression:(MPExpression *)expression +//{ +// _expression.parent = nil; +// _expression = expression; +// _expression.parent = self; +//} -- (void)setExpression:(MPExpression *)expression +- (NSArray *)childrenAccessors { - _expression.parent = nil; - _expression = expression; - _expression.parent = self; -} - -- (NSUInteger)numberOfChildren -{ - return 1; -} - -- (MPExpression *)childAtIndex:(NSUInteger)index -{ - return index == 0 ? self.expression : nil; -} - -- (void)setChild:(MPExpression *)child - atIndex:(NSUInteger)index -{ - if (index == 0) { - self.expression = child; - } -} - -- (NSArray *)children -{ - return @[self.expression]; + return @[@"expression"]; } - (MPTerm *)parseWithError:(MPParseError *__autoreleasing *)error @@ -64,27 +33,9 @@ error:error]; } -- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error -{ - return [self.expression evaluateWithError:error]; -} - - (NSString *)description { return [NSString stringWithFormat:@"(%@)", self.expression.description]; } -- (NSUInteger)hash -{ -#warning Unimplemented Method - return [super hash] * self.expression.hash; -} - -- (id)copyWithZone:(NSZone *)zone -{ - MPParenthesisFunction *copy = [[MPParenthesisFunction allocWithZone:zone] init]; - copy.expression = self.expression.copy; - return copy; -} - @end diff --git a/MathPad/MPPowerFunction.h b/MathPad/MPPowerFunction.h new file mode 100644 index 0000000..1cc123b --- /dev/null +++ b/MathPad/MPPowerFunction.h @@ -0,0 +1,17 @@ +// +// MPPowerFunction.h +// MathPad +// +// Created by Kim Wittenburg on 30.09.14. +// Copyright (c) 2014 Kim Wittenburg. All rights reserved. +// + +#import + +@interface MPPowerFunction : MPFunction + +@property (nonatomic, strong) MPExpression *exponentExpression; + +@property (nonatomic, strong) MPTerm *baseTerm; + +@end diff --git a/MathPad/MPPowerFunction.m b/MathPad/MPPowerFunction.m new file mode 100644 index 0000000..d6782a0 --- /dev/null +++ b/MathPad/MPPowerFunction.m @@ -0,0 +1,40 @@ +// +// MPPowerFunction.m +// MathPad +// +// Created by Kim Wittenburg on 30.09.14. +// Copyright (c) 2014 Kim Wittenburg. All rights reserved. +// + +#import "MPPowerFunction.h" +#import "MPExpressionEvaluator.h" + +@implementation MPPowerFunction + +MPFunctionAccessorImplementation(ExponentExpression, _exponentExpression) + +- (NSArray *)childrenAccessors +{ + return @[@"exponentExpression"]; +} + +- (MPTerm *)parseWithError:(MPParseError *__autoreleasing *)error +{ + MPTerm *exponentTerm = [self.exponentExpression.evaluator parseExpectingVariable:NO + error:error]; + if (exponentTerm == nil) { + return nil; + } + + return [[MPTerm alloc] initWithBlock:^NSDecimalNumber *{ + double power = pow([self.baseTerm evaluate].doubleValue, [exponentTerm evaluate].doubleValue); + return [[NSDecimalNumber alloc] initWithDouble:power]; + }]; +} + +- (NSString *)description +{ + return [NSString stringWithFormat:@"^%@", self.exponentExpression.description]; +} + +@end diff --git a/MathPad/MPSumFunction.m b/MathPad/MPSumFunction.m index 31d5e01..5c94637 100644 --- a/MathPad/MPSumFunction.m +++ b/MathPad/MPSumFunction.m @@ -12,98 +12,18 @@ #import "MPExpressionEvaluator.h" #import "MPEvaluationContext.h" +// TODO: Use this macro elsewhere #define ReturnIfNil(test) if(test == nil) return nil @implementation MPSumFunction -+ (NSString *)localizedFunctionName -{ - return NSLocalizedString(@"Sum", @"Name of Sum Function"); -} +MPFunctionAccessorImplementation(StartExpression, _startExpression) +MPFunctionAccessorImplementation(TargetExpression, _targetExpression) +MPFunctionAccessorImplementation(SumExpression, _sumExpression) -#pragma mark Creation Methods -- (instancetype)init +- (NSArray *)childrenAccessors { - self = [super init]; - if (self) { - _startExpression = [[MPExpression alloc] init]; - _startExpression.parent = self; - _targetExpression = [[MPExpression alloc] init]; - _targetExpression.parent = self; - _sumExpression = [[MPExpression alloc] init]; - _sumExpression.parent = self; - } - return self; -} - -#pragma mark Properties -- (void)setStartExpression:(MPExpression *)startExpression -{ - _startExpression.parent = nil; - _startExpression = startExpression; - _startExpression.parent = self; - [self didChangeChildAtIndex:0]; -} - -- (void)setTargetExpression:(MPExpression *)targetExpression -{ - _targetExpression.parent = nil; - _targetExpression = targetExpression; - _targetExpression.parent = self; - [self didChangeChildAtIndex:1]; -} - -- (void)setSumExpression:(MPExpression *)sumExpression -{ - _sumExpression.parent = nil; - _sumExpression = sumExpression; - _sumExpression.parent = self; - [self didChangeChildAtIndex:2]; -} - -#pragma mark Working With the Expression Tree -- (NSUInteger)numberOfChildren -{ - return 3; -} - -- (MPExpression *)childAtIndex:(NSUInteger)index -{ - switch (index) { - case 0: - return self.startExpression; - case 1: - return self.targetExpression; - case 2: - return self.sumExpression; - default: - return nil; - } -} - -- (void)setChild:(MPExpression *)child - atIndex:(NSUInteger)index -{ - switch (index) { - // TODO: Copy child? - case 0: - self.startExpression = child; - break; - case 1: - self.targetExpression = child; - break; - case 2: - self.sumExpression = child; - break; - default: - return; - } - [self didChangeChildAtIndex:index]; -} - -- (NSArray *)children -{ - return @[self.startExpression, self.targetExpression, self.sumExpression]; + return @[@"startExpression", @"targetExpression", @"sumExpression"]; } #pragma mark Evaluating Functions @@ -167,20 +87,4 @@ return [NSString stringWithFormat:@"Sum(From: %@; To: %@; Using: %@)", self.startExpression, self.targetExpression, self.sumExpression]; } -- (NSUInteger)hash -{ -#warning Unimplemented Method - return [super hash]; -} - -#pragma mark - NSCopying -- (id)copyWithZone:(NSZone *)zone -{ - MPSumFunction *copy = [[MPSumFunction allocWithZone:zone] init]; - copy.startExpression = [self.startExpression copy]; - copy.targetExpression = [self.targetExpression copy]; - copy.sumExpression = [self.sumExpression copy]; - return copy; -} - @end