Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -137,10 +137,16 @@ - (OFString*)componentsJoinedByString: (OFString*)separator; - addObject: (OFObject*)obj; - addObject: (OFObject*)obj atIndex: (size_t)index; +- replaceObject: (OFObject*)old + withObject: (OFObject*)new; +- replaceObjectAtIndex: (size_t)index + withObject: (OFObject*)obj; +- replaceObjectIdenticalTo: (OFObject*)old + withObject: (OFObject*)new; - removeObject: (OFObject*)obj; - removeObjectIdenticalTo: (OFObject*)obj; - removeObjectAtIndex: (size_t)index; - removeNObjects: (size_t)nobjects; - removeNObjects: (size_t)nobjects Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -308,10 +308,31 @@ selector: _cmd]; } - addObject: (OFObject*)obj atIndex: (size_t)index +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} + +- replaceObject: (OFObject*)old + withObject: (OFObject*)new +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} + +- replaceObjectAtIndex: (size_t)index + withObject: (OFObject*)obj +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} + +- replaceObjectIdenticalTo: (OFObject*)old + withObject: (OFObject*)new { @throw [OFNotImplementedException newWithClass: isa selector: _cmd]; } Index: src/OFMutableArray.h ================================================================== --- src/OFMutableArray.h +++ src/OFMutableArray.h @@ -31,18 +31,47 @@ */ - addObject: (OFObject*)obj atIndex: (size_t)index; /** - * Removes the first object equivalent to the specified object. + * Replaces all objects equivalent to the first specified object with the + * second specified object. + * + * \param old The object to replace + * \param new The replacement object + */ +- replaceObject: (OFObject*)old + withObject: (OFObject*)new; + +/** + * Replaces the object at the specified index with the specified object. + * + * \param index The index of the object to replace + * \param obj The replacement object + */ +- replaceObjectAtIndex: (size_t)index + withObject: (OFObject*)obj; + +/** + * Replaces all objects that have the same address as the first specified object + * with the second specified object. + * + * \param old The object to replace + * \param new The replacement object + */ +- replaceObjectIdenticalTo: (OFObject*)old + withObject: (OFObject*)new; + +/** + * Removes all objects equivalent to the specified object. * * \param obj The object to remove */ - removeObject: (OFObject*)obj; /** - * Removes the first object that has the same address as the specified object. + * Removes all objects that have the same address as the specified object. * * \param obj The object to remove */ - removeObjectIdenticalTo: (OFObject*)obj; Index: src/OFMutableArray.m ================================================================== --- src/OFMutableArray.m +++ src/OFMutableArray.m @@ -48,10 +48,59 @@ atIndex: index]; [obj retain]; return self; } + +- replaceObject: (OFObject*)old + withObject: (OFObject*)new +{ + OFObject **objs = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) { + if ([objs[i] isEqual: old]) { + [new retain]; + [objs[i] release]; + objs[i] = new; + } + } + + return self; +} + +- replaceObjectAtIndex: (size_t)index + withObject: (OFObject*)obj +{ + OFObject **objs = [array cArray]; + + if (index >= [array count]) + @throw [OFOutOfRangeException newWithClass: isa]; + + [obj retain]; + [objs[index] release]; + objs[index] = obj; + + return self; +} + +- replaceObjectIdenticalTo: (OFObject*)old + withObject: (OFObject*)new +{ + OFObject **objs = [array cArray]; + size_t i, count = [array count]; + + for (i = 0; i < count; i++) { + if (objs[i] == old) { + [new retain]; + [objs[i] release]; + objs[i] = new; + } + } + + return self; +} - removeObject: (OFObject*)obj { OFObject **objs = [array cArray]; size_t i, count = [array count]; @@ -58,11 +107,10 @@ for (i = 0; i < count; i++) { if ([objs[i] isEqual: obj]) { [objs[i] release]; [array removeItemAtIndex: i]; - return self; } } return self; } Index: tests/OFArray.m ================================================================== --- tests/OFArray.m +++ tests/OFArray.m @@ -64,10 +64,31 @@ TEST(@"-[indexOfObject:]", [a[0] indexOfObject: c_ary[1]] == 1) TEST(@"-[indexOfObjectIdenticalTo:]", [a[0] indexOfObjectIdenticalTo: c_ary[1]] == 1) + TEST(@"-[replaceObject:withObject:]", + [a[0] replaceObject: c_ary[1] + withObject: c_ary[0]] && + [[a[0] objectAtIndex: 0] isEqual: c_ary[0]] && + [[a[0] objectAtIndex: 1] isEqual: c_ary[0]] && + [[a[0] objectAtIndex: 2] isEqual: c_ary[2]]) + + TEST(@"-[replaceObject:identicalTo:]", + [a[0] replaceObjectIdenticalTo: c_ary[0] + withObject: c_ary[1]] && + [[a[0] objectAtIndex: 0] isEqual: c_ary[1]] && + [[a[0] objectAtIndex: 1] isEqual: c_ary[1]] && + [[a[0] objectAtIndex: 2] isEqual: c_ary[2]]) + + TEST(@"-[replaceObjectAtIndex:withObject:]", + [a[0] replaceObjectAtIndex: 0 + withObject: c_ary[0]] && + [[a[0] objectAtIndex: 0] isEqual: c_ary[0]] && + [[a[0] objectAtIndex: 1] isEqual: c_ary[1]] && + [[a[0] objectAtIndex: 2] isEqual: c_ary[2]]) + TEST(@"-[removeObject:]", [a[0] removeObject: c_ary[1]] && [a[0] count] == 2) TEST(@"-[removeObjectIdenticalTo:]", [a[0] removeObjectIdenticalTo: c_ary[2]] && [a[0] count] == 1)