Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -18,10 +18,11 @@ @class OFDataArray; @class OFString; #ifdef OF_HAVE_BLOCKS typedef void (^of_array_enumeration_block_t)(id obj, size_t idx, BOOL *stop); +typedef BOOL (^of_array_filter_block_t)(id odj, size_t idx); #endif /** * \brief A class for storing objects in an array. */ @@ -203,10 +204,20 @@ * Executes a block for each object. * * \param block The block to execute for each object */ - (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block; + +/** + * Returns a new array, only containing the objects for which the block returns + * YES. + * + * \param block A block which determines if the object should be in the new + * array + * \return A new, autoreleased OFArray + */ +- (OFArray*)filteredArrayUsingBlock: (of_array_filter_block_t)block; #endif @end @interface OFArrayEnumerator: OFEnumerator { Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -400,10 +400,31 @@ BOOL stop = NO; for (i = 0; i < count && !stop; i++) block(objs[i], i, &stop); } + +- (OFArray*)filteredArrayUsingBlock: (of_array_filter_block_t)block +{ + size_t count = [array count]; + id *tmp = [self allocMemoryForNItems: count + withSize: sizeof(id)]; + + @try { + id *objs = [array cArray]; + size_t i, j = 0; + + for (i = 0; i < count; i++) + if (block(objs[i], i)) + tmp[j++] = objs[i]; + + return [OFArray arrayWithCArray: tmp + length: j]; + } @finally { + [self freeMemory: tmp]; + } +} #endif - (void)dealloc { id *objs = [array cArray]; Index: src/OFDictionary.h ================================================================== --- src/OFDictionary.h +++ src/OFDictionary.h @@ -17,10 +17,11 @@ @class OFArray; #ifdef OF_HAVE_BLOCKS typedef void (^of_dictionary_enumeration_block_t)(id key, id obj, BOOL *stop); +typedef BOOL (^of_dictionary_filter_block_t)(id key, id obj); #endif struct of_dictionary_bucket { id key; @@ -162,10 +163,21 @@ * * \param block The block to execute for each key / object pair. */ - (void)enumerateKeysAndObjectsUsingBlock: (of_dictionary_enumeration_block_t)block; + +/** + * Returns a new dictionary, only containing the objects for which the block + * returns YES. + * + * \param block A block which determines if the object should be in the new + * dictionary + * \return A new, autoreleased OFDictionary + */ +- (OFDictionary*)filteredDictionaryUsingBlock: + (of_dictionary_filter_block_t)block; #endif @end @interface OFDictionaryEnumerator: OFEnumerator { Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -572,10 +572,25 @@ for (i = 0; i < size && !stop; i++) if (data[i] != NULL && data[i] != DELETED) block(data[i]->key, data[i]->object, &stop); } + +- (OFDictionary*)filteredDictionaryUsingBlock: + (of_dictionary_filter_block_t)block +{ + OFMutableDictionary *dict = [OFMutableDictionary dictionary]; + size_t i; + + for (i = 0; i < size; i++) + if (data[i] != NULL && data[i] != DELETED) + if (block(data[i]->key, data[i]->object)) + [dict setObject: data[i]->object + forKey: data[i]->key]; + + return dict; +} #endif - (void)dealloc { uint32_t i; Index: tests/OFArrayTests.m ================================================================== --- tests/OFArrayTests.m +++ tests/OFArrayTests.m @@ -246,12 +246,16 @@ case 1: return @"bar"; } return nil; - }]) && [[m[0] objectAtIndex: 0] isEqual: @"foo"] && - [[m[0] objectAtIndex: 1] isEqual: @"bar"]) + }]) && [[m[0] description] isEqual: @"(foo, bar)"]) + + TEST(@"-[filteredArrayUsingBlock:]", + [[[m[0] filteredArrayUsingBlock: ^ BOOL (id obj, size_t idx) { + return ([obj isEqual: @"foo"] ? YES : NO); + }] description] isEqual: @"(foo)"]) #endif [pool drain]; } @end Index: tests/OFDictionaryTests.m ================================================================== --- tests/OFDictionaryTests.m +++ tests/OFDictionaryTests.m @@ -141,10 +141,15 @@ return @"value_2"; return nil; }]) && [[dict objectForKey: keys[0]] isEqual: @"value_1"] && [[dict objectForKey: keys[1]] isEqual: @"value_2"]) + + TEST(@"-[filteredDictionaryUsingBlock:]", + [[[dict filteredDictionaryUsingBlock: ^ BOOL (id key, id obj) { + return ([key isEqual: keys[0]] ? YES : NO); + }] description] isEqual: @"{key1 = value_1}"]) #endif TEST(@"-[count]", [dict count] == 2) TEST(@"+[dictionaryWithKeysAndObjects:]",