diff --git a/ULIMultiMap.h b/ULIMultiMap.h new file mode 100644 index 0000000..5adfd63 --- /dev/null +++ b/ULIMultiMap.h @@ -0,0 +1,26 @@ +// +// ULIMultiMap.h +// Propaganda +// +// Created by Uli Kusterer on 21.03.10. +// Copyright 2010 The Void Software. All rights reserved. +// + +#import + + +@interface ULIMultiMap : NSObject +{ + NSMutableDictionary* mDictionary; +} + +-(void) addObject: (id)theObj forKey: (id)theKey; // Adds it to the list of objects. +-(void) setObject: (id)theObj forKey: (id)theKey; // Removes all other objects. + +-(void) removeObject: (id)theObj forKey: (id)theKey; +-(void) removeObjectsForKey: (id)theKey; +-(void) removeObject: (id)theObj; + +-(NSArray*) objectsForKey: (id)theKey; + +@end diff --git a/ULIMultiMap.m b/ULIMultiMap.m new file mode 100644 index 0000000..280f91f --- /dev/null +++ b/ULIMultiMap.m @@ -0,0 +1,70 @@ +// +// ULIMultiMap.m +// Propaganda +// +// Created by Uli Kusterer on 21.03.10. +// Copyright 2010 The Void Software. All rights reserved. +// + +#import "ULIMultiMap.h" + + +@implementation ULIMultiMap + +-(void) addObject: (id)theObj forKey: (id)theKey +{ + if( !mDictionary ) + mDictionary = [[NSMutableDictionary alloc] init]; + + NSMutableArray* arr = [mDictionary objectForKey: theKey]; + if( !arr ) + { + arr = [NSMutableArray arrayWithObject: theObj]; + [mDictionary setObject: arr forKey: theKey]; + } + else + [arr addObject: theObj]; +} + + +-(void) setObject: (id)theObj forKey: (id)theKey +{ + if( !mDictionary ) + mDictionary = [[NSMutableDictionary alloc] init]; + + [mDictionary setObject: [NSMutableArray arrayWithObject: theObj] + forKey: theKey]; +} + + +-(void) removeObject: (id)theObj forKey: (id)theKey +{ + if( !mDictionary ) + return; + + NSMutableArray* arr = [mDictionary objectForKey: theKey]; + if( !arr ) + return; + + [arr removeObject: theObj]; +} + + +-(void) removeObjectsForKey: (id)theKey; +{ + [mDictionary removeObjectForKey: theKey]; +} + + +-(void) removeObject: (id)theObj +{ + for( NSMutableArray* arr in [mDictionary allValues] ) + [arr removeObject: theObj]; +} + +-(NSArray*) objectsForKey: (id)theKey +{ + return [mDictionary objectForKey: theKey]; +} + +@end