// ////////////////////////////////////////////////////////
// Copyright (1994) Columbia University
// Department of Biochemistry and Molecular Biophysics
//
// Author: Weider Chang, Ph.D
// ///////////////////////////////////////////////////////

#include "iterator.h"

@implementation WDCObjListIterator;

//
// need to collect all the temporary WDCObjList objects.
//

- initFrom:list;
{
  if ([list isMemberOfClassNamed: "WDCObjList"]){ 
    [super init:[(WDCObjList*)list getObjcList]];
  }
  else {
    [self error:
     "Error in init iterator. The object should be typed WDCObjList."];
  }
  return self;
}

- getCurrentObject;
{
  return [self getNth:_currentPos];
}

- (char*)getCurrentCString;
{
  [self notImplemented:_cmd];
  return 0;
}

- (elt*)getCurrentElt;
{
  [self notImplemented:_cmd];
  return 0;
}

- (BOOL)goNext;
{
  if (_currentPos< [self getListLength]){
    _currentPos++;
    return YES;
  }
  return NO;
}

- (BOOL)goPrevious;
{
  if (_currentPos) {
    _currentPos--;
    return YES;
  }
  return NO;
}

- (BOOL)goFirst;
{
  if ([self getListLength]) {
    _currentPos=1;
    return YES;
  }
  return NO;
}

- (BOOL)goLast;
{
  _currentPos=[self getListLength];
  return YES;
}

- reset;
{
  _currentPos=0;
  return self;
}

@end


@implementation WDCListIterator;

- free
{
  [super free];
}
  

- initFrom:list;
{
  if ([list respondsTo:@selector(getNextElement:withEnumState:)]){
    _list=list;
    [self reset];
  }
  else
    [self error:"Can't initiate from non-list object"];
  return self;
}

- getCurrentObject;
{
  return _elePtr.id_u;
}

- (char*)getCurrentCString;
{
  return _elePtr.char_ptr_u;
}

// Important ***, the elt* pointer will be invalid when iterators have
// been freed.

- (elt*)getCurrentElt;
{
  return &_elePtr;
}

- (BOOL)goFirst;
{
  _enumState=0;
  return [_list getNextElement:&_elePtr withEnumState:&_enumState];
}

- (BOOL)goLast;
{
  if ([_list respondsTo:@selector(getPrevElement:withEnumState:)]){
    _enumState=0;
    return [_list getPrevElement:&_elePtr withEnumState:&_enumState];
  }
  [_list error:"Does not recognize getPrevElement:withEnumState:"];
  return NO;
}

- (BOOL)goNext;
{
  return [_list getNextElement:&_elePtr withEnumState:&_enumState];
}

- (BOOL)goPrevious;
{
  if ([_list respondsTo:@selector(getPrevElement:withEnumState:)])
    return [_list getPrevElement:&_elePtr withEnumState:&_enumState];
  [_list error:"Does not recognize getPrevElement:withEnumState:"];
  return NO;
}

- reset;
{
  _elePtr.id_u=0;
  _enumState=0;
  return self;
}

@end

