diff --git a/R-package/src/xgboost_R.cpp b/R-package/src/xgboost_R.cpp index cb2db8ddb..a7753dfa5 100644 --- a/R-package/src/xgboost_R.cpp +++ b/R-package/src/xgboost_R.cpp @@ -10,7 +10,6 @@ #include "src/utils/matrix_csr.h" using namespace std; using namespace xgboost; -using namespace xgboost::utils; extern "C" { void XGBoostAssert_R(int exp, const char *fmt, ...); diff --git a/src/io/simple_dmatrix-inl.hpp b/src/io/simple_dmatrix-inl.hpp index ca2eeadac..0883955fe 100644 --- a/src/io/simple_dmatrix-inl.hpp +++ b/src/io/simple_dmatrix-inl.hpp @@ -54,8 +54,10 @@ class DMatrixSimple : public DataMatrix { for (size_t i = 0; i < batch.size; ++i) { RowBatch::Inst inst = batch[i]; row_data_.resize(row_data_.size() + inst.length); - memcpy(&row_data_[row_ptr_.back()], inst.data, - sizeof(RowBatch::Entry) * inst.length); + if (inst.length != 0) { + memcpy(&row_data_[row_ptr_.back()], inst.data, + sizeof(RowBatch::Entry) * inst.length); + } row_ptr_.push_back(row_ptr_.back() + inst.length); } } @@ -244,8 +246,8 @@ class DMatrixSimple : public DataMatrix { at_first_ = false; batch_.size = parent_->row_ptr_.size() - 1; batch_.base_rowid = 0; - batch_.ind_ptr = &parent_->row_ptr_[0]; - batch_.data_ptr = &parent_->row_data_[0]; + batch_.ind_ptr = BeginPtr(parent_->row_ptr_); + batch_.data_ptr = BeginPtr(parent_->row_data_); return true; } virtual const RowBatch &Value(void) const { diff --git a/src/io/simple_fmatrix-inl.hpp b/src/io/simple_fmatrix-inl.hpp index 86763a105..7c8631a29 100644 --- a/src/io/simple_fmatrix-inl.hpp +++ b/src/io/simple_fmatrix-inl.hpp @@ -110,9 +110,9 @@ class FMatrixS : public IFMatrix{ const std::vector &data) { size_t nrow = ptr.size() - 1; fo.Write(&nrow, sizeof(size_t)); - fo.Write(&ptr[0], ptr.size() * sizeof(size_t)); + fo.Write(BeginPtr(ptr), ptr.size() * sizeof(size_t)); if (data.size() != 0) { - fo.Write(&data[0], data.size() * sizeof(RowBatch::Entry)); + fo.Write(BeginPtr(data), data.size() * sizeof(RowBatch::Entry)); } } /*! @@ -127,11 +127,11 @@ class FMatrixS : public IFMatrix{ size_t nrow; utils::Check(fi.Read(&nrow, sizeof(size_t)) != 0, "invalid input file format"); out_ptr->resize(nrow + 1); - utils::Check(fi.Read(&(*out_ptr)[0], out_ptr->size() * sizeof(size_t)) != 0, + utils::Check(fi.Read(BeginPtr(*out_ptr), out_ptr->size() * sizeof(size_t)) != 0, "invalid input file format"); out_data->resize(out_ptr->back()); if (out_data->size() != 0) { - utils::Assert(fi.Read(&(*out_data)[0], out_data->size() * sizeof(RowBatch::Entry)) != 0, + utils::Assert(fi.Read(BeginPtr(*out_data), out_data->size() * sizeof(RowBatch::Entry)) != 0, "invalid input file format"); } } @@ -213,8 +213,8 @@ class FMatrixS : public IFMatrix{ col_data_[i] = SparseBatch::Inst(&data[0] + ptr[ridx], static_cast(ptr[ridx+1] - ptr[ridx])); } - batch_.col_index = &col_index_[0]; - batch_.col_data = &col_data_[0]; + batch_.col_index = BeginPtr(col_index_); + batch_.col_data = BeginPtr(col_data_); this->BeforeFirst(); } // data content diff --git a/src/utils/utils.h b/src/utils/utils.h index e16d03778..5c3342d8e 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -154,6 +154,8 @@ inline FILE *FopenCheck(const char *fname, const char *flag) { Check(fp != NULL, "can not open file \"%s\"\n", fname); return fp; } +} // namespace utils +// easy utils that can be directly acessed in xgboost /*! \brief get the beginning address of a vector */ template inline T *BeginPtr(std::vector &vec) { @@ -163,6 +165,14 @@ inline T *BeginPtr(std::vector &vec) { return &vec[0]; } } -} // namespace utils +/*! \brief get the beginning address of a vector */ +template +inline const T *BeginPtr(const std::vector &vec) { + if (vec.size() == 0) { + return NULL; + } else { + return &vec[0]; + } +} } // namespace xgboost #endif // XGBOOST_UTILS_UTILS_H_ diff --git a/wrapper/xgboost_wrapper.cpp b/wrapper/xgboost_wrapper.cpp index 2c48bd8f1..abb844bce 100644 --- a/wrapper/xgboost_wrapper.cpp +++ b/wrapper/xgboost_wrapper.cpp @@ -13,6 +13,7 @@ using namespace std; #include "../src/data.h" #include "../src/learner/learner-inl.hpp" #include "../src/io/io.h" +#include "../src/utils/utils.h" #include "../src/io/simple_dmatrix-inl.hpp" using namespace xgboost; @@ -32,7 +33,7 @@ class Booster: public learner::BoostLearner { this->CheckInitModel(); this->Predict(dmat, output_margin != 0, &this->preds_, ntree_limit); *len = static_cast(this->preds_.size()); - return &this->preds_[0]; + return BeginPtr(this->preds_); } inline void BoostOneIter(const DataMatrix &train, float *grad, float *hess, bst_ulong len) { @@ -60,7 +61,7 @@ class Booster: public learner::BoostLearner { model_dump_cptr[i] = model_dump[i].c_str(); } *len = static_cast(model_dump.size()); - return &model_dump_cptr[0]; + return BeginPtr(model_dump_cptr); } // temporal fields // temporal data to save evaluation dump @@ -177,13 +178,13 @@ extern "C"{ std::vector &vec = static_cast(handle)->info.GetFloatInfo(field); vec.resize(len); - memcpy(&vec[0], info, sizeof(float) * len); + memcpy(BeginPtr(vec), info, sizeof(float) * len); } void XGDMatrixSetUIntInfo(void *handle, const char *field, const unsigned *info, bst_ulong len) { std::vector &vec = static_cast(handle)->info.GetUIntInfo(field); vec.resize(len); - memcpy(&vec[0], info, sizeof(unsigned) * len); + memcpy(BeginPtr(vec), info, sizeof(unsigned) * len); } void XGDMatrixSetGroup(void *handle, const unsigned *group, bst_ulong len) { DataMatrix *pmat = static_cast(handle); @@ -197,13 +198,13 @@ extern "C"{ const std::vector &vec = static_cast(handle)->info.GetFloatInfo(field); *len = static_cast(vec.size()); - return &vec[0]; + return BeginPtr(vec); } const unsigned* XGDMatrixGetUIntInfo(const void *handle, const char *field, bst_ulong* len) { const std::vector &vec = static_cast(handle)->info.GetUIntInfo(field); *len = static_cast(vec.size()); - return &vec[0]; + return BeginPtr(vec); } bst_ulong XGDMatrixNumRow(const void *handle) { return static_cast(static_cast(handle)->info.num_row());