Archived
1

Renamed NSIndexPath+MPRemoveFirstIndex Category to NSIndexPath+MPReverseIndexPath

Added MPDisplayExtension Category to MPExpression and MPFunction
Added Methods to Invalidate the Cache to MPExpressionLayout and MPFunctionLayout
This commit is contained in:
Kim Wittenburg
2014-04-23 16:05:20 +02:00
parent e048deef1f
commit fa499d299c
9 changed files with 140 additions and 14 deletions

View File

@@ -0,0 +1,21 @@
//
// MPExpression+MPDisplayExtension.h
// MathPad
//
// Created by Kim Wittenburg on 23.04.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "MPModel.h"
@interface MPExpression (MPDisplayExtension)
- (void)subexpressionChangedAtRangePath:(MPRangePath *)rangePath;
@end
@interface MPFunction (MPDisplayExtension)
- (void)subexpressionChangedAtRangePath:(MPRangePath *)rangePath;
@end

View File

@@ -0,0 +1,33 @@
//
// MPExpression+MPDisplayExtension.m
// MathPad
//
// Created by Kim Wittenburg on 23.04.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "MPDisplayExtension.h"
@implementation MPExpression (MPDisplayExtension)
- (void)subexpressionChangedAtRangePath:(MPRangePath *)rangePath
{
NSUInteger index = [self.parent indexOfChild:self];
NSIndexPath *newLocation = [rangePath.location indexPathByPrecedingIndex:index];
[self.parent subexpressionChangedAtRangePath:[[MPRangePath alloc] initWithLocation:newLocation
length:rangePath.length]];
}
@end
@implementation MPFunction (MPDisplayExtension)
- (void)subexpressionChangedAtRangePath:(MPRangePath *)rangePath
{
NSUInteger index = [self.parent indexOfSymbol:self];
NSIndexPath *newLocation = [rangePath.location indexPathByPrecedingIndex:index];
[self.parent subexpressionChangedAtRangePath:[[MPRangePath alloc] initWithLocation:newLocation
length:rangePath.length]];
}
@end

View File

@@ -74,6 +74,7 @@
- (void)invalidate
{
_valid = NO;
[self.parent invalidate];
}
- (void)expressionEditedInRange:(NSRange)range replacementLength:(NSUInteger)length
@@ -86,7 +87,7 @@
[newPlaceholders addObject:[NSNull null]];
}
[_symbolCache replaceObjectsInRange:range withObjectsFromArray:newPlaceholders];
_valid = NO;
[self invalidate];
}
- (BOOL)hasCacheForSymbolAtIndex:(NSUInteger)index

View File

@@ -40,6 +40,7 @@
#pragma mark Cache Methods
- (void)invalidate;
- (void)editedChildAtIndex:(NSUInteger)index;
- (BOOL)hasCacheForChildAtIndex:(NSUInteger)index;
- (MPExpressionLayout *)expressionLayoutForChildAtIndex:(NSUInteger)index;

View File

@@ -73,6 +73,15 @@
- (void)invalidate
{
_valid = NO;
[self.parent invalidate];
}
- (void)editedChildAtIndex:(NSUInteger)index
{
if ([self hasCacheForChildAtIndex:index]) {
_childCache[index] = [NSNull null];
}
[self invalidate];
}
- (BOOL)hasCacheForChildAtIndex:(NSUInteger)index

View File

@@ -16,9 +16,10 @@
#import "MPException.h"
#import "MPExpression.h"
#import "MPFunction.h"
#import "MPRangePath.h"
#import "NSObject+MPStringTest.h"
#import "NSTextStorage+MPSetContents.h"
#import "NSIndexPath+MPRemoveFirstIndex.h"
#import "NSIndexPath+MPReverseIndexPath.h"
#endif

View File

@@ -0,0 +1,23 @@
//
// NSIndexPath+MPRemoveFirstIndex.h
// MathPad
//
// Created by Kim Wittenburg on 23.04.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSIndexPath (MPReverseIndexPath)
/*!
@method indexPathByRemovingFirstIndex
@brief Provides an index path with the indexes in the receiving index path, excluding the first one.
@discussion Returns an empty NSIndexPath instance if the receiving index paths length is 1 or less.
@return New index path with the receiving index paths indexes, excluding the first one.
*/
- (NSIndexPath *)indexPathByRemovingFirstIndex;
- (NSIndexPath *)indexPathByPrecedingIndex:(NSUInteger)index;
@end

View File

@@ -0,0 +1,37 @@
//
// NSIndexPath+MPRemoveFirstIndex.m
// MathPad
//
// Created by Kim Wittenburg on 23.04.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "NSIndexPath+MPReverseIndexPath.h"
@implementation NSIndexPath (MPReverseIndexPath)
- (NSIndexPath *)indexPathByRemovingFirstIndex
{
if (self.length <= 1) {
return [[NSIndexPath alloc] init];
}
NSUInteger indexes[self.length];
[self getIndexes:indexes];
NSUInteger newIndexes[self.length-1];
for (NSUInteger i = 0; i < self.length-1; i++) {
newIndexes[i] = indexes[i+1];
}
return [[NSIndexPath alloc] initWithIndexes:newIndexes length:self.length-1];
}
- (NSIndexPath *)indexPathByPrecedingIndex:(NSUInteger)index
{
NSUInteger newIndexes[self.length+1];
newIndexes[0] = index;
for (NSUInteger i = 0; i < self.length; i++) {
newIndexes[i+1] = [self indexAtPosition:i];
}
return [[NSIndexPath alloc] initWithIndexes:newIndexes length:self.length+1];
}
@end