Treelite
base.h
Go to the documentation of this file.
1 
7 #ifndef TREELITE_BASE_H_
8 #define TREELITE_BASE_H_
9 
10 #include <cstdint>
11 #include <typeinfo>
12 #include <string>
13 #include <unordered_map>
14 #include <stdexcept>
15 #include "./typeinfo.h"
16 
17 namespace treelite {
18 
20 typedef float tl_float;
22 enum class SplitFeatureType : int8_t {
23  kNone, kNumerical, kCategorical
24 };
26 enum class Operator : int8_t {
27  kNone,
28  kEQ,
29  kLT,
30  kLE,
31  kGT,
32  kGE,
33 };
34 
36 extern const std::unordered_map<std::string, Operator> optable;
37 
43 inline std::string OpName(Operator op) {
44  switch (op) {
45  case Operator::kEQ: return "==";
46  case Operator::kLT: return "<";
47  case Operator::kLE: return "<=";
48  case Operator::kGT: return ">";
49  case Operator::kGE: return ">=";
50  default: return "";
51  }
52 }
53 
59 inline std::string SplitFeatureTypeName(SplitFeatureType type) {
60  switch (type) {
61  case SplitFeatureType::kNone: return "none";
62  case SplitFeatureType::kNumerical: return "numerical";
63  case SplitFeatureType::kCategorical: return "categorical";
64  default: return "";
65  }
66 }
67 
76 template <typename ElementType, typename ThresholdType>
77 inline bool CompareWithOp(ElementType lhs, Operator op, ThresholdType rhs) {
78  switch (op) {
79  case Operator::kEQ: return lhs == rhs;
80  case Operator::kLT: return lhs < rhs;
81  case Operator::kLE: return lhs <= rhs;
82  case Operator::kGT: return lhs > rhs;
83  case Operator::kGE: return lhs >= rhs;
84  default:
85  throw std::runtime_error("operator undefined");
86  return false;
87  }
88 }
89 
90 } // namespace treelite
91 
92 #endif // TREELITE_BASE_H_
SplitFeatureType
feature split type
Definition: base.h:22
float tl_float
float type to be used internally
Definition: base.h:20
std::string OpName(Operator op)
get string representation of comparison operator
Definition: base.h:43
bool CompareWithOp(ElementType lhs, Operator op, ThresholdType rhs)
perform comparison between two float&#39;s using a comparsion operator The comparison will be in the form...
Definition: base.h:77
Defines TypeInfo class and utilities.
std::string SplitFeatureTypeName(SplitFeatureType type)
Get string representation of split type.
Definition: base.h:59
const std::unordered_map< std::string, Operator > optable
conversion table from string to Operator, defined in tables.cc
Definition: optable.cc:14
Operator
comparison operators
Definition: base.h:26