using System;
namespace MemoryPools.Collections.Specialized
{
	/// 
	/// Represents ideal dictionary with extra fast access to its items. Items should inherit IdealHashObjectBase to be
	/// able to set hashcode.
	/// 
	/// Key of dictionary
	/// Corresponding Value
	public class IdealHashDictionary : 
		IDisposable
		where TK : IdealHashObjectBase
		where TV : class
	{
		readonly PoolingListCanon _list = Pool>.Get().Init();
		readonly PoolingQueue _freeNodes = new PoolingQueueVal();
  
		public TV this[TK key]
		{
			get
			{
				var hc = key.IdealHashCode;
				if(hc >= _list.Count)
					throw new ArgumentOutOfRangeException(nameof(key));
				return _list[hc];
			}
		}
  
		public void Add(TK key, TV value)
		{
			var index = AcquireHashCode(value);
			key.IdealHashCode = index;
		}
  
		public bool Remove(TK key)
		{
			var index = key.IdealHashCode;
			_freeNodes.Enqueue(index);
			_list[index] = default!;
			return true;
		}
		private int AcquireHashCode(TV value)
		{
			if(_freeNodes.Count > 0)
			{
				return _freeNodes.Dequeue();
			}
			_list.Add(value);
			return _list.Count - 1;
		}
		public void Dispose()
		{
			_list.Clear();
			_freeNodes.Clear();
		}
	}
}