From f2ccdab2cefea6c4ba904d7671afce11654e2d95 Mon Sep 17 00:00:00 2001 From: Andrew McCallum Date: Tue, 12 Mar 1996 20:39:58 +0000 Subject: [PATCH] Several minor fixes to return types and return values. ([ReverseEnumerator -nextObject]): Pass pointer to enum_state. ([ConstantIndexedCollection -prevObjectWithEnumState:]): Method overhauled. ([ConstantIndexedCollection -nextObjectWithEnumState:]): Likewise. ([ConstantIndexedCollection -newEnumState]): New method. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1129 72102866-910b-0410-8b05-ffd578937521 --- Source/IndexedCollection.m | 63 +++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/Source/IndexedCollection.m b/Source/IndexedCollection.m index 9ab067335..087fe7ef9 100644 --- a/Source/IndexedCollection.m +++ b/Source/IndexedCollection.m @@ -26,12 +26,13 @@ #include #include #include +#include @implementation ReverseEnumerator - nextObject { - return [collection prevObjectWithEnumState: enum_state]; + return [collection prevObjectWithEnumState: &enum_state]; } @end @@ -45,6 +46,7 @@ - objectAtIndex: (unsigned)index { [self subclassResponsibility: _cmd]; + return nil; } - firstObject @@ -263,12 +265,24 @@ - prevObjectWithEnumState: (void**)enumState { - /* *(unsigned*)enumState-1 is the index of the element - that will be returned */ - if (!(*enumState)) - *(unsigned*)enumState = [self count]-1; + /* *(int*)enumState is the index of the element that was returned + last time -prevObjectWithEnumState: or -nextObjectWithEnumState + was called. In -newEnumState, *(int*)enumState is initialized to + -2; The implementation of -newEnumState can be found below. */ + + /* If there are not objects in this collection, or we are being + asked for the object before the first object, return nil. */ + if ([self isEmpty] || (*(int*)enumState) == 0) + return NO_OBJECT; + + if (*(int*)enumState == -2) + /* enumState was just initialized by -newEnumState, start + at the end of the sequence. */ + *(int*)enumState = [self count]-1; else - (*(unsigned*)enumState)--; + /* ...otherwise go to the previous index.*/ + (*(int*)enumState)--; + return [self objectAtIndex:(*(unsigned*)enumState)]; } @@ -279,31 +293,50 @@ - shallowCopyRange: (IndexRange)aRange { [self notImplemented: _cmd]; + return nil; } - shallowCopyInReverse { [self notImplemented: _cmd]; + return nil; } - shallowCopyInReverseRange: (IndexRange)aRange { [self notImplemented: _cmd]; + return nil; } // OVERRIDE SOME COLLECTION METHODS; +- (void*) newEnumState +{ + return (void*) -2; +} + - nextObjectWithEnumState: (void**)enumState { - id ret; - /* *(unsigned*)enumState is the index of the element that will be returned */ - if ([self isEmpty] - || (*(unsigned*)enumState) > [self count]-1) + /* *(int*)enumState is the index of the element that was returned + last time -prevObjectWithEnumState: or -nextObjectWithEnumState + was called. In -newEnumState, *(int*)enumState is initialized to + -2. */ + + /* If there are not objects in this collection, or we are being + asked for the object after the last object, return nil. */ + if ([self isEmpty] || (*(int*)enumState) > [self count]-1) return NO_OBJECT; - ret = [self objectAtIndex:(*(unsigned*)enumState)]; - *(unsigned*)enumState = *(unsigned*)enumState + 1; - return ret; + + if (*(int*)enumState == -2) + /* enumState was just initialized by -newEnumState, start + at the beginning of the sequence. */ + *(int*)enumState = 0; + else + /* ...otherwise go the next index. */ + (*(int*)enumState)++; + + return [self objectAtIndex:(*(unsigned*)enumState)]; } /* is this what we want? */ @@ -334,7 +367,7 @@ // REPLACING; -- replaceObjectAtIndex: (unsigned)index withObject: newObject +- (void) replaceObjectAtIndex: (unsigned)index withObject: newObject { [self subclassResponsibility: _cmd]; } @@ -359,7 +392,7 @@ - (void) removeRange: (IndexRange)aRange { - int count; + int count = aRange.length; CHECK_INDEX_RANGE_ERROR(aRange.location, [self count]); CHECK_INDEX_RANGE_ERROR(aRange.location+aRange.length-1, [self count]);