MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / value_compare

Function value_compare

subprojects/llama.cpp/common/jinja/value.cpp:1144–1205  ·  view source on GitHub ↗

compare operator for value_t

Source from the content-addressed store, hash-verified

1142
1143// compare operator for value_t
1144bool value_compare(const value & a, const value & b, value_compare_op op) {
1145 auto cmp = [&]() {
1146 // compare numeric types
1147 if ((is_val<value_int>(a) || is_val<value_float>(a)) &&
1148 (is_val<value_int>(b) || is_val<value_float>(b))){
1149 try {
1150 if (op == value_compare_op::eq) {
1151 return a->as_float() == b->as_float();
1152 } else if (op == value_compare_op::ge) {
1153 return a->as_float() >= b->as_float();
1154 } else if (op == value_compare_op::gt) {
1155 return a->as_float() > b->as_float();
1156 } else if (op == value_compare_op::lt) {
1157 return a->as_float() < b->as_float();
1158 } else if (op == value_compare_op::ne) {
1159 return a->as_float() != b->as_float();
1160 } else {
1161 throw std::runtime_error("Unsupported comparison operator for numeric types");
1162 }
1163 } catch (...) {}
1164 }
1165 // compare string and number
1166 // TODO: not sure if this is the right behavior
1167 if ((is_val<value_string>(b) && (is_val<value_int>(a) || is_val<value_float>(a))) ||
1168 (is_val<value_string>(a) && (is_val<value_int>(b) || is_val<value_float>(b))) ||
1169 (is_val<value_string>(a) && is_val<value_string>(b))) {
1170 try {
1171 if (op == value_compare_op::eq) {
1172 return a->as_string().str() == b->as_string().str();
1173 } else if (op == value_compare_op::ge) {
1174 return a->as_string().str() >= b->as_string().str();
1175 } else if (op == value_compare_op::gt) {
1176 return a->as_string().str() > b->as_string().str();
1177 } else if (op == value_compare_op::lt) {
1178 return a->as_string().str() < b->as_string().str();
1179 } else if (op == value_compare_op::ne) {
1180 return a->as_string().str() != b->as_string().str();
1181 } else {
1182 throw std::runtime_error("Unsupported comparison operator for string/number types");
1183 }
1184 } catch (...) {}
1185 }
1186 // compare boolean simple
1187 if (is_val<value_bool>(a) && is_val<value_bool>(b)) {
1188 if (op == value_compare_op::eq) {
1189 return a->as_bool() == b->as_bool();
1190 } else if (op == value_compare_op::ne) {
1191 return a->as_bool() != b->as_bool();
1192 } else {
1193 throw std::runtime_error("Unsupported comparison operator for bool type");
1194 }
1195 }
1196 // compare by type
1197 if (a->type() != b->type()) {
1198 return false;
1199 }
1200 return false;
1201 };

Callers 2

test_compare_fnFunction · 0.85
value.cppFile · 0.85

Calls 5

typeMethod · 0.65
as_floatMethod · 0.45
strMethod · 0.45
as_stringMethod · 0.45
as_boolMethod · 0.45

Tested by 1

test_compare_fnFunction · 0.68