/*	Table widget for FLTK-2
	class Collecton holds integer key-value pairs
	Copyright(C) 2007 Alex Ivanov
	http://www.aleksoft.net
*/

#include "Collection.H"

Collection::Collection(int defaultValue)
	{
		data=0;
		count_=0;
		default_ = defaultValue;
	}
Collection::~Collection() {if(data) free (data);};

void Collection::add(int key, int value)
{
	data = (KeyValue*) realloc(data,(count_+1) * sizeof(KeyValue));
	data[count_].key=key;
	data[count_++].value=value;
};

void Collection::clear()
{
	count_=0;
	if(data) free(data);
	data=0;
};

int Collection::key(uint index)
{
	return data[index].key;
}

int Collection::value(int key)
{
	for(uint i=0;i<count_;i++)
	{
		if(data[i].key == key)
			return data[i].value;
	}
 	return default_;
}

int Collection::value(uint index)
{
	return data[index].value;
}

void Collection::value(int key, int value)
{
	for(int i=0; i<count_;i++)
	{
		if(data[i].key == key)
		{
			data[i].value=value;
			return;
		}
	}
	add(key,value);
};

void Collection::setDefault(int newVal)
{
	default_ = newVal;
};

int Collection::getDefault()
{
	return default_;
};

int Collection::count()
{
	return count_;
};
