Hi, i've just refactored a class that originally used a STL set in order to keep track of the possibles values for each cell in my sudoku solver, by using a WORD instead to store values from 1 to 9, since i used the exact same interface(almost), no code changes we're required elsewhere.
Everything worked fine the first shoot except for the set difference operation. I got it to work, but something is puzzling me. First, let me show you the old code
/***************************************************************************************************************/
// Perform a difference operation on the set
/***************************************************************************************************************/
Possibles Possibles::operator-=(Possibles &s)
{
Possibles TmpSet = *this;
possibles.clear();
set_difference(TmpSet.GetSet()->begin(), TmpSet.GetSet()->end(), s.GetSet()->begin(), s.GetSet()->end(), inserter(possibles, possibles.begin()));
return *this;
}
/***************************************************************************************************************/
// Perform an union operation on the set
/***************************************************************************************************************/
Possibles Possibles::operator+=(Possibles &s)
{
Possibles TmpSet = *this;
possibles.clear();
set_union(TmpSet.GetSet()->begin(), TmpSet.GetSet()->end(), s.GetSet()->begin(), s.GetSet()->end(), inserter(possibles, possibles.begin()));
return *this;
}
/***************************************************************************************************************/
// Perform an intersection operation on the set
/***************************************************************************************************************/
Possibles Possibles::operator|=(Possibles &s)
{
Possibles TmpSet = *this;
possibles.clear();
set_intersection(TmpSet.GetSet()->begin(), TmpSet.GetSet()->end(), s.GetSet()->begin(), s.GetSet()->end(), inserter(possibles, possibles.begin()));
return *this;
}
(NOTE: not sure i used the right operator sign...)
Then, when i emulated this with a WORD, the code looked like this:
/***************************************************************************************************************/
// Perform an union operation on the set
/***************************************************************************************************************/
Possibles Possibles::operator+=(Possibles &s)
{
p |= s.Get();
return *this;
}
/***************************************************************************************************************/
// Perform a difference operation on the set
/***************************************************************************************************************/
Possibles Possibles::operator-=(Possibles &s)
{
p ^= s.Get();
return *this;
}
/***************************************************************************************************************/
// Perform an intersection operation on the set
/***************************************************************************************************************/
Possibles Possibles::operator|=(Possibles &s)
{
p &= s.Get();
return *this;
}
All worked fine except the -= operator.
For example, the first code only added the difference IN THE FIRST SET instead of both (wich is the required behavior for my game), but the 2nd example added the difference in BOTH set, wich is normal but not what i wanted. So, to fix it, i had to write some ugly code like this:
/***************************************************************************************************************/
// Perform a difference operation on the set
/***************************************************************************************************************/
Possibles Possibles::operator-=(Possibles &s)
{
WORD w1 = p;
WORD w2 = s.Get();
WORD res = 0;
for(int i = 0; i < 9; i++){
if(((w1 & (1<<i)) > 0) && ((w2 & (1<<i)) == 0))
res |= (1<<i);
}
p = res;
return *this;
}
That way, everything work fine except the code is not like the other 2 functions, and i was simply wondering why is that? To be more clear, why can i use simple bitwise operation for 2 of the 3 operation but not the 3rd???
EDIT: I might not be able to respond to this thread for a few days.