
#import "Cache.h"

@implementation Cache

- initWithCapacity:(unsigned)cap
{
    [super init];

    data = [[NSMutableDictionary alloc] initWithCapacity:cap];
    queue = [[NSMutableArray alloc] initWithCapacity:cap];
    capacity = cap;

    return self;
}

- (id)objectForKey:(id)aKey
{
    return [data objectForKey:aKey];
}

- (void)setObject:(id)anObject forKey:(id)aKey
{
    id lookup = [data objectForKey:aKey];

    if(lookup==nil && [data count]==capacity){
	id first = [queue objectAtIndex:0];
	[queue removeObjectAtIndex:0];

	[data removeObjectForKey:first];
    }

    [queue addObject:aKey];
    [data setObject:anObject forKey:aKey];    
}

- (void)dealloc
{
    // NSLog(@"release data/queue of size %d", capacity);
    [data release];
    [queue release];
    
    [super dealloc];
}

@end
