00001 //---------------------------------------------------------------------------- 00002 /** @file VC.cpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #include <sstream> 00007 #include "VC.hpp" 00008 00009 using namespace benzene; 00010 00011 //---------------------------------------------------------------------------- 00012 00013 VC::VC() 00014 : m_x(INVALID_POINT), 00015 m_y(INVALID_POINT), 00016 m_key(VC::NO_KEY), 00017 m_carrier(), 00018 m_rule(VC_RULE_BASE), 00019 m_processed(false), 00020 m_count(0) 00021 { 00022 } 00023 00024 VC::VC(HexPoint x, HexPoint y) 00025 : m_x(std::min(x,y)), 00026 m_y(std::max(x,y)), 00027 m_key(VC::NO_KEY), 00028 m_carrier(), 00029 m_rule(VC_RULE_BASE), 00030 m_processed(false), 00031 m_count(0) 00032 { 00033 } 00034 00035 VC::VC(HexPoint x, HexPoint y, const bitset_t& carrier, VcCombineRule rule) 00036 : m_x(std::min(x,y)), 00037 m_y(std::max(x,y)), 00038 m_key(VC::NO_KEY), 00039 m_carrier(carrier), 00040 m_rule(rule), 00041 m_processed(false), 00042 m_count(static_cast<byte>(carrier.count())) 00043 { 00044 } 00045 00046 VC::VC(HexPoint x, HexPoint y, HexPoint key, const bitset_t& carrier, 00047 VcCombineRule rule) 00048 : m_x(std::min(x,y)), 00049 m_y(std::max(x,y)), 00050 m_key(key), 00051 m_carrier(carrier), 00052 m_rule(rule), 00053 m_processed(false), 00054 m_count(static_cast<byte>(carrier.count())) 00055 { 00056 if (type() == VC::SEMI) 00057 HexAssert(m_carrier.test(key)); 00058 } 00059 00060 std::string VC::toString() const 00061 { 00062 std::ostringstream os; 00063 os << std::setw(6) << this->x(); 00064 os << std::setw(6) << this->y(); 00065 os << std::setw(6) << VCTypeUtil::toString(this->type()); 00066 os << std::setw(7) << this->rule(); 00067 00068 os << " ["; 00069 os << HexPointUtil::ToString(this->carrier()); 00070 os << " ]"; 00071 00072 os << " ["; // Removed stones: leave this so the gui doesn't break 00073 os << " ]"; 00074 00075 if (this->type() == VC::SEMI) 00076 os << " " << this->key(); 00077 00078 return os.str(); 00079 } 00080 00081 //---------------------------------------------------------------------------- 00082 00083 VC VC::AndVCs(HexPoint x, HexPoint y, const VC& v1, const VC& v2) 00084 { 00085 HexAssert((v1.carrier() & v2.carrier()).none()); 00086 return VC(x, y, v1.carrier() | v2.carrier(), VC_RULE_AND); 00087 } 00088 00089 VC VC::AndVCs(HexPoint x, HexPoint y, const VC& v1, const VC& v2, 00090 const bitset_t& capturedSet) 00091 { 00092 HexAssert(BitsetUtil::IsSubsetOf(v1.carrier() & v2.carrier(), 00093 capturedSet)); 00094 return VC(x, y, v1.carrier() | v2.carrier() | capturedSet, VC_RULE_AND); 00095 } 00096 00097 VC VC::AndVCs(HexPoint x, HexPoint y, const VC& v1, const VC& v2, HexPoint key) 00098 { 00099 HexAssert((v1.carrier() & v2.carrier()).none()); 00100 return VC(x, y, key, (v1.carrier() | v2.carrier()).set(key), VC_RULE_AND); 00101 } 00102 00103 VC VC::AndVCs(HexPoint x, HexPoint y, const VC& v1, const VC& v2, 00104 const bitset_t& capturedSet, HexPoint key) 00105 { 00106 HexAssert(BitsetUtil::IsSubsetOf(v1.carrier() & v2.carrier(), 00107 capturedSet)); 00108 return VC(x, y, key, (v1.carrier() | v2.carrier() | capturedSet).set(key), 00109 VC_RULE_AND); 00110 } 00111 00112 VC VC::UpgradeSemi(const VC& v1, const bitset_t& takeout, 00113 HexPoint outx, HexPoint outy) 00114 { 00115 HexAssert(v1.key() != NO_KEY); 00116 HexAssert(takeout.test(v1.key())); 00117 return VC(outx, outy, v1.carrier() - takeout, VC_RULE_AND); 00118 } 00119 00120 VC VC::ShrinkFull(const VC& v1, const bitset_t& takeout, 00121 HexPoint outx, HexPoint outy) 00122 { 00123 HexAssert(v1.key() == NO_KEY); 00124 HexAssert((v1.carrier() & takeout).any()); 00125 return VC(outx, outy, v1.carrier() - takeout, v1.rule()); 00126 } 00127 00128 VC VC::ShrinkSemi(const VC& v1, const bitset_t& takeout, 00129 HexPoint outx, HexPoint outy) 00130 { 00131 HexAssert(v1.key() != NO_KEY); 00132 HexAssert(!takeout.test(v1.key())); 00133 HexAssert((v1.carrier() & takeout).any()); 00134 return VC(outx, outy, v1.key(), v1.carrier() - takeout, v1.rule()); 00135 } 00136 00137 //---------------------------------------------------------------------------- 00138 00139 bool VCTypeUtil::IsValidType(VC::Type type) 00140 { 00141 return (type == VC::FULL || type == VC::SEMI); 00142 } 00143 00144 std::string VCTypeUtil::toString(VC::Type type) 00145 { 00146 HexAssert(IsValidType(type)); 00147 if (type == VC::FULL) 00148 return "full"; 00149 return "semi"; 00150 } 00151 00152 VC::Type VCTypeUtil::fromString(std::string name) 00153 { 00154 if (name == "full") 00155 return VC::FULL; 00156 if (name == "semi") 00157 return VC::SEMI; 00158 std::istringstream is(name); 00159 int num = 0; 00160 #ifdef NDEBUG 00161 is >> num; 00162 #else 00163 HexAssert(is >> num); 00164 #endif 00165 HexAssert(num == 0 || num == 1); 00166 if (num == 0) 00167 return VC::FULL; 00168 return VC::SEMI; 00169 } 00170 00171 //----------------------------------------------------------------------------