Overview
Comment: | Add -[mapped{Array,Dictionary}UsingBlock:]. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
90c26dded53c98591dbe2edb26dd3c6e |
User & Date: | js on 2010-12-12 01:21:13 |
Other Links: | manifest | tags |
Context
2010-12-12
| ||
14:18 | Fix missing colon. check-in: 54dda023d6 user: js tags: trunk | |
01:21 | Add -[mapped{Array,Dictionary}UsingBlock:]. check-in: 90c26dded5 user: js tags: trunk | |
00:38 | Add -[filtered{Array,Dictionary}UsingBlock]. check-in: 42060f220c user: js tags: trunk | |
Changes
Modified src/OFArray.h from [1b937822bd] to [eb78f84a2f].
︙ | ︙ | |||
17 18 19 20 21 22 23 24 25 26 27 28 29 30 | @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. */ @interface OFArray: OFObject <OFCopying, OFMutableCopying, OFCollection, OFFastEnumeration> | > | 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | @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); typedef id (^of_array_map_block_t)(id obj, size_t idx); #endif /** * \brief A class for storing objects in an array. */ @interface OFArray: OFObject <OFCopying, OFMutableCopying, OFCollection, OFFastEnumeration> |
︙ | ︙ | |||
203 204 205 206 207 208 209 210 211 212 213 214 215 216 | /** * 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 | > > > > > > > > | 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | /** * 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, mapping each object using the specified block. * * \param block A block which maps an object for each object * \return A new, autoreleased OFArray */ - (OFArray*)mappedArrayUsingBlock: (of_array_map_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 |
︙ | ︙ |
Modified src/OFArray.m from [654b3f415b] to [e5f9ad914b].
︙ | ︙ | |||
398 399 400 401 402 403 404 405 406 407 408 409 410 411 | id *objs = [array cArray]; size_t i, count = [array count]; 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)]; | > > > > > > > > > > > > > > > > > > > > | 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | id *objs = [array cArray]; size_t i, count = [array count]; BOOL stop = NO; for (i = 0; i < count && !stop; i++) block(objs[i], i, &stop); } - (OFArray*)mappedArrayUsingBlock: (of_array_map_block_t)block { size_t count = [array count]; id *tmp = [self allocMemoryForNItems: count withSize: sizeof(id)]; @try { id *objs = [array cArray]; size_t i; for (i = 0; i < count; i++) tmp[i] = block(objs[i], i); return [OFArray arrayWithCArray: tmp length: count]; } @finally { [self freeMemory: tmp]; } } - (OFArray*)filteredArrayUsingBlock: (of_array_filter_block_t)block { size_t count = [array count]; id *tmp = [self allocMemoryForNItems: count withSize: sizeof(id)]; |
︙ | ︙ |
Modified src/OFDictionary.h from [03c29285b1] to [058db422e8].
︙ | ︙ | |||
16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #import "OFEnumerator.h" @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 <OFCopying> key; id object; uint32_t hash; | > | 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #import "OFEnumerator.h" @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); typedef id (^of_dictionary_map_block_t)(id key, id obj); #endif struct of_dictionary_bucket { id <OFCopying> key; id object; uint32_t hash; |
︙ | ︙ | |||
162 163 164 165 166 167 168 169 170 171 172 173 174 175 | * Executes a block for each key / object pair. * * \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 | > > > > > > > > | 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | * Executes a block for each key / object pair. * * \param block The block to execute for each key / object pair. */ - (void)enumerateKeysAndObjectsUsingBlock: (of_dictionary_enumeration_block_t)block; /** * Returns a new dictionary, mapping each object using the specified block. * * \param block A block which maps an object for each object * \return A new, autorelease OFDictionary */ - (OFDictionary*)mappedDictionaryUsingBlock: (of_dictionary_map_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 |
︙ | ︙ |
Modified src/OFDictionary.m from [971d7fc20d] to [f78b09f33a].
︙ | ︙ | |||
570 571 572 573 574 575 576 577 578 579 580 581 582 583 | size_t i; BOOL stop = NO; 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; | > > > > > > > > > > > > > | 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | size_t i; BOOL stop = NO; for (i = 0; i < size && !stop; i++) if (data[i] != NULL && data[i] != DELETED) block(data[i]->key, data[i]->object, &stop); } - (OFDictionary*)mappedDictionaryUsingBlock: (of_dictionary_map_block_t)block { OFMutableDictionary *dict = [OFMutableDictionary dictionary]; size_t i; for (i = 0; i < size; i++) if (data[i] != NULL && data[i] != DELETED) [dict setObject: block(data[i]->key, data[i]->object) forKey: data[i]->key]; return dict; } - (OFDictionary*)filteredDictionaryUsingBlock: (of_dictionary_filter_block_t)block { OFMutableDictionary *dict = [OFMutableDictionary dictionary]; size_t i; |
︙ | ︙ |
Modified tests/OFArrayTests.m from [07eb64f276] to [284bdfc798].
︙ | ︙ | |||
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | case 1: return @"bar"; } return nil; }]) && [[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 | > > > > > > > > > > > > | 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | case 1: return @"bar"; } return nil; }]) && [[m[0] description] isEqual: @"(foo, bar)"]) TEST(@"-[mappedArrayUsingBLock]", [[[m[0] mappedArrayUsingBlock: ^ id (id obj, size_t idx) { switch (idx) { case 0: return @"foobar"; case 1: return @"qux"; } return nil; }] description] isEqual: @"(foobar, qux)"]) TEST(@"-[filteredArrayUsingBlock:]", [[[m[0] filteredArrayUsingBlock: ^ BOOL (id obj, size_t idx) { return ([obj isEqual: @"foo"] ? YES : NO); }] description] isEqual: @"(foo)"]) #endif [pool drain]; } @end |
Modified tests/OFDictionaryTests.m from [5becf67107] to [6e72394016].
︙ | ︙ | |||
140 141 142 143 144 145 146 147 148 149 150 151 152 153 | if ([key isEqual: keys[1]]) 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) | > > > > > > > > > > | 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | if ([key isEqual: keys[1]]) return @"value_2"; return nil; }]) && [[dict objectForKey: keys[0]] isEqual: @"value_1"] && [[dict objectForKey: keys[1]] isEqual: @"value_2"]) TEST(@"-[mappedDictionaryUsingBlock:]", [[[dict mappedDictionaryUsingBlock: ^ id (id key, id obj) { if ([key isEqual: keys[0]]) return @"val1"; if ([key isEqual: keys[1]]) return @"val2"; return nil; }] description] isEqual: @"{key1 = val1; key2 = val2}"]) 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) |
︙ | ︙ |