piecePairRawTable.cc
Go to the documentation of this file.
00001 /* piecePairRawTable.cc
00002  */
00003 #include "osl/eval/ppair/piecePairRawEval.h"
00004 #include "osl/eval/ppair/piecePairEval.tcc"
00005 #include "osl/stl/vector.h"
00006 #include "osl/oslConfig.h"
00007 #include <boost/filesystem.hpp>
00008 #ifdef __MINGW32__
00009 #  include <boost/filesystem/fstream.hpp>
00010 #endif
00011 #include <boost/static_assert.hpp>
00012 #include <algorithm>
00013 #include <fstream>
00014 #include <iostream>
00015 
00016 namespace osl
00017 {
00018   namespace eval
00019   {
00020     namespace ppair
00021     {
00022       BOOST_STATIC_ASSERT(PiecePairIndex::maxPtypeOIndex == 32);
00023       // roundup が 2^n かつ 2 以上であることの確認
00024       BOOST_STATIC_ASSERT((PiecePairEvalBase::ROUND_UP 
00025                            & (PiecePairEvalBase::ROUND_UP-1))
00026                           == 0);
00027       BOOST_STATIC_ASSERT(PiecePairEvalBase::ROUND_UP >= 2);
00028 
00029       template class PiecePairEvalTableBase<PiecePairRawTable>;
00030       template class PiecePairEval<PiecePairRawEval,PiecePairRawTable>;
00031     } // namespace ppair
00032   } // namespace eval
00033 } // namespace osl
00034 
00035 osl::eval::ppair::
00036 PiecePairRawTable::PiecePairRawTable()
00037 {
00038 }
00039 
00040 osl::eval::ppair::
00041 PiecePairRawTable::~PiecePairRawTable()
00042 {
00043 }
00044 
00045 void osl::eval::ppair::
00046 PiecePairRawTable::writeInBinaryFile(const char *filename) const
00047 {
00048   std::ofstream os(filename);
00049   osl::stl::vector<signed char> buffer(82*PTYPEO_SIZE*82*PTYPEO_SIZE);
00050   for(int i=0;i<82;i++){
00051     Square pos0=SquareCompressor::melt(i);
00052     for(int j=0;j<82;j++){
00053       Square pos1=SquareCompressor::melt(j);
00054       for(int k=0;k<PTYPEO_SIZE;k++)
00055         for(int l=0;l<PTYPEO_SIZE;l++){
00056           int index0=PiecePairIndex::indexOf(pos0,static_cast<PtypeO>(k+PTYPEO_MIN));
00057           int index1=PiecePairIndex::indexOf(pos1,static_cast<PtypeO>(l+PTYPEO_MIN));
00058           int index=PiecePairIndex::indexOf(index0,index1);
00059 
00060           buffer[(i*32+k)*82*32+(j*32+l)]=values[index];
00061         }
00062     }
00063 
00064   }
00065   os.write(reinterpret_cast<const char*>(&*buffer.begin()),buffer.size());
00066 }
00067 
00068 bool osl::eval::ppair::
00069 PiecePairRawTable::
00070 loadFromBinaryFile(const char *filename) const
00071 {
00072   if (OslConfig::verbose())
00073     std::cerr << "PiecePairRawTable loading...  " << filename << "\n";
00074 
00075 #ifdef __MINGW32__
00076   namespace bf = boost::filesystem;
00077   bf::path filename_path(filename);
00078   bf::ifstream is(filename_path, std::ios_base::in | std::ios_base::binary);
00079 
00080   is.exceptions(std::ios_base::badbit | std::ios_base::failbit);
00081   // Workaround a bug of MINGW.
00082   osl::stl::vector<signed char> temp_buffer(82*PTYPEO_SIZE);
00083   is.rdbuf()->pubsetbuf(reinterpret_cast<char*>(&*temp_buffer.begin()), temp_buffer.size());
00084 #else
00085   std::ifstream is(filename);
00086 #endif
00087   osl::stl::vector<signed char> buffer(82*PTYPEO_SIZE*82*PTYPEO_SIZE);
00088   is.read(reinterpret_cast<char*>(&*buffer.begin()), buffer.size());
00089   for(int i=0;i<82;i++){
00090     Square pos0=SquareCompressor::melt(i);
00091     for(int j=0;j<82;j++){
00092       Square pos1=SquareCompressor::melt(j);
00093       for(int k=0;k<PTYPEO_SIZE;k++)
00094        for(int l=0;l<PTYPEO_SIZE;l++){
00095          int index0=PiecePairIndex::indexOf(pos0,static_cast<PtypeO>(k+PTYPEO_MIN));
00096          int index1=PiecePairIndex::indexOf(pos1,static_cast<PtypeO>(l+PTYPEO_MIN));
00097          int index=PiecePairIndex::indexOf(index0,index1);
00098          if(!pos0.isPieceStand() && !pos1.isPieceStand())
00099            values[index]=buffer[(i*32+k)*82*32+(j*32+l)];
00100          else
00101            values[index]=0;
00102        }
00103     }
00104   }
00105 
00106   if (! is)
00107     return false;
00108 
00109   if (OslConfig::verbose())
00110     std::cerr << "done.\n";
00111   return true;
00112 }
00113 
00114 bool osl::eval::ppair::
00115 PiecePairRawTable::
00116 setUp(const char *filename) const
00117 {
00118   static std::string filename_memory;
00119   if (! filename_memory.empty())
00120   {
00121     if (filename_memory != filename)
00122     {
00123       std::cerr << "PiecePairRawTable: don't load " << filename 
00124                 << ", since " << filename_memory
00125                 << " already loaded \n";
00126       return false;
00127     }
00128     return true;
00129   }
00130   filename_memory = filename;
00131   return loadFromBinaryFile(filename);
00132 }
00133 
00134 /* ------------------------------------------------------------------------- */
00135 // ;;; Local Variables:
00136 // ;;; mode:c++
00137 // ;;; c-basic-offset:2
00138 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines