diff options
Diffstat (limited to 'eval')
8 files changed, 116 insertions, 51 deletions
diff --git a/eval/src/vespa/eval/eval/basic_nodes.h b/eval/src/vespa/eval/eval/basic_nodes.h index 027d6c28d78..c76a43468db 100644 --- a/eval/src/vespa/eval/eval/basic_nodes.h +++ b/eval/src/vespa/eval/eval/basic_nodes.h @@ -9,9 +9,9 @@ #include <map> #include <vector> #include <cassert> +#include <limits> -namespace vespalib { -namespace eval { +namespace vespalib::eval { namespace nodes { class Node; } @@ -24,6 +24,7 @@ struct NodeVisitor; **/ struct NodeHandler { virtual void handle(std::unique_ptr<nodes::Node> node) = 0; + virtual ~NodeHandler() {} }; @@ -35,9 +36,10 @@ namespace nodes { **/ struct DumpContext { const std::vector<vespalib::string> ¶m_names; - std::vector<vespalib::string> let_names; + std::vector<vespalib::string> let_names; + DumpContext(const std::vector<vespalib::string> ¶m_names_in) - : param_names(param_names_in), let_names() {} + : param_names(param_names_in), let_names() {} }; /** @@ -46,19 +48,32 @@ struct DumpContext { **/ struct Node { virtual bool is_forest() const { return false; } + virtual bool is_tree() const { return false; } + virtual bool is_const() const { return false; } + virtual bool is_param() const { return false; } + virtual double get_const_value() const; + void traverse(NodeTraverser &traverser) const; + virtual vespalib::string dump(DumpContext &ctx) const = 0; + virtual void accept(NodeVisitor &visitor) const = 0; + virtual size_t num_children() const = 0; + virtual const Node &get_child(size_t idx) const = 0; + virtual void detach_children(NodeHandler &handler) = 0; + bool is_leaf() const { return (num_children() == 0); } + virtual ~Node() {} }; + typedef std::unique_ptr<Node> Node_UP; /** @@ -70,7 +85,7 @@ typedef std::unique_ptr<Node> Node_UP; * } * </pre> **/ -template <typename T> +template<typename T> const T *as(const Node &node) { return dynamic_cast<const T *>(&node); } /** @@ -79,9 +94,11 @@ const T *as(const Node &node) { return dynamic_cast<const T *>(&node); } **/ struct Leaf : public Node { size_t num_children() const override { return 0; } + const Node &get_child(size_t) const override { abort(); } + void detach_children(NodeHandler &) override {} }; @@ -91,7 +108,9 @@ struct Leaf : public Node { **/ struct CommaTracker { bool first; + CommaTracker() : first(true) {} + void maybe_comma(vespalib::string &dst) { if (first) { first = false; @@ -106,12 +125,17 @@ private: double _value; public: Number(double value_in) : _value(value_in) {} + virtual bool is_const() const override { return true; } + virtual double get_const_value() const override { return value(); } + double value() const { return _value; } + vespalib::string dump(DumpContext &) const override { return make_string("%g", _value); } + void accept(NodeVisitor &visitor) const override; }; @@ -120,11 +144,15 @@ private: int _id; public: static const int UNDEF = std::numeric_limits<int>::max(); + explicit Symbol(int id_in) : _id(id_in) {} + int id() const { return _id; } + bool is_param() const override { return (_id >= 0); } + vespalib::string dump(DumpContext &ctx) const override { if (_id >= 0) { // param value assert(size_t(_id) < ctx.param_names.size()); @@ -135,6 +163,7 @@ public: return ctx.let_names[let_offset]; } } + void accept(NodeVisitor &visitor) const override; }; @@ -143,11 +172,17 @@ private: vespalib::string _value; public: String(const vespalib::string &value_in) : _value(value_in) {} + bool is_const() const override { return true; } + double get_const_value() const override { return hash(); } + const vespalib::string value() const { return _value; } + uint32_t hash() const { return hash_code(_value.data(), _value.size()); } + vespalib::string dump(DumpContext &ctx) const override; + void accept(NodeVisitor &visitor) const override; }; @@ -157,17 +192,24 @@ private: bool _is_const; public: Array() : _nodes(), _is_const(false) {} + bool is_const() const override { return _is_const; } + size_t size() const { return _nodes.size(); } + const Node &get(size_t i) const { return *_nodes[i]; } + size_t num_children() const override { return size(); } + const Node &get_child(size_t idx) const override { return get(idx); } + void detach_children(NodeHandler &handler) override { for (size_t i = 0; i < _nodes.size(); ++i) { handler.handle(std::move(_nodes[i])); } _nodes.clear(); } + void add(Node_UP node) { if (_nodes.empty()) { _is_const = node->is_const(); @@ -176,6 +218,7 @@ public: } _nodes.push_back(std::move(node)); } + vespalib::string dump(DumpContext &ctx) const override { vespalib::string str; str += "["; @@ -187,6 +230,7 @@ public: str += "]"; return str; } + void accept(NodeVisitor &visitor) const override; }; @@ -196,17 +240,23 @@ private: bool _is_const; public: Neg(Node_UP child_in) : _child(std::move(child_in)), _is_const(_child->is_const()) {} + bool is_const() const override { return _is_const; } + const Node &child() const { return *_child; } + size_t num_children() const override { return _child ? 1 : 0; } + const Node &get_child(size_t idx) const override { (void) idx; assert(idx == 0); return child(); } + void detach_children(NodeHandler &handler) override { handler.handle(std::move(_child)); } + vespalib::string dump(DumpContext &ctx) const override { vespalib::string str; str += "(-"; @@ -214,6 +264,7 @@ public: str += ")"; return str; } + void accept(NodeVisitor &visitor) const override; }; @@ -223,17 +274,23 @@ private: bool _is_const; public: Not(Node_UP child_in) : _child(std::move(child_in)), _is_const(_child->is_const()) {} + bool is_const() const override { return _is_const; } + const Node &child() const { return *_child; } + size_t num_children() const override { return _child ? 1 : 0; } + const Node &get_child(size_t idx) const override { (void) idx; assert(idx == 0); return child(); } + void detach_children(NodeHandler &handler) override { handler.handle(std::move(_child)); } + vespalib::string dump(DumpContext &ctx) const override { vespalib::string str; str += "(!"; @@ -241,6 +298,7 @@ public: str += ")"; return str; } + void accept(NodeVisitor &visitor) const override; }; @@ -249,18 +307,25 @@ private: Node_UP _cond; Node_UP _true_expr; Node_UP _false_expr; - double _p_true; - bool _is_tree; + double _p_true; + bool _is_tree; public: If(Node_UP cond_in, Node_UP true_expr_in, Node_UP false_expr_in, double p_true_in); + const Node &cond() const { return *_cond; } + const Node &true_expr() const { return *_true_expr; } + const Node &false_expr() const { return *_false_expr; } + double p_true() const { return _p_true; } + bool is_tree() const override { return _is_tree; } + size_t num_children() const override { return (_cond && _true_expr && _false_expr) ? 3 : 0; } + const Node &get_child(size_t idx) const override { assert(idx < 3); if (idx == 0) { @@ -271,11 +336,13 @@ public: return false_expr(); } } + void detach_children(NodeHandler &handler) override { handler.handle(std::move(_cond)); handler.handle(std::move(_true_expr)); handler.handle(std::move(_false_expr)); } + vespalib::string dump(DumpContext &ctx) const override { vespalib::string str; str += "if("; @@ -290,29 +357,37 @@ public: str += ")"; return str; } + void accept(NodeVisitor &visitor) const override; }; class Let : public Node { private: vespalib::string _name; - Node_UP _value; - Node_UP _expr; + Node_UP _value; + Node_UP _expr; public: Let(const vespalib::string &name_in, Node_UP value_in, Node_UP expr_in) - : _name(name_in), _value(std::move(value_in)), _expr(std::move(expr_in)) {} + : _name(name_in), _value(std::move(value_in)), _expr(std::move(expr_in)) {} + const vespalib::string &name() const { return _name; } + const Node &value() const { return *_value; } + const Node &expr() const { return *_expr; } + size_t num_children() const override { return (_value && _expr) ? 2 : 0; } + const Node &get_child(size_t idx) const override { assert(idx < 2); return (idx == 0) ? value() : expr(); } + void detach_children(NodeHandler &handler) override { handler.handle(std::move(_value)); handler.handle(std::move(_expr)); } + vespalib::string dump(DumpContext &ctx) const override { vespalib::string str; str += "let("; @@ -326,6 +401,7 @@ public: str += ")"; return str; } + void accept(NodeVisitor &visitor) const override; }; @@ -334,11 +410,13 @@ private: vespalib::string _message; public: Error(const vespalib::string &message_in) : _message(message_in) {} + const vespalib::string &message() const { return _message; } + vespalib::string dump(DumpContext &) const override { return _message; } + void accept(NodeVisitor &visitor) const override; }; -} // namespace vespalib::eval::nodes -} // namespace vespalib::eval -} // namespace vespalib +} +} diff --git a/eval/src/vespa/eval/eval/test/eval_spec.cpp b/eval/src/vespa/eval/eval/test/eval_spec.cpp index c4f349c614e..f9c36c9eb93 100644 --- a/eval/src/vespa/eval/eval/test/eval_spec.cpp +++ b/eval/src/vespa/eval/eval/test/eval_spec.cpp @@ -4,10 +4,9 @@ #include <vespa/vespalib/util/stringfmt.h> #include <vespa/vespalib/util/string_hash.h> #include <cmath> +#include <limits> -namespace vespalib { -namespace eval { -namespace test { +namespace vespalib::eval::test { constexpr double my_nan = std::numeric_limits<double>::quiet_NaN(); constexpr double my_inf = std::numeric_limits<double>::infinity(); @@ -414,6 +413,4 @@ EvalSpec::add_complex_cases() { [](double a)->double{ return -double(!bool(a)); }); } -} // namespace vespalib::eval::test -} // namespace vespalib::eval -} // namespace vespalib +} diff --git a/eval/src/vespa/eval/eval/value_type.cpp b/eval/src/vespa/eval/eval/value_type.cpp index 1553c6e064e..03e6d2bbcdf 100644 --- a/eval/src/vespa/eval/eval/value_type.cpp +++ b/eval/src/vespa/eval/eval/value_type.cpp @@ -2,9 +2,9 @@ #include "value_type.h" #include "value_type_spec.h" +#include <algorithm> -namespace vespalib { -namespace eval { +namespace vespalib::eval { namespace { @@ -270,5 +270,4 @@ operator<<(std::ostream &os, const ValueType &type) { return os << type.to_spec(); } -} // namespace vespalib::eval -} // namespace vespalib +} diff --git a/eval/src/vespa/eval/tensor/dense/dense_tensor_builder.cpp b/eval/src/vespa/eval/tensor/dense/dense_tensor_builder.cpp index caf42c9ae60..0b66dd51206 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_tensor_builder.cpp +++ b/eval/src/vespa/eval/tensor/dense/dense_tensor_builder.cpp @@ -2,14 +2,15 @@ #include "dense_tensor_builder.h" #include <vespa/vespalib/util/exceptions.h> -#include <vespa/vespalib/util/stringfmt.h> #include <cassert> +#include <limits> +#include <algorithm> + using vespalib::IllegalArgumentException; using vespalib::make_string; -namespace vespalib { -namespace tensor { +namespace vespalib::tensor { namespace { @@ -169,5 +170,4 @@ DenseTensorBuilder::build() return result; } -} // namespace vespalib::tensor -} // namespace vespalib +} diff --git a/eval/src/vespa/eval/tensor/dense/dense_tensor_reduce.hpp b/eval/src/vespa/eval/tensor/dense/dense_tensor_reduce.hpp index 24e12f83b24..c6fc04bb27b 100644 --- a/eval/src/vespa/eval/tensor/dense/dense_tensor_reduce.hpp +++ b/eval/src/vespa/eval/tensor/dense/dense_tensor_reduce.hpp @@ -2,10 +2,9 @@ #include "dense_tensor_reduce.h" #include <cassert> +#include <algorithm> -namespace vespalib { -namespace tensor { -namespace dense { +namespace vespalib::tensor::dense { using Cells = DenseTensorView::Cells; using CellsRef = DenseTensorView::CellsRef; @@ -120,6 +119,4 @@ reduce(const DenseTensorView &tensor, const std::vector<vespalib::string> &dimen } } -} // namespace dense -} // namespace tensor -} // namespace vespalib +} diff --git a/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp b/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp index 4a6cb4dedd5..92f0b40e259 100644 --- a/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp +++ b/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp @@ -13,11 +13,11 @@ #include <vespa/vespalib/stllike/hash_map_equal.hpp> #include <vespa/vespalib/util/array_equal.hpp> #include <sstream> +#include <algorithm> using vespalib::eval::TensorSpec; -namespace vespalib { -namespace tensor { +namespace vespalib::tensor { namespace { @@ -307,8 +307,6 @@ SparseTensor::reduce(const eval::BinaryOperation &op, { return op.eval(lhsValue, rhsValue); }); } -} // namespace vespalib::tensor - -} // namespace vespalib +} VESPALIB_HASH_MAP_INSTANTIATE(vespalib::tensor::SparseTensorAddressRef, double); diff --git a/eval/src/vespa/eval/tensor/sparse/sparse_tensor_unsorted_address_builder.cpp b/eval/src/vespa/eval/tensor/sparse/sparse_tensor_unsorted_address_builder.cpp index 1ca32aad6be..1e112cbaa6e 100644 --- a/eval/src/vespa/eval/tensor/sparse/sparse_tensor_unsorted_address_builder.cpp +++ b/eval/src/vespa/eval/tensor/sparse/sparse_tensor_unsorted_address_builder.cpp @@ -4,9 +4,9 @@ #include "sparse_tensor_address_builder.h" #include <vespa/eval/eval/value_type.h> #include <cassert> +#include <algorithm> -namespace vespalib { -namespace tensor { +namespace vespalib::tensor { SparseTensorUnsortedAddressBuilder::SparseTensorUnsortedAddressBuilder() : _elementStrings(), @@ -46,6 +46,4 @@ SparseTensorUnsortedAddressBuilder::buildTo(SparseTensorAddressBuilder & } } - -} // namespace vespalib::tensor -} // namespace vespalib +} diff --git a/eval/src/vespa/eval/tensor/tensor_mapper.cpp b/eval/src/vespa/eval/tensor/tensor_mapper.cpp index c15494ed7fe..7c2c72abd46 100644 --- a/eval/src/vespa/eval/tensor/tensor_mapper.cpp +++ b/eval/src/vespa/eval/tensor/tensor_mapper.cpp @@ -3,18 +3,17 @@ #include "tensor_mapper.h" #include "tensor.h" #include "tensor_visitor.h" -#include <vespa/eval/tensor/sparse/direct_sparse_tensor_builder.h> -#include <vespa/eval/tensor/dense/dense_tensor.h> -#include <vespa/eval/eval/simple_tensor.h> #include "tensor_address_element_iterator.h" #include "default_tensor.h" #include "wrapped_simple_tensor.h" +#include <vespa/eval/tensor/sparse/direct_sparse_tensor_builder.h> +#include <vespa/eval/tensor/dense/dense_tensor.h> +#include <limits> using vespalib::eval::ValueType; using vespalib::eval::TensorSpec; -namespace vespalib { -namespace tensor { +namespace vespalib::tensor { namespace { @@ -422,5 +421,4 @@ std::unique_ptr<Tensor> TensorMapper::mapToSparse<SparseTensor>(const Tensor &tensor, const ValueType &type); -} // namespace vespalib::tensor -} // namespace vespalib +} |