Skip to content
Browse files

Added ULIMultiMap, a dictionary that can store several objects for on…

…e key. Actually just a wrapper around a dictionary of arrays.
  • Loading branch information...
1 parent 29823db commit fb640451ee1c28089ace378882d239ae7850bf73 @uliwitness committed
Showing with 96 additions and 0 deletions.
  1. +26 −0 ULIMultiMap.h
  2. +70 −0 ULIMultiMap.m
View
26 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 <Cocoa/Cocoa.h>
+
+
+@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
View
70 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

0 comments on commit fb64045

Please sign in to comment.
Something went wrong with that request. Please try again.