Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -101,9 +101,12 @@ * \return The last object of the OFDataArray */ - (id)lastObject; - addObject: (OFObject*)obj; +- removeObjectAtIndex: (size_t)index; - removeNObjects: (size_t)nobjects; +- removeNObjects: (size_t)nobjects + atIndex: (size_t)index; @end #import "OFMutableArray.h" Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -200,10 +200,32 @@ if (![objs[i] isEqual: objs2[i]]) return NO; return YES; } + +- (uint32_t)hash +{ + OFObject **carray = [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]; + + 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); + } + + OF_HASH_FINALIZE(hash); + + return hash; +} - (void)dealloc { OFObject **objs; size_t len, i; @@ -224,34 +246,25 @@ - addObject: (OFObject*)obj { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } + +- removeObjectAtIndex: (size_t)index +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} - removeNObjects: (size_t)nobjects { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } -- (uint32_t)hash -{ - OFObject **carray = [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]; - - 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); - } - - OF_HASH_FINALIZE(hash); - - return hash; +- removeNObjects: (size_t)nobjects + atIndex: (size_t)index +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; } @end Index: src/OFMutableArray.h ================================================================== --- src/OFMutableArray.h +++ src/OFMutableArray.h @@ -21,12 +21,28 @@ * * \param obj An object to add */ - addObject: (OFObject*)obj; +/** + * Removes the object at the specified index. + * + * \param index The index of the object to remove + */ +- removeObjectAtIndex: (size_t)index; + /** * Removes the specified amount of objects from the end of the OFDataArray. * * \param nobjects The number of objects to remove */ - removeNObjects: (size_t)nobjects; + +/** + * Removes the specified amount of objects at the specified index. + * + * \param nobjects The number of objects to remove + * \param index The index at which the objects are removed + */ +- removeNObjects: (size_t)nitems + atIndex: (size_t)index; @end Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -38,10 +38,16 @@ [array addItem: &obj]; [obj retain]; return self; } + +- removeObjectAtIndex: (size_t)index +{ + return [self removeNObjects: 1 + atIndex: index]; +} - removeNObjects: (size_t)nobjects { OFObject **objs; size_t len, i; @@ -55,8 +61,29 @@ for (i = len - nobjects; i < len; i++) [objs[i] release]; [array removeNItems: nobjects]; + return self; +} + +- removeNObjects: (size_t)nobjects + atIndex: (size_t)index +{ + OFObject **objs; + size_t len, i; + + objs = [array cArray]; + len = [array count]; + + if (nobjects > len) + @throw [OFOutOfRangeException newWithClass: isa]; + + for (i = index; i < len && i < index + nobjects; i++) + [objs[i] release]; + + [array removeNItems: nobjects + atIndex: index]; + return self; } @end Index: tests/OFArray.m ================================================================== --- tests/OFArray.m +++ tests/OFArray.m @@ -59,13 +59,22 @@ [[a[2] objectAtIndex: 2] isEqual: c_ary[2]]) TEST(@"-[removeNObjects:]", [a[0] removeNObjects: 2] && [a[0] count] == 1 && [[a[0] objectAtIndex: 0] isEqual: c_ary[0]]) + a[1] = [[a[1] mutableCopy] autorelease]; + TEST(@"-[removeObjectAtIndex:]", [a[1] removeObjectAtIndex: 1] && + [a[1] count] == 2 && [[a[1] objectAtIndex: 1] isEqual: c_ary[2]]) + + a[2] = [[a[2] mutableCopy] autorelease]; + TEST(@"-[removeNObjects:atIndex:]", [a[2] removeNObjects: 2 + atIndex: 0] && + [a[2] count] == 1 && [[a[2] objectAtIndex: 0] isEqual: c_ary[2]]) + EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]", OFOutOfRangeException, [a[0] objectAtIndex: [a[0] count]]) EXPECT_EXCEPTION(@"Detect out of range in -[removeNItems:]", OFOutOfRangeException, [a[0] removeNObjects: [a[0] count] + 1]) [pool release]; }