piecePairRawTable.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
piecePairRawTable.cc
Go to the documentation of this file.
1 /* piecePairRawTable.cc
2 */
3 #include "osl/eval/ppair/piecePairRawEval.h"
4 #include "osl/eval/ppair/piecePairEval.tcc"
5 #include "osl/stl/vector.h"
6 #include "osl/oslConfig.h"
7 #include <boost/filesystem.hpp>
8 #ifdef __MINGW32__
9 # include <boost/filesystem/fstream.hpp>
10 #endif
11 #include <boost/static_assert.hpp>
12 #include <algorithm>
13 #include <fstream>
14 #include <iostream>
15
16 namespace osl
17 {
18 namespace eval
19 {
20 namespace ppair
21 {
22 BOOST_STATIC_ASSERT(PiecePairIndex::maxPtypeOIndex == 32);
23 // roundup が 2^n かつ 2 以上であることの確認
24 BOOST_STATIC_ASSERT((PiecePairEvalBase::ROUND_UP
25 & (PiecePairEvalBase::ROUND_UP-1))
26 == 0);
27 BOOST_STATIC_ASSERT(PiecePairEvalBase::ROUND_UP >= 2);
28
29 template class PiecePairEvalTableBase<PiecePairRawTable>;
30 template class PiecePairEval<PiecePairRawEval,PiecePairRawTable>;
31 } // namespace ppair
32 } // namespace eval
33 } // namespace osl
34
35 osl::eval::ppair::
36 PiecePairRawTable::PiecePairRawTable()
37 {
38 }
39
40 osl::eval::ppair::
41 PiecePairRawTable::~PiecePairRawTable()
42 {
43 }
44
45 void osl::eval::ppair::
46 PiecePairRawTable::writeInBinaryFile(const char *filename) const
47 {
48 std::ofstream os(filename);
49 osl::stl::vector<signed char> buffer(82*PTYPEO_SIZE*82*PTYPEO_SIZE);
50 for(int i=0;i<82;i++){
51 Square pos0=SquareCompressor::melt(i);
52 for(int j=0;j<82;j++){
53 Square pos1=SquareCompressor::melt(j);
54 for(int k=0;k<PTYPEO_SIZE;k++)
55 for(int l=0;l<PTYPEO_SIZE;l++){
56 int index0=PiecePairIndex::indexOf(pos0,static_cast<PtypeO>(k+PTYPEO_MIN));
57 int index1=PiecePairIndex::indexOf(pos1,static_cast<PtypeO>(l+PTYPEO_MIN));
58 int index=PiecePairIndex::indexOf(index0,index1);
59
60 buffer[(i*32+k)*82*32+(j*32+l)]=values[index];
61 }
62 }
63
64 }
65 os.write(reinterpret_cast<const char*>(&*buffer.begin()),buffer.size());
66 }
67
68 bool osl::eval::ppair::
69 PiecePairRawTable::
70 loadFromBinaryFile(const char *filename) const
71 {
72 if (OslConfig::verbose())
73 std::cerr << "PiecePairRawTable loading... " << filename << "\n";
74
75 #ifdef __MINGW32__
76 namespace bf = boost::filesystem;
77 bf::path filename_path(filename);
78 bf::ifstream is(filename_path, std::ios_base::in | std::ios_base::binary);
79
80 is.exceptions(std::ios_base::badbit | std::ios_base::failbit);
81 // Workaround a bug of MINGW.
82 osl::stl::vector<signed char> temp_buffer(82*PTYPEO_SIZE);
83 is.rdbuf()->pubsetbuf(reinterpret_cast<char*>(&*temp_buffer.begin()), temp_buffer.size());
84 #else
85 std::ifstream is(filename);
86 #endif
87 osl::stl::vector<signed char> buffer(82*PTYPEO_SIZE*82*PTYPEO_SIZE);
88 is.read(reinterpret_cast<char*>(&*buffer.begin()), buffer.size());
89 for(int i=0;i<82;i++){
90 Square pos0=SquareCompressor::melt(i);
91 for(int j=0;j<82;j++){
92 Square pos1=SquareCompressor::melt(j);
93 for(int k=0;k<PTYPEO_SIZE;k++)
94 for(int l=0;l<PTYPEO_SIZE;l++){
95 int index0=PiecePairIndex::indexOf(pos0,static_cast<PtypeO>(k+PTYPEO_MIN));
96 int index1=PiecePairIndex::indexOf(pos1,static_cast<PtypeO>(l+PTYPEO_MIN));
97 int index=PiecePairIndex::indexOf(index0,index1);
98 if(!pos0.isPieceStand() && !pos1.isPieceStand())
99 values[index]=buffer[(i*32+k)*82*32+(j*32+l)];
100 else
101 values[index]=0;
102 }
103 }
104 }
105
106 if (! is)
107 return false;
108
109 if (OslConfig::verbose())
110 std::cerr << "done.\n";
111 return true;
112 }
113
114 bool osl::eval::ppair::
115 PiecePairRawTable::
116 setUp(const char *filename) const
117 {
118 static std::string filename_memory;
119 if (! filename_memory.empty())
120 {
121 if (filename_memory != filename)
122 {
123 std::cerr << "PiecePairRawTable: don't load " << filename
124 << ", since " << filename_memory
125 << " already loaded \n";
126 return false;
127 }
128 return true;
129 }
130 filename_memory = filename;
131 return loadFromBinaryFile(filename);
132 }
133
134 /* ------------------------------------------------------------------------- */
135 // ;;; Local Variables:
136 // ;;; mode:c++
137 // ;;; c-basic-offset:2
138 // ;;; End: