piecePairIndex.h
Go to the documentation of this file.
00001 /* piecePairIndex.h
00002  */
00003 #ifndef EVAL_PIECEPAIRINDEX_H
00004 #define EVAL_PIECEPAIRINDEX_H
00005 
00006 #include "osl/state/simpleState.h"
00007 #include "osl/squareCompressor.h"
00008 
00009 namespace osl
00010 {
00011   namespace eval
00012   {
00013     namespace ppair
00014     {
00021       struct PiecePairIndex
00022       {
00023         static const unsigned int maxSquareIndex = 82;
00024         static const unsigned int maxPtypeOIndex = PTYPEO_SIZE;
00025         static const unsigned int maxPieceIndex = maxSquareIndex*maxPtypeOIndex;
00026         static const unsigned int maxPairIndex = maxPieceIndex*maxPieceIndex;
00027 
00028         static unsigned int selfIndexOf(unsigned int i) 
00029         {
00030           return indexOf(i, i);
00031         }
00032 
00033         static unsigned int indexOf(unsigned int i1, unsigned int i2)
00034         {
00035           assert(i1 < maxPieceIndex);
00036           assert(i2 < maxPieceIndex);
00037           return i1*maxPieceIndex + i2;
00038         }
00039         static unsigned int canonicalIndexOf(unsigned int i1, unsigned int i2)
00040         {
00041           if (i1 > i2)
00042             std::swap(i1,i2);
00043           return indexOf(i1,i2);
00044         }
00046         static void meltIndex(size_t index, 
00047                               size_t& i1, size_t& i2)
00048         {
00049           i1 = index / maxPieceIndex;
00050           i2 = index % maxPieceIndex;
00051         }
00052         static unsigned int positionIndexOf(Square pos)
00053         {
00054           unsigned int result = SquareCompressor::compress(pos);
00055           assert(result < maxSquareIndex);
00056           return result;
00057         }
00058         static unsigned int ptypeOIndexOf(PtypeO ptypeo)
00059         {
00060           return ptypeo - PTYPEO_MIN;
00061         }
00062         static unsigned int indexOf(Square pos, PtypeO ptypeo)
00063         {
00064           const int result = maxSquareIndex*ptypeOIndexOf(ptypeo)
00065             + positionIndexOf(pos);
00066           return result;
00067         }
00069         static void meltIndex(size_t index, Square& pos, PtypeO& ptypeo)
00070         {
00071           ptypeo = static_cast<PtypeO>(static_cast<int>(index / maxSquareIndex)+PTYPEO_MIN);
00072           pos    = SquareCompressor::melt(index % maxSquareIndex);
00073         }
00074 
00075         static unsigned int indexOf(Piece piece)
00076         {
00077           return indexOf(piece.square(), piece.ptypeO());
00078         }
00079         static unsigned int indexOf(Piece p1, Piece p2)
00080         {
00081           return indexOf(indexOf(p1), indexOf(p2));
00082         }
00083 
00084         static unsigned int indexOfPieceNum(const SimpleState& s, int id)
00085         {
00086           return indexOf(s.pieceOf(id));
00087         }
00088 
00089 
00091         template <class F>
00092         static void forEachRelation(F f);
00093       };
00094     
00095     } // namespace ppair
00096     using ppair::PiecePairIndex;
00097   } // namespace eval
00098 } // namespace osl
00099 
00100 
00101 template <class F>
00102 void osl::eval::ppair::
00103 PiecePairIndex::forEachRelation(F f)
00104 {
00105   for (int x=1; x<=9; ++x)
00106   {
00107     for (int y=1; y<=9; ++y)
00108     {
00109       const Square pos1(x,y);
00110       for (int ptype=PPAWN; ptype<=PTYPE_MAX; ++ptype)
00111       {
00112         const Ptype p1 = static_cast<Ptype>(ptype);
00113         const unsigned int i1 = 
00114           indexOf(pos1, newPtypeO(BLACK, p1));
00115         const unsigned int i1w = 
00116           indexOf(pos1, newPtypeO(WHITE, p1));
00117         f(indexOf(i1,i1));
00118         f(indexOf(i1w,i1w));
00119         
00120         for (int x2=x; x2<=9; ++x2)
00121         {
00122           for (int y2=((x2 == x) ? y+1 : 1); y2<=9; ++y2)
00123           {
00124             const Square pos2(x2,y2);
00125             
00126             for (int ptype2=PPAWN; ptype2<=PTYPE_MAX; ++ptype2)
00127             {
00128               const Ptype p2 = static_cast<Ptype>(ptype2);
00129               const unsigned int i2 = 
00130                 indexOf(pos2, newPtypeO(BLACK, p2));
00131               const unsigned int i2w = 
00132                 indexOf(pos2, newPtypeO(WHITE, p2));
00133 
00134               f(indexOf(i1, i2));
00135               f(indexOf(i1, i2w));
00136               f(indexOf(i1w, i2));
00137               f(indexOf(i1w, i2w));
00138             }
00139           }
00140         }
00141 
00142         for (int ptype2=KING; ptype2<=PTYPE_MAX; ++ptype2)
00143         {
00144           const Ptype p2 = static_cast<Ptype>(ptype2);
00145           const unsigned int i2 = 
00146             indexOf(Square::STAND(), newPtypeO(BLACK, p2));
00147           const unsigned int i2w = 
00148             indexOf(Square::STAND(), newPtypeO(WHITE, p2));
00149 
00150           f(indexOf(i1, i2));
00151           f(indexOf(i1, i2w));
00152           f(indexOf(i1w, i2));
00153           f(indexOf(i1w, i2w));
00154         }
00155       } // ptype
00156     } // x
00157   } // y
00158 
00159   // 持駒同士は最後に
00160   for (int ptype=KING; ptype<=PTYPE_MAX; ++ptype)
00161   {
00162     const Ptype p1 = static_cast<Ptype>(ptype);
00163     const unsigned int i1 = 
00164       indexOf(Square::STAND(), newPtypeO(BLACK, p1));
00165     const unsigned int i1w = 
00166       indexOf(Square::STAND(), newPtypeO(WHITE, p1));
00167     f(indexOf(i1, i1));
00168     f(indexOf(i1, i1w));
00169     f(indexOf(i1w, i1w));
00170     for (int ptype2=ptype+1; ptype2<=PTYPE_MAX; ++ptype2)
00171     {
00172       const Ptype p2 = static_cast<Ptype>(ptype2);
00173       const unsigned int i2 = 
00174         indexOf(Square::STAND(), newPtypeO(BLACK, p2));
00175       const unsigned int i2w = 
00176         indexOf(Square::STAND(), newPtypeO(WHITE, p2));
00177 
00178       f(indexOf(i1, i2));
00179       f(indexOf(i1, i2w));
00180       f(indexOf(i1w, i2));
00181       f(indexOf(i1w, i2w));
00182     }
00183   }
00184 }
00185 
00186 #endif /* EVAL_PIECEPAIRINDEX_H */
00187 // ;;; Local Variables:
00188 // ;;; mode:c++
00189 // ;;; c-basic-offset:2
00190 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines