Added Expression Tree Inspection Methods
Cleaned Code
This commit is contained in:
@@ -19,6 +19,14 @@ extern NSString *MPDivisionOperator;
|
|||||||
|
|
||||||
- (instancetype)initWithSymbols:(NSArray *)symbols;
|
- (instancetype)initWithSymbols:(NSArray *)symbols;
|
||||||
|
|
||||||
|
#pragma mark Working With the Expression Tree
|
||||||
|
|
||||||
|
@property (nonatomic, weak) MPFunction *parent; // Documentation: Do not set, may be nil
|
||||||
|
- (void)functionSymbolChanged:(MPFunction *)symbol
|
||||||
|
atLocalIndexPath:(NSIndexPath *)indexPath; // Index path of change in symbol
|
||||||
|
|
||||||
|
- (void)fixSymbols;
|
||||||
|
|
||||||
#pragma mark Primitive Methods
|
#pragma mark Primitive Methods
|
||||||
|
|
||||||
- (NSUInteger)numberOfSymbols;
|
- (NSUInteger)numberOfSymbols;
|
||||||
@@ -81,6 +89,9 @@ extern NSString *MPDivisionOperator;
|
|||||||
- (void)replaceSymbolsInRange:(NSRange)range
|
- (void)replaceSymbolsInRange:(NSRange)range
|
||||||
withSymbols:(NSArray *)symbols;
|
withSymbols:(NSArray *)symbols;
|
||||||
|
|
||||||
|
- (void)beginEditing;
|
||||||
|
- (void)endEditing;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface MPMutableExpression (MPMutableExpressionExtensionMethods)
|
@interface MPMutableExpression (MPMutableExpressionExtensionMethods)
|
||||||
|
|||||||
@@ -21,8 +21,7 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
|
|
||||||
- (NSInteger)lengthOfSymbol:(id)symbol;
|
- (NSInteger)lengthOfSymbol:(id)symbol;
|
||||||
- (void)validateSymbols:(NSArray *)symbols;
|
- (void)validateSymbols:(NSArray *)symbols;
|
||||||
- (NSArray *)repairedSymbols:(NSArray *)symbols; // Merges subsequent strings
|
- (NSArray *)fixedSymbols:(NSArray *)symbols;
|
||||||
- (void)repairSymbols:(NSMutableArray *)symbols;
|
|
||||||
- (void)getSplitOffset:(out NSUInteger *)offset
|
- (void)getSplitOffset:(out NSUInteger *)offset
|
||||||
inSymbolAtIndex:(out NSUInteger *)symbolIndex
|
inSymbolAtIndex:(out NSUInteger *)symbolIndex
|
||||||
forSplitLocation:(NSUInteger)loc;
|
forSplitLocation:(NSUInteger)loc;
|
||||||
@@ -42,13 +41,28 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
[self validateSymbols:symbols];
|
[self validateSymbols:symbols];
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (self) {
|
if (self) {
|
||||||
symbols = [self repairedSymbols:symbols];
|
|
||||||
_symbols = [[NSArray alloc] initWithArray:symbols
|
_symbols = [[NSArray alloc] initWithArray:symbols
|
||||||
copyItems:YES];
|
copyItems:YES];
|
||||||
|
[self fixSymbols];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma mark Working With the Expression Tree
|
||||||
|
|
||||||
|
- (void)functionSymbolChanged:(MPFunction *)symbol atLocalIndexPath:(NSIndexPath *)indexPath
|
||||||
|
{
|
||||||
|
NSUInteger index = [_symbols indexOfObject:symbol];
|
||||||
|
if (index != NSNotFound) {
|
||||||
|
[self.parent childChanged:self atLocalIndexPath:[indexPath indexPathByAddingIndex:index]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)fixSymbols
|
||||||
|
{
|
||||||
|
_symbols = [self fixedSymbols:_symbols];
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark Primitive Methods
|
#pragma mark Primitive Methods
|
||||||
|
|
||||||
- (NSUInteger)numberOfSymbols
|
- (NSUInteger)numberOfSymbols
|
||||||
@@ -108,7 +122,6 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Deal with subsequent strings
|
|
||||||
- (void)validateSymbols:(NSArray *)symbols
|
- (void)validateSymbols:(NSArray *)symbols
|
||||||
{
|
{
|
||||||
for (id symbol in symbols) {
|
for (id symbol in symbols) {
|
||||||
@@ -121,31 +134,27 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Deal with empty strings
|
- (NSArray *)fixedSymbols:(NSArray *)symbols
|
||||||
- (NSArray *)repairedSymbols:(NSArray *)symbols
|
|
||||||
{
|
{
|
||||||
NSMutableArray *mutableSymbols = [symbols mutableCopy];
|
NSMutableArray *mutableSymbols = [symbols mutableCopy];
|
||||||
[self repairSymbols:mutableSymbols];
|
for (NSInteger index = 0; index < mutableSymbols.count; index++) {
|
||||||
return [mutableSymbols copy];
|
id next = index+1 < mutableSymbols.count ? mutableSymbols[index+1] : nil;
|
||||||
}
|
id current = mutableSymbols[index];
|
||||||
|
|
||||||
- (void)repairSymbols:(NSMutableArray *)symbols
|
|
||||||
{
|
|
||||||
for (NSInteger index = 0; index < symbols.count; index++) {
|
|
||||||
id next = index+1 < symbols.count ? symbols[index+1] : nil;
|
|
||||||
id current = symbols[index];
|
|
||||||
if ([current isString]) {
|
if ([current isString]) {
|
||||||
if ([current length] == 0) {
|
if ([current length] == 0) {
|
||||||
[symbols removeObjectAtIndex:index];
|
[mutableSymbols removeObjectAtIndex:index];
|
||||||
index--;
|
index--;
|
||||||
} else if ([next isString]) {
|
} else if ([next isString]) {
|
||||||
NSString *new = [NSString stringWithFormat:@"%@%@", current, next];
|
NSString *new = [NSString stringWithFormat:@"%@%@", current, next];
|
||||||
[symbols replaceObjectAtIndex:index withObject:new];
|
[mutableSymbols replaceObjectAtIndex:index withObject:new];
|
||||||
[symbols removeObjectAtIndex:index+1];
|
[mutableSymbols removeObjectAtIndex:index+1];
|
||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
[(MPFunction *)current setParent:self];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [mutableSymbols copy];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)getSplitOffset:(out NSUInteger *)offset
|
- (void)getSplitOffset:(out NSUInteger *)offset
|
||||||
@@ -396,20 +405,28 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation MPMutableExpression
|
@implementation MPMutableExpression {
|
||||||
|
@protected
|
||||||
|
NSInteger _editCount;
|
||||||
|
}
|
||||||
|
|
||||||
- (instancetype)initWithSymbols:(NSArray *)symbols
|
- (instancetype)initWithSymbols:(NSArray *)symbols
|
||||||
{
|
{
|
||||||
[self validateSymbols:symbols];
|
[self validateSymbols:symbols];
|
||||||
self = [super initWithSymbols:nil];
|
self = [super initWithSymbols:nil];
|
||||||
if (self) {
|
if (self) {
|
||||||
symbols = [self repairedSymbols:symbols];
|
|
||||||
_symbols = [[NSMutableArray alloc] initWithArray:symbols
|
_symbols = [[NSMutableArray alloc] initWithArray:symbols
|
||||||
copyItems:YES];
|
copyItems:YES];
|
||||||
|
[self fixSymbols];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)fixSymbols
|
||||||
|
{
|
||||||
|
_symbols = [[self fixedSymbols:_symbols] mutableCopy];
|
||||||
|
}
|
||||||
|
|
||||||
- (NSArray *)symbols
|
- (NSArray *)symbols
|
||||||
{
|
{
|
||||||
// Return an immutable array:
|
// Return an immutable array:
|
||||||
@@ -451,7 +468,9 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Revalidate structure and invalidate length
|
// Revalidate structure and invalidate length
|
||||||
[self repairSymbols:(NSMutableArray *)_symbols];
|
if (_editCount == 0) {
|
||||||
|
[self fixSymbols];
|
||||||
|
}
|
||||||
_length = 0;
|
_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -481,6 +500,22 @@ NSString *MPDivisionOperator = @"/";
|
|||||||
*insertionIndex = splitSymbolIndex;
|
*insertionIndex = splitSymbolIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)beginEditing
|
||||||
|
{
|
||||||
|
_editCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)endEditing
|
||||||
|
{
|
||||||
|
if (_editCount == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_editCount--;
|
||||||
|
if (_editCount == 0) {
|
||||||
|
[self fixSymbols];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation MPMutableExpression (MPMutableExpressionExtensionMethods)
|
@implementation MPMutableExpression (MPMutableExpressionExtensionMethods)
|
||||||
|
|||||||
@@ -14,7 +14,11 @@
|
|||||||
|
|
||||||
- (instancetype)init;
|
- (instancetype)init;
|
||||||
|
|
||||||
#pragma mark Children
|
#pragma mark Working With the Expression Tree
|
||||||
|
|
||||||
|
@property (nonatomic, weak) MPExpression *parent; // Documentation: Do not set
|
||||||
|
- (void)childChanged:(MPExpression *)child
|
||||||
|
atLocalIndexPath:(NSIndexPath *)indexPath;
|
||||||
|
|
||||||
- (NSUInteger)numberOfChildren;
|
- (NSUInteger)numberOfChildren;
|
||||||
- (MPExpression *)childAtIndex:(NSUInteger)index;
|
- (MPExpression *)childAtIndex:(NSUInteger)index;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#import "MPFunction.h"
|
#import "MPFunction.h"
|
||||||
|
#import "MPExpression.h"
|
||||||
|
|
||||||
@implementation MPFunction
|
@implementation MPFunction
|
||||||
|
|
||||||
@@ -20,7 +21,13 @@
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark Children
|
#pragma mark Working With the Expression Tree
|
||||||
|
|
||||||
|
- (void)childChanged:(MPExpression *)child atLocalIndexPath:(NSIndexPath *)indexPath
|
||||||
|
{
|
||||||
|
[self.parent functionSymbolChanged:self
|
||||||
|
atLocalIndexPath:[indexPath indexPathByAddingIndex:0]];
|
||||||
|
}
|
||||||
|
|
||||||
- (NSUInteger)numberOfChildren
|
- (NSUInteger)numberOfChildren
|
||||||
{
|
{
|
||||||
|
|||||||
15
MathPad/NSObject+MPStringTest.h
Normal file
15
MathPad/NSObject+MPStringTest.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// NSObject+MPStringTest.h
|
||||||
|
// MathPad
|
||||||
|
//
|
||||||
|
// Created by Kim Wittenburg on 20.04.14.
|
||||||
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
@interface NSObject (MPStringTest)
|
||||||
|
|
||||||
|
- (BOOL)isString;
|
||||||
|
|
||||||
|
@end
|
||||||
18
MathPad/NSObject+MPStringTest.m
Normal file
18
MathPad/NSObject+MPStringTest.m
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// NSObject+MPStringTest.m
|
||||||
|
// MathPad
|
||||||
|
//
|
||||||
|
// Created by Kim Wittenburg on 20.04.14.
|
||||||
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "NSObject+MPStringTest.h"
|
||||||
|
|
||||||
|
@implementation NSObject (MPStringTest)
|
||||||
|
|
||||||
|
- (BOOL)isString
|
||||||
|
{
|
||||||
|
return [self isKindOfClass:[NSString class]];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
Reference in New Issue
Block a user