Drastic Simplification of MPFunction subclass implementations
This commit is contained in:
@@ -11,12 +11,19 @@
|
|||||||
#import "MPParseError.h"
|
#import "MPParseError.h"
|
||||||
#import "MPTerm.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;
|
@class MPFunction, MPExpression, MPRangePath;
|
||||||
|
|
||||||
@interface MPFunction : NSObject <NSCoding, NSCopying, MPExpressionElement>
|
@interface MPFunction : NSObject <NSCoding, NSCopying, MPExpressionElement>
|
||||||
|
|
||||||
+ (NSString *)localizedFunctionName; // Override
|
|
||||||
|
|
||||||
#pragma mark Creation Methods
|
#pragma mark Creation Methods
|
||||||
|
|
||||||
- (instancetype)init;
|
- (instancetype)init;
|
||||||
@@ -29,13 +36,14 @@
|
|||||||
- (MPExpression *)rootExpression;
|
- (MPExpression *)rootExpression;
|
||||||
- (NSIndexPath *)indexPath;
|
- (NSIndexPath *)indexPath;
|
||||||
|
|
||||||
- (NSUInteger)numberOfChildren; // Override
|
- (NSArray *)childrenAccessors; // Override
|
||||||
- (MPExpression *)childAtIndex:(NSUInteger)index; // Override
|
|
||||||
- (void)setChild:(MPExpression *)child
|
|
||||||
atIndex:(NSUInteger)index; // Override
|
|
||||||
|
|
||||||
// May be overridden for performance improvements
|
- (NSUInteger)numberOfChildren;
|
||||||
- (NSArray *)children; // Indexes must equal the ones from the native methods
|
- (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;
|
- (NSUInteger)indexOfChild:(MPExpression *)child;
|
||||||
|
|
||||||
- (id)elementAtIndexPath:(NSIndexPath *)indexPath;
|
- (id)elementAtIndexPath:(NSIndexPath *)indexPath;
|
||||||
@@ -47,15 +55,14 @@
|
|||||||
#pragma mark Messages
|
#pragma mark Messages
|
||||||
- (void)didChangeElementsInRangePath:(MPRangePath *)rangePath
|
- (void)didChangeElementsInRangePath:(MPRangePath *)rangePath
|
||||||
replacementLength:(NSUInteger)replacementLength;
|
replacementLength:(NSUInteger)replacementLength;
|
||||||
- (void)didChangeChild:(MPExpression *)child;
|
|
||||||
- (void)didChangeChildAtIndex:(NSUInteger)index;
|
- (void)didChangeChildAtIndex:(NSUInteger)index;
|
||||||
|
|
||||||
#pragma mark Working With Functions
|
#pragma mark Working With Functions
|
||||||
// - (BOOL)isEqualToFunction:(MPFunction *)aFunction; // Override
|
// - (BOOL)isEqualToFunction:(MPFunction *)aFunction; // Override
|
||||||
|
|
||||||
- (NSString *)description; // Should be overridden
|
- (NSString *)description; // Should be overridden
|
||||||
- (NSUInteger)hash;// Override
|
- (NSUInteger)hash;
|
||||||
|
|
||||||
- (id)copyWithZone:(NSZone *)zone; // Override
|
- (id)copyWithZone:(NSZone *)zone;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -24,6 +24,12 @@
|
|||||||
{
|
{
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
|
for (NSString *key in self.childrenAccessors) {
|
||||||
|
MPExpression *emptyExpression = [[MPExpression alloc] init];
|
||||||
|
emptyExpression.parent = self;
|
||||||
|
[self setValue:emptyExpression
|
||||||
|
forKey:key];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@@ -42,17 +48,18 @@
|
|||||||
|
|
||||||
- (NSUInteger)numberOfChildren
|
- (NSUInteger)numberOfChildren
|
||||||
{
|
{
|
||||||
return 0;
|
return self.childrenAccessors.count;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MPExpression *)childAtIndex:(NSUInteger)index
|
- (MPExpression *)childAtIndex:(NSUInteger)index
|
||||||
{
|
{
|
||||||
return nil;
|
return [self valueForKey:self.childrenAccessors[index]];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setChild:(MPExpression *)child
|
- (void)setChild:(MPExpression *)child
|
||||||
atIndex:(NSUInteger)index
|
atIndex:(NSUInteger)index
|
||||||
{
|
{
|
||||||
|
[self setValue:child forKey:self.childrenAccessors[index]];
|
||||||
[self didChangeChildAtIndex:index];
|
[self didChangeChildAtIndex:index];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,12 +93,6 @@
|
|||||||
return [child elementAtIndexPath:[indexPath indexPathByRemovingFirstIndex]];
|
return [child elementAtIndexPath:[indexPath indexPathByRemovingFirstIndex]];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark Evaluating Functions
|
|
||||||
- (MPTerm *)parseWithError:(MPParseError *__autoreleasing *)error
|
|
||||||
{
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark Notifications
|
#pragma mark Notifications
|
||||||
- (void)didChangeElementsInRangePath:(MPRangePath *)rangePath
|
- (void)didChangeElementsInRangePath:(MPRangePath *)rangePath
|
||||||
replacementLength:(NSUInteger)replacementLength
|
replacementLength:(NSUInteger)replacementLength
|
||||||
@@ -102,11 +103,6 @@
|
|||||||
replacementLength:replacementLength];
|
replacementLength:replacementLength];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)didChangeChild:(MPExpression *)child
|
|
||||||
{
|
|
||||||
[self didChangeChildAtIndex:[self indexOfChild:child]];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)didChangeChildAtIndex:(NSUInteger)index
|
- (void)didChangeChildAtIndex:(NSUInteger)index
|
||||||
{
|
{
|
||||||
MPRangePath *path = [[MPRangePath alloc] initWithRange:NSMakeRange(index, 1)];
|
MPRangePath *path = [[MPRangePath alloc] initWithRange:NSMakeRange(index, 1)];
|
||||||
@@ -142,13 +138,19 @@
|
|||||||
|
|
||||||
- (NSUInteger)hash
|
- (NSUInteger)hash
|
||||||
{
|
{
|
||||||
|
#warning Unimplemented Method
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - NSCopying
|
#pragma mark - NSCopying
|
||||||
- (id)copyWithZone:(NSZone *)zone
|
- (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
|
#pragma mark - NSCoding
|
||||||
|
|||||||
@@ -12,49 +12,18 @@
|
|||||||
|
|
||||||
@implementation MPParenthesisFunction
|
@implementation MPParenthesisFunction
|
||||||
|
|
||||||
+ (NSString *)localizedFunctionName
|
MPFunctionAccessorImplementation(Expression, _expression)
|
||||||
{
|
|
||||||
return NSLocalizedString(@"Parenthesis", @"Name of Parenthesis Function");
|
|
||||||
}
|
|
||||||
|
|
||||||
- (instancetype)init
|
//- (void)setExpression:(MPExpression *)expression
|
||||||
{
|
//{
|
||||||
self = [super init];
|
// _expression.parent = nil;
|
||||||
if (self) {
|
// _expression = expression;
|
||||||
_expression = [[MPExpression alloc] init];
|
// _expression.parent = self;
|
||||||
_expression.parent = self;
|
//}
|
||||||
}
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)setExpression:(MPExpression *)expression
|
- (NSArray *)childrenAccessors
|
||||||
{
|
{
|
||||||
_expression.parent = nil;
|
return @[@"expression"];
|
||||||
_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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MPTerm *)parseWithError:(MPParseError *__autoreleasing *)error
|
- (MPTerm *)parseWithError:(MPParseError *__autoreleasing *)error
|
||||||
@@ -64,27 +33,9 @@
|
|||||||
error:error];
|
error:error];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSDecimalNumber *)evaluateWithError:(MPParseError *__autoreleasing *)error
|
|
||||||
{
|
|
||||||
return [self.expression evaluateWithError:error];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSString *)description
|
- (NSString *)description
|
||||||
{
|
{
|
||||||
return [NSString stringWithFormat:@"(%@)", self.expression.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
|
@end
|
||||||
|
|||||||
17
MathPad/MPPowerFunction.h
Normal file
17
MathPad/MPPowerFunction.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// MPPowerFunction.h
|
||||||
|
// MathPad
|
||||||
|
//
|
||||||
|
// Created by Kim Wittenburg on 30.09.14.
|
||||||
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import <MathKit/MathKit.h>
|
||||||
|
|
||||||
|
@interface MPPowerFunction : MPFunction
|
||||||
|
|
||||||
|
@property (nonatomic, strong) MPExpression *exponentExpression;
|
||||||
|
|
||||||
|
@property (nonatomic, strong) MPTerm *baseTerm;
|
||||||
|
|
||||||
|
@end
|
||||||
40
MathPad/MPPowerFunction.m
Normal file
40
MathPad/MPPowerFunction.m
Normal file
@@ -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
|
||||||
@@ -12,98 +12,18 @@
|
|||||||
#import "MPExpressionEvaluator.h"
|
#import "MPExpressionEvaluator.h"
|
||||||
#import "MPEvaluationContext.h"
|
#import "MPEvaluationContext.h"
|
||||||
|
|
||||||
|
// TODO: Use this macro elsewhere
|
||||||
#define ReturnIfNil(test) if(test == nil) return nil
|
#define ReturnIfNil(test) if(test == nil) return nil
|
||||||
|
|
||||||
@implementation MPSumFunction
|
@implementation MPSumFunction
|
||||||
|
|
||||||
+ (NSString *)localizedFunctionName
|
MPFunctionAccessorImplementation(StartExpression, _startExpression)
|
||||||
{
|
MPFunctionAccessorImplementation(TargetExpression, _targetExpression)
|
||||||
return NSLocalizedString(@"Sum", @"Name of Sum Function");
|
MPFunctionAccessorImplementation(SumExpression, _sumExpression)
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark Creation Methods
|
- (NSArray *)childrenAccessors
|
||||||
- (instancetype)init
|
|
||||||
{
|
{
|
||||||
self = [super init];
|
return @[@"startExpression", @"targetExpression", @"sumExpression"];
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark Evaluating Functions
|
#pragma mark Evaluating Functions
|
||||||
@@ -167,20 +87,4 @@
|
|||||||
return [NSString stringWithFormat:@"Sum(From: %@; To: %@; Using: %@)", self.startExpression, self.targetExpression, self.sumExpression];
|
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
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user