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
|
/*
* FeatureStats.cpp
* met - Minimum Error Training
*
* Created by Nicola Bertoldi on 13/05/08.
*
*/
#include "Util.h"
#include "ScoreStats.h"
namespace {
const int kAvailableSize = 8;
} // namespace
ScoreStats::ScoreStats()
: available_(kAvailableSize), entries_(0),
array_(new ScoreStatsType[available_]) {}
ScoreStats::ScoreStats(const size_t size)
: available_(size), entries_(size),
array_(new ScoreStatsType[available_])
{
memset(array_, 0, GetArraySizeWithBytes());
}
ScoreStats::ScoreStats(std::string &theString)
: available_(0), entries_(0), array_(NULL)
{
set(theString);
}
ScoreStats::~ScoreStats()
{
if (array_) {
delete [] array_;
array_ = NULL;
}
}
void ScoreStats::Copy(const ScoreStats &stats)
{
available_ = stats.available();
entries_ = stats.size();
array_ = new ScoreStatsType[available_];
memcpy(array_, stats.getArray(), GetArraySizeWithBytes());
}
ScoreStats::ScoreStats(const ScoreStats &stats)
{
Copy(stats);
}
ScoreStats& ScoreStats::operator=(const ScoreStats &stats)
{
delete [] array_;
Copy(stats);
return *this;
}
void ScoreStats::expand()
{
available_ *= 2;
scorestats_t buf = new ScoreStatsType[available_];
memcpy(buf, array_, GetArraySizeWithBytes());
delete [] array_;
array_ = buf;
}
void ScoreStats::add(ScoreStatsType v)
{
if (isfull()) expand();
array_[entries_++]=v;
}
void ScoreStats::set(std::string &theString)
{
std::string substring, stringBuf;
reset();
while (!theString.empty()) {
getNextPound(theString, substring);
add(ConvertStringToScoreStatsType(substring));
}
}
void ScoreStats::loadbin(std::ifstream& inFile)
{
inFile.read((char*)array_, GetArraySizeWithBytes());
}
void ScoreStats::loadtxt(std::ifstream& inFile)
{
std::string theString;
std::getline(inFile, theString);
set(theString);
}
void ScoreStats::loadtxt(const std::string &file)
{
// TRACE_ERR("loading the stats from " << file << std::endl);
std::ifstream inFile(file.c_str(), std::ios::in); // matches a stream with a file. Opens the file
loadtxt(inFile);
}
void ScoreStats::savetxt(const std::string &file)
{
// TRACE_ERR("saving the stats into " << file << std::endl);
std::ofstream outFile(file.c_str(), std::ios::out); // matches a stream with a file. Opens the file
savetxt(outFile);
}
void ScoreStats::savetxt(std::ofstream& outFile)
{
outFile << *this;
}
void ScoreStats::savebin(std::ofstream& outFile)
{
outFile.write((char*)array_, GetArraySizeWithBytes());
}
ostream& operator<<(ostream& o, const ScoreStats& e)
{
for (size_t i=0; i< e.size(); i++)
o << e.get(i) << " ";
return o;
}
|