Last active
March 26, 2019 19:18
-
-
Save limtbk/d4b65eee982eaad8ae90d4541d416ef6 to your computer and use it in GitHub Desktop.
Test exercise - caching class with FIFO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// CacheImpl | |
// | |
// Created by lim on 3/26/19. | |
// Copyright © 2019 lim. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface CacheItem : NSObject | |
@property (strong) NSString *string; | |
@property (assign) NSInteger first; | |
@property (assign) NSInteger second; | |
- (instancetype) initWithString:(NSString *) string first:(NSInteger)first second:(NSInteger)second; | |
@end | |
@implementation CacheItem | |
- (instancetype) initWithString:(NSString *) string first:(NSInteger)first second:(NSInteger)second { | |
self = [super init]; | |
if (self) { | |
self.string = string; | |
self.first = first; | |
self.second = second; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"\"%@\", %li, %li", self.string, self.first, self.second]; | |
} | |
@end | |
@interface CacheContainer : NSObject | |
- (instancetype) initWithCapacity:(NSInteger) capacity; | |
- (void)put:(NSString *)uri item:(NSObject *)item; | |
- (NSObject *)get:(NSString *)uri; | |
@end | |
@interface CacheContainer () | |
@property (assign) NSInteger capacity; | |
@property (assign) NSInteger index; | |
@property (strong) NSMutableDictionary *storage; | |
@property (strong) NSMutableDictionary *indexes; | |
@end | |
@implementation CacheContainer | |
- (instancetype) initWithCapacity:(NSInteger) capacity { | |
self = [super init]; | |
if (self) { | |
self.capacity = capacity; | |
self.storage = [NSMutableDictionary dictionary]; | |
self.indexes = [NSMutableDictionary dictionary]; | |
} | |
return self; | |
} | |
- (void)put:(NSString *)uri item:(NSObject *)item { | |
@synchronized (self.storage) { | |
if (self.storage.count >= self.capacity) { | |
NSString *keyToRemove = self.indexes[@(self.index - self.capacity)]; | |
if (keyToRemove) { | |
[self.storage removeObjectForKey:keyToRemove]; | |
[self.indexes removeObjectForKey:@(self.index - self.capacity)]; | |
} | |
} | |
self.storage[uri] = item; | |
self.indexes[@(self.index)] = uri; | |
self.index++; | |
} | |
} | |
- (NSObject *)get:(NSString *)uri { | |
@synchronized (self.storage) { | |
return self.storage[uri]; | |
} | |
} | |
@end | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
CacheContainer *cc = [[CacheContainer alloc] initWithCapacity:3]; | |
[cc put:@"uri1" item:[[CacheItem alloc] initWithString:@"1" first:1 second:1]]; | |
[cc put:@"uri2" item:[[CacheItem alloc] initWithString:@"2" first:2 second:2]]; | |
[cc put:@"uri3" item:[[CacheItem alloc] initWithString:@"3" first:3 second:3]]; | |
NSLog(@"%@", [cc get:@"uri1"]); | |
NSLog(@"%@", [cc get:@"uri2"]); | |
NSLog(@"%@", [cc get:@"uri3"]); | |
[cc put:@"uri4" item:[[CacheItem alloc] initWithString:@"4" first:4 second:4]]; | |
NSLog(@"%@", [cc get:@"uri1"]); | |
NSLog(@"%@", [cc get:@"uri2"]); | |
NSLog(@"%@", [cc get:@"uri3"]); | |
NSLog(@"%@", [cc get:@"uri4"]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment