Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -95,10 +95,29 @@ * \param index The number of the object to return * \return The specified object of the OFArray */ - (id)objectAtIndex: (size_t)index; +/** + * Returns the index of the first object that is equivalent to the specified + * object. + * + * \param obj The object whose index is returned + * \return The index of the first object equivalent to the specified object + */ +- (size_t)indexOfObject: (id)obj; + +/** + * Returns the index of the first object that has the same address as the + * specified object. + * + * \param obj The object whose index is returned + * \return The index of the first object that has the same aaddress as + * the specified object + */ +- (size_t)indexOfObjectIdenticalTo: (id)obj; + /** * \return The first object of the OFArray or nil */ - (id)firstObject; Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -169,23 +169,53 @@ - (id)objectAtIndex: (size_t)index { return *((OFObject**)[array itemAtIndex: index]); } + +- (size_t)indexOfObject: (id)obj +{ + id *objs = [array cArray]; + size_t i, count = [array count]; + + if (objs == NULL) + return SIZE_MAX; + + for (i = 0; i < count; i++) + if ([objs[i] isEqual: obj]) + return i; + + return SIZE_MAX; +} + +- (size_t)indexOfObjectIdenticalTo: (id)obj +{ + id *objs = [array cArray]; + size_t i, count = [array count]; + + if (objs == NULL) + return SIZE_MAX; + + for (i = 0; i < count; i++) + if (objs[i] == obj) + return i; + + return SIZE_MAX; +} - (id)firstObject { - void *first = [array firstItem]; + id *first = [array firstItem]; - return (first != NULL ? *((id*)first) : nil); + return (first != NULL ? *first : nil); } - (id)lastObject { - void *last = [array lastItem]; + id *last = [array lastItem]; - return (last != NULL ? *((id*)last) : nil); + return (last != NULL ? *last : nil); } - (BOOL)isEqual: (id)obj { OFObject **objs, **objs2; @@ -210,18 +240,18 @@ return YES; } - (uint32_t)hash { - OFObject **carray = [array cArray]; + OFObject **objs = [array cArray]; size_t i, count = [array count]; uint32_t hash; OF_HASH_INIT(hash); for (i = 0; i < count; i++) { - uint32_t h = [carray[i] hash]; + uint32_t h = [objs[i] hash]; OF_HASH_ADD(hash, h >> 24); OF_HASH_ADD(hash, (h >> 16) & 0xFF); OF_HASH_ADD(hash, (h >> 8) & 0xFF); OF_HASH_ADD(hash, h & 0xFF); Index: tests/OFArray.m ================================================================== --- tests/OFArray.m +++ tests/OFArray.m @@ -55,10 +55,15 @@ [[a[1] objectAtIndex: 1] isEqual: c_ary[1]] && [[a[1] objectAtIndex: 2] isEqual: c_ary[2]] && [[a[2] objectAtIndex: 0] isEqual: c_ary[0]] && [[a[2] objectAtIndex: 1] isEqual: c_ary[1]] && [[a[2] objectAtIndex: 2] isEqual: c_ary[2]]) + + TEST(@"-[indexOfObject:]", [a[0] indexOfObject: c_ary[1]] == 1) + + TEST(@"-[indexOfObjectIdenticalTo:]", + [a[0] indexOfObjectIdenticalTo: c_ary[1]] == 1) TEST(@"-[removeNObjects:]", [a[0] removeNObjects: 2] && [a[0] count] == 1 && [[a[0] objectAtIndex: 0] isEqual: c_ary[0]]) a[1] = [[a[1] mutableCopy] autorelease];