Skip to content

Commit

Permalink
Added ULIMultiMap, a dictionary that can store several objects for on…
Browse files Browse the repository at this point in the history
…e key. Actually just a wrapper around a dictionary of arrays.
  • Loading branch information
uliwitness committed Mar 5, 2011
1 parent 29823db commit fb64045
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 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
70 changes: 70 additions & 0 deletions 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.