Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathPad/MPArrayCache.m
2014-09-06 01:54:15 +02:00

60 lines
1.3 KiB
Objective-C

//
// MPArrayCache.m
// MathPad
//
// Created by Kim Wittenburg on 31.08.14.
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
//
#import "MPArrayCache.h"
@implementation MPArrayCache {
NSUInteger lastIndex;
}
- (instancetype)init
{
self = [super init];
if (self) {
lastIndex = 0;
_cache = [[NSCache alloc] init];
}
return self;
}
- (void)cacheObject:(id)object
forIndex:(NSUInteger)index
{
[_cache setObject:object
forKey:@(index)];
if (index > lastIndex) {
lastIndex = index;
}
}
- (id)cachedObjectForIndex:(NSUInteger)index
{
return [_cache objectForKey:@(index)];
}
- (void)clearCacheAtIndex:(NSUInteger)index replacementLength:(NSUInteger)replacementLength
{
[_cache removeObjectForKey:@(index)];
for (NSUInteger i = index+1; i < lastIndex; i++) {
id object = [_cache objectForKey:@(i)];
if (object) {
[_cache removeObjectForKey:@(i)];
}
}
lastIndex += replacementLength - 1;
}
- (void)clearCacheInRange:(NSRange)range replacementLength:(NSUInteger)replacementLength;
- (void)replaceCachedObjectAtIndex:(NSUInteger)index
withObjects:(NSArray *)objects;
- (void)replaceCachedObjectsInRange:(NSRange)range
withObjects:(NSArray *)objects;
@end