// 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: 31-Jul-1996
// 

#include<LINK/graph/Graph.h>
#include<LINK/graph/Vertex.h>
#include<LINK/graph/Edge.h>
#include<LINK/graph/Buckets.h>
#include<LINK/basic/Array.h>
#include<LINK/basic/DList.h>
#include<LINK/basic/Iterator.h>

ContainerNode * BucketArray::insert(int cost, VertexInfo *v)
{
	int bnum = cost + num_pos_buckets;

	if (bnum < 0 || bnum >= num_buckets)
		cout << "BucketArray::insert() Error: " << bnum 
		     << " out of bucket array bounds" << endl;

	v->lp = _ia[bnum].insert(v);
	v->lock = 0;
	v->cost = cost;
	v->bucket_num = bnum;
	if (bnum > current_max_bucket)
		current_max_bucket = bnum;
	if (bnum < current_min_bucket)
		current_min_bucket = bnum;
	return v->lp;
}

ContainerNode *BucketArray::insert(int cost, ContainerNode *cn)
{
	int bnum = cost + num_pos_buckets;
	VertexInfoPtr v = * (VertexInfoPtr *) cn->info();

	if (bnum < 0 || bnum >= num_buckets)
		cout << "BucketArray::insert() Error: " << bnum 
		     << " out of bucket array bounds" << endl;
	
	v->lp = _ia[bnum].insert(cn);
	v->cost = cost;
	v->bucket_num = bnum;
	if (bnum > current_max_bucket)
		current_max_bucket = bnum;
	if (bnum < current_min_bucket)
		current_min_bucket = bnum;
	return v->lp;
}

ContainerNode * BucketArray::remove(VertexInfo *p)
{
	ContainerNode *temp;

	if (p->bucket_num < 0 || p->bucket_num >= num_buckets)
		cout << "BucketArray::remove() Error: " << p->bucket_num 
		     << " out of bucket array bounds" << endl;
	
	temp = _ia[p->bucket_num].unlink(p->lp);
	return temp;
}

int BucketArray::findMax()
{
	int max = current_max_bucket;
	ContainerNode * temp;

	while ( (max >= 1) && _ia[max].emptyQ() )
		max--;
	current_max_bucket = max;
	return max - num_pos_buckets;
}

int BucketArray::findMin()
{
	int min = current_min_bucket;
	ContainerNode * temp;

	while ( (min < num_buckets) && _ia[min].emptyQ() )
		min++;
	current_min_bucket = min;
	return min - num_pos_buckets;
}

ContainerNode * BucketArray::extractMax()
{
	int max = current_max_bucket;
	ContainerNode * temp;

	while ( (max >= 1) && _ia[max].emptyQ() )
		max--;
	current_max_bucket = max;

	temp = _ia[max].unlink(_ia[max].search(_ia[max].first()));
	return temp;
}

ContainerNode * BucketArray::extractMin()
{
	int min = current_min_bucket;
	ContainerNode * temp;

	while ( (min < num_buckets) && _ia[min].emptyQ() )
		min++;
	current_min_bucket = min;

	temp = _ia[min].unlink(_ia[min].search(_ia[min].first()));
	return temp;
}

void BucketArray::pfirst(int bnum)
{
	cout << _ia[bnum].first()->node_num << endl;
}

void BucketArray::print()
{
	int i;
	VertexInfo *v;
	ContainerNode *cn;

	cout << "buckets" << endl;
	cout << "-------" << endl;
	for (i=0; i<num_buckets; i++) {
		if (_ia[i].emptyQ())
			continue;
		cout << i-num_pos_buckets << ":: " ;
		{
			Iterator<VertexInfo*> getv(&_ia[i]);
			while (getv(v)) 
				cout << v->node_num << " ";
			cout << endl;
		}
	}
}

ostream& operator<<(ostream& os, BucketArray& b)
{
	b.print();
	return os;
}
