11 #include <boost/accumulators/accumulators.hpp> 
   12 #include <boost/accumulators/statistics/mean.hpp> 
   13 #include <boost/accumulators/statistics/max.hpp> 
   14 #include <boost/scoped_ptr.hpp> 
   15 #include <boost/program_options.hpp> 
   16 #include <boost/filesystem/convenience.hpp> 
   17 #include <boost/foreach.hpp> 
   18 #include <boost/format.hpp> 
   24                    const osl::vector<osl::Move> &in,
 
   28   osl::NumEffectState state(initial);
 
   31     const int see = osl::See::see
 
   32       (state, move, state.pin(state.turn()), state.pin(
alt(state.turn())));
 
   35     if (state.inCheck() && see < 0
 
   44           const osl::vector<osl::Move> &in,
 
   50   osl::HistoryState history(initial);
 
   52     history.makeMove(move);
 
   55   for (; length > 0; length -= 2)
 
   57     osl::NumEffectState current = history.state();
 
   58     assert(current.turn() == 
alt(last_played));
 
   59     if (! current.inCheck())
 
   71 static void convert(
const std::vector<std::string> &input_filename,
 
   72                     const std::string &output_kisen_filename,
 
   75   namespace acc = boost::accumulators;
 
   76   acc::accumulator_set<double, acc::features<acc::tag::max, acc::tag::mean> > accumulator;
 
   77   std::ofstream ofs(output_kisen_filename.c_str());
 
   80   boost::scoped_ptr<osl::record::KisenIpxWriter> ipx_writer;
 
   81   boost::scoped_ptr<std::ofstream> ipx_ofs;
 
   84     const boost::filesystem::path ipx_path =
 
   85       boost::filesystem::change_extension(boost::filesystem::path(output_kisen_filename), 
".ipx");
 
   87     ipx_ofs.reset(
new std::ofstream(ipx.c_str()));
 
   91   for (
size_t i = 0; i < input_filename.size(); ++i)
 
   93     osl::KisenFile kisen(input_filename[i]);
 
   94     osl::KisenIpxFile ipx(kisen.ipxFileName());
 
   95     for (
size_t j=0; j<kisen.size(); ++j) 
 
   97       osl::NumEffectState state = kisen.getInitialState();
 
   98       osl::vector<osl::Move> 
moves = kisen.getMoves(j);
 
   99       osl::vector<osl::Move> new_moves;
 
  101         trim_last(state, moves, new_moves, checkmate_limit);
 
  108       new_record.
setDate(ipx.getStartDate(j));
 
  109       osl::SimpleState record_state = state;
 
  113       for (
size_t k=0; k<new_moves.size(); ++k) 
 
  116         state.makeMove(new_moves[k]);
 
  120       accumulator(moves.size() - new_moves.size());
 
  121       if (new_moves.size() >= 256)
 
  122         std::cerr << 
"long record " << j << 
' ' << new_moves.size() << 
"\n";
 
  123       ks.save(&new_record);
 
  126         ipx_writer->save(new_record,
 
  130       if ((j % 1000) == 999)
 
  131         std::cerr << input_filename[i] << 
" " << j
 
  133                   << 
" mean " << acc::mean(accumulator) << 
"\n";
 
  135     std::cerr << input_filename[i]
 
  137               << 
" mean " << acc::mean(accumulator) << 
"\n";
 
  141 int main(
int argc, 
char **argv)
 
  143   bool output_ipx, trim;
 
  146   boost::program_options::options_description command_line_options;
 
  147   command_line_options.add_options()
 
  149      boost::program_options::value<bool>(&trim)->default_value(
true),
 
  150      "trim last checkmate sequence")
 
  152      boost::program_options::value<bool>(&output_ipx)->default_value(
true),
 
  153      "Whether output IPX file in addition to KIF file")
 
  154     (
"output-kisen-filename,o",
 
  155      boost::program_options::value<std::string>(&kisen_filename)->
 
  156      default_value(
"test.kif"),
 
  157      "Output filename of Kisen file")
 
  158     (
"checkmate-limit,l",
 
  159      boost::program_options::value<size_t>(&checkmate_limit)->default_value(1000),
 
  160      "Whether output IPX file in addition to KIF file")
 
  161     (
"input-file", boost::program_options::value< std::vector<std::string> >(),
 
  162      "input files in kisen format")
 
  163     (
"help", 
"Show help message");
 
  164   boost::program_options::variables_map 
vm;
 
  165   boost::program_options::positional_options_description p;
 
  166   p.add(
"input-file", -1);
 
  171       boost::program_options::command_line_parser(
 
  172         argc, argv).options(command_line_options).positional(p).
run(), vm);
 
  173     boost::program_options::notify(vm);
 
  174     if (vm.count(
"help"))
 
  176       std::cerr << 
"Usage: " << argv[0] << 
" [options] kisen-files\n";
 
  177       std::cerr << 
"       " << argv[0] << 
" [options]\n";
 
  178       std::cout << command_line_options << std::endl;
 
  182   catch (std::exception &e)
 
  184     std::cerr << 
"error in parsing options" << std::endl
 
  185               << e.what() << std::endl;
 
  186     std::cerr << 
"Usage: " << argv[0] << 
" [options] kisen-files\n";
 
  187     std::cerr << 
"       " << argv[0] << 
" [options]\n";
 
  188     std::cerr << command_line_options << std::endl;
 
  192   std::vector<std::string> 
files;
 
  193   if (vm.count(
"input-file"))
 
  194     files = vm[
"input-file"].as<std::vector<std::string> >();
 
  196   convert(files, kisen_filename, checkmate_limit, output_ipx, trim);