Skip to content

Commit

Permalink
Merge pull request #3 from levinkov/master
Browse files Browse the repository at this point in the history
bug fix related to i/o of (unsigned) char
  • Loading branch information
bjoern-andres committed Mar 21, 2016
2 parents 5e0eb90 + 358cebd commit f27c926
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions include/andres/ml/decision-trees.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,48 @@ private:
std::vector<size_t>& = std::vector<size_t>()
);

template<typename T>
T read(std::istream& s, T)
{
T value;
s >> value;

return value;
}

unsigned char read(std::istream& s, unsigned char)
{
size_t value;
s >> value;

return value;
}

char read(std::istream& s, char)
{
ptrdiff_t value;
s >> value;

return value;
}

template<typename T>
void write(std::ostream& s, T value) const
{
s << value;
}

void write(std::ostream& s, unsigned char value) const
{
s << static_cast<size_t>(label_);
}

void write(std::ostream& s, char value) const
{
s << static_cast<ptrdiff_t>(label_);
}


size_t featureIndex_;
Feature threshold_;
size_t childNodeIndices_[2]; // 0 means <, 1 means >=
Expand Down Expand Up @@ -565,10 +607,12 @@ inline void
DecisionNode<FEATURE, LABEL>::serialize(std::ostream& s) const
{
s << " " << featureIndex_;
s << " " << threshold_;
s << " ";
write(s, threshold_);
s << " " << childNodeIndices_[0];
s << " " << childNodeIndices_[1];
s << " " << label_;
s << " ";
write(s, label_);
s << " " << isLeaf_;
}

Expand All @@ -579,10 +623,10 @@ inline void
DecisionNode<FEATURE, LABEL>::deserialize(std::istream& s)
{
s >> featureIndex_;
s >> threshold_;
threshold_ = read(s, FEATURE());
s >> childNodeIndices_[0];
s >> childNodeIndices_[1];
s >> label_;
label_ = read(s, LABEL());
s >> isLeaf_;
}

Expand Down

0 comments on commit f27c926

Please sign in to comment.