fix all cpp lint

This commit is contained in:
tqchen 2015-07-03 19:42:44 -07:00
parent 1123253f79
commit 57ec922214
5 changed files with 302 additions and 288 deletions

View File

@ -1,3 +1,4 @@
// Copyright (c) 2014 by Contributors
#include <vector> #include <vector>
#include <string> #include <string>
#include <utility> #include <utility>
@ -196,7 +197,7 @@ extern "C" {
_WrapperBegin(); _WrapperBegin();
int len = length(dmats); int len = length(dmats);
std::vector<void*> dvec; std::vector<void*> dvec;
for (int i = 0; i < len; ++i){ for (int i = 0; i < len; ++i) {
dvec.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i))); dvec.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i)));
} }
void *handle = XGBoosterCreate(BeginPtr(dvec), dvec.size()); void *handle = XGBoosterCreate(BeginPtr(dvec), dvec.size());
@ -313,7 +314,7 @@ extern "C" {
SEXP out = PROTECT(allocVector(STRSXP, olen)); SEXP out = PROTECT(allocVector(STRSXP, olen));
for (size_t i = 0; i < olen; ++i) { for (size_t i = 0; i < olen; ++i) {
stringstream stream; stringstream stream;
stream << "booster["<<i<<"]\n" << res[i]; stream << "booster[" << i <<"]\n" << res[i];
SET_STRING_ELT(out, i, mkChar(stream.str().c_str())); SET_STRING_ELT(out, i, mkChar(stream.str().c_str()));
} }
UNPROTECT(1); UNPROTECT(1);

View File

@ -1,10 +1,12 @@
#ifndef XGBOOST_WRAPPER_R_H_
#define XGBOOST_WRAPPER_R_H_
/*! /*!
* Copyright 2014 (c) by Contributors
* \file xgboost_wrapper_R.h * \file xgboost_wrapper_R.h
* \author Tianqi Chen * \author Tianqi Chen
* \brief R wrapper of xgboost * \brief R wrapper of xgboost
*/ */
#ifndef XGBOOST_WRAPPER_R_H_ // NOLINT(*)
#define XGBOOST_WRAPPER_R_H_ // NOLINT(*)
extern "C" { extern "C" {
#include <Rinternals.h> #include <Rinternals.h>
#include <R_ext/Random.h> #include <R_ext/Random.h>
@ -153,4 +155,4 @@ extern "C" {
*/ */
SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats); SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats);
} }
#endif // XGBOOST_WRAPPER_R_H_ #endif // XGBOOST_WRAPPER_R_H_ // NOLINT(*)

View File

@ -1,3 +1,4 @@
// Copyright (c) 2014 by Contributors
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <Rinternals.h> #include <Rinternals.h>

View File

@ -1,3 +1,4 @@
// Copyright (c) 2014 by Contributors
// implementations in ctypes // implementations in ctypes
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE #define _CRT_SECURE_NO_DEPRECATE
@ -31,9 +32,11 @@ class Booster: public learner::BoostLearner {
this->init_model = false; this->init_model = false;
this->SetCacheData(mats); this->SetCacheData(mats);
} }
inline const float *Pred(const DataMatrix &dmat, int option_mask, unsigned ntree_limit, bst_ulong *len) { inline const float *Pred(const DataMatrix &dmat, int option_mask,
unsigned ntree_limit, bst_ulong *len) {
this->CheckInitModel(); this->CheckInitModel();
this->Predict(dmat, (option_mask&1) != 0, &this->preds_, ntree_limit, (option_mask&2) != 0); this->Predict(dmat, (option_mask&1) != 0, &this->preds_,
ntree_limit, (option_mask&2) != 0);
*len = static_cast<bst_ulong>(this->preds_.size()); *len = static_cast<bst_ulong>(this->preds_.size());
return BeginPtr(this->preds_); return BeginPtr(this->preds_);
} }
@ -57,7 +60,7 @@ class Booster: public learner::BoostLearner {
this->init_model = true; this->init_model = true;
} }
inline void LoadModelFromBuffer(const void *buf, size_t size) { inline void LoadModelFromBuffer(const void *buf, size_t size) {
utils::MemoryFixSizeBuffer fs((void*)buf, size); utils::MemoryFixSizeBuffer fs((void*)buf, size); // NOLINT(*)
learner::BoostLearner::LoadModel(fs, true); learner::BoostLearner::LoadModel(fs, true);
this->init_model = true; this->init_model = true;
} }
@ -99,11 +102,10 @@ class Booster: public learner::BoostLearner {
using namespace xgboost::wrapper; using namespace xgboost::wrapper;
extern "C"{ void* XGDMatrixCreateFromFile(const char *fname, int silent) {
void* XGDMatrixCreateFromFile(const char *fname, int silent) {
return LoadDataMatrix(fname, silent != 0, false, false); return LoadDataMatrix(fname, silent != 0, false, false);
} }
void* XGDMatrixCreateFromCSR(const bst_ulong *indptr, void* XGDMatrixCreateFromCSR(const bst_ulong *indptr,
const unsigned *indices, const unsigned *indices,
const float *data, const float *data,
bst_ulong nindptr, bst_ulong nindptr,
@ -122,8 +124,8 @@ extern "C"{
} }
mat.info.info.num_row = nindptr - 1; mat.info.info.num_row = nindptr - 1;
return p_mat; return p_mat;
} }
XGB_DLL void* XGDMatrixCreateFromCSC(const bst_ulong *col_ptr, void* XGDMatrixCreateFromCSC(const bst_ulong *col_ptr,
const unsigned *indices, const unsigned *indices,
const float *data, const float *data,
bst_ulong nindptr, bst_ulong nindptr,
@ -138,9 +140,9 @@ extern "C"{
DMatrixSimple &mat = *p_mat; DMatrixSimple &mat = *p_mat;
utils::ParallelGroupBuilder<RowBatch::Entry> builder(&mat.row_ptr_, &mat.row_data_); utils::ParallelGroupBuilder<RowBatch::Entry> builder(&mat.row_ptr_, &mat.row_data_);
builder.InitBudget(0, nthread); builder.InitBudget(0, nthread);
long ncol = static_cast<long>(nindptr - 1); long ncol = static_cast<long>(nindptr - 1); // NOLINT(*)
#pragma omp parallel for schedule(static) #pragma omp parallel for schedule(static)
for (long i = 0; i < ncol; ++i) { for (long i = 0; i < ncol; ++i) { // NOLINT(*)
int tid = omp_get_thread_num(); int tid = omp_get_thread_num();
for (unsigned j = col_ptr[i]; j < col_ptr[i+1]; ++j) { for (unsigned j = col_ptr[i]; j < col_ptr[i+1]; ++j) {
builder.AddBudget(indices[j], tid); builder.AddBudget(indices[j], tid);
@ -148,7 +150,7 @@ extern "C"{
} }
builder.InitStorage(); builder.InitStorage();
#pragma omp parallel for schedule(static) #pragma omp parallel for schedule(static)
for (long i = 0; i < ncol; ++i) { for (long i = 0; i < ncol; ++i) { // NOLINT(*)
int tid = omp_get_thread_num(); int tid = omp_get_thread_num();
for (unsigned j = col_ptr[i]; j < col_ptr[i+1]; ++j) { for (unsigned j = col_ptr[i]; j < col_ptr[i+1]; ++j) {
builder.Push(indices[j], builder.Push(indices[j],
@ -159,8 +161,8 @@ extern "C"{
mat.info.info.num_row = mat.row_ptr_.size() - 1; mat.info.info.num_row = mat.row_ptr_.size() - 1;
mat.info.info.num_col = static_cast<size_t>(ncol); mat.info.info.num_col = static_cast<size_t>(ncol);
return p_mat; return p_mat;
} }
void* XGDMatrixCreateFromMat(const float *data, void* XGDMatrixCreateFromMat(const float *data,
bst_ulong nrow, bst_ulong nrow,
bst_ulong ncol, bst_ulong ncol,
float missing) { float missing) {
@ -185,8 +187,8 @@ extern "C"{
mat.row_ptr_.push_back(mat.row_ptr_.back() + nelem); mat.row_ptr_.push_back(mat.row_ptr_.back() + nelem);
} }
return p_mat; return p_mat;
} }
void* XGDMatrixSliceDMatrix(void *handle, void* XGDMatrixSliceDMatrix(void *handle,
const int *idxset, const int *idxset,
bst_ulong len) { bst_ulong len) {
DMatrixSimple tmp; DMatrixSimple tmp;
@ -231,80 +233,80 @@ extern "C"{
} }
} }
return p_ret; return p_ret;
} }
void XGDMatrixFree(void *handle) { void XGDMatrixFree(void *handle) {
delete static_cast<DataMatrix*>(handle); delete static_cast<DataMatrix*>(handle);
} }
void XGDMatrixSaveBinary(void *handle, const char *fname, int silent) { void XGDMatrixSaveBinary(void *handle, const char *fname, int silent) {
SaveDataMatrix(*static_cast<DataMatrix*>(handle), fname, silent != 0); SaveDataMatrix(*static_cast<DataMatrix*>(handle), fname, silent != 0);
} }
void XGDMatrixSetFloatInfo(void *handle, const char *field, const float *info, bst_ulong len) { void XGDMatrixSetFloatInfo(void *handle, const char *field, const float *info, bst_ulong len) {
std::vector<float> &vec = std::vector<float> &vec =
static_cast<DataMatrix*>(handle)->info.GetFloatInfo(field); static_cast<DataMatrix*>(handle)->info.GetFloatInfo(field);
vec.resize(len); vec.resize(len);
memcpy(BeginPtr(vec), info, sizeof(float) * len); memcpy(BeginPtr(vec), info, sizeof(float) * len);
} }
void XGDMatrixSetUIntInfo(void *handle, const char *field, const unsigned *info, bst_ulong len) { void XGDMatrixSetUIntInfo(void *handle, const char *field, const unsigned *info, bst_ulong len) {
std::vector<unsigned> &vec = std::vector<unsigned> &vec =
static_cast<DataMatrix*>(handle)->info.GetUIntInfo(field); static_cast<DataMatrix*>(handle)->info.GetUIntInfo(field);
vec.resize(len); vec.resize(len);
memcpy(BeginPtr(vec), info, sizeof(unsigned) * len); memcpy(BeginPtr(vec), info, sizeof(unsigned) * len);
} }
void XGDMatrixSetGroup(void *handle, const unsigned *group, bst_ulong len) { void XGDMatrixSetGroup(void *handle, const unsigned *group, bst_ulong len) {
DataMatrix *pmat = static_cast<DataMatrix*>(handle); DataMatrix *pmat = static_cast<DataMatrix*>(handle);
pmat->info.group_ptr.resize(len + 1); pmat->info.group_ptr.resize(len + 1);
pmat->info.group_ptr[0] = 0; pmat->info.group_ptr[0] = 0;
for (uint64_t i = 0; i < len; ++i) { for (uint64_t i = 0; i < len; ++i) {
pmat->info.group_ptr[i+1] = pmat->info.group_ptr[i] + group[i]; pmat->info.group_ptr[i+1] = pmat->info.group_ptr[i] + group[i];
} }
} }
const float* XGDMatrixGetFloatInfo(const void *handle, const char *field, bst_ulong* len) { const float* XGDMatrixGetFloatInfo(const void *handle, const char *field, bst_ulong* len) {
const std::vector<float> &vec = const std::vector<float> &vec =
static_cast<const DataMatrix*>(handle)->info.GetFloatInfo(field); static_cast<const DataMatrix*>(handle)->info.GetFloatInfo(field);
*len = static_cast<bst_ulong>(vec.size()); *len = static_cast<bst_ulong>(vec.size());
return BeginPtr(vec); return BeginPtr(vec);
} }
const unsigned* XGDMatrixGetUIntInfo(const void *handle, const char *field, bst_ulong* len) { const unsigned* XGDMatrixGetUIntInfo(const void *handle, const char *field, bst_ulong* len) {
const std::vector<unsigned> &vec = const std::vector<unsigned> &vec =
static_cast<const DataMatrix*>(handle)->info.GetUIntInfo(field); static_cast<const DataMatrix*>(handle)->info.GetUIntInfo(field);
*len = static_cast<bst_ulong>(vec.size()); *len = static_cast<bst_ulong>(vec.size());
return BeginPtr(vec); return BeginPtr(vec);
} }
bst_ulong XGDMatrixNumRow(const void *handle) { bst_ulong XGDMatrixNumRow(const void *handle) {
return static_cast<bst_ulong>(static_cast<const DataMatrix*>(handle)->info.num_row()); return static_cast<bst_ulong>(static_cast<const DataMatrix*>(handle)->info.num_row());
} }
// xgboost implementation // xgboost implementation
void *XGBoosterCreate(void *dmats[], bst_ulong len) { void *XGBoosterCreate(void *dmats[], bst_ulong len) {
std::vector<DataMatrix*> mats; std::vector<DataMatrix*> mats;
for (bst_ulong i = 0; i < len; ++i) { for (bst_ulong i = 0; i < len; ++i) {
DataMatrix *dtr = static_cast<DataMatrix*>(dmats[i]); DataMatrix *dtr = static_cast<DataMatrix*>(dmats[i]);
mats.push_back(dtr); mats.push_back(dtr);
} }
return new Booster(mats); return new Booster(mats);
} }
void XGBoosterFree(void *handle) { void XGBoosterFree(void *handle) {
delete static_cast<Booster*>(handle); delete static_cast<Booster*>(handle);
} }
void XGBoosterSetParam(void *handle, const char *name, const char *value) { void XGBoosterSetParam(void *handle, const char *name, const char *value) {
static_cast<Booster*>(handle)->SetParam(name, value); static_cast<Booster*>(handle)->SetParam(name, value);
} }
void XGBoosterUpdateOneIter(void *handle, int iter, void *dtrain) { void XGBoosterUpdateOneIter(void *handle, int iter, void *dtrain) {
Booster *bst = static_cast<Booster*>(handle); Booster *bst = static_cast<Booster*>(handle);
DataMatrix *dtr = static_cast<DataMatrix*>(dtrain); DataMatrix *dtr = static_cast<DataMatrix*>(dtrain);
bst->CheckInitModel(); bst->CheckInitModel();
bst->CheckInit(dtr); bst->CheckInit(dtr);
bst->UpdateOneIter(iter, *dtr); bst->UpdateOneIter(iter, *dtr);
} }
void XGBoosterBoostOneIter(void *handle, void *dtrain, void XGBoosterBoostOneIter(void *handle, void *dtrain,
float *grad, float *hess, bst_ulong len) { float *grad, float *hess, bst_ulong len) {
Booster *bst = static_cast<Booster*>(handle); Booster *bst = static_cast<Booster*>(handle);
DataMatrix *dtr = static_cast<DataMatrix*>(dtrain); DataMatrix *dtr = static_cast<DataMatrix*>(dtrain);
bst->CheckInitModel(); bst->CheckInitModel();
bst->CheckInit(dtr); bst->CheckInit(dtr);
bst->BoostOneIter(*dtr, grad, hess, len); bst->BoostOneIter(*dtr, grad, hess, len);
} }
const char* XGBoosterEvalOneIter(void *handle, int iter, void *dmats[], const char* XGBoosterEvalOneIter(void *handle, int iter, void *dmats[],
const char *evnames[], bst_ulong len) { const char *evnames[], bst_ulong len) {
Booster *bst = static_cast<Booster*>(handle); Booster *bst = static_cast<Booster*>(handle);
std::vector<std::string> names; std::vector<std::string> names;
@ -316,29 +318,31 @@ extern "C"{
bst->CheckInitModel(); bst->CheckInitModel();
bst->eval_str = bst->EvalOneIter(iter, mats, names); bst->eval_str = bst->EvalOneIter(iter, mats, names);
return bst->eval_str.c_str(); return bst->eval_str.c_str();
} }
const float *XGBoosterPredict(void *handle, void *dmat, int option_mask, unsigned ntree_limit, bst_ulong *len) { const float *XGBoosterPredict(void *handle, void *dmat, int option_mask,
return static_cast<Booster*>(handle)->Pred(*static_cast<DataMatrix*>(dmat), option_mask, ntree_limit, len); unsigned ntree_limit, bst_ulong *len) {
} return static_cast<Booster*>(handle)->Pred(*static_cast<DataMatrix*>(dmat),
void XGBoosterLoadModel(void *handle, const char *fname) { option_mask, ntree_limit, len);
}
void XGBoosterLoadModel(void *handle, const char *fname) {
static_cast<Booster*>(handle)->LoadModel(fname); static_cast<Booster*>(handle)->LoadModel(fname);
} }
void XGBoosterSaveModel(void *handle, const char *fname) { void XGBoosterSaveModel(void *handle, const char *fname) {
Booster *bst = static_cast<Booster*>(handle); Booster *bst = static_cast<Booster*>(handle);
bst->CheckInitModel(); bst->CheckInitModel();
bst->SaveModel(fname, false); bst->SaveModel(fname, false);
} }
void XGBoosterLoadModelFromBuffer(void *handle, const void *buf, bst_ulong len) { void XGBoosterLoadModelFromBuffer(void *handle, const void *buf, bst_ulong len) {
static_cast<Booster*>(handle)->LoadModelFromBuffer(buf, len); static_cast<Booster*>(handle)->LoadModelFromBuffer(buf, len);
} }
const char *XGBoosterGetModelRaw(void *handle, bst_ulong *out_len) { const char *XGBoosterGetModelRaw(void *handle, bst_ulong *out_len) {
return static_cast<Booster*>(handle)->GetModelRaw(out_len); return static_cast<Booster*>(handle)->GetModelRaw(out_len);
} }
const char** XGBoosterDumpModel(void *handle, const char *fmap, int with_stats, bst_ulong *len){ const char** XGBoosterDumpModel(void *handle, const char *fmap,
int with_stats, bst_ulong *len) {
utils::FeatMap featmap; utils::FeatMap featmap;
if (strlen(fmap) != 0) { if (strlen(fmap) != 0) {
featmap.LoadText(fmap); featmap.LoadText(fmap);
} }
return static_cast<Booster*>(handle)->GetModelDump(featmap, with_stats != 0, len); return static_cast<Booster*>(handle)->GetModelDump(featmap, with_stats != 0, len);
}
} }

View File

@ -1,18 +1,20 @@
#ifndef XGBOOST_WRAPPER_H_
#define XGBOOST_WRAPPER_H_
/*! /*!
* Copyright (c) 2014 by Contributors
* \file xgboost_wrapper.h * \file xgboost_wrapper.h
* \author Tianqi Chen * \author Tianqi Chen
* \brief a C style wrapper of xgboost * \brief a C style wrapper of xgboost
* can be used to create wrapper of other languages * can be used to create wrapper of other languages
*/ */
#ifndef XGBOOST_WRAPPER_H_
#define XGBOOST_WRAPPER_H_
#if defined(_MSC_VER) || defined(_WIN32) #if defined(_MSC_VER) || defined(_WIN32)
#define XGB_DLL __declspec(dllexport) #define XGB_DLL __declspec(dllexport)
#else #else
#define XGB_DLL #define XGB_DLL
#endif #endif
// manually define unsign long // manually define unsign long
typedef unsigned long bst_ulong; typedef unsigned long bst_ulong; // NOLINT(*)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -92,7 +94,8 @@ extern "C" {
* \param array pointer to float vector * \param array pointer to float vector
* \param len length of array * \param len length of array
*/ */
XGB_DLL void XGDMatrixSetFloatInfo(void *handle, const char *field, const float *array, bst_ulong len); XGB_DLL void XGDMatrixSetFloatInfo(void *handle, const char *field,
const float *array, bst_ulong len);
/*! /*!
* \brief set uint32 vector to a content in info * \brief set uint32 vector to a content in info
* \param handle a instance of data matrix * \param handle a instance of data matrix
@ -100,7 +103,8 @@ extern "C" {
* \param array pointer to float vector * \param array pointer to float vector
* \param len length of array * \param len length of array
*/ */
XGB_DLL void XGDMatrixSetUIntInfo(void *handle, const char *field, const unsigned *array, bst_ulong len); XGB_DLL void XGDMatrixSetUIntInfo(void *handle, const char *field,
const unsigned *array, bst_ulong len);
/*! /*!
* \brief set label of the training matrix * \brief set label of the training matrix
* \param handle a instance of data matrix * \param handle a instance of data matrix
@ -115,7 +119,8 @@ extern "C" {
* \param out_len used to set result length * \param out_len used to set result length
* \return pointer to the result * \return pointer to the result
*/ */
XGB_DLL const float* XGDMatrixGetFloatInfo(const void *handle, const char *field, bst_ulong* out_len); XGB_DLL const float* XGDMatrixGetFloatInfo(const void *handle,
const char *field, bst_ulong* out_len);
/*! /*!
* \brief get uint32 info vector from matrix * \brief get uint32 info vector from matrix
* \param handle a instance of data matrix * \param handle a instance of data matrix
@ -123,7 +128,8 @@ extern "C" {
* \param out_len used to set result length * \param out_len used to set result length
* \return pointer to the result * \return pointer to the result
*/ */
XGB_DLL const unsigned* XGDMatrixGetUIntInfo(const void *handle, const char *field, bst_ulong* out_len); XGB_DLL const unsigned* XGDMatrixGetUIntInfo(const void *handle,
const char *field, bst_ulong* out_len);
/*! /*!
* \brief return number of rows * \brief return number of rows
*/ */