
#include "TabuSearch.hh"
#include <LEDA/list.h>


namespace TabuSearch {


	TabuStorage::TabuStorage (const SetUpParams& setup, const Problem& pbm)
	: config(setup),
      problem(pbm),
	  tlist()
	{
	}


	bool TabuStorage::is_in_tabu_storage (const Movement& mov, 
										  const Solution& sol,
										  const State& state) const
	{
		list_item it;
		bool found = false;
		int  i=0;
		
		while ((!found) && (i<tlist.size())) {
		
			it = tlist[i];
			found = (mov == tlist[it]);			
			i++;
		}
		return found;
 	}


	bool TabuStorage::is_tabu (const Movement& mov, const Solution& sol,
							   const State& state) const
	{
		return is_in_tabu_storage(mov,sol,state);
	}


	void TabuStorage::make_tabu (Movement& mov, const Solution& sol,
	                             const State& state)
	{
	    mov.tabulife = 0;

	    if (!is_tabu( mov,sol,state ))  tlist.append( mov );
	}


        void TabuStorage::make_tabu_inv (Movement& mov, const Solution& sol,
	                                 const State& state)
	{
		mov.invert();
		mov.tabulife = 0;
		tlist.append( mov );
	}


	void TabuStorage::update ()
	{
		list_item it;
		forall_items ( it, tlist )
		{
			Movement& mov=tlist[it];
			
			mov.tabulife++;

			if ((mov.tabulife > config.min_tabu_status) &&
                            (mov.tabulife < config.max_tabu_status))
			{
				tlist.erase( it );
			}
		}
	}


	int  TabuStorage::size () const
	{
	    return tlist.size();
	}


}



