Treelite
base.h
Go to the documentation of this file.
1 
7 #ifndef TREELITE_BASE_H_
8 #define TREELITE_BASE_H_
9 
10 #include <treelite/error.h>
11 #include <treelite/typeinfo.h>
12 #include <cstdint>
13 #include <typeinfo>
14 #include <string>
15 #include <unordered_map>
16 #include <stdexcept>
17 
18 namespace treelite {
19 
21 typedef float tl_float;
23 enum class SplitFeatureType : int8_t {
24  kNone, kNumerical, kCategorical
25 };
27 enum class Operator : int8_t {
28  kNone,
29  kEQ,
30  kLT,
31  kLE,
32  kGT,
33  kGE,
34 };
35 
37 extern const std::unordered_map<std::string, Operator> optable;
38 
44 inline std::string OpName(Operator op) {
45  switch (op) {
46  case Operator::kEQ: return "==";
47  case Operator::kLT: return "<";
48  case Operator::kLE: return "<=";
49  case Operator::kGT: return ">";
50  case Operator::kGE: return ">=";
51  default: return "";
52  }
53 }
54 
60 inline std::string SplitFeatureTypeName(SplitFeatureType type) {
61  switch (type) {
62  case SplitFeatureType::kNone: return "none";
63  case SplitFeatureType::kNumerical: return "numerical";
64  case SplitFeatureType::kCategorical: return "categorical";
65  default: return "";
66  }
67 }
68 
77 template <typename ElementType, typename ThresholdType>
78 inline bool CompareWithOp(ElementType lhs, Operator op, ThresholdType rhs) {
79  switch (op) {
80  case Operator::kEQ: return lhs == rhs;
81  case Operator::kLT: return lhs < rhs;
82  case Operator::kLE: return lhs <= rhs;
83  case Operator::kGT: return lhs > rhs;
84  case Operator::kGE: return lhs >= rhs;
85  default:
86  throw Error("operator undefined");
87  return false;
88  }
89 }
90 
91 } // namespace treelite
92 
93 #endif // TREELITE_BASE_H_
SplitFeatureType
feature split type
Definition: base.h:23
float tl_float
float type to be used internally
Definition: base.h:21
Exception class that will be thrown by Treelite.
Definition: error.h:18
std::string OpName(Operator op)
get string representation of comparison operator
Definition: base.h:44
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:78
Exception class used throughout the Treelite codebase.
Defines TypeInfo class and utilities.
std::string SplitFeatureTypeName(SplitFeatureType type)
Get string representation of split type.
Definition: base.h:60
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:27