Representing a state in a compact way


Many algorithms depend on representing the states of many objects in the domain world in the smallest way ever.

This is some C++ code to do this:

int GetWorldState(vector<Obj> *Objs)
{
int state = 0;

for(int i = 0 ; i <Objs->size(); i++)
{
//Shift the next bit to be initialized
state <<= 1;

//Get the current object
Obj * ch = &Objs->operator [](i);

//Fill the bit if object has a special property, otherwise don't
if ( Obj->isLeft ) state |= 1;
}

//Return the final result
return state;
}

This idea can be generalized to include complex objects.

Advertisements