remove using std from cpp

This commit is contained in:
tqchen 2014-09-02 22:43:19 -07:00
parent 1dbcebb6fe
commit 10648a1ca7
19 changed files with 53 additions and 27 deletions

View File

@ -24,6 +24,7 @@ class GBLinear : public IGradBooster {
}
// set model parameters
virtual void SetParam(const char *name, const char *val) {
using namespace std;
if (!strncmp(name, "bst:", 4)) {
param.SetParam(name + 4, val);
}
@ -166,6 +167,7 @@ class GBLinear : public IGradBooster {
learning_rate = 1.0f;
}
inline void SetParam(const char *name, const char *val) {
using namespace std;
// sync-names
if (!strcmp("eta", name)) learning_rate = static_cast<float>(atof(val));
if (!strcmp("lambda", name)) reg_lambda = static_cast<float>(atof(val));
@ -207,9 +209,10 @@ class GBLinear : public IGradBooster {
Param(void) {
num_feature = 0;
num_output_group = 1;
memset(reserved, 0, sizeof(reserved));
std::memset(reserved, 0, sizeof(reserved));
}
inline void SetParam(const char *name, const char *val) {
using namespace std;
if (!strcmp(name, "bst:num_feature")) num_feature = atoi(val);
if (!strcmp(name, "num_output_group")) num_output_group = atoi(val);
}

View File

@ -1,7 +1,6 @@
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <cstring>
using namespace std;
#include "./gbm.h"
#include "./gbtree-inl.hpp"
#include "./gblinear-inl.hpp"
@ -9,6 +8,7 @@ using namespace std;
namespace xgboost {
namespace gbm {
IGradBooster* CreateGradBooster(const char *name) {
using namespace std;
if (!strcmp("gbtree", name)) return new GBTree();
if (!strcmp("gblinear", name)) return new GBLinear();
utils::Error("unknown booster type: %s", name);

View File

@ -23,6 +23,7 @@ class GBTree : public IGradBooster {
this->Clear();
}
virtual void SetParam(const char *name, const char *val) {
using namespace std;
if (!strncmp(name, "bst:", 4)) {
cfg.push_back(std::make_pair(std::string(name+4), std::string(val)));
// set into updaters, if already intialized
@ -171,14 +172,14 @@ class GBTree : public IGradBooster {
updaters.clear();
std::string tval = tparam.updater_seq;
char *pstr;
pstr = strtok(&tval[0], ",");
pstr = std::strtok(&tval[0], ",");
while (pstr != NULL) {
updaters.push_back(tree::CreateUpdater(pstr));
for (size_t j = 0; j < cfg.size(); ++j) {
// set parameters
updaters.back()->SetParam(cfg[j].first.c_str(), cfg[j].second.c_str());
}
pstr = strtok(NULL, ",");
pstr = std::strtok(NULL, ",");
}
tparam.updater_initialized = 1;
}
@ -279,6 +280,7 @@ class GBTree : public IGradBooster {
updater_initialized = 0;
}
inline void SetParam(const char *name, const char *val){
using namespace std;
if (!strcmp(name, "updater") &&
strcmp(updater_seq.c_str(), val) != 0) {
updater_seq = val;
@ -319,7 +321,7 @@ class GBTree : public IGradBooster {
num_pbuffer = 0;
num_output_group = 1;
size_leaf_vector = 0;
memset(reserved, 0, sizeof(reserved));
std::memset(reserved, 0, sizeof(reserved));
}
/*!
* \brief set parameters from outside
@ -327,6 +329,7 @@ class GBTree : public IGradBooster {
* \param val value of the parameter
*/
inline void SetParam(const char *name, const char *val) {
using namespace std;
if (!strcmp("num_pbuffer", name)) num_pbuffer = atol(val);
if (!strcmp("num_output_group", name)) num_output_group = atol(val);
if (!strcmp("bst:num_roots", name)) num_roots = atoi(val);

View File

@ -1,7 +1,6 @@
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <string>
using namespace std;
#include "./io.h"
#include "../utils/io.h"
#include "../utils/utils.h"

View File

@ -55,8 +55,8 @@ class DMatrixSimple : public DataMatrix {
RowBatch::Inst inst = batch[i];
row_data_.resize(row_data_.size() + inst.length);
if (inst.length != 0) {
memcpy(&row_data_[row_ptr_.back()], inst.data,
sizeof(RowBatch::Entry) * inst.length);
std::memcpy(&row_data_[row_ptr_.back()], inst.data,
sizeof(RowBatch::Entry) * inst.length);
}
row_ptr_.push_back(row_ptr_.back() + inst.length);
}
@ -82,6 +82,7 @@ class DMatrixSimple : public DataMatrix {
* \param silent whether print information or not
*/
inline void LoadText(const char* fname, bool silent = false) {
using namespace std;
this->Clear();
FILE* file = utils::FopenCheck(fname, "r");
float label; bool init = true;
@ -135,7 +136,7 @@ class DMatrixSimple : public DataMatrix {
* \return whether loading is success
*/
inline bool LoadBinary(const char* fname, bool silent = false) {
FILE *fp = fopen64(fname, "rb");
std::FILE *fp = fopen64(fname, "rb");
if (fp == NULL) return false;
utils::FileStream fs(fp);
this->LoadBinary(fs, silent, fname);
@ -208,6 +209,7 @@ class DMatrixSimple : public DataMatrix {
* \param savebuffer whether do save binary buffer if it is text
*/
inline void CacheLoad(const char *fname, bool silent = false, bool savebuffer = true) {
using namespace std;
size_t len = strlen(fname);
if (len > 8 && !strcmp(fname + len - 7, ".buffer")) {
if (!this->LoadBinary(fname, silent)) {

View File

@ -90,6 +90,7 @@ struct MetaInfo {
}
// try to load group information from file, if exists
inline bool TryLoadGroup(const char* fname, bool silent = false) {
using namespace std;
FILE *fi = fopen64(fname, "r");
if (fi == NULL) return false;
group_ptr.push_back(0);
@ -105,6 +106,7 @@ struct MetaInfo {
return true;
}
inline std::vector<float>& GetFloatInfo(const char *field) {
using namespace std;
if (!strcmp(field, "label")) return labels;
if (!strcmp(field, "weight")) return weights;
if (!strcmp(field, "base_margin")) return base_margin;
@ -115,6 +117,7 @@ struct MetaInfo {
return ((MetaInfo*)this)->GetFloatInfo(field);
}
inline std::vector<unsigned> &GetUIntInfo(const char *field) {
using namespace std;
if (!strcmp(field, "root_index")) return info.root_index;
if (!strcmp(field, "fold_index")) return info.fold_index;
utils::Error("unknown field %s", field);
@ -125,6 +128,7 @@ struct MetaInfo {
}
// try to load weight information from file, if exists
inline bool TryLoadFloatInfo(const char *field, const char* fname, bool silent = false) {
using namespace std;
std::vector<float> &data = this->GetFloatInfo(field);
FILE *fi = fopen64(fname, "r");
if (fi == NULL) return false;

View File

@ -147,10 +147,11 @@ struct EvalAMS : public IEvaluator {
explicit EvalAMS(const char *name) {
name_ = name;
// note: ams@0 will automatically select which ratio to go
utils::Check(sscanf(name, "ams@%f", &ratio_) == 1, "invalid ams format");
utils::Check(std::sscanf(name, "ams@%f", &ratio_) == 1, "invalid ams format");
}
virtual float Eval(const std::vector<float> &preds,
const MetaInfo &info) const {
using namespace std;
const bst_omp_uint ndata = static_cast<bst_omp_uint>(info.labels.size());
utils::Check(info.weights.size() == ndata, "we need weight to evaluate ams");
@ -202,6 +203,7 @@ struct EvalAMS : public IEvaluator {
struct EvalPrecisionRatio : public IEvaluator{
public:
explicit EvalPrecisionRatio(const char *name) : name_(name) {
using namespace std;
if (sscanf(name, "apratio@%f", &ratio_) == 1) {
use_ap = 1;
} else {
@ -342,6 +344,7 @@ struct EvalRankList : public IEvaluator {
protected:
explicit EvalRankList(const char *name) {
using namespace std;
name_ = name;
minus_ = false;
if (sscanf(name, "%*[^@]@%u[-]?", &topn_) != 1) {
@ -388,7 +391,7 @@ struct EvalNDCG : public EvalRankList{
for (size_t i = 0; i < rec.size() && i < this->topn_; ++i) {
const unsigned rel = rec[i].second;
if (rel != 0) {
sumdcg += ((1 << rel) - 1) / log(i + 2.0);
sumdcg += ((1 << rel) - 1) / std::log(i + 2.0);
}
}
return static_cast<float>(sumdcg);

View File

@ -36,6 +36,7 @@ struct IEvaluator{
namespace xgboost {
namespace learner {
inline IEvaluator* CreateEvaluator(const char *name) {
using namespace std;
if (!strcmp(name, "rmse")) return new EvalRMSE();
if (!strcmp(name, "error")) return new EvalError();
if (!strcmp(name, "merror")) return new EvalMatchError();
@ -56,6 +57,7 @@ inline IEvaluator* CreateEvaluator(const char *name) {
class EvalSet{
public:
inline void AddEval(const char *name) {
using namespace std;
for (size_t i = 0; i < evals_.size(); ++i) {
if (!strcmp(name, evals_[i]->Name())) return;
}

View File

@ -79,6 +79,7 @@ class BoostLearner {
* \param val value of the parameter
*/
inline void SetParam(const char *name, const char *val) {
using namespace std;
// in this version, bst: prefix is no longer required
if (strncmp(name, "bst:", 4) != 0) {
std::string n = "bst:"; n += name;
@ -290,7 +291,7 @@ class BoostLearner {
base_score = 0.5f;
num_feature = 0;
num_class = 0;
memset(reserved, 0, sizeof(reserved));
std::memset(reserved, 0, sizeof(reserved));
}
/*!
* \brief set parameters from outside
@ -298,6 +299,7 @@ class BoostLearner {
* \param val value of the parameter
*/
inline void SetParam(const char *name, const char *val) {
using namespace std;
if (!strcmp("base_score", name)) base_score = static_cast<float>(atof(val));
if (!strcmp("num_class", name)) num_class = atoi(val);
if (!strcmp("bst:num_feature", name)) num_feature = atoi(val);

View File

@ -101,6 +101,7 @@ class RegLossObj : public IObjFunction{
}
virtual ~RegLossObj(void) {}
virtual void SetParam(const char *name, const char *val) {
using namespace std;
if (!strcmp("scale_pos_weight", name)) {
scale_pos_weight = static_cast<float>(atof(val));
}
@ -156,6 +157,7 @@ class SoftmaxMultiClassObj : public IObjFunction {
}
virtual ~SoftmaxMultiClassObj(void) {}
virtual void SetParam(const char *name, const char *val) {
using namespace std;
if (!strcmp( "num_class", name )) nclass = atoi(val);
}
virtual void GetGradient(const std::vector<float> &preds,
@ -247,6 +249,7 @@ class LambdaRankObj : public IObjFunction {
}
virtual ~LambdaRankObj(void) {}
virtual void SetParam(const char *name, const char *val) {
using namespace std;
if (!strcmp( "loss_type", name )) loss.loss_type = atoi(val);
if (!strcmp( "fix_list_weight", name)) fix_list_weight = static_cast<float>(atof(val));
if (!strcmp( "num_pairsample", name)) num_pairsample = atoi(val);

View File

@ -67,6 +67,7 @@ namespace xgboost {
namespace learner {
/*! \brief factory funciton to create objective function by name */
inline IObjFunction* CreateObjFunction(const char *name) {
using namespace std;
if (!strcmp("reg:linear", name)) return new RegLossObj(LossType::kLinearSquare);
if (!strcmp("reg:logistic", name)) return new RegLossObj(LossType::kLogisticNeglik);
if (!strcmp("binary:logistic", name)) return new RegLossObj(LossType::kLogisticClassify);

View File

@ -53,7 +53,7 @@ class TreeModel {
Param(void) {
max_depth = 0;
size_leaf_vector = 0;
memset(reserved, 0, sizeof(reserved));
std::memset(reserved, 0, sizeof(reserved));
}
/*!
* \brief set parameters from outside
@ -61,6 +61,7 @@ class TreeModel {
* \param val value of the parameter
*/
inline void SetParam(const char *name, const char *val) {
using namespace std;
if (!strcmp("num_roots", name)) num_roots = atoi(val);
if (!strcmp("num_feature", name)) num_feature = atoi(val);
if (!strcmp("size_leaf_vector", name)) size_leaf_vector = atoi(val);

View File

@ -62,6 +62,7 @@ struct TrainParam{
* \param val value of the parameter
*/
inline void SetParam(const char *name, const char *val) {
using namespace std;
// sync-names
if (!strcmp(name, "gamma")) min_split_loss = static_cast<float>(atof(val));
if (!strcmp(name, "eta")) learning_rate = static_cast<float>(atof(val));

View File

@ -1,7 +1,6 @@
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <cstring>
using namespace std;
#include "./updater.h"
#include "./updater_prune-inl.hpp"
#include "./updater_refresh-inl.hpp"
@ -10,6 +9,7 @@ using namespace std;
namespace xgboost {
namespace tree {
IUpdater* CreateUpdater(const char *name) {
using namespace std;
if (!strcmp(name, "prune")) return new TreePruner();
if (!strcmp(name, "refresh")) return new TreeRefresher<GradStats>();
if (!strcmp(name, "grow_colmaker")) return new ColMaker<GradStats>();

View File

@ -17,6 +17,7 @@ class TreePruner: public IUpdater {
virtual ~TreePruner(void) {}
// set training parameter
virtual void SetParam(const char *name, const char *val) {
using namespace std;
param.SetParam(name, val);
if (!strcmp(name, "silent")) silent = atoi(val);
}

View File

@ -24,15 +24,15 @@ class FeatMap {
// function definitions
/*! \brief load feature map from text format */
inline void LoadText(const char *fname) {
FILE *fi = utils::FopenCheck(fname, "r");
std::FILE *fi = utils::FopenCheck(fname, "r");
this->LoadText(fi);
fclose(fi);
std::fclose(fi);
}
/*! \brief load feature map from text format */
inline void LoadText(FILE *fi) {
inline void LoadText(std::FILE *fi) {
int fid;
char fname[1256], ftype[1256];
while (fscanf(fi, "%d\t%[^\t]\t%s\n", &fid, fname, ftype) == 3) {
while (std::fscanf(fi, "%d\t%[^\t]\t%s\n", &fid, fname, ftype) == 3) {
this->PushBack(fid, fname, ftype);
}
}
@ -62,6 +62,7 @@ class FeatMap {
private:
inline static Type GetType(const char *tname) {
using namespace std;
if (!strcmp("i", tname)) return kIndicator;
if (!strcmp("q", tname)) return kQuantitive;
if (!strcmp("int", tname)) return kInteger;

View File

@ -91,21 +91,21 @@ class IStream {
/*! \brief implementation of file i/o stream */
class FileStream : public IStream {
private:
FILE *fp;
std::FILE *fp;
public:
explicit FileStream(FILE *fp) : fp(fp) {
explicit FileStream(std::FILE *fp) : fp(fp) {
}
virtual size_t Read(void *ptr, size_t size) {
return fread(ptr, size, 1, fp);
return std::fread(ptr, size, 1, fp);
}
virtual void Write(const void *ptr, size_t size) {
fwrite(ptr, size, 1, fp);
std::fwrite(ptr, size, 1, fp);
}
inline void Seek(size_t pos) {
fseek(fp, 0, SEEK_SET);
std::fseek(fp, 0, SEEK_SET);
}
inline void Close(void) {
fclose(fp);
std::fclose(fp);
}
};

View File

@ -53,7 +53,7 @@ inline double NextDouble(void) {
}
/*! \brief return a random number in n */
inline uint32_t NextUInt32(uint32_t n) {
return (uint32_t)floor(NextDouble() * n);
return (uint32_t)std::floor(NextDouble() * n);
}
/*! \brief return x~N(mu,sigma^2) */
inline double SampleNormal(double mu, double sigma) {

View File

@ -149,8 +149,8 @@ inline void Error(const char *fmt, ...) {
#endif
/*! \brief replace fopen, report error when the file open fails */
inline FILE *FopenCheck(const char *fname, const char *flag) {
FILE *fp = fopen64(fname, flag);
inline std::FILE *FopenCheck(const char *fname, const char *flag) {
std::FILE *fp = fopen64(fname, flag);
Check(fp != NULL, "can not open file \"%s\"\n", fname);
return fp;
}