Fundamental Redesign of the Model
Started to add Documentation
This commit is contained in:
@@ -19,6 +19,9 @@
|
||||
|
||||
@interface MPExpression (MPExpressionPrivate)
|
||||
|
||||
- (NSUInteger)lengthOfElements:(NSArray *)elements;
|
||||
- (NSUInteger)indexOfElementAtLocation:(NSUInteger)location;
|
||||
|
||||
- (void)validateElements:(NSArray *)elements;
|
||||
- (BOOL)splitElementsAtLocation:(NSUInteger)location
|
||||
insertionIndex:(out NSUInteger *)insertionIndex;
|
||||
@@ -29,6 +32,22 @@
|
||||
|
||||
@implementation MPExpression (MPExpressionPrivate)
|
||||
|
||||
- (NSUInteger)lengthOfElements:(NSArray *)elements
|
||||
{
|
||||
NSUInteger length = 0;
|
||||
for (id<MPExpressionElement> element in elements) {
|
||||
length += element.length;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfElementAtLocation:(NSUInteger)location
|
||||
{
|
||||
NSUInteger index = 0;
|
||||
[self calculateSplitOffsetForSplitLocation:location inElementAtIndex:&index];
|
||||
return index;
|
||||
}
|
||||
|
||||
- (void)validateElements:(NSArray *)elements
|
||||
{
|
||||
for (id element in elements) {
|
||||
@@ -89,8 +108,9 @@
|
||||
|
||||
@implementation MPExpression {
|
||||
NSUInteger _cachedLength;
|
||||
NSRange editedRange;
|
||||
NSRange replacementRange;
|
||||
NSRange _editedRange;
|
||||
BOOL _didSplitEndOnEditing;
|
||||
NSUInteger _replacementLength;
|
||||
}
|
||||
|
||||
@synthesize elements = _elements;
|
||||
@@ -111,7 +131,6 @@
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_cachedLength = 0;
|
||||
_elements = [[NSMutableArray alloc] initWithCapacity:elements.count];
|
||||
_elements = [[NSMutableArray alloc] initWithArray:elements
|
||||
copyItems:YES];
|
||||
[self fixElements];
|
||||
@@ -123,29 +142,29 @@
|
||||
- (void)fixElements
|
||||
{
|
||||
for (NSUInteger index = 0; index < self.elements.count; index++) {
|
||||
id<MPExpressionElement> next = index+1 < self.elements.count ? self.elements[index+1] :nil;
|
||||
id<MPExpressionElement> next = index+1 < self.elements.count ? self.elements[index+1] : nil;
|
||||
id<MPExpressionElement> current = self.elements[index];
|
||||
if ([current isString]) {
|
||||
if (current.length == 0) {
|
||||
[self.elements removeObjectAtIndex:index];
|
||||
if (index < replacementRange.location) {
|
||||
replacementRange.location--;
|
||||
} else if (index < NSMaxRange(replacementRange)-1) {
|
||||
replacementRange.length--;
|
||||
} else if (index == NSMaxRange(replacementRange)) {
|
||||
editedRange.length++;
|
||||
if (index >= _editedRange.location && index < NSMaxRange(_editedRange)) {
|
||||
--_replacementLength;
|
||||
}
|
||||
--index;
|
||||
} else if ([next isString]) {
|
||||
NSString *new = [NSString stringWithFormat:@"%@%@", current, next];
|
||||
[self.elements replaceObjectsInRange:NSMakeRange(index, 2)
|
||||
withObjectsFromArray:@[new]];
|
||||
if (index < replacementRange.location) {
|
||||
replacementRange.location--;
|
||||
} else if (index < NSMaxRange(replacementRange)-1) {
|
||||
replacementRange.length--;
|
||||
} else if (index == NSMaxRange(replacementRange)-1) {
|
||||
editedRange.length++;
|
||||
NSUInteger maxReplacementIndex = _editedRange.location + _replacementLength;
|
||||
if (index == _editedRange.location - 1) {
|
||||
--_editedRange.location;
|
||||
++_editedRange.length;
|
||||
} else if (index >= _editedRange.location && index < maxReplacementIndex - 1) {
|
||||
--_replacementLength;
|
||||
} else if (index == maxReplacementIndex - 1) {
|
||||
if (!_didSplitEndOnEditing) {
|
||||
++_editedRange.length;
|
||||
}
|
||||
}
|
||||
--index;
|
||||
}
|
||||
@@ -159,9 +178,7 @@
|
||||
- (NSUInteger)length
|
||||
{
|
||||
if (_cachedLength == 0) {
|
||||
for (id<MPExpressionElement> element in self.elements) {
|
||||
_cachedLength += element.length;
|
||||
}
|
||||
_cachedLength = [self lengthOfElements:self.elements];
|
||||
}
|
||||
return _cachedLength;
|
||||
}
|
||||
@@ -171,9 +188,20 @@
|
||||
return self.elements.count;
|
||||
}
|
||||
|
||||
- (id<MPExpressionElement>)elementAtIndex:(NSUInteger)index
|
||||
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx
|
||||
{
|
||||
return self.elements[index];
|
||||
[self replaceSymbolsInRange:NSMakeRange(idx, 1)
|
||||
withElements:@[obj]];
|
||||
}
|
||||
|
||||
- (id)objectAtIndexedSubscript:(NSUInteger)idx
|
||||
{
|
||||
return [self elementAtIndex:idx];
|
||||
}
|
||||
|
||||
- (id<MPExpressionElement>)elementAtIndex:(NSUInteger)anIndex
|
||||
{
|
||||
return self.elements[anIndex];
|
||||
}
|
||||
|
||||
- (NSArray *)elementsInRange:(NSRange)range
|
||||
@@ -181,12 +209,13 @@
|
||||
return [self.elements subarrayWithRange:range];
|
||||
}
|
||||
|
||||
#warning If multiple equal expressions exist errors may occur...
|
||||
- (NSUInteger)indexOfElement:(id<MPExpressionElement>)element
|
||||
{
|
||||
return [self.elements indexOfObject:element];
|
||||
}
|
||||
|
||||
- (void)replaceElementsInRange:(NSRange)range
|
||||
- (void)replaceSymbolsInRange:(NSRange)range
|
||||
withElements:(NSArray *)elements
|
||||
{
|
||||
if (NSMaxRange(range) > self.length) {
|
||||
@@ -197,7 +226,7 @@
|
||||
[self validateElements:elements];
|
||||
|
||||
// Locate the position, split the elements
|
||||
NSUInteger startIndex;
|
||||
NSUInteger startIndex; // startIndex is inclusive
|
||||
BOOL didSplitStart = NO;
|
||||
if ([self numberOfElements] == 0) {
|
||||
startIndex = 0;
|
||||
@@ -205,27 +234,30 @@
|
||||
didSplitStart = [self splitElementsAtLocation:range.location
|
||||
insertionIndex:&startIndex];
|
||||
}
|
||||
NSUInteger endIndex;
|
||||
NSUInteger endIndex; // endIndex is exclusive
|
||||
BOOL didSplitEnd = [self splitElementsAtLocation:NSMaxRange(range)
|
||||
insertionIndex:&endIndex];
|
||||
|
||||
// Perform the replacement
|
||||
NSArray *newElements = [[NSArray alloc] initWithArray:elements
|
||||
copyItems:YES];
|
||||
NSMutableArray *newElements = [[NSMutableArray alloc] initWithArray:elements
|
||||
copyItems:YES];
|
||||
[self.elements replaceObjectsInRange:NSMakeRange(startIndex, endIndex-startIndex)
|
||||
withObjectsFromArray:newElements];
|
||||
|
||||
|
||||
_cachedLength = 0;
|
||||
NSUInteger editingStart = startIndex - (didSplitStart?1:0);
|
||||
NSUInteger editingLength = endIndex - startIndex + (didSplitStart?1:0) + (didSplitEnd?1:0);
|
||||
editedRange = NSMakeRange(editingStart, editingLength);
|
||||
replacementRange = NSMakeRange(startIndex, elements.count);
|
||||
|
||||
NSUInteger editLocation = startIndex - (didSplitStart ? 1 : 0);
|
||||
NSUInteger editLength = range.length > 0 ? (endIndex - startIndex) : 0;
|
||||
_editedRange = NSMakeRange(editLocation, editLength);
|
||||
_didSplitEndOnEditing = didSplitEnd;
|
||||
_replacementLength = elements.count + (didSplitStart ? 1 : 0) + (didSplitEnd ? 1 : 0);
|
||||
|
||||
[self fixElements];
|
||||
MPRangePath *changePath = [[MPRangePath alloc] initWithRange:editedRange];
|
||||
[self didChangeElementsInRangePath:changePath replacementLength:replacementRange.length];
|
||||
[self didChangeElementsInRangePath:[[MPRangePath alloc] initWithRange:_editedRange]
|
||||
replacementLength:_replacementLength];
|
||||
}
|
||||
|
||||
|
||||
- (double)doubleValue
|
||||
{
|
||||
#warning Unimplemented Method
|
||||
@@ -266,6 +298,7 @@
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
#warning Bad Implementation
|
||||
NSMutableString *description = [[NSMutableString alloc] init];
|
||||
NSUInteger index = 0;
|
||||
for (id element in self.elements) {
|
||||
@@ -347,6 +380,16 @@
|
||||
return [targetExpression elementsInRange:rangePath.rangeAtLastIndex];
|
||||
}
|
||||
|
||||
- (NSIndexPath *)indexPath
|
||||
{
|
||||
if (self.parent) {
|
||||
NSUInteger selfIndex = [self.parent indexOfChild:self];
|
||||
return [[self.parent indexPath] indexPathByAddingIndex:selfIndex];
|
||||
} else {
|
||||
return [[NSIndexPath alloc] init];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark Working With Expressions
|
||||
- (MPExpression *)subexpressionFromLocation:(NSUInteger)from
|
||||
{
|
||||
@@ -377,7 +420,7 @@
|
||||
|
||||
- (void)appendElements:(NSArray *)elements
|
||||
{
|
||||
[self replaceElementsInRange:NSMakeRange(self.length, 0) withElements:elements];
|
||||
[self replaceSymbolsInRange:NSMakeRange(self.length, 0) withElements:elements];
|
||||
}
|
||||
|
||||
- (void)insertElement:(id<MPExpressionElement>)anElement atLocation:(NSUInteger)index
|
||||
@@ -387,12 +430,12 @@
|
||||
|
||||
- (void)insertElements:(NSArray *)elements atLocation:(NSUInteger)index
|
||||
{
|
||||
[self replaceElementsInRange:NSMakeRange(index, 0) withElements:elements];
|
||||
[self replaceSymbolsInRange:NSMakeRange(index, 0) withElements:elements];
|
||||
}
|
||||
|
||||
- (void)deleteElementsInRange:(NSRange)range
|
||||
{
|
||||
[self replaceElementsInRange:range withElements:@[]];
|
||||
[self replaceSymbolsInRange:range withElements:@[]];
|
||||
}
|
||||
|
||||
#pragma mark Evaluating Expressions
|
||||
|
||||
Reference in New Issue
Block a user