// Copyright (C) 1996 DIMACS Center, Rutgers, The State University of New Jersey
// Author(s): ??, Jonathan Berry
// This software is copyrighted by the DIMACS Center at Rutgers, The State
// University of New Jersey.  IT IS PROVIDED AS IS, AND THE AUTHORS, DIMACS, AND
// RUTGERS, THE STATE UNIVERSITY OF NEW JERSEY  DISCLAIM
// ALL LIABILITY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
// DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
// IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
// NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
// MODIFICATIONS.
// The authors hereby grant permission to use, copy, modify, distribute,
// and license this software and its documentation for any purpose, provided
// that existing copyright notices are retained in all copies and that this
// notice is included verbatim in any distributions. No written agreement,
// license, or royalty fee is required for any of the authorized uses.
// Modifications to this software may be copyrighted by their authors
// and need not follow the licensing terms described here, provided that
// the new terms are clearly indicated on the first page of each file where
// they apply.
// Last File Update: 7-Aug-1997
// 
#include <iostream.h>
#ifndef SortedArray_CC
#define SortedArray_CC

template <class Item>
SortedArray<Item>::SortedArray(int sz)
{
    _count = 0;
    _ia = new Item[_size = sz];
    static Item it;
    for (int i = 0; i < sz; i++)
        _ia[i] = it;  // empty_data; //(Item)0;
}
template <class Item>
SortedArray<Item>::SortedArray(const SortedArray<Item>& c) 
{
	_count = 0;
	Iterator<Item> get_next(&c);
	Item elem;
	while (get_next(elem))
		append(elem);
}
template <class Item>
SortedArray<Item>::SortedArray(const Container<Item>& c) 
{
	_count = 0;
	Iterator<Item> get_next(&c);
	Item elem;
	while (get_next(elem))
		insert(elem);
}
template <class Item>
SortedArray<Item>&
SortedArray<Item>::operator=(const SortedArray<Item> &iA)
{
    if (this == &iA) return *this;
    delete [] _ia;
    _size = iA._size;
    _count = iA._count;
    _ia = new Item[_size];
    for (int ix=0; ix < _size;  ix++)
        _ia[ix] = iA._ia[ix];
    return *this;
}
template <class Item>
Bool
SortedArray<Item>::memberQ(const Item& val) const
{
    for (int ix = 0; ix < _count; ++ix)
    {
        if (ElementOps<Item>::compareItems(val,_ia[ix])==0) return TRUE;
	if (ElementOps<Item>::compareItems(val, _ia[ix])>0) return FALSE;
    }
    return FALSE;
}
    
template <class Item>
Item
SortedArray<Item>::min()
{
    return _ia[0];
}
template <class Item>
Item
SortedArray<Item>::max()
{
    if (_count == 0) {
	static Item i;
	return i;
    }
    return _ia[_count-1];
}
template <class Item>
int
SortedArray<Item>::search(const Item& val) const
{
    int low=0, high=_count-1, mid, comp;
    while (low <= high) {
	mid = low + (int) (high-low)/2;
        if ((comp=ElementOps<Item>::compareItems(_ia[mid],val))==0) 
		return mid;
	else if (comp < 0)
		low = mid+1;
	else
		high = mid-1;
    }
    return -1;
}
template <class Item>
ContainerNode*
SortedArray<Item>::insert(const Item e)
{
    int i = _count-1;
    if (_count == _size)
        resize(2*_size);
    while ((i >= 0) && (ElementOps<Item>::compareItems(_ia[i], e)>0))
    {
        _ia[i+1] = _ia[i];
	i--;
    }
    _ia[i+1] = e;
    _count++;
    return (ContainerNode*) 0;
}
template <class Item>
ContainerNode*
SortedArray<Item>::append(const Item e)
{
    if (_count == _size)
        resize(2*_size);
    if ((_count ==0) || (ElementOps<Item>::compareItems(_ia[_count-1], e) > 0))
    {
	return insert(e);
    }
    _ia[_count] = e;
    _count++;
    return (ContainerNode*) 0;
}
#endif
