Added some methods to „Working With the Expression Tree“ section
This commit is contained in:
@@ -44,13 +44,17 @@ extern NSString *MPDivisionOperator;
|
||||
- (instancetype)initWithString:(NSString *)aString;
|
||||
- (instancetype)initWithFunction:(MPFunction *)aFunction;
|
||||
|
||||
|
||||
+ (instancetype)expression;
|
||||
+ (instancetype)expressionWithString:(NSString *)aString;
|
||||
+ (instancetype)expressionWithFunction:(MPFunction *)aFunction;
|
||||
+ (instancetype)expressionWithSymbols:(NSArray *)symbols;
|
||||
|
||||
#pragma mark Working with Expressions
|
||||
#pragma mark Working With the Expression Tree
|
||||
|
||||
- (NSUInteger)indexOfSymbol:(id)symbol;
|
||||
- (id)symbolAtIndexPath:(NSIndexPath *)indexPath; // May also return MPExpression
|
||||
|
||||
#pragma mark Working With Expressions
|
||||
|
||||
- (NSUInteger)length;
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#import "MPException.h"
|
||||
|
||||
#import "NSObject+MPStringTest.h"
|
||||
#import "NSIndexPath+MPRemoveFirstIndex.h"
|
||||
|
||||
NSString *MPAdditionOperator = @"+";
|
||||
NSString *MPSubtractionOperator = @"-";
|
||||
@@ -214,7 +215,29 @@ NSString *MPDivisionOperator = @"/";
|
||||
return [[self alloc] initWithSymbols:symbols];
|
||||
}
|
||||
|
||||
#pragma mark Working with Expressions
|
||||
#pragma mark Working With the Expression Tree
|
||||
|
||||
- (NSUInteger)indexOfSymbol:(id)symbol
|
||||
{
|
||||
return [_symbols indexOfObject:symbol];
|
||||
}
|
||||
|
||||
- (id)symbolAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath.length == 0) {
|
||||
return self;
|
||||
}
|
||||
id symbol = [self symbolAtIndex:[indexPath indexAtPosition:0]];
|
||||
if (indexPath.length == 1) {
|
||||
return symbol;
|
||||
}
|
||||
if ([symbol isKindOfClass:[MPFunction class]]) {
|
||||
return [symbol symbolAtIndexPath:[indexPath indexPathByRemovingFirstIndex]];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
#pragma mark Working With Expressions
|
||||
|
||||
- (NSUInteger)length
|
||||
{
|
||||
|
||||
@@ -35,10 +35,13 @@
|
||||
|
||||
@interface MPFunction (MPFunctionExtensionMethods)
|
||||
|
||||
#pragma mark Children
|
||||
#pragma mark Working With the Expression Tree
|
||||
|
||||
// May be overridden for performance improvements
|
||||
- (NSArray *)children;
|
||||
- (NSUInteger)indexOfChild:(MPExpression *)child;
|
||||
|
||||
- (id)symbolAtIndexPath:(NSIndexPath *)indexPath;
|
||||
|
||||
#pragma mark Evaluating Functions
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#import "MPFunction.h"
|
||||
#import "MPExpression.h"
|
||||
|
||||
#import "NSIndexPath+MPRemoveFirstIndex.h"
|
||||
|
||||
@implementation MPFunction
|
||||
|
||||
#pragma mark Creation Methods
|
||||
@@ -90,7 +92,7 @@
|
||||
|
||||
@implementation MPFunction (MPFunctionExtensionMethods)
|
||||
|
||||
#pragma mark Children
|
||||
#pragma mark Working With the Expression Tree
|
||||
|
||||
- (NSArray *)children
|
||||
{
|
||||
@@ -102,6 +104,26 @@
|
||||
return [children copy];
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfChild:(MPExpression *)child
|
||||
{
|
||||
NSUInteger index = 0;
|
||||
for (; index < [self numberOfChildren]; index++) {
|
||||
if ([[self childAtIndex:index] isEqualToExpression:child]) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return NSNotFound;
|
||||
}
|
||||
|
||||
- (id)symbolAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath.length == 0) {
|
||||
return self;
|
||||
}
|
||||
MPExpression *child = [self childAtIndex:[indexPath indexAtPosition:0]];
|
||||
return [child symbolAtIndexPath:[indexPath indexPathByRemovingFirstIndex]];
|
||||
}
|
||||
|
||||
#pragma mark Evaluating Functions
|
||||
|
||||
- (float)floatValue
|
||||
|
||||
Reference in New Issue
Block a user