From 09bae157fe9b465838dc9375f58bb670bda4c099 Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Tue, 28 May 2019 05:40:22 -0400 Subject: [PATCH] Implement more methods --- Headers/Foundation/NSOrderedSet.h | 2 +- Source/NSOrderedSet.m | 93 ++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/Headers/Foundation/NSOrderedSet.h b/Headers/Foundation/NSOrderedSet.h index a1ee3953c..3a3bf71b6 100644 --- a/Headers/Foundation/NSOrderedSet.h +++ b/Headers/Foundation/NSOrderedSet.h @@ -181,7 +181,7 @@ extern "C" { - (void)removeObjectAtIndex:(NSUInteger)integer; - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes; - (void)removeObjectsInArray:(GS_GENERIC_CLASS(NSArray, ElementT)*)otherArray; -- (void)removeObjectsInRange:(NSRange *)range; +- (void)removeObjectsInRange:(NSRange)range; - (void)removeAllObjects; - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(GS_GENERIC_TYPE(ElementT))object; diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 0b19fb3ed..a05a1488c 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -971,16 +971,107 @@ static SEL rlSel; [self subclassResponsibility: _cmd]; } +- (void) _removeObjectsFromIndices: (NSUInteger*)indices + numIndices: (NSUInteger)count +{ + if (count > 0) + { + NSUInteger to = 0; + NSUInteger from = 0; + NSUInteger i; + GS_BEGINITEMBUF(sorted, count, NSUInteger); + + while (from < count) + { + NSUInteger val = indices[from++]; + + i = to; + while (i > 0 && sorted[i-1] > val) + { + i--; + } + if (i == to) + { + sorted[to++] = val; + } + else if (sorted[i] != val) + { + NSUInteger j = to++; + + if (sorted[i] < val) + { + i++; + } + while (j > i) + { + sorted[j] = sorted[j-1]; + j--; + } + sorted[i] = val; + } + } + + if (to > 0) + { + IMP rem = [self methodForSelector: remSel]; + + while (to--) + { + (*rem)(self, remSel, sorted[to]); + } + } + GS_ENDITEMBUF(); + } +} + - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes { + NSUInteger count = [indexes count]; + NSUInteger indexArray[count]; + + [indexes getIndexes: indexArray + maxCount: count + inIndexRange: NULL]; + + [self _removeObjectsFromIndices: indexArray + numIndices: count]; } - (void)removeObjectsInArray:(NSArray *)otherArray { + NSUInteger c = [otherArray count]; + + if (c > 0) + { + NSUInteger i; + IMP get = [otherArray methodForSelector: oaiSel]; + IMP rem = [self methodForSelector: @selector(removeObject:)]; + + for (i = 0; i < c; i++) + (*rem)(self, @selector(removeObject:), (*get)(otherArray, oaiSel, i)); + } } -- (void)removeObjectsInRange:(NSRange *)range +- (void)removeObjectsInRange:(NSRange)aRange { + NSUInteger i; + NSUInteger s = aRange.location; + NSUInteger c = [self count]; + + i = aRange.location + aRange.length; + + if (c < i) + i = c; + + if (i > s) + { + IMP rem = [self methodForSelector: remSel]; + + while (i-- > s) + { + (*rem)(self, remSel, i); + } + } } - (void)removeAllObjects