Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 fb64045
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |