00001 #include "osl/checkmate/twinTable.h" 00002 #include "osl/checkmate/twinList.h" 00003 #include "osl/checkmate/checkMoveListProvider.h" 00004 #include "osl/stl/hash_set.h" 00005 00006 namespace osl 00007 { 00008 namespace checkmate 00009 { 00010 #ifdef OSL_SMP 00011 static const int initial_capacity = 0; 00012 #else 00013 static const int initial_capacity = 100; 00014 #endif 00015 00022 struct TwinAgeMutable 00023 { 00024 mutable TwinAgeEntry body; 00025 TwinAgeMutable(const TwinAgeEntry& e) : body(e) 00026 { 00027 } 00028 }; 00029 00030 struct TwinPathHash 00031 { 00032 unsigned long operator()(const TwinAgeMutable& e) const 00033 { 00034 return e.body.entry.path.getPath(); 00035 } 00036 }; 00037 struct TwinPathEqual 00038 { 00039 bool operator()(const TwinAgeMutable& l, const TwinAgeMutable& r) const 00040 { 00041 return l.body.entry.path == r.body.entry.path; 00042 } 00043 }; 00044 00045 typedef hash_set<TwinAgeMutable,TwinPathHash,TwinPathEqual> hash_set_t; 00046 00047 struct TwinTable::Table : public hash_set_t 00048 { 00049 explicit Table(size_t capacity) : hash_set_t(capacity) 00050 { 00051 } 00052 }; 00053 } // namespace checkmate 00054 } // namespace osl 00055 00056 osl::checkmate:: 00057 TwinTable::TwinTable() : table(new Table(initial_capacity)) 00058 { 00059 } 00060 00061 osl::checkmate:: 00062 TwinTable::~TwinTable() 00063 { 00064 } 00065 00066 void osl::checkmate:: 00067 TwinTable::addTwin(const TwinEntry& e) 00068 { 00069 const TwinAgeEntry ae(e.path, e.move, e.loopTo); 00070 std::pair<Table::iterator,bool> ib = table->insert(TwinAgeMutable(ae)); 00071 if (! ib.second) 00072 { 00073 ib.first->body.setTwinEntry(e); 00074 } 00075 } 00076 00077 void osl::checkmate:: 00078 TwinTable::clearTwins() 00079 { 00080 table->clear(); 00081 } 00082 00083 const osl::checkmate::TwinEntry * osl::checkmate:: 00084 TwinTable::findTwin(const PathEncoding& key) const 00085 { 00086 Table::const_iterator p=table->find(TwinAgeMutable(TwinAgeEntry(key))); 00087 if ((p == table->end()) 00088 || (! p->body.hasTwinEntry())) 00089 return 0; 00090 return &(p->body.entry); 00091 } 00092 00093 osl::checkmate::TwinAgeEntry& osl::checkmate:: 00094 TwinTable::allocateTwin(const PathEncoding& key) 00095 { 00096 return table->insert(TwinAgeMutable(TwinAgeEntry((key)))).first->body; 00097 } 00098 00099 size_t osl::checkmate:: 00100 TwinTable::size() const 00101 { 00102 return table->size(); 00103 } 00104 00105 /* ------------------------------------------------------------------------- */ 00106 00107 osl::checkmate:: 00108 TwinTableHolder::TwinTableHolder() : list_provider(new CheckMoveListProvider()) 00109 { 00110 } 00111 00112 osl::checkmate:: 00113 TwinTableHolder::~TwinTableHolder() 00114 { 00115 } 00116 00117 void osl::checkmate:: 00118 TwinTableHolder::clear() 00119 { 00120 list_provider->clear(); 00121 clearTwins(); 00122 } 00123 00124 /* ------------------------------------------------------------------------- */ 00125 // ;;; Local Variables: 00126 // ;;; mode:c++ 00127 // ;;; c-basic-offset:2 00128 // ;;; End: