Reduce repetitive implementation of the same algorithm in the code. Make sure that insertion happens in one method only

This commit is contained in:
Gregory John Casamento 2019-06-18 22:07:11 -04:00
parent f2ba8a3093
commit d826bb5a25
3 changed files with 13 additions and 30 deletions

View file

@ -249,21 +249,7 @@ static Class mutableSetClass;
- (void) addObject: (id)anObject
{
GSIArrayItem item;
if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Tried to add nil to set"];
}
if([self containsObject: anObject] == NO)
{
item.obj = anObject;
GSIArrayAddItem(&array, item);
RETAIN(anObject);
_version++;
}
[self insertObject: anObject atIndex: [self count]];
}
- (void) insertObject: (id)object atIndex: (NSUInteger)index
@ -271,7 +257,8 @@ static Class mutableSetClass;
GSIArrayItem item;
if(object == nil)
{
NSWarnLog(@"Attempt to insert nil object");
[NSException raise: NSInvalidArgumentException
format: @"Tried to add nil to set"];
}
else
{
@ -329,13 +316,7 @@ static Class mutableSetClass;
}
else
{
GSIArrayItem item;
if(![self containsObject: anObject])
{
item.obj = anObject;
GSIArrayAddItem(&array, item);
RETAIN(anObject);
}
[self addObject: anObject];
}
}
}

View file

@ -50,6 +50,9 @@
@class GSMutableOrderedSet;
@interface GSMutableOrderedSet : NSObject // Help the compiler
@end
@interface NSOrderedSet (Private)
- (void) _raiseRangeExceptionWithIndex: (NSUInteger)index from: (SEL)sel;
@end
@ -1453,16 +1456,16 @@ static SEL rlSel;
* first equal object we don't get left with a bad object
* pointer for later comparisons.
*/
RETAIN(anObject);
// RETAIN(anObject);
}
(*rem)(self, remSel, i);
break; // since this is a set we should only have one copy...
}
}
if (rem != 0)
{
RELEASE(anObject);
}
//if (rem != 0)
// {
// RELEASE(anObject);
// }
}
}
@ -1799,7 +1802,7 @@ static SEL rlSel;
[self removeAllObjects];
[self addObjectsFromArray: res];
RELEASE(res);
// RELEASE(res);
GS_ENDIDBUF();
}
}

View file

@ -338,7 +338,6 @@ int main()
PASS([[mutableTest4 objectAtIndex: 9] isEqual:@"Flee From"] == YES,
"replaceObjectsAtIndexes: adds to correct indexes");
id uobj = [NSKeyedUnarchiver unarchiveObjectWithData: data];
PASS((uobj != nil &&
[uobj isKindOfClass: [NSMutableOrderedSet class]] &&