Add missing array filtering functionality

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25056 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2007-04-15 09:50:48 +00:00
parent b6c9a9e42a
commit 06d1115491
3 changed files with 38 additions and 4 deletions

View file

@ -3,7 +3,11 @@
* Source/NSBundle.m:
([+pathForResource:ofType:inRootPath:inDirectory:withVersion:])
Tolerate nil/empty resource name for compatibility with MacOS-X
nad to fix bug #19588
and to fix bug #19588
* Source/NSPredicate.m: Implement ([-filterUsingPredicate:]) for
NSMutableArray.
* Headers/Foundation/NSPredicate.h: Add the
([-filterUsingPredicate:]) method and document it.
2007-04-13 Ricccardo Mottola <riccardo@kaffe.org>

View file

@ -51,9 +51,19 @@ extern "C" {
@end
@interface NSArray (NSPredicate)
/** Evaluate each object in the array using the specified predicate and
* return an array containing all the objects which evaluate to YES.
*/
- (NSArray *) filteredArrayUsingPredicate: (NSPredicate *)predicate;
@end
@interface NSMutableArray (NSPredicate)
/** Evaluate each object in the array using the specified predicate and
* remove each objects which evaluates to NO.
*/
- (void) filterUsingPredicate: (NSPredicate *)predicate;
@end
#if defined(__cplusplus)
}
#endif

View file

@ -37,6 +37,8 @@
#include <Foundation/NSString.h>
#include <Foundation/NSValue.h>
#include "GSPrivate.h"
#include <stdarg.h>
#define NIMP [NSException raise: NSGenericException \
@ -1252,11 +1254,10 @@
- (NSArray *) filteredArrayUsingPredicate: (NSPredicate *)predicate
{
NSMutableArray *result;
NSEnumerator *e = [self objectEnumerator];
id object;
result = [NSMutableArray arrayWithCapacity: [self count]];
result = [GSMutableArray arrayWithCapacity: [self count]];
while ((object = [e nextObject]) != nil)
{
if ([predicate evaluateWithObject: object] == YES)
@ -1264,7 +1265,26 @@
[result addObject: object]; // passes filter
}
}
return result; // we could/should convert to a non-mutable copy
return [result makeImmutableCopyOnFail: NO];
}
@end
@implementation NSMutableArray (NSPredicate)
- (void) filterUsingPredicate: (NSPredicate *)predicate
{
unsigned count = [self count];
while (count-- > 0)
{
id object = [self objectAtIndex: count];
if ([predicate evaluateWithObject: object] == NO)
{
[self removeObjectAtIndex: count];
}
}
}
@end