Use passing objects by const references not passing by their values.

This commit is contained in:
Tetsuo Kiso 2011-11-14 14:00:47 +09:00
parent 0c7a38d9d2
commit be1506e759
10 changed files with 40 additions and 44 deletions

View File

@ -267,16 +267,16 @@ void Data::outputSample( ostream &out, const FeatureStats &f1, const FeatureStat
void Data::createShards(size_t shard_count, float shard_size, const string& scorerconfig,
std::vector<Data>& shards)
std::vector<Data>& shards)
{
assert(shard_count);
assert(shard_size >=0);
assert(shard_size >= 0);
assert(shard_size <= 1);
size_t data_size = scoredata->size();
assert(data_size == featdata->size());
shard_size *= data_size;
shard_size *= data_size;
for (size_t shard_id = 0; shard_id < shard_count; ++shard_id) {
vector<size_t> shard_contents;

View File

@ -65,7 +65,7 @@ public:
inline std::string Features() const {
return featdata->Features();
}
inline void Features(const std::string f) {
inline void Features(const std::string &f) {
featdata->Features(f);
}

View File

@ -47,17 +47,17 @@ public:
inline std::string getIndex() const {
return idx;
}
inline void setIndex(const std::string & value) {
idx=value;
inline void setIndex(const std::string& value) {
idx = value;
}
inline FeatureStats& get(size_t i) {
inline FeatureStats& get(size_t i) {
return array_.at(i);
}
inline const FeatureStats& get(size_t i)const {
inline const FeatureStats& get(size_t i)const {
return array_.at(i);
}
void add(FeatureStats e) {
void add(FeatureStats& e) {
array_.push_back(e);
}
@ -75,7 +75,7 @@ public:
inline std::string Features() const {
return features;
}
inline void Features(const std::string f) {
inline void Features(const std::string& f) {
features = f;
}

View File

@ -91,7 +91,7 @@ void FeatureData::add(FeatureArray& e)
}
}
void FeatureData::add(FeatureStats& e, const std::string & sent_idx)
void FeatureData::add(FeatureStats& e, const std::string& sent_idx)
{
if (exists(sent_idx)) { // array at position e.getIndex() already exists
//enlarge array at position e.getIndex()
@ -130,19 +130,18 @@ void FeatureData::setIndex()
}
}
void FeatureData::setFeatureMap(const std::string feat)
void FeatureData::setFeatureMap(const std::string& feat)
{
number_of_features = 0;
features=feat;
features = feat;
std::string substring, stringBuf;
stringBuf=features;
stringBuf = features;
while (!stringBuf.empty()) {
getNextPound(stringBuf, substring);
featname2idx_[substring]=idx2featname_.size();
idx2featname_[idx2featname_.size()]=substring;
featname2idx_[substring] = idx2featname_.size();
idx2featname_[idx2featname_.size()] = substring;
number_of_features++;
}
}

View File

@ -54,7 +54,7 @@ public:
return array_.at(idx);
}
inline bool exists(const std::string & sent_idx) {
inline bool exists(const std::string& sent_idx) {
return exists(getIndex(sent_idx));
}
inline bool exists(int sent_idx) {
@ -83,7 +83,7 @@ public:
inline std::string Features() const {
return features;
}
inline void Features(const std::string f) {
inline void Features(const std::string& f) {
features = f;
}
@ -136,7 +136,7 @@ public:
return it->second;
}
void setFeatureMap(const std::string feat);
void setFeatureMap(const std::string& feat);
};
#endif // FEATURE_DATA_H

View File

@ -17,7 +17,7 @@ const int kAvailableSize = 8;
SparseVector::name2id_t SparseVector::name2id_;
SparseVector::id2name_t SparseVector::id2name_;
FeatureStatsType SparseVector::get(string name) const {
FeatureStatsType SparseVector::get(const string& name) const {
name2id_t::const_iterator name2id_iter = name2id_.find(name);
if (name2id_iter == name2id_.end()) return 0;
size_t id = name2id_iter->second;
@ -30,7 +30,7 @@ FeatureStatsType SparseVector::get(size_t id) const {
return fvector_iter->second;
}
void SparseVector::set(string name, FeatureStatsType value) {
void SparseVector::set(const string& name, FeatureStatsType value) {
name2id_t::const_iterator name2id_iter = name2id_.find(name);
size_t id = 0;
if (name2id_iter == name2id_.end()) {
@ -55,10 +55,6 @@ void SparseVector::clear() {
fvector_.clear();
}
size_t SparseVector::size() const {
return fvector_.size();
}
SparseVector& SparseVector::operator-=(const SparseVector& rhs) {
//All the elements that have values in *this
for (fvector_t::iterator i = fvector_.begin(); i != fvector_.end(); ++i) {
@ -142,7 +138,7 @@ void FeatureStats::add(FeatureStatsType v)
array_[entries_++]=v;
}
void FeatureStats::addSparse(string name, FeatureStatsType v)
void FeatureStats::addSparse(const string& name, FeatureStatsType v)
{
map_.set(name,v);
}

View File

@ -24,11 +24,13 @@ public:
typedef std::map<std::string, size_t> name2id_t;
typedef std::vector<std::string> id2name_t;
FeatureStatsType get(std::string name) const;
FeatureStatsType get(const std::string& name) const;
FeatureStatsType get(size_t id) const;
void set(std::string name, FeatureStatsType value);
void set(const std::string& name, FeatureStatsType value);
void clear();
size_t size() const;
size_t size() const {
return fvector_.size();
}
void write(std::ostream& out, const std::string& sep = " ") const;
@ -70,7 +72,7 @@ public:
}
void expand();
void add(FeatureStatsType v);
void addSparse(string name, FeatureStatsType v);
void addSparse(const string& name, FeatureStatsType v);
void clear() {
memset((void*)array_, 0, GetArraySizeWithBytes());

View File

@ -118,7 +118,8 @@ private:
enum OptType {
POWELL = 0,
RANDOM_DIRECTION = 1,
RANDOM,NOPTIMIZER
RANDOM,
NOPTIMIZER
};
static OptType GetOType(string);

View File

@ -53,7 +53,7 @@ public:
return array_.at(idx);
}
inline bool exists(const std::string & sent_idx) const {
inline bool exists(const std::string& sent_idx) const {
return exists(getIndex(sent_idx));
}
inline bool exists(int sent_idx) const {
@ -71,7 +71,7 @@ public:
return score_type;
}
inline std::string name(std::string &sctype) {
inline std::string name(const std::string &sctype) {
return score_type = sctype;
}

View File

@ -16,7 +16,7 @@ using namespace std;
// global variables
Timer g_timer;
int verbose=0;
int verbose = 0;
namespace {
@ -34,7 +34,7 @@ int verboselevel()
int setverboselevel(int v)
{
verbose=v;
verbose = v;
return verbose;
}
@ -74,18 +74,16 @@ void Tokenize(const char *str, const char delim,
}
}
int swapbytes(char *p, int sz, int n)
{
char c, *l, *h;
if((n<1) || (sz<2)) return 0;
for (; n--; p+=sz) {
for(h=(l=p)+sz; --h>l; l++) {
c=*h;
*h=*l;
*l=c;
if((n < 1) || (sz < 2)) return 0;
for (; n--; p += sz) {
for (h = (l = p) + sz; --h > l; l++) {
c = *h;
*h = *l;
*l = c;
}
}
return 0;