00001
00008 #pragma once
00009
00010 #include "utils.h"
00011 #include <map>
00012 #include <list>
00013
00014 using std::map;
00015 using std::list;
00016
00017 typedef pair<u64, int> PositionPair;
00018 typedef map<u64, int> PositionMap;
00019
00020
00021 u64 getRandomU64();
00022
00023
00024 #define MAX_LEVELS 50
00025
00026 template<typename T> class HashTable
00027 {
00028 protected:
00029 map <u64, T> table;
00030
00031 public:
00032
00033 HashTable()
00034 {
00035 table.clear();
00036 }
00037
00038
00039
00040 void clear()
00041 {
00042 table.clear();
00043 }
00044
00045
00046
00047 bool isEmpty()
00048 {
00049 return table.empty();
00050 }
00051
00052
00053
00059 bool hasItem(u64 key)
00060 {
00061 if (table.find(key) != table.end())
00062 return true;
00063 return false;
00064 }
00065
00069 void insertItem(u64 key, T item)
00070 {
00071 table[key] = item;
00072 }
00073
00080 bool loadItem(u64 key, T& item)
00081 {
00082 if (hasItem(key)){
00083 item = HashTable<T>::table[key];
00084 return true;
00085 }
00086 return false;
00087 }
00088 };
00089
00090 template<typename T> class HashTableBoard : public HashTable<T>
00091 {
00092 public:
00093 HashTableBoard(): HashTable<T>()
00094 {
00095 playerSignature_[0] = getRandomU64();
00096 playerSignature_[1] = getRandomU64();
00097 for (int i = 0; i < MAX_LEVELS; i++){
00098 levelSignature_[i] = getRandomU64();
00099 }
00100 }
00101
00102
00103
00107 bool hasItem(u64 key, uint playerIndex, uint level=0)
00108 {
00109 assert(playerIndex == 0 || playerIndex == 1);
00110 key ^= playerSignature_[playerIndex];
00111 key ^= levelSignature_[level % MAX_LEVELS];
00112 return HashTable<T>::hasItem(key);
00113 }
00114
00115
00116
00120 void insertItem(u64 key, uint playerIndex, T item, uint level=0)
00121 {
00122 assert(playerIndex == 0 || playerIndex == 1);
00123 key ^= playerSignature_[playerIndex];
00124 key ^= levelSignature_[level % MAX_LEVELS];
00125 HashTable<T>::insertItem(key, item);
00126 }
00127
00128
00129
00134 bool loadItem(u64 key, uint playerIndex, T& item, uint level=0)
00135 {
00136 assert(playerIndex == 0 || playerIndex == 1);
00137 key ^= playerSignature_[playerIndex];
00138 key ^= levelSignature_[level % MAX_LEVELS];
00139
00140 return HashTable<T>::loadItem(key, item);
00141 }
00142
00143 protected:
00144 u64 playerSignature_[2];
00145 u64 levelSignature_[MAX_LEVELS];
00146
00147 };
00148
00154 class ThirdRep: public HashTableBoard<int>
00155 {
00156 public:
00157 ThirdRep(): HashTableBoard<int>()
00158 {
00159 playerSignature_[0] = getRandomU64();
00160 playerSignature_[1] = getRandomU64();
00161 }
00162
00163 void print() {
00164 cerr << playerSignature_[0] << " | " << playerSignature_[1] << "|" << isEmpty() << endl;
00165 }
00171 void update(u64 key, uint playerIndex );
00172
00176 bool isThirdRep(u64 key, uint playerIndex );
00177 protected:
00178 u64 playerSignature_[2];
00179 };
00180
00181 extern ThirdRep thirdRep;
00182
00183
00184 class Node;
00185
00186 typedef list<Node*> NodeList;
00187
00188
00189 class ttItem;
00190
00191
00192
00193
00201 typedef HashTableBoard<NodeList *> TT;
00202
00203 typedef HashTable<float> EvalTT;