#ifndef SEARCH_HPP_
#define SEARCH_HPP_

#include "aps.hpp"
#include "search.h"

namespace search {

template<SitId nsit>
void PosFixed<nsit>::resize(SitId ansit)
{
	clear();
}

template<SitId nsit>
SitId PosFixed<nsit>::size(void) const
{
	return (nsit);
}

template<SitId nsit>
SitId PosFixed<nsit>::firstzero(void) const
{
	Val j;
	int i, k, l;

	j = 0;
	for (i = 0; i < words - 1; i++)
		if (!!(j = val[i] ^ ~(~0 << bits - 1 << 1))) break;
	if (i == words - 1 && !(j = val[i] ^ ~(~0 << (nsit - 1 & bits - 1) << 1)))
		return (nsit);
	for (k = bits; k >>= 1; i <<= 1, i |= l)
		if (!!(l = !(j & ~(~0 << k)))) j >>= k;
	return (i);
}


template<SitId nsit>
bool PosFixed<nsit>::isand(const PosFixed &src) const
{
	int i;

	for (i = 0; i < words; i++)
		if (val[i] & src.val[i]) return (true);
	return (false);
}

template<SitId nsit>
PosFixed<nsit> &PosFixed<nsit>::setor(const PosFixed &src1, const PosFixed &src2)
{
	int i;

	for (i = 0; i < words; i++)
		val[i] = src1.val[i] | src2.val[i];
	return (*this);
}

template<SitId nsit>
PosFixed<nsit> &PosFixed<nsit>::clear(void)
{
	int i;

	for (i = 0; i < words; i++) val[i] = 0;
	return (*this);
}

template<SitId nsit>
PosFixed<nsit> &PosFixed<nsit>::set(LocId loc)
{
	val[loc >> bitsbits] |= 1 << (loc & bits - 1);
	return (*this);
}

template<SitId nsit>
bool PosFixed<nsit>::get(LocId loc) const
{
	return (val[loc >> bitsbits] >> (loc & bits - 1) & 1);
}

template<SitId nsit>
std::ostream &operator<<(std::ostream &output, const PosFixed<nsit> &pos)
{
	SitId i;

	for (i = 0; i < nsit; i++) output << pos.get(i);
	return (output);
}

template<class POS>
Shape<POS>::Shape(int an, int ashapeid)
	: n(an), shapeid(ashapeid)
{
}

template<class POS>
ShapeId Shape<POS>::getshapeid(void)
{
		return (shapeid);
}

template<class POS>
int Shape<POS>::pick(void)
{
	return (--n);
}

template<class POS>
int Shape<POS>::place(void)
{
	return (n++);
}

template<class POS>
std::ostream &operator<<(std::ostream &output, const Shape<POS> &shape)
{
	output << shape.n << ':' << shape.shapeid << ':' << shape.heds;
	return (output);
}

template<class POS>
Shape<POS> Server<POS>::loadshape(int ipart, ShapeId shapeid)
{
	typename Heds<POS>::iterator si;
	typename Hed<POS>::iterator li;
	Shape<POS> shape;
	SitId nsit;
	int ncell;

	shape = Shape<POS>(ipart, shapeid);
	shape.heds.resize(nsit = pclient->getnsit());
	for (si = shape.heds.begin(); si != shape.heds.end(); si++) {
		si->resize(pclient->gethedposs());
		for (li = si->begin(); li != si->end(); li++) {
			li->resize(nsit);
			ncell = pclient->getpossits();
			while (ncell--) li->set(pclient->getsit());
		}
	}
	return (shape);
}

template<class POS>
int Server<POS>::load(void)
{
	ShapeId shapeid, nshape;
	int ipart, npart;

	nshape = pclient->getwholeshapes();
	npart = 0;
	for (shapeid = 0; shapeid < nshape; shapeid++) {
		npart += ipart = pclient->getshapeparts();
		shapes.push_back(loadshape(ipart, shapeid));
	}
	return (npart);
}

template<class POS>
void Server<POS>::pick(
		typename Shapes<POS>::iterator &iter,
		typename Shapes<POS>::iterator &next)
{
	if (iter->pick()) return;
	(next = iter)++;
	empties.splice(empties.end(), shapes, iter);
	(iter = empties.end())--;
}

template<class POS>
void Server<POS>::place(
		typename Shapes<POS>::iterator &iter,
		typename Shapes<POS>::iterator &next)
{
	if (iter->place()) return;
	shapes.splice(next, empties, iter);
	(iter = next)--;
}

template<class POS>
void Server<POS>::run(std::vector<Part>::iterator i,
		const POS &pos)
{
	typename Shapes<POS>::iterator sb, si, se, sn;
	SitId hedid;
	typename Hed<POS>::iterator hb, hi, he;
	POS newpos;

	if (i == solution.end()) {
		pclient->setsolparts(i - solution.begin());
		for (i = solution.begin(); i != solution.end(); i++)
			pclient->setsolpart(i->shapeid, i->hedid, i->sitid);
	}
	else {
		newpos.resize(pos.size());
		sb = shapes.begin();
		se = shapes.end();
		hedid = pos.firstzero();
		for (si = sb; si != se; si++) {
			hb = si->heds[hedid].begin();
			he = si->heds[hedid].end();
			for (hi = hb; hi != he; hi++) if (!hi->isand(pos)) {
				*i++ = Part(si->getshapeid(), hedid, hi - hb);
				pick(si, sn);
				run(i--, newpos.setor(pos, *hi));
				place(si, sn);
			}
		}
	}
}

template<class POS>
void Server<POS>::clear()
{
	shapes.clear();
	empties.clear();
	solution.clear();
}

template<class POS>
void Server<POS>::run(Client *apclient)
{
	POS pos;

	pclient = apclient;
	pos.resize(pclient->getnsit());
	solution = vector<Part>(load());
	pclient->setsol();
	run(solution.begin(), pos.clear());
	clear();
}

template<class POS>
std::ostream &operator<<(std::ostream &output, const Server<POS> &search)
{
	output << "shapes=" << search.shapes;
	output << ",empties=" << search.empties;
	output << ",solution=" << search.solution;
	return (output);
}

} // end of namespace search

#endif // end of SEARCH_HPP_
