Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing empty vector in parameter. #5087

Merged
merged 3 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions amalgamation/xgboost-all0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

// tress
#include "../src/tree/split_evaluator.cc"
#include "../src/tree/param.cc"
#include "../src/tree/tree_model.cc"
#include "../src/tree/tree_updater.cc"
#include "../src/tree/updater_colmaker.cc"
Expand Down
79 changes: 79 additions & 0 deletions src/tree/param.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*!
* Copyright by Contributors 2019
*/
#include <iostream>
#include <vector>

#include "param.h"

namespace std {
std::istream &operator>>(std::istream &is, std::vector<int> &t) {
// get (
while (true) {
char ch = is.peek();
if (isdigit(ch)) {
int idx;
if (is >> idx) {
t.assign(&idx, &idx + 1);
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
}
return is;
}
is.get();
if (ch == '(') {
break;
}
if (!isspace(ch)) {
is.setstate(std::ios::failbit);
return is;
}
}
int idx;
std::vector<int> tmp;
while (true) {
char ch = is.peek();
if (isspace(ch)) {
is.get();
} else {
break;
}
}
if (is.peek() == ')') {
is.get();
return is;
}
while (is >> idx) {
tmp.push_back(idx);
char ch;
do {
ch = is.get();
} while (isspace(ch));
if (ch == 'L') {
ch = is.get();
}
if (ch == ',') {
while (true) {
ch = is.peek();
if (isspace(ch)) {
is.get();
continue;
}
if (ch == ')') {
is.get();
break;
}
break;
}
if (ch == ')') {
break;
}
} else if (ch == ')') {
break;
} else {
is.setstate(std::ios::failbit);
return is;
}
}
t.assign(tmp.begin(), tmp.end());
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
return is;
}
} // namespace std
60 changes: 2 additions & 58 deletions src/tree/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2014 by Contributors
* \file param.h
* \brief training parameters, statistics used to support tree construction.
* \author Tianqi Chen
n * \author Tianqi Chen
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
*/
#ifndef XGBOOST_TREE_PARAM_H_
#define XGBOOST_TREE_PARAM_H_
Expand Down Expand Up @@ -495,63 +495,7 @@ inline std::ostream &operator<<(std::ostream &os, const std::vector<int> &t) {
return os;
}

inline std::istream &operator>>(std::istream &is, std::vector<int> &t) {
// get (
while (true) {
char ch = is.peek();
if (isdigit(ch)) {
int idx;
if (is >> idx) {
t.assign(&idx, &idx + 1);
}
return is;
}
is.get();
if (ch == '(') {
break;
}
if (!isspace(ch)) {
is.setstate(std::ios::failbit);
return is;
}
}
int idx;
std::vector<int> tmp;
while (is >> idx) {
tmp.push_back(idx);
char ch;
do {
ch = is.get();
} while (isspace(ch));
if (ch == 'L') {
ch = is.get();
}
if (ch == ',') {
while (true) {
ch = is.peek();
if (isspace(ch)) {
is.get();
continue;
}
if (ch == ')') {
is.get();
break;
}
break;
}
if (ch == ')') {
break;
}
} else if (ch == ')') {
break;
} else {
is.setstate(std::ios::failbit);
return is;
}
}
t.assign(tmp.begin(), tmp.end());
return is;
}
std::istream &operator>>(std::istream &is, std::vector<int> &t);
} // namespace std

#endif // XGBOOST_TREE_PARAM_H_
6 changes: 6 additions & 0 deletions tests/cpp/tree/test_param.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ TEST(Param, VectorStreamRead) {
ss << "(3,2,1";
ss >> vals_in;
EXPECT_NE(vals_in, vals);

vals_in.clear(); ss.flush(); ss.clear(); ss.str("");
vals_in.emplace_back(3);
ss << "( )";
ss >> vals_in;
ASSERT_TRUE(ss.good());
}

TEST(Param, SplitEntry) {
Expand Down