// // MPLayout.m // MathPad // // Created by Kim Wittenburg on 11.08.14. // Copyright (c) 2014 Kim Wittenburg. All rights reserved. // #import "MPLayout.h" @interface MPLayout () // Querying and Storing Caches - (BOOL)hasCacheForElementAtIndex:(NSUInteger)index; - (void)ensureCacheSizeForIndex:(NSUInteger)index; @end @implementation MPLayout { NSMutableArray *_cache; NSSize _cachedSize; } #pragma mark Creation Methods - (id)init { self = [super init]; if (self) { _cache = [[NSMutableArray alloc] init]; _cachedSize = NSZeroSize; } return self; } - (id)initWithPath:(NSIndexPath *)path parent:(MPLayout *)parent { self = [self init]; if (self) { _parent = parent; } return self; } #pragma Text System Objects - (MPExpressionStorage *)expressionStorage { return self.parent.expressionStorage; } #pragma mark Cache Tree // Querying and Storing Caches - (BOOL)hasCacheForElementAtIndex:(NSUInteger)index { if (index >= _cache.count) { return NO; } return _cache[index] != MPNull; } - (id)cachableObjectForIndex:(NSUInteger)index generator:(id (^)())generator { if ([self hasCacheForElementAtIndex:index]) { return _cache[index]; } id object = generator(); [self ensureCacheSizeForIndex:index]; _cache[index] = object; return object; } - (void)ensureCacheSizeForIndex:(NSUInteger)index { while (index >= _cache.count) { [_cache addObject:MPNull]; } } // Clearing Caches - (void)clearCacheInRange:(NSRange)range replacementLength:(NSUInteger)replacementLength { NSMutableArray *placeholders = [[NSMutableArray alloc] initWithCapacity:replacementLength]; while (placeholders.count < replacementLength) { [placeholders addObject:MPNull]; } [_cache replaceObjectsInRange:range withObjectsFromArray:placeholders]; [self invalidate]; } - (void)invalidate { _cachedSize = NSZeroSize; [self.parent invalidate]; } #pragma mark Calculation and Drawing Methods - (NSSize)size { if (NSEqualSizes(_cachedSize, NSZeroSize)) { _cachedSize = [self generateSize]; } return _cachedSize; } - (void)drawAtPoint:(NSPoint)point { } @end @implementation MPLayout (MPSubclassImplement) - (MPLayout *)childLayoutAtIndex:(NSUInteger)index { return nil; } - (NSSize)sizeForChildAtIndex:(NSUInteger)index { return NSZeroSize; } - (NSSize)generateSize { return NSZeroSize; } @end