Index: src/OFDataArray.h ================================================================== --- src/OFDataArray.h +++ src/OFDataArray.h @@ -92,16 +92,32 @@ * \param carray A C array containing the items to add */ - addNItems: (size_t)nitems fromCArray: (void*)carray; +/** + * Removes the item at the specified index. + * + * \param index The index of the item to remove + */ +- removeItemAtIndex: (size_t)index; + /** * Removes the specified amount of items from the end of the OFDataArray. * * \param nitems The number of items to remove */ - removeNItems: (size_t)nitems; + +/** + * Removes the specified amount of items at the specified index. + * + * \param nitems The number of items to remove + * \param index The index at which the items are removed + */ +- removeNItems: (size_t)nitems + atIndex: (size_t)index; @end /** * The OFBigDataArray class provides a class for storing arbitrary data in an * array and is designed to store large hunks of data. Therefore, it allocates Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -115,21 +115,44 @@ memcpy(data + count * itemsize, carray, nitems * itemsize); count += nitems; return self; } + +- removeItemAtIndex: (size_t)index +{ + return [self removeNItems: 1 + atIndex: index]; +} + +- removeNItems: (size_t)nitems +{ + if (nitems > count) + @throw [OFOutOfRangeException newWithClass: isa]; + + + count -= nitems; + data = [self resizeMemory: data + toNItems: count + withSize: itemsize]; + + return self; +} - removeNItems: (size_t)nitems + atIndex: (size_t)index { if (nitems > count) @throw [OFOutOfRangeException newWithClass: isa]; - data = [self resizeMemory: data - toNItems: count - nitems - withSize: itemsize]; + memmove(data + index * itemsize, data + (index + nitems) * itemsize, + nitems * itemsize); count -= nitems; + data = [self resizeMemory: data + toNItems: count + withSize: itemsize]; return self; } - (id)copy @@ -261,17 +284,38 @@ size_t nsize; if (nitems > count) @throw [OFOutOfRangeException newWithClass: isa]; - nsize = ((count - nitems) * itemsize + lastpagebyte) & ~lastpagebyte; + count -= nitems; + nsize = (count * itemsize + lastpagebyte) & ~lastpagebyte; if (size != nsize) data = [self resizeMemory: data toSize: nsize]; + size = nsize; + + return self; +} + +- removeNItems: (size_t)nitems + atIndex: (size_t)index +{ + size_t nsize; + + if (nitems > count) + @throw [OFOutOfRangeException newWithClass: isa]; + + memmove(data + index * itemsize, data + (index + nitems) * itemsize, + nitems * itemsize); count -= nitems; + nsize = (count * itemsize + lastpagebyte) & ~lastpagebyte; + + if (size != nsize) + data = [self resizeMemory: data + toSize: nsize]; size = nsize; return self; } Index: tests/OFDataArray.m ================================================================== --- tests/OFDataArray.m +++ tests/OFDataArray.m @@ -72,11 +72,23 @@ [array[1] compare: array[0]] == OF_ORDERED_ASCENDING && [array[2] compare: array[3]] == OF_ORDERED_ASCENDING) TEST(@"-[hash]", [array[0] hash] == 0xC54621B6) - TEST(@"-[removeNItems:]", [array[0] removeNItems: 1]) + array[0] = [class dataArrayWithItemSize: 1]; + [array[0] addNItems: 6 + fromCArray: "abcdef"]; + + TEST(@"-[removeNItems:]", [array[0] removeNItems: 1] && + [array[0] count] == 5 && + !memcmp([array[0] cArray], "abcde", 5)) + + TEST(@"-[removeNItems:atIndex:]", + [array[0] removeNItems: 2 + atIndex: 1] && [array[0] count] == 3 && + !memcmp([array[0] cArray], "ade", 3)) + [array[0] addItem: ""]; TEST(@"Building strings", (array[0] = [class dataArrayWithItemSize: 1]) && [array[0] addNItems: 6 fromCArray: (void*)str] && [array[0] addItem: ""] &&