From 358cebdc046f2c25fd21fb2cb517838839f9eb85 Mon Sep 17 00:00:00 2001 From: Evgeny Levinkov Date: Wed, 9 Mar 2016 17:25:48 +0100 Subject: [PATCH] a small bug fix to make sure that (unsigned) chars are read/written properly --- include/andres/ml/decision-trees.hxx | 52 +++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/include/andres/ml/decision-trees.hxx b/include/andres/ml/decision-trees.hxx index 5884821..98ae828 100644 --- a/include/andres/ml/decision-trees.hxx +++ b/include/andres/ml/decision-trees.hxx @@ -123,6 +123,48 @@ private: std::vector& = std::vector() ); + template + 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 + void write(std::ostream& s, T value) const + { + s << value; + } + + void write(std::ostream& s, unsigned char value) const + { + s << static_cast(label_); + } + + void write(std::ostream& s, char value) const + { + s << static_cast(label_); + } + + size_t featureIndex_; Feature threshold_; size_t childNodeIndices_[2]; // 0 means <, 1 means >= @@ -565,10 +607,12 @@ inline void DecisionNode::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_; } @@ -579,10 +623,10 @@ inline void DecisionNode::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_; }