Added Documentation
This commit is contained in:
@@ -20,12 +20,16 @@
|
||||
|
||||
#import "NSIndexPath+MPAdditions.h"
|
||||
|
||||
|
||||
|
||||
NSString *const MPIllegalElementException = @"MPIllegalElementException";
|
||||
NSString *const MPIllegalElementExceptionElementKey = @"MPIllegalElementExceptionElementKey";
|
||||
|
||||
NSString *const MPMathKitErrorDomain = @"MPMathKitErrorDomain";
|
||||
NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
|
||||
|
||||
|
||||
@interface MPExpression () {
|
||||
NSMutableArray * _elements;
|
||||
}
|
||||
@@ -62,11 +66,13 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return [self initWithElements:@[]];
|
||||
}
|
||||
|
||||
|
||||
- (instancetype)initWithElement:(id<MPExpressionElement>)element
|
||||
{
|
||||
return [self initWithElements:@[element]];
|
||||
}
|
||||
|
||||
|
||||
- (instancetype)initWithElements:(NSArray *)elements
|
||||
{
|
||||
self = [super init];
|
||||
@@ -96,7 +102,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
{
|
||||
for (id element in elements) {
|
||||
if (![element conformsToProtocol:@protocol(MPExpressionElement)]) {
|
||||
@throw [NSException exceptionWithName:@"MPIllegalElementException"
|
||||
@throw [NSException exceptionWithName:MPIllegalElementException
|
||||
reason:@"Elements must conform to the MPExpressionElement protocol."
|
||||
userInfo:@{MPIllegalElementExceptionElementKey: element}];
|
||||
}
|
||||
@@ -141,6 +147,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#pragma mark Querying Expressions
|
||||
|
||||
|
||||
@@ -152,6 +159,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return [self.parent rootExpression];
|
||||
}
|
||||
|
||||
|
||||
- (NSIndexPath *)indexPath
|
||||
{
|
||||
if (self.parent) {
|
||||
@@ -162,6 +170,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (NSUInteger)countItemsInReferenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
switch (referenceFrame) {
|
||||
@@ -182,6 +191,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (id)itemAtIndex:(NSUInteger)anIndex
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
@@ -210,6 +220,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (id<MPExpressionElement>)elementAtIndex:(NSUInteger)anIndex
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
@@ -219,20 +230,23 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return _elements[elementIndex];
|
||||
}
|
||||
|
||||
|
||||
#warning If multiple equal expressions exist errors may occur...
|
||||
- (NSUInteger)indexOfElement:(id<MPExpressionElement>)element
|
||||
{
|
||||
return [_elements indexOfObject:element];
|
||||
}
|
||||
|
||||
- (NSArray *)itemsInRange:(NSRange)range
|
||||
|
||||
- (NSArray *)itemsInRange:(NSRange)aRange
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
MPExpression *subexpression = [self subexpressionWithRange:range
|
||||
MPExpression *subexpression = [self subexpressionWithRange:aRange
|
||||
referenceFrame:referenceFrame];
|
||||
return [subexpression allItemsInReferenceFrame:referenceFrame];
|
||||
}
|
||||
|
||||
|
||||
- (NSArray *)allItemsInReferenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
switch (referenceFrame) {
|
||||
@@ -259,6 +273,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (id)elementAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath.length == 0) {
|
||||
@@ -275,26 +290,28 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSUInteger)convertIndex:(NSUInteger)index
|
||||
|
||||
- (NSUInteger)convertIndex:(NSUInteger)anIndex
|
||||
fromReferenceFrame:(MPReferenceFrame)fromReferenceFrame
|
||||
toReferenceFrame:(MPReferenceFrame)toReferenceFrame
|
||||
{
|
||||
return [self convertIndex:index
|
||||
return [self convertIndex:anIndex
|
||||
fromReferenceFrame:fromReferenceFrame
|
||||
toReferenceFrame:toReferenceFrame
|
||||
offset:NULL];
|
||||
}
|
||||
|
||||
- (NSUInteger)convertIndex:(NSUInteger)index
|
||||
|
||||
- (NSUInteger)convertIndex:(NSUInteger)anIndex
|
||||
fromReferenceFrame:(MPReferenceFrame)fromReferenceFrame
|
||||
toReferenceFrame:(MPReferenceFrame)toReferenceFrame
|
||||
offset:(NSUInteger *)offset
|
||||
offset:(out NSUInteger *)offset
|
||||
{
|
||||
if (fromReferenceFrame == toReferenceFrame || index == 0) {
|
||||
if (fromReferenceFrame == toReferenceFrame || anIndex == 0) {
|
||||
if (offset) {
|
||||
*offset = 0;
|
||||
}
|
||||
return index;
|
||||
return anIndex;
|
||||
}
|
||||
|
||||
NSUInteger symbolIndex __block = 0;
|
||||
@@ -302,16 +319,16 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
case MPElementReferenceFrame:
|
||||
[_elements enumerateObjectsUsingBlock:^(id<MPExpressionElement> obj, NSUInteger idx, BOOL *stop) {
|
||||
symbolIndex += obj.length;
|
||||
*stop = idx >= index - 1;
|
||||
*stop = idx >= anIndex - 1;
|
||||
}];
|
||||
break;
|
||||
|
||||
case MPSymbolReferenceFrame:
|
||||
symbolIndex = index;
|
||||
symbolIndex = anIndex;
|
||||
break;
|
||||
|
||||
case MPTokenReferenceFrame:
|
||||
symbolIndex = [self.tokens[index] range].location;
|
||||
symbolIndex = [self.tokens[anIndex] range].location;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -351,7 +368,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
if (NSMaxRange(token.range) < symbolIndex || token.range.location > symbolIndex) {
|
||||
continue;
|
||||
}
|
||||
NSUInteger offsetInToken = index - token.range.location;
|
||||
NSUInteger offsetInToken = anIndex - token.range.location;
|
||||
if (offsetInToken == token.range.length) {
|
||||
offsetInToken = 0;
|
||||
tokenIndex++;
|
||||
@@ -367,6 +384,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (NSRange)convertRange:(NSRange)aRange
|
||||
fromReferenceFrame:(MPReferenceFrame)fromReferenceFrame
|
||||
toReferenceFrame:(MPReferenceFrame)toReferenceFrame
|
||||
@@ -378,6 +396,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
trailingOffset:NULL];
|
||||
}
|
||||
|
||||
|
||||
- (NSRange)convertRange:(NSRange)aRange
|
||||
fromReferenceFrame:(MPReferenceFrame)fromReferenceFrame
|
||||
toReferenceFrame:(MPReferenceFrame)toReferenceFrame
|
||||
@@ -399,20 +418,21 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
#pragma mark Mutating Expressions
|
||||
|
||||
|
||||
- (void)replaceItemsInRange:(NSRange)range
|
||||
- (void)replaceItemsInRange:(NSRange)aRange
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
withElements:(NSArray *)elements
|
||||
{
|
||||
NSUInteger start = [self convertIndex:range.location
|
||||
NSUInteger start = [self convertIndex:aRange.location
|
||||
fromReferenceFrame:referenceFrame
|
||||
toReferenceFrame:MPSymbolReferenceFrame];
|
||||
NSUInteger end = [self convertIndex:NSMaxRange(range)
|
||||
NSUInteger end = [self convertIndex:NSMaxRange(aRange)
|
||||
fromReferenceFrame:referenceFrame
|
||||
toReferenceFrame:MPSymbolReferenceFrame];
|
||||
[self _replaceSymbolsInRange:NSMakeRange(start, end - start)
|
||||
withElements:elements];
|
||||
}
|
||||
|
||||
|
||||
- (void)_replaceSymbolsInRange:(NSRange)range
|
||||
withElements:(NSArray *)elements
|
||||
{
|
||||
@@ -439,8 +459,14 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
// Perform the replacement
|
||||
NSMutableArray *newElements = [[NSMutableArray alloc] initWithArray:elements
|
||||
copyItems:YES];
|
||||
NSArray *removedElements = [_elements subarrayWithRange:NSMakeRange(startIndex, endIndex-startIndex)];
|
||||
[_elements replaceObjectsInRange:NSMakeRange(startIndex, endIndex-startIndex)
|
||||
withObjectsFromArray:newElements];
|
||||
for (id<MPExpressionElement> element in removedElements) {
|
||||
if ([element isFunction]) {
|
||||
((MPFunction *)element).parent = nil;
|
||||
}
|
||||
}
|
||||
|
||||
_tokenCache = nil;
|
||||
|
||||
@@ -469,6 +495,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
replacementLength:_replacementLength];
|
||||
}
|
||||
|
||||
|
||||
- (BOOL)_splitElementsAtLocation:(NSUInteger)location
|
||||
insertionIndex:(out NSUInteger *)insertionIndex
|
||||
{
|
||||
@@ -494,6 +521,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return splitOffset != 0;
|
||||
}
|
||||
|
||||
|
||||
- (MPExpression *)subexpressionFromIndex:(NSUInteger)from
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
@@ -501,6 +529,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
referenceFrame:referenceFrame];
|
||||
}
|
||||
|
||||
|
||||
- (MPExpression *)subexpressionToIndex:(NSUInteger)to
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
@@ -508,12 +537,13 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
referenceFrame:referenceFrame];
|
||||
}
|
||||
|
||||
- (MPExpression *)subexpressionWithRange:(NSRange)range
|
||||
|
||||
- (MPExpression *)subexpressionWithRange:(NSRange)aRange
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
MPExpression *subexpression = [self copy];
|
||||
NSRange preceedingRange = NSMakeRange(0, range.location);
|
||||
NSUInteger firstOut = NSMaxRange(range);
|
||||
NSRange preceedingRange = NSMakeRange(0, aRange.location);
|
||||
NSUInteger firstOut = NSMaxRange(aRange);
|
||||
NSRange exceedingRange = NSMakeRange(firstOut, [self countItemsInReferenceFrame:referenceFrame] - firstOut);
|
||||
[subexpression deleteElementsInRange:exceedingRange
|
||||
referenceFrame:referenceFrame];
|
||||
@@ -522,8 +552,10 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return subexpression;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark Evaluating Expressions
|
||||
|
||||
|
||||
- (NSDecimalNumber *)evaluateWithError:(NSError *__autoreleasing *)error
|
||||
{
|
||||
MPExpressionTree *tree = [self parse];
|
||||
@@ -537,6 +569,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return [[MPExpressionTree alloc] initWithTokenStream:tokenStream];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark Notifications
|
||||
|
||||
|
||||
@@ -553,27 +586,6 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
#pragma mark Basic NSObject Methods
|
||||
|
||||
|
||||
/*
|
||||
- (BOOL)isEqual:(id)object
|
||||
{
|
||||
if (self == object) {
|
||||
return YES;
|
||||
}
|
||||
if (object == nil) {
|
||||
return NO;
|
||||
}
|
||||
if (![object isKindOfClass:[MPExpression class]]) {
|
||||
return NO;
|
||||
}
|
||||
return [self isEqualToExpression:(MPExpression *)object];
|
||||
}
|
||||
|
||||
- (BOOL)isEqualToExpression:(MPExpression *)anExpression
|
||||
{
|
||||
return [self.elements isEqualToArray:anExpression.elements];
|
||||
}
|
||||
*/
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
#warning Bad Implementation
|
||||
@@ -608,6 +620,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return description;
|
||||
}
|
||||
|
||||
|
||||
- (NSUInteger)hash
|
||||
{
|
||||
return [_elements hash];
|
||||
@@ -633,6 +646,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
return [self initWithElements:[aDecoder decodeObject]];
|
||||
}
|
||||
|
||||
|
||||
- (void)encodeWithCoder:(NSCoder *)aCoder
|
||||
{
|
||||
[aCoder encodeObject:_elements];
|
||||
@@ -641,51 +655,60 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
@end
|
||||
|
||||
|
||||
|
||||
@implementation MPExpression (MPExpressionConvenience)
|
||||
|
||||
#pragma mark Querying Expressions
|
||||
|
||||
|
||||
- (NSUInteger)countElements
|
||||
{
|
||||
return [self countItemsInReferenceFrame:MPElementReferenceFrame];
|
||||
}
|
||||
|
||||
|
||||
- (NSUInteger)countSymbols
|
||||
{
|
||||
return [self countItemsInReferenceFrame:MPSymbolReferenceFrame];
|
||||
}
|
||||
|
||||
|
||||
- (NSUInteger)countTokens
|
||||
{
|
||||
return [self countItemsInReferenceFrame:MPTokenReferenceFrame];
|
||||
}
|
||||
|
||||
- (id<MPExpressionElement>)elementAtIndex:(NSUInteger)index
|
||||
|
||||
- (id<MPExpressionElement>)elementAtIndex:(NSUInteger)anIndex
|
||||
{
|
||||
return [self itemAtIndex:index
|
||||
return [self itemAtIndex:anIndex
|
||||
referenceFrame:MPElementReferenceFrame];
|
||||
}
|
||||
|
||||
- (id<MPExpressionElement>)symbolAtIndex:(NSUInteger)index
|
||||
|
||||
- (id<MPExpressionElement>)symbolAtIndex:(NSUInteger)anIndex
|
||||
{
|
||||
return [self itemAtIndex:index
|
||||
return [self itemAtIndex:anIndex
|
||||
referenceFrame:MPSymbolReferenceFrame];
|
||||
}
|
||||
|
||||
- (id<MPToken>)tokenAtIndex:(NSUInteger)index
|
||||
|
||||
- (id<MPToken>)tokenAtIndex:(NSUInteger)anIndex
|
||||
{
|
||||
return [self itemAtIndex:index
|
||||
return [self itemAtIndex:anIndex
|
||||
referenceFrame:MPTokenReferenceFrame];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark Mutating Expressions
|
||||
|
||||
|
||||
- (void)appendElement:(id<MPExpressionElement>)anElement
|
||||
{
|
||||
[self appendElements:@[anElement]];
|
||||
}
|
||||
|
||||
|
||||
- (void)appendElements:(NSArray *)elements
|
||||
{
|
||||
[self replaceItemsInRange:NSMakeRange([self countItemsInReferenceFrame:MPSymbolReferenceFrame], 0)
|
||||
@@ -693,6 +716,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
withElements:elements];
|
||||
}
|
||||
|
||||
|
||||
- (void)insertElement:(id<MPExpressionElement>)anElement
|
||||
atIndex:(NSUInteger)index
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
@@ -702,6 +726,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
referenceFrame:referenceFrame];
|
||||
}
|
||||
|
||||
|
||||
- (void)insertElements:(NSArray *)elements
|
||||
atIndex:(NSUInteger)index
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
@@ -711,6 +736,7 @@ NSString *const MPExpressionPathErrorKey = @"MPExpressionPathErrorKey";
|
||||
withElements:elements];
|
||||
}
|
||||
|
||||
|
||||
- (void)deleteElementsInRange:(NSRange)range
|
||||
referenceFrame:(MPReferenceFrame)referenceFrame
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user