Source

DTFoundation / DTFoundation / DTOrderedDictionary.m

//
//  DTOrderedDictionary.m
//  DTKit
//
//  Created by Daniel Tull on 21.05.2009.
//  Copyright 2009 Daniel Tull. All rights reserved.
//

#import "DTOrderedDictionary.h"


@implementation DTOrderedDictionary

@synthesize sortedKeys;

- (id)init {
	return [self initWithSortSelector:@selector(caseInsensitiveCompare:)];
}

- (id)initWithSortSelector:(SEL)aSelector {
	
	if (!(self = [super init])) return nil;
	
	superDictionary = [[NSMutableDictionary alloc] init];
	
	sortSelector = aSelector;
	sortedKeys = [[NSArray alloc] init];
	
	return self;
}

- (void)dealloc {
	[superDictionary release];
	[sortedKeys release];
	[super dealloc];
}

#pragma mark NSMutableDictionary specific overrides

- (void)setObject:(id)anObject forKey:(id)aKey {
	if (![self.sortedKeys containsObject:aKey])
		self.sortedKeys = [sortedKeys arrayByAddingObject:aKey];//[[sortedKeys arrayByAddingObject:aKey] sortedArrayUsingSelector:sortSelector];
	
	[superDictionary setObject:anObject forKey:aKey];
}

- (void)removeObjectForKey:(id)aKey {
	NSMutableArray *newArray = [self.sortedKeys mutableCopy];
	[newArray removeObject:aKey];
	self.sortedKeys = newArray;//[newArray sortedArrayUsingSelector:sortSelector];
	[newArray release];
	
	[superDictionary removeObjectForKey:aKey];
}

- (NSUInteger)count {
	return [superDictionary count];
}

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

- (NSEnumerator *)keyEnumerator {
	return [superDictionary keyEnumerator];
}

- (NSArray *)allKeys {
	return self.sortedKeys;
}

- (NSString *)description {
	return [superDictionary description];
}

@end