From 42eeec076cd0fa748266a920af7ddc265d43f34e Mon Sep 17 00:00:00 2001 From: cacaodev Date: Thu, 13 Nov 2008 22:56:00 +0100 Subject: [PATCH] insertObjects:atIndexes for loop --- Foundation/CPArray.j | 23 ++++++++++++++++++----- 1 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Foundation/CPArray.j b/Foundation/CPArray.j index 90942d1..c11bb9b 100755 --- a/Foundation/CPArray.j +++ b/Foundation/CPArray.j @@ -906,13 +906,26 @@ @param objects the objects to add to this array @param anIndexSet the indices for the objects */ -- (void)insertObjects:(CPArray)objects atIndexes:(CPIndexSet)anIndexSet +- (void)insertObjects:(CPArray)objects atIndexes:(CPIndexSet)indexes { - var index = 0, - position = CPNotFound; + var count = [indexes count], + objects_count = [objects count]; + + if(count != objects_count) + [CPException raise:CPRangeException reason:"the counts of the passed-in array ("+objects_count+") and index set ("+count+") must be identical."] + + var lastIndex = [indexes lastIndex]; + if(lastIndex >= [self count] + count) + [CPException raise:CPRangeException reason:"the last index ("+lastIndex+") must be less than the sum of the original count ("+[self count]+") and the insertion count ("+count+")."]; - while ((position = [indexes indexGreaterThanIndex:position]) != CPNotFound) - [self insertObject:objects[index++] atindex:position]; + var i, + currentIndex = [indexes firstIndex]; + + for (i = 0; i < count; i++) + { + [self insertObject:objects[i] atIndex:currentIndex]; + currentIndex = [indexes indexGreaterThanIndex:currentIndex]; + } } /*! -- 1.6.0.2