/* Implementation for Objective-C LinkedList collection object

   Copyright (C) 1993 R. Andrew McCallum <mccallum@cs.rochester.edu>
   Dept. of Computer Science, U. of Rochester, Rochester, NY  14627

   This file is part of the GNU Objective-C Collection library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ 

/* I should override a few more methods to increase efficiency. */

#include <coll/LinkedList.h>
#include <coll/IndexedCollectionPrivate.h>
#include <coll/EltListLink.h>

@implementation LinkedList

+ initialize
{
    if (self == [LinkedList class])
	[self setVersion:-1];	/* alpha release */
    return self;
}


// DETERMINING CLASS OF AUTOCREATED LINKS;
- linkClass
{
  return [EltListLink class];
}

// GETTING THE UNDERLYING LIST;
- contentsList
{
  return _contents_list;
}
  
// INITIALIZING AND FREEING;

/* This is the designated initializer of this class */
- initDescription: (const char *)type
{
  [super initDescription:type];
  _contents_list = [[LinkList alloc] init];
  return self;
}

- free
{
  [[_contents_list freeContents] free];
  return [super free];
}

// ADDING;

- insertElement: (elt)newElement atIndex: (unsigned)index
{
  unsigned count = [_contents_list count];
  if (INDEX_RANGE_WARNING(index, count+1))
    return nil;
  if (index == count)
    [_contents_list 
     appendElement:[[[self linkClass] alloc] initElement:newElement]];
  else
    [_contents_list 
     insertElement:[[[self linkClass] alloc] initElement:newElement] 
     before:[_contents_list elementAtIndex:index]];
  return self;
}


// REMOVING AND REPLACING;

- (elt) removeElementAtIndex: (unsigned)index
{
  id link;
  elt ret;

  if (INDEX_RANGE_WARNING(index, [_contents_list count]))
    return COLL_NO_ELEMENT;
  link = [_contents_list removeElementAtIndex:index].id_t;
  ret = [link elementData];
  [link free];
  return ret;
}

- (elt) removeElement: (elt)oldElement
{
  id aLink = [self linkWithElement:oldElement];
  elt ret;

  if (!aLink)
    return COLL_NO_ELEMENT;
  ret = [aLink elementData];
  [_contents_list removeElement:aLink];
  [aLink free];
  return ret;
}

/* I should add replace... here */


// GETTING ELEMENTS BY INDEX;

- (elt) elementAtIndex: (unsigned)index
{
  if (INDEX_RANGE_WARNING(index, [_contents_list count]))
      return COLL_NO_ELEMENT;
  return [[_contents_list elementAtIndex:index].id_t elementData];
}

// TESTING;

- linkWithElement: (elt)anElement
{
  id link;

  BOOL test(elt link)
    {
       if (_compare_func([link.id_t elementData].void_ptr_t, 
			 anElement.void_ptr_t))
	 return YES;
       else
	 return NO;
     }
  link = [_contents_list detectElementByCalling:test].id_t;
  if (link == COLL_NO_OBJECT)
    [self errorElementNotFound:anElement inMethod:_cmd];
  return link;
}

- (unsigned) count
{
  return [_contents_list count];
}


// ENUMERATING;

- (BOOL) getNextElement:(elt *)anElementPtr withEnumState: (void**)enumState
{
  elt link;
  BOOL flag;

  flag = [_contents_list getNextElement:&link withEnumState:enumState];
  if (flag)
    *anElementPtr = [link.id_t elementData];
  return flag;
}

- (BOOL) getPrevElement:(elt *)anElementPtr withEnumState: (void**)enumState
{
  elt link;
  BOOL flag;

  flag = [_contents_list getPrevElement:&link withEnumState:enumState];
  if (flag)
    *anElementPtr = [link.id_t elementData];
  return flag;
}

- withElementsCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag
{
  void doIt(elt link)
    {
      aFunc([link.id_t elementData]);
    }
  [_contents_list withElementsCall:doIt whileTrue:flag];
  /* crashed using 'withObjectsCall:whileTrue:' */
  return self;
}

- withElementsInReverseCall: (void(*)(elt))aFunc whileTrue:(BOOL *)flag
{
  void doIt(elt link)
    {
      aFunc([link.id_t elementData]);
    }
  [_contents_list withElementsInReverseCall:doIt whileTrue:flag];
  return self;
}


@end
