From 061f56ea2dd20f818c29f789df23dea2d522cd10 Mon Sep 17 00:00:00 2001 From: Jeff Teunissen Date: Thu, 16 Dec 2010 06:22:33 -0500 Subject: [PATCH] Add missing methods Array: Implement -indexOfObject: and -indexOfObjectIdenticalTo: --- ruamoko/include/Array.h | 39 +++++++++++++++++++++++++ ruamoko/lib/Array.r | 65 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/ruamoko/include/Array.h b/ruamoko/include/Array.h index 139ade27c..f3a666690 100644 --- a/ruamoko/include/Array.h +++ b/ruamoko/include/Array.h @@ -142,6 +142,45 @@ #endif //\} +///\name Finding objects +//\{ +/** + Returns the lowest index of an object equal to \a anObject. + + If no object is equal, returns #NotFound. +*/ +- (unsigned) indexOfObject: (id)anObject; + +#if 0 +/** + Returns the lowest index, within the range \a aRange, of an object equal + to \a anObject. + + If no object is equal, returns #NotFound. +*/ +- (unsigned) indexOfObject: (id)anObject + inRange: (Range)aRange; +#endif + +/** + Returns the lowest index of an object with the same address as \a anObject. + + Returns #NotFound if \a anObject is not found within the array. +*/ +- (unsigned) indexOfObjectIdenticalTo: (id)anObject; + +#if 0 +/** + Returns the lowest index, within the range \a aRange, of an object with + the same address as \a anObject. + + Returns #NotFound if \a anObject is not found within the range. +*/ +- (unsigned) indexOfObjectIdenticalTo: (id)anObject + inRange: (Range)aRange; +#endif +//\} + ///\name Adding objects //\{ diff --git a/ruamoko/lib/Array.r b/ruamoko/lib/Array.r index 019a4ae41..ef5f9e102 100644 --- a/ruamoko/lib/Array.r +++ b/ruamoko/lib/Array.r @@ -148,16 +148,16 @@ return self; } -- (unsigned) count -{ - return count; -} - - (BOOL) containsObject: (id)anObject { return [self indexOfObject: anObject] ? YES : NO; } +- (unsigned) count +{ + return count; +} + - (id) objectAtIndex: (unsigned)index { if (index >= count) // FIXME: need exceptions @@ -171,6 +171,61 @@ return [self objectAtIndex: [self count] - 1]; } +/* + Finding Objects +*/ +- (unsigned) indexOfObject: (id)anObject +{ + local unsigned i; + + for (i = 0; i < [self count]; i++) { + if ([[self objectAtIndex: i] isEqual: anObject]) + return i; + } + return NotFound; +} + +#if 0 +- (unsigned) indexOfObject: (id)anObject + inRange: (Range)range; +{ + local unsigned i; + local unsigned end = range.location + range.length; + + for (i = range.location; i < end && i < [self count]; i++) { + if ([[self objectAtIndex: i] isEqual: anObject]) + return i; + } + return NotFound; +} +#endif + +- (unsigned) indexOfObjectIdenticalTo: (id)anObject +{ + local unsigned i; + + for (i = 0; i < [self count]; i++) { + if ([self objectAtIndex: i] == anObject) + return i; + } + return NotFound; +} + +#if 0 +- (unsigned) indexOfObjectIdenticalTo: (id)anObject + inRange: (Range)range; +{ + local unsigned i; + local unsigned end = range.location + range.length; + + for (i = range.location; i < end && i < [self count]; i++) { + if ([self objectAtIndex: i] == anObject) + return i; + } + return NotFound; +} +#endif + /* Adding objects */