/* Implementation for Objective-C SortedArray 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.
*/ 

#include <coll/SortedArray.h>
#include <coll/ArrayPrivate.h>
#include <objc/objc-api.h>

@implementation SortedArray

// INITIALIZING;

/* This is the designated initializer of this class */
- initDescription: (const char *)type 
    comparisonFunc: (int(*)(elt,elt))aFunc
    capacity: (unsigned)aCapacity
{
  [super initDescription:type capacity:aCapacity];
  _comparator = aFunc;
  return self;
}

- initDescription: (const char *)type 
    comparisonFunc: (int(*)(elt,elt))aFunc
{
  return [self initDescription:type comparisonFunc:aFunc 
	       capacity:[[self class] defaultCapacity]];
}

- initComparisonFunc: (int(*)(elt,elt))aFunc
{
  return [self initDescription:"@" comparisonFunc:aFunc];
}

/* Catch all Array init's without a comparator */
- initDescription: (const char *)type capacity: (unsigned)aCapacity
{
  /* Perhaps I should put in default comparitors */
  return [self error:
	       "instead of %s, use initializer that specifies comparator",
	       sel_get_name(_cmd)];
}

/* I should use Array's sortAddElement: */
- addElement: (elt)newElement
{
  int i = 0;
  int (*comparisonFunc)(elt e1, elt e2);
  int comparatorForSel(id o1, id o2)
    {
      return (int)[o1 perform:_comparator with:o2];
    }

  if (sel_is_mapped(_comparator))
    comparisonFunc = (int(*)(elt,elt))comparatorForSel;
  else
    comparisonFunc = _comparator;

  for (i = 0;
       i < _count && comparisonFunc(newElement,_contents_array[i]) > 0;
       i++)
    ;
  [self insertElement:newElement atIndex:i];
  return self;
}


// TESTING;

- (BOOL) comparatorIsFunc
{
  return ! sel_is_mapped(_comparator);
}

- (int(*)(elt,elt)) comparisonFunc
{
  if ([self comparatorIsFunc])
    return _comparator;
  else
    return 0;
}


// SMALLTALK BLOCKS AS SELECTORS;

- initComparisonSel: (SEL)aCompareSel
{
  return [self initComparisonFunc:aCompareSel];
}

- (SEL) comparisonSel
{
  if ([self comparatorIsSel])
    return (SEL)_comparator;
  else
    return 0;
}

- setComparisonFunc: (int(*)(elt,elt))aFunc
{
  _comparator = aFunc;
  return self;
}

- setComparisonSel: (SEL)aCompareSel
{
  if (sel_is_mapped(aCompareSel))
    _comparator = (int(*)(elt,elt))aCompareSel;
  else
    [self warning:"in %s, argument is not a selector", sel_get_name(_cmd)];
  return self;
}

- (BOOL) comparatorIsSel
{
  if (sel_is_mapped(_comparator))
    return YES;
  else
    return NO;
}


@end
