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

#include "objlist.h"
#include "datablk.h"
// #include "itemasso.h"
#include "wstring.h"

@implementation WDCObjList

- init: (struct objc_list*)list;
{
  _list=list;
  //  if (list->tail)
  //    _next=[[WDCObjList new] init: list->tail];
  return self;
}

- (int) getListLength;
{
  return list_length(_list);
}

- getNth: (int) n;
{
  return (id)list_nth(n, _list);
}

- remove: obj;
{
  /*  id temp, previous;
      if ([self nth:1]==obj){
      temp=[self nextObjList];
      [self assignNextObjList: [temp nextObjList]];
      }
      else {
      temp=[self nextObjList];
      previous=self;
      while (temp){
      if ([temp nth:1]==obj){
      [previous assignNextObjList: [temp nextObjList]];
      break;
      }
      previous=temp;
      temp=[temp nextObjList];
      }
      }  
      [temp init:NULL];
      [temp free];
      */
  list_remove_element(&_list, (void*)obj);
  return self;
}

- (BOOL) isExist: obj;
{
  if (list_find(&_list, (void*)obj)) return YES;
  else return NO;
}

- add: obj;
{
  if (![self isExist: obj]){
    struct objc_list** list=
      list_find(&_list, list_nth(list_length(_list), _list));
    (*list)->tail=list_cons((void*)obj, NULL);
    //    _next=[[WDCObjList new] init: (*list)->tail];    
  }  
  return self;
}

- getNextOf: obj;
{
  struct objc_list** list=list_find(&_list, (void*)obj);
  if (*list && (*list)->tail) return (id)(*list)->tail->head;
  else return nil;
}

- getPreviousOf: obj;
{
  struct objc_list* list=_list;
  while (list){
    if (list->tail && list->tail->head==(void*)obj)
      return (id)list->head;
    list=list->tail;
  }
  return nil;
}

- (struct objc_list*)getObjcList;
{
  return _list;
}

- (struct objc_list*)getNextObjcList;  
{
  return _list->tail;
}

- insertNth:(int)n with:obj;
{
  list_insert_element(&_list, (void*)obj, n);
  return self;
}


/*
   - setNextObjList:(WDCObjList*)list;
   {
   _next=list;
   return self;
   }
   */

@end  

@implementation WDCSortedObjList;

- init:(struct objc_list*)list with:(SEL)aSel;
{
  [super init:list];
  _aSel=aSel;
  return self;
}

- add:obj;
{ 
  int n;
  // search for the proper position to insert.
  [self insertNth:n with:obj];
  return self;
}

- getObjectAssoKey:key;
{
  return self;
}

@end

@implementation WDCStarFile;

- init;
{
  [super init:0 with:@selector(getDataBlockName)];
  _next=nil;
}

- (const char*)getFileName;
{
  return [[[[self getDataBlk:"data_file_name"] 
	  getDataItem:"_item.name"] 
	   getValue] cStringValue]; // _name must associates with a WDCString.
} 

- getDataBlk:(const char*)blkname;
{
  return self;
}

- getNext;
{
  return _next;
}

- setNext:file;
{
  if ([file isMemberOfClassNamed:"WDCStarFile"]) _next=(WDCStarFile*)file;
  return self;
}

- add:file;
{
  if (!_next) {
    [self setNext:file];
    return self;
  }
  [_next add:file];
}

@end

