philsupertramp/game-math
Loading...
Searching...
No Matches
MathNode.h
Go to the documentation of this file.
1#pragma once
2#include "../format.h"
3#include <cstdlib>
4#include <cstring>
5#include <memory>
6#include <regex>
7#include <string>
8
21};
22
29};
30
36std::regex GetRegex(MathNodeType type);
37
38
43{
44public:
46 bool isNegative = false;
48 bool hasParentheses = false;
52 NodeConnectionType connectionType = NodeConnectionType::ConnectionType_Dual;
54 std::shared_ptr<MathNode> left = nullptr;
56 std::shared_ptr<MathNode> right = nullptr;
58 char* value{};
59
60protected:
62 size_t valSize = 0;
63
64public:
69 explicit MathNode(const std::string& val) {
70 auto realValue = strip(val); // strip to allow comparison
71 isNegative = realValue.find('-') != std::string::npos && realValue.size() > 1;
72 value = new char[realValue.size() + 1];
73 std::copy(realValue.begin(), realValue.end(), value);
74 value[realValue.size()] = '\0'; // don't forget the terminating 0
75 valSize = realValue.size() + 1;
76 }
77
82 MathNode(const MathNode& other) {
83 valSize = other.valSize;
84 isNegative = other.isNegative;
86 value = (char*)malloc(other.valSize);
87 strcpy(value, other.value);
88 left = other.left;
89 right = other.right;
90 type = other.type;
91 }
92
98 [[nodiscard]] virtual double Evaluate() const = 0;
99
104 [[nodiscard]] std::string GetString() const {
105 std::stringstream out;
106 if(type == NodeType_Parentheses || hasParentheses) out << "(";
107 if(left != nullptr) out << left;
108 out << value;
109 if(right != nullptr) out << right;
110 if(type == NodeType_Parentheses || hasParentheses) out << ")";
111 return out.str();
112 }
113
114 friend bool operator==(const MathNode& lhs, const MathNode& rhs) {
115 return strcmp(lhs.value, rhs.value) == 0 && lhs.isNegative == rhs.isNegative;
116 }
117
118 bool operator==(MathNode* rhs) { return strcmp(value, rhs->value) == 0 && isNegative == rhs->isNegative; }
119
126 friend std::ostream& operator<<(std::ostream& ostr, const MathNode& other) {
127 if(other.hasParentheses) ostr << "(";
128 if(other.left != nullptr) ostr << other.left;
129 ostr << (other.isNegative ? "-" : "") << other.value;
130 if(other.right != nullptr) ostr << other.right;
131 if(other.hasParentheses) ostr << ")";
132 return ostr;
133 }
140 friend std::ostream& operator<<(std::ostream& ostr, MathNode* other) {
141 // early exit
142 if(other == nullptr) return ostr;
143
144 if(other->hasParentheses) ostr << "(";
145 if(other->left != nullptr) ostr << other->left;
146 ostr << (other->isNegative ? "-" : "") << other->value;
147 if(other->right != nullptr) ostr << other->right;
148 if(other->hasParentheses) ostr << ")";
149 return ostr;
150 }
151};
152
std::regex GetRegex(MathNodeType type)
NodeConnectionType
Definition: MathNode.h:23
@ ConnectionType_Right
Definition: MathNode.h:26
@ ConnectionType_Dual
Definition: MathNode.h:24
@ ConnectionType_None
Definition: MathNode.h:27
@ ConnectionType_Unknown
Definition: MathNode.h:28
@ ConnectionType_Left
Definition: MathNode.h:25
MathNodeType
Definition: MathNode.h:12
@ NodeType_Operator
Definition: MathNode.h:13
@ NodeType_Symbolic
Definition: MathNode.h:14
@ NodeType_Any
Definition: MathNode.h:19
@ NodeType_Functional
Definition: MathNode.h:17
@ NodeType_DefaultSymbol
Definition: MathNode.h:18
@ NodeType_Operator_or_Parentheses
Definition: MathNode.h:20
@ NodeType_Parentheses
Definition: MathNode.h:16
@ NodeType_Numeric
Definition: MathNode.h:15
Definition: MathNode.h:43
MathNode(const MathNode &other)
Definition: MathNode.h:82
friend std::ostream & operator<<(std::ostream &ostr, const MathNode &other)
Definition: MathNode.h:126
bool isNegative
helper to determine whether a node is negated or not
Definition: MathNode.h:46
char * value
char representation of value
Definition: MathNode.h:58
MathNode(const std::string &val)
Definition: MathNode.h:69
size_t valSize
helper to store size of value
Definition: MathNode.h:62
friend std::ostream & operator<<(std::ostream &ostr, MathNode *other)
Definition: MathNode.h:140
friend bool operator==(const MathNode &lhs, const MathNode &rhs)
Definition: MathNode.h:114
std::shared_ptr< MathNode > left
next node on left side
Definition: MathNode.h:54
bool operator==(MathNode *rhs)
Definition: MathNode.h:118
bool hasParentheses
helper to determine if node is within parentheses
Definition: MathNode.h:48
MathNodeType type
node type representation
Definition: MathNode.h:50
std::string GetString() const
Definition: MathNode.h:104
virtual double Evaluate() const =0
std::shared_ptr< MathNode > right
next node on right side
Definition: MathNode.h:56
NodeConnectionType connectionType
type of connection allowed for this node
Definition: MathNode.h:52
std::string strip(const std::string &in)