Index: src/OFMutableSet.h ================================================================== --- src/OFMutableSet.h +++ src/OFMutableSet.h @@ -35,6 +35,21 @@ * \brief Removes the specified object from the set. * * \param object The object to remove from the set */ - (void)removeObject: (id)object; + +/** + * \brief Removes all objects from the receiver that are in the specified set. + * + * \param set The set whose objects will be removed from the receiver + */ +- (void)minusSet: (OFSet*)set; + +/** + * \brief Removes all objects from the receiver that are not in the specified + * set. + * + * \param set The set to intersect + */ +- (void)intersectSet: (OFSet*)set; @end Index: src/OFMutableSet.m ================================================================== --- src/OFMutableSet.m +++ src/OFMutableSet.m @@ -19,10 +19,11 @@ #define OF_MUTABLE_SET_M #import "OFMutableSet.h" #import "OFDictionary.h" #import "OFNull.h" +#import "OFArray.h" #import "OFAutoreleasePool.h" @implementation OFMutableSet - (void)addObject: (id)object { @@ -37,10 +38,37 @@ { [dictionary removeObjectForKey: object]; mutations++; } + +- (void)minusSet: (OFSet*)set +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFEnumerator *enumerator = [set objectEnumerator]; + id object; + + while ((object = [enumerator nextObject]) != nil) + [self removeObject: object]; + + [pool release]; +} + +- (void)intersectSet: (OFSet*)set +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + OFArray *objects = [dictionary allKeys]; + id *cArray = [objects cArray]; + size_t count = [objects count]; + size_t i; + + for (i = 0; i < count; i++) + if (![set containsObject: cArray[i]]) + [self removeObject: cArray[i]]; + + [pool release]; +} - (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state objects: (id*)objects count: (int)count { Index: tests/OFSet.m ================================================================== --- tests/OFSet.m +++ tests/OFSet.m @@ -75,10 +75,22 @@ TEST(@"-[intersectsSet:]", [(set2 = [OFSet setWithObjects: @"x", nil]) intersectsSet: set1] && [set1 intersectsSet: set2] && ![([OFSet setWithObjects: @"1", nil]) intersectsSet: set1]); + TEST(@"-[minusSet:]", + R([mutableSet minusSet: ([OFSet setWithObjects: @"x", nil])]) && + [mutableSet isEqual: ([OFSet setWithObjects: @"baz", @"bar", nil])]) + + TEST(@"-[intersectSet:]", + R([mutableSet intersectSet: ([OFSet setWithObjects: @"baz", + nil])]) && [mutableSet isEqual: ([OFSet setWithObjects: @"baz", + nil])]) + + [mutableSet addObject: @"baz"]; + [mutableSet addObject: @"x"]; + #ifdef OF_HAVE_FAST_ENUMERATION ok = YES; i = 0; for (OFString *s in set1) {