@@ -111,12 +111,12 @@ while (![file isAtEndOfStream]) { size_t length; length = [file readNBytes: of_pagesize intoBuffer: buffer]; - [self addNItems: length - fromCArray: buffer]; + [self addItemsFromCArray: buffer + count: length]; } [self freeMemory: buffer]; } @finally { [file release]; @@ -263,20 +263,20 @@ memcpy(data + count * itemSize, item, itemSize); count++; } -- (void)addItem: (const void*)item - atIndex: (size_t)index +- (void)insertItem: (const void*)item + atIndex: (size_t)index { - [self addNItems: 1 - fromCArray: item - atIndex: index]; + [self insertItemsFromCArray: item + atIndex: index + count: 1]; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray +- (void)addItemsFromCArray: (const void*)cArray + count: (size_t)nItems { if (nItems > SIZE_MAX - count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data @@ -285,13 +285,13 @@ memcpy(data + count * itemSize, cArray, nItems * itemSize); count += nItems; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray - atIndex: (size_t)index +- (void)insertItemsFromCArray: (const void*)cArray + atIndex: (size_t)index + count: (size_t)nItems { if (nItems > SIZE_MAX - count || index > count) @throw [OFOutOfRangeException exceptionWithClass: isa]; data = [self resizeMemory: data @@ -305,40 +305,23 @@ count += nItems; } - (void)removeItemAtIndex: (size_t)index { - [self removeNItems: 1 - atIndex: index]; -} - -- (void)removeNItems: (size_t)nItems -{ - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - - count -= nItems; - @try { - data = [self resizeMemory: data - toNItems: count - ofSize: itemSize]; - } @catch (OFOutOfMemoryException *e) { - /* We don't really care, as we only made it smaller */ - } -} - -- (void)removeNItems: (size_t)nItems - atIndex: (size_t)index -{ - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - memmove(data + index * itemSize, data + (index + nItems) * itemSize, - (count - index - nItems) * itemSize); - - count -= nItems; + [self removeItemsInRange: of_range(index, 1)]; +} + +- (void)removeItemsInRange: (of_range_t)range +{ + if (range.start + range.length > count) + @throw [OFOutOfRangeException exceptionWithClass: isa]; + + memmove(data + range.start * itemSize, + data + (range.start + range.length) * itemSize, + (count - range.start - range.length) * itemSize); + + count -= range.length; @try { data = [self resizeMemory: data toNItems: count ofSize: itemSize]; } @catch (OFOutOfMemoryException *e) { @@ -371,12 +354,12 @@ - copy { OFDataArray *copy = [[isa alloc] initWithItemSize: itemSize]; - [copy addNItems: count - fromCArray: data]; + [copy addItemsFromCArray: data + count: count]; return copy; } - (BOOL)isEqual: (id)object @@ -503,12 +486,12 @@ count++; size = newSize; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray +- (void)addItemsFromCArray: (const void*)cArray + count: (size_t)nItems { size_t newSize, lastPageByte; if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: isa]; @@ -524,17 +507,18 @@ count += nItems; size = newSize; } -- (void)addNItems: (size_t)nItems - fromCArray: (const void*)cArray - atIndex: (size_t)index +- (void)insertItemsFromCArray: (const void*)cArray + atIndex: (size_t)index + count: (size_t)nItems { size_t newSize, lastPageByte; - if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize) + if (nItems > SIZE_MAX - count || index > count || + count + nItems > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exceptionWithClass: isa]; lastPageByte = of_pagesize - 1; newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte; @@ -548,39 +532,22 @@ count += nItems; size = newSize; } -- (void)removeNItems: (size_t)nItems -{ - size_t newSize, lastPageByte; - - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - count -= nItems; - lastPageByte = of_pagesize - 1; - newSize = (count * itemSize + lastPageByte) & ~lastPageByte; - - if (size != newSize) - data = [self resizeMemory: data - toSize: newSize]; - size = newSize; -} - -- (void)removeNItems: (size_t)nItems - atIndex: (size_t)index -{ - size_t newSize, lastPageByte; - - if (nItems > count) - @throw [OFOutOfRangeException exceptionWithClass: isa]; - - memmove(data + index * itemSize, data + (index + nItems) * itemSize, - (count - index - nItems) * itemSize); - - count -= nItems; +- (void)removeItemsInRange: (of_range_t)range +{ + size_t newSize, lastPageByte; + + if (range.start + range.length > count) + @throw [OFOutOfRangeException exceptionWithClass: isa]; + + memmove(data + range.start * itemSize, + data + (range.start + range.length) * itemSize, + (count - range.start - range.length) * itemSize); + + count -= range.length; lastPageByte = of_pagesize - 1; newSize = (count * itemSize + lastPageByte) & ~lastPageByte; if (size != newSize) data = [self resizeMemory: data