/* -*-ObjC-*-
*******************************************************************************
*
* File:         Enumerator.m
* RCS:          Enumerator.m,v 1.1 1998/05/30 01:30:13 tom Exp
* Description:  Implementation of EnhanceEnumerator class. (shades of NSEnumerator)
* Author:       Tom Hageman <tom@basil.icce.rug.nl>
* Created:      May 1998
* Modified:     
* Language:     Objective-C
* Package:      EnhanceMail
* Status:       Experimental
*
* Copyright (C) 1996-1998 Tom Hageman, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/
#import "Enumerator.h"


@implementation EnhanceEnumerator

+ allocFromZone:(NXZone *)zone
{
   if ([self class] == [EnhanceEnumerator class])
   {
      [self error:"BUG: cannot create instances of abstract class %s\n", [self name]];
      return nil;
   }
   return [super allocFromZone:zone];
}

- nextObject
{
   return [self subclassResponsibility:_cmd];
}

@end // EnhanceEnumerator


@interface EnhanceListEnumerator : EnhanceEnumerator
{
   List *list;
   int index;
}

- initWithList:(List *)aList;
- nextObject;

@end // EnhanceListEnumerator


@implementation EnhanceListEnumerator

- initWithList:(List *)aList
{
   if ((self = [super init]) != nil)
   {
      list = aList;
      index = 0;
   }
   return self;
}

- nextObject
{
   if (index >= [list count])
   {
      [self free];   // cleanup myself, in lieu of autorelease pool.
      return nil;
   }
   return [list objectAt:index++];
}

@end // EnhanceListEnumerator


@interface EnhanceListReverseEnumerator : EnhanceListEnumerator
- initWithList:(List *)aList;
- nextObject;
@end


@implementation EnhanceListReverseEnumerator

- initWithList:(List *)aList
{
   if ((self = [super initWithList:aList]) != nil)
   {
      index = [list count];
   }
   return self;
}

- nextObject
{
   if (index <= 0)
   {
      [self free];   // cleanup myself, in lieu of autorelease pool.
      return nil;
   }
   return [list objectAt:--index];
}

@end // EnhanceListReverseEnumerator


@implementation List (EnhanceEnumerator)

- (EnhanceEnumerator *)enhanceObjectEnumerator
{
   return [[EnhanceListEnumerator alloc] initWithList:self];
}

- (EnhanceEnumerator *)enhanceReverseObjectEnumerator;
{
   return [[EnhanceListReverseEnumerator alloc] initWithList:self];
}

@end // List (EnhanceEnumerator)
