2008-05-14 12:14:13 +04:00
|
|
|
/*
|
|
|
|
* ScoreStats.h
|
2012-02-20 03:29:53 +04:00
|
|
|
* mert - Minimum Error Rate Training
|
2008-05-14 12:14:13 +04:00
|
|
|
*
|
|
|
|
* Created by Nicola Bertoldi on 13/05/08.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-02-20 04:46:08 +04:00
|
|
|
#ifndef MERT_SCORE_STATS_H_
|
|
|
|
#define MERT_SCORE_STATS_H_
|
2008-05-14 12:14:13 +04:00
|
|
|
|
|
|
|
#include <vector>
|
2012-05-10 02:51:05 +04:00
|
|
|
#include <iosfwd>
|
2008-10-17 01:14:38 +04:00
|
|
|
#include <cstdlib>
|
2011-11-14 10:15:30 +04:00
|
|
|
#include <cstring>
|
2008-05-14 12:14:13 +04:00
|
|
|
|
2011-11-14 10:15:30 +04:00
|
|
|
#include "Types.h"
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2008-05-14 12:14:13 +04:00
|
|
|
class ScoreStats
|
|
|
|
{
|
|
|
|
private:
|
2012-05-10 02:51:05 +04:00
|
|
|
std::size_t m_available_size;
|
|
|
|
std::size_t m_entries;
|
2011-11-12 17:04:22 +04:00
|
|
|
|
|
|
|
// TODO: Use smart pointer for exceptional-safety.
|
2012-03-10 12:12:34 +04:00
|
|
|
scorestats_t m_array;
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2008-05-14 12:14:13 +04:00
|
|
|
public:
|
2011-02-24 15:42:19 +03:00
|
|
|
ScoreStats();
|
2012-05-10 02:51:05 +04:00
|
|
|
explicit ScoreStats(const std::size_t size);
|
2012-02-26 08:44:47 +04:00
|
|
|
|
2011-11-12 04:51:27 +04:00
|
|
|
~ScoreStats();
|
|
|
|
|
|
|
|
// We intentionally allow copying.
|
2011-02-24 15:42:19 +03:00
|
|
|
ScoreStats(const ScoreStats &stats);
|
|
|
|
ScoreStats& operator=(const ScoreStats &stats);
|
|
|
|
|
2011-11-12 13:12:07 +04:00
|
|
|
void Copy(const ScoreStats &stats);
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
bool isfull() const {
|
|
|
|
return (m_entries < m_available_size) ? 0 : 1;
|
|
|
|
}
|
2011-11-12 06:26:13 +04:00
|
|
|
|
2011-02-24 15:42:19 +03:00
|
|
|
void expand();
|
|
|
|
void add(ScoreStatsType v);
|
|
|
|
|
2011-11-12 17:04:22 +04:00
|
|
|
void clear() {
|
2012-05-10 02:51:05 +04:00
|
|
|
std::memset((void*)m_array, 0, GetArraySizeWithBytes());
|
2011-02-24 15:42:19 +03:00
|
|
|
}
|
|
|
|
|
2011-11-12 17:04:22 +04:00
|
|
|
void reset() {
|
2012-03-10 12:12:34 +04:00
|
|
|
m_entries = 0;
|
2011-11-12 17:04:22 +04:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
ScoreStatsType get(std::size_t i) {
|
|
|
|
return m_array[i];
|
|
|
|
}
|
|
|
|
ScoreStatsType get(std::size_t i) const {
|
|
|
|
return m_array[i];
|
|
|
|
}
|
|
|
|
scorestats_t getArray() const {
|
|
|
|
return m_array;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-02-26 08:44:47 +04:00
|
|
|
void set(const std::string& str);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2012-02-25 20:41:17 +04:00
|
|
|
// Much more efficient than the above.
|
|
|
|
void set(const std::vector<ScoreStatsType>& stats) {
|
|
|
|
reset();
|
2012-05-10 02:51:05 +04:00
|
|
|
for (std::size_t i = 0; i < stats.size(); ++i) {
|
2012-02-25 20:41:17 +04:00
|
|
|
add(stats[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t bytes() const {
|
|
|
|
return GetArraySizeWithBytes();
|
|
|
|
}
|
2011-11-12 12:30:33 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
std::size_t GetArraySizeWithBytes() const {
|
2012-03-10 12:12:34 +04:00
|
|
|
return m_entries * sizeof(ScoreStatsType);
|
2011-11-12 12:30:33 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t size() const {
|
|
|
|
return m_entries;
|
|
|
|
}
|
2012-03-10 12:12:34 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
std::size_t available() const {
|
|
|
|
return m_available_size;
|
|
|
|
}
|
2011-02-24 15:42:19 +03:00
|
|
|
|
|
|
|
void savetxt(const std::string &file);
|
2012-05-10 02:51:05 +04:00
|
|
|
void savetxt(std::ostream* os);
|
|
|
|
void savebin(std::ostream* os);
|
2012-03-10 14:04:43 +04:00
|
|
|
void savetxt();
|
2011-02-24 15:42:19 +03:00
|
|
|
|
|
|
|
void loadtxt(const std::string &file);
|
2012-05-10 02:51:05 +04:00
|
|
|
void loadtxt(std::istream* is);
|
|
|
|
void loadbin(std::istream* is);
|
2011-02-24 15:42:19 +03:00
|
|
|
|
2011-11-12 03:58:23 +04:00
|
|
|
/**
|
|
|
|
* Write the whole object to a stream.
|
|
|
|
*/
|
2012-05-10 02:51:05 +04:00
|
|
|
friend std::ostream& operator<<(std::ostream& o, const ScoreStats& e);
|
2008-05-14 12:14:13 +04:00
|
|
|
};
|
|
|
|
|
2012-02-20 04:46:08 +04:00
|
|
|
bool operator==(const ScoreStats& s1, const ScoreStats& s2);
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|
2011-12-12 17:48:42 +04:00
|
|
|
|
2012-02-20 04:46:08 +04:00
|
|
|
#endif // MERT_SCORE_STATS_H_
|