// id_generator.js - CIdGenerator class

// uses array.js

function CIdGenerator( nMaxId)
{
	this.m_hashOccupedId = new CHash();
	this.m_nNextId = 1;
	this.m_nMaxId = nMaxId;

	this.Clear = CIdGenerator;

	this.NextId = function()
	{
		var nCount = 0;
		while (( nCount < this.m_nMaxId)
					&&( this.m_hashOccupedId.Item( this.m_nNextId))
						)
		{
			if ( this.m_nNextId >= this.m_nMaxId)
				this.m_nNextId = 0;
			this.m_nNextId++;
			nCount++;
		}
		if ( nCount >= this.m_nMaxId)
			return 0;
		return this.m_nNextId;
	}

	this.OccupeId = function( nId)
	{
		this.m_hashOccupedId.Set( nId, true);
	}

	this.FreeId = function( nId)
	{
		this.m_hashOccupedId.Del( nId);
	}
}
