2008-05-14 12:14:13 +04:00
|
|
|
/*
|
|
|
|
* FeatureStats.cpp
|
|
|
|
* met - Minimum Error Training
|
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-11-14 10:15:30 +04:00
|
|
|
#include "Util.h"
|
2008-05-14 12:14:13 +04:00
|
|
|
#include "ScoreStats.h"
|
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
namespace {
|
|
|
|
const int kAvailableSize = 8;
|
|
|
|
} // namespace
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2008-05-16 00:32:37 +04:00
|
|
|
ScoreStats::ScoreStats()
|
2011-11-12 13:12:07 +04:00
|
|
|
: available_(kAvailableSize), entries_(0),
|
|
|
|
array_(new ScoreStatsType[available_]) {}
|
|
|
|
|
|
|
|
ScoreStats::ScoreStats(const size_t size)
|
|
|
|
: available_(size), entries_(size),
|
|
|
|
array_(new ScoreStatsType[available_])
|
2008-06-05 21:03:54 +04:00
|
|
|
{
|
2011-11-12 13:12:07 +04:00
|
|
|
memset(array_, 0, GetArraySizeWithBytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
ScoreStats::ScoreStats(std::string &theString)
|
|
|
|
: available_(0), entries_(0), array_(NULL)
|
|
|
|
{
|
|
|
|
set(theString);
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
ScoreStats::~ScoreStats()
|
|
|
|
{
|
2011-11-12 17:04:22 +04:00
|
|
|
if (array_) {
|
2011-11-12 13:12:07 +04:00
|
|
|
delete [] array_;
|
2011-11-12 17:04:22 +04:00
|
|
|
array_ = NULL;
|
|
|
|
}
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
void ScoreStats::Copy(const ScoreStats &stats)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
available_ = stats.available();
|
|
|
|
entries_ = stats.size();
|
|
|
|
array_ = new ScoreStatsType[available_];
|
2011-11-12 12:30:33 +04:00
|
|
|
memcpy(array_, stats.getArray(), GetArraySizeWithBytes());
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
ScoreStats::ScoreStats(const ScoreStats &stats)
|
2008-06-05 21:03:54 +04:00
|
|
|
{
|
2011-11-12 13:12:07 +04:00
|
|
|
Copy(stats);
|
2011-11-12 04:40:01 +04:00
|
|
|
}
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
ScoreStats& ScoreStats::operator=(const ScoreStats &stats)
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2011-11-12 13:12:07 +04:00
|
|
|
delete [] array_;
|
|
|
|
Copy(stats);
|
|
|
|
return *this;
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::expand()
|
2008-05-14 12:14:13 +04:00
|
|
|
{
|
2011-11-12 12:30:33 +04:00
|
|
|
available_ *= 2;
|
2011-11-12 13:12:07 +04:00
|
|
|
scorestats_t buf = new ScoreStatsType[available_];
|
|
|
|
memcpy(buf, array_, GetArraySizeWithBytes());
|
2011-11-11 14:11:10 +04:00
|
|
|
delete [] array_;
|
2011-11-12 13:12:07 +04:00
|
|
|
array_ = buf;
|
2008-06-05 21:03:54 +04:00
|
|
|
}
|
2008-05-23 15:48:16 +04:00
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::add(ScoreStatsType v)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
if (isfull()) expand();
|
|
|
|
array_[entries_++]=v;
|
2008-06-05 21:03:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScoreStats::set(std::string &theString)
|
|
|
|
{
|
|
|
|
std::string substring, stringBuf;
|
2011-02-24 15:42:19 +03:00
|
|
|
reset();
|
|
|
|
|
|
|
|
while (!theString.empty()) {
|
|
|
|
getNextPound(theString, substring);
|
2011-11-12 12:30:33 +04:00
|
|
|
add(ConvertStringToScoreStatsType(substring));
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::loadbin(std::ifstream& inFile)
|
|
|
|
{
|
2011-11-12 12:30:33 +04:00
|
|
|
inFile.read((char*)array_, GetArraySizeWithBytes());
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2008-05-14 12:14:13 +04:00
|
|
|
void ScoreStats::loadtxt(std::ifstream& inFile)
|
|
|
|
{
|
2008-05-27 20:50:52 +04:00
|
|
|
std::string theString;
|
2011-02-24 15:42:19 +03:00
|
|
|
std::getline(inFile, theString);
|
|
|
|
set(theString);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScoreStats::loadtxt(const std::string &file)
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("loading the stats from " << file << std::endl);
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
std::ifstream inFile(file.c_str(), std::ios::in); // matches a stream with a file. Opens the file
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
loadtxt(inFile);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ScoreStats::savetxt(const std::string &file)
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
// TRACE_ERR("saving the stats into " << file << std::endl);
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
std::ofstream outFile(file.c_str(), std::ios::out); // matches a stream with a file. Opens the file
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
savetxt(outFile);
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ScoreStats::savetxt(std::ofstream& outFile)
|
|
|
|
{
|
2011-02-24 15:42:19 +03:00
|
|
|
outFile << *this;
|
2008-05-14 12:14:13 +04:00
|
|
|
}
|
|
|
|
|
2008-06-05 21:03:54 +04:00
|
|
|
void ScoreStats::savebin(std::ofstream& outFile)
|
|
|
|
{
|
2011-11-12 12:30:33 +04:00
|
|
|
outFile.write((char*)array_, GetArraySizeWithBytes());
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
2008-06-05 21:03:54 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
ostream& operator<<(ostream& o, const ScoreStats& e)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i< e.size(); i++)
|
|
|
|
o << e.get(i) << " ";
|
|
|
|
return o;
|
2008-05-27 20:50:52 +04:00
|
|
|
}
|