File: record.cc

package info (click to toggle)
libosl 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 273,976 kB
  • sloc: cpp: 129,625; ansic: 7,145; ruby: 1,290; makefile: 558; perl: 413; sh: 35
file content (320 lines) | stat: -rw-r--r-- 8,583 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "osl/record/record.h"
#include "osl/record/csaIOError.h"
#include "osl/record/kanjiCode.h"
#include "osl/misc/iconvConvert.h"
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#include <stack>
#include <iostream>

osl::record::
MoveRecord::MoveRecord(const Move& mv, int ni)
  : move(mv), nodeIndex(ni), time(0)
{
}

namespace osl
{
namespace record
{
  IRecordStream::~IRecordStream(){}
  
  ORecordStream::~ORecordStream(){}
  
  const Move MoveRecord::getMove() const { return move; }
  
  int MoveRecord::getNodeIndex() const { return nodeIndex; }
  
  void MoveRecord::setTime(int t){
    time=t;
  }
  
  void NodeRecord::addMoveRecord(int moveIndex){
    moves.push_back(moveIndex);
  }
  
  
  Record::Record(){ init(); }
  Record::Record(const SimpleState& initial, const vector<Move>& moves)
  {
    init();
    NumEffectState copy(initial);
    RecordVisitor visitor;
    visitor.setState(&copy);
    visitor.setRecord(this);
    BOOST_FOREACH(Move move, moves) {
      visitor.addMoveAndAdvance(move);
    }
  }  
  void Record::init(){
    version="";
    playerNames[0]=playerNames[1]="";
    nrs.clear();
    nrs.push_back(NodeRecord());
    initialState.init(HIRATE);
    result = UNKNOWN;
    start_date = boost::gregorian::date();
  }

  void Record::load(IRecordStream& irs){
    irs.load(this);
  }
  void Record::save(ORecordStream& /*irs*/){
    // irs.save(this);
  }
  
  void Record::setVersion(const std::string& str){
    version=str;
  }
  void Record::setPlayer(Player player,const std::string& str){
    playerNames[player]=str;
  }
  const std::string& Record::getPlayer(Player player) const{
    return playerNames[player];
  }
  void Record::setInitialState(const SimpleState& state){
    initialState=state;
    initialState.initPawnMask();
  }
  const NumEffectState Record::getInitialState() const {
    if (! initialState.isConsistent(true))
    {
      const char *msg = "Record: bad initial state";
      std::cerr << msg << " " << __FILE__ << " " << __LINE__ << "\n";
      throw CsaIOError(msg);
    }
    return NumEffectState(initialState);
  }
  int Record::addNodeRecord(){
    nrs.push_back(NodeRecord());
    return nrs.size()-1;
  }
  int Record::addMoveRecord(const MoveRecord& moveRecord){
    mrs.push_back(moveRecord);
    return mrs.size()-1;
  }
  NodeRecord* Record::nodeOf(int index){
    return &nrs.at(index);
  }
  const NodeRecord* Record::nodeOf(int index) const{
    return &nrs.at(index);
  }
  MoveRecord* Record::moveOf(int index){
    if (static_cast<size_t>(index) >= mrs.size())
      return NULL;
    else
      return &mrs.at(index);
  }
  const MoveRecord* Record::moveOf(int index) const {
    if (static_cast<size_t>(index) >= mrs.size())
      return NULL;
    else
      return &mrs.at(index);
  }
  NodeRecord& Record::operator[](int index){
    return nrs.at(index);
  }
  void Record::setDate(const std::string& date_str)
  {
    std::vector<std::string> values;
    boost::algorithm::split(values, date_str, boost::algorithm::is_any_of("/"));
    if (values.size() < 3) {
      std::cerr << "ERROR: Invalid date format found: "
#ifndef MINIMAL
		<< IconvConvert::eucToLang(date_str)
#endif
		<< "\n";
      return;
    } else if (values.size() > 3) {
      std::cerr << "WARNING: Invalid date format found: "
#ifndef MINIMAL
		<< IconvConvert::eucToLang(date_str)
#endif
		<< "\n";
      // go through
    }
    BOOST_FOREACH(std::string& value, values) {
      static const CArray<const char *,9> kanji = {{
	  K_R1, K_R2, K_R3, K_R4, K_R5, K_R6, K_R7, K_R8, K_R9, 
	}};
      for (size_t i=0; i<kanji.size(); ++i)
	boost::algorithm::replace_all(value, kanji[i], std::string(1, char('0'+i)));
    }
    int year  = 0;
    int month = 0;
    int day   = 0;
    try {
      year  = boost::lexical_cast<int>(values[0]);
      month = boost::lexical_cast<int>(values[1]);
      if (month == 0) month = 1;
      if ("??" == values[2]) {
        std::cerr << "WARNING: Invalid date format found: "
#ifndef MINIMAL
		  << IconvConvert::eucToLang(values[2])
#endif
		  << "\n";
        // go through
        day = 1;
      } else if (values[2].size() > 2) {
        std::cerr << "WARNING: Invalid date format found: "
#ifndef MINIMAL
		  << IconvConvert::eucToLang(values[2])
#endif
		  << "\n";
        // go through
        day = boost::lexical_cast<int>(values[2].substr(0,2));
      } else {
        day = boost::lexical_cast<int>(values[2]);
      }
      if (day == 0) day = 1;
      start_date = boost::gregorian::date(year, month, day);
      assert(!start_date.is_special());
    } catch (boost::bad_lexical_cast& e) {
      std::cerr << "Invalid date format found: "
#ifndef MINIMAL
		<< IconvConvert::eucToLang(date_str)
#endif
		<< "\n"
                << e.what() << std::endl;
    } catch (boost::gregorian::bad_day_of_month& ebdm) {
      std::cerr << "Bad day of month: "
#ifndef MINIMAL
		<< IconvConvert::eucToLang(date_str)
#endif
		<< "\n"
                << ebdm.what() << std::endl;
    }
    return;
  }
  void Record::setDate(const boost::gregorian::date& date)
  {
    start_date = date;
  }
  boost::gregorian::date Record::getDate() const
  {
    return start_date;
  }
  
  void RecordVisitor::addMoveAndAdvance(Move move){
    assert(state->isValidMove(move));

    int newNode=rec->addNodeRecord();
    int newMove=rec->addMoveRecord(MoveRecord(move,newNode));
    (*rec)[nodeIndex].addMoveRecord(newMove);
    nodeIndex=newNode;
    lastMoveIndex=newMove;

    assert(state->isConsistent() || ((std::cerr << move <<"\n"<< *state),0));
    NumEffectState copy(*state);
    copy.makeMove(move);	// XXX: RecordVisitor should have NumEffectState
    *state = copy;
    assert(state->isConsistent() || ((std::cerr << move <<"\n"<< *state),0));
    for(boost::ptr_vector<record::RecordVisitorObserver>::iterator each = observers.begin(); each != observers.end(); ++each){ 
      each->update(this); 
    }
  }
  
  
  std::ostream& operator<<(std::ostream& os,const MoveRecord & mr){
    return os << "MoveRecord(" << // mr.getMove() << ',' <<
      mr.getNodeIndex()  << ')';
  }
#ifndef MINIMAL  
  std::ostream& operator<<(std::ostream& os,Record & r){
    os << "Record(";
    os << "version=" << r.getVersion()
       << ",BLACK=" << r.getPlayer(BLACK)
       << ",WHITE=" << r.getPlayer(WHITE);
    os << ",initial=" << std:: endl << r.getInitialState() << std::endl;
    SimpleState initial_state=r.getInitialState();
    NumEffectState state(initial_state);
    RecordVisitor visitor;
    visitor.setState(&state);
    visitor.setRecord(&r);
    NodeRecord* node=visitor.getNode();
    while(node->size()>0){
      int moveIndex=node->at(0);
      MoveRecord* mr=r.moveOf(moveIndex);
      Move move=mr->getMove();
      os << move << "," << mr->getTime() << "," << mr->getComment() << std::endl;
      node=r.nodeOf(mr->getNodeIndex());
      state.makeMove(move);
      assert(state.isConsistent());
    }
    os << state;
    os << initial_state;
    return os << ')';
  }
#endif  
  
  const vector<Move> Record::getMoves() const {
    vector<Move> moves;
    vector<int> dummy_time;
    getMoves(moves, dummy_time);
    return moves;
  }

  int readInt(std::istream& is)
  {
    int ret=0;
    CArray<char,4> cs;
    is.read(&cs[0],4);
    for (int i=0;i<4;i++) {
      ret = (ret<<8)|(cs[i]&255);
    }
    return ret;
  }
  
  void
  writeInt(std::ostream& os, int n)
  {
    CArray<char,4> buf;
    for (int i = 0; i < 4; i++)
      {
	buf[i] = (n >> (8 * (4 - i - 1))) & 255;
      }
    os.write(&buf[0], 4);
  }

} // namespace record
} // namespace osl

void osl::record::
Record::getMoves(vector<Move>& moves, vector<int>& times,
		 vector<std::string>& comments,
		 vector<SearchInfo>& info) const 
{
  const NodeRecord* node=nodeOf(0);
  while(node->size()>0){
    const int moveIndex=node->at(0);
    const MoveRecord* mr=moveOf(moveIndex);
    const Move move=mr->getMove();
    moves.push_back(move);
    times.push_back(mr->getTime());
    comments.push_back(mr->getComment());
    info.push_back(mr->info);

    node=nodeOf(mr->getNodeIndex());
  }
}

void osl::record::
Record::getMoves(vector<Move>& moves, vector<int>& times) const 
{
  vector<std::string> dummy_comments;
  vector<SearchInfo> dummy_info;
  getMoves(moves, times, dummy_comments, dummy_info);
}

osl::record::
RecordVisitor::~RecordVisitor()
{
}

// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: