diff options
author | HÃ¥vard Pettersen <3535158+havardpe@users.noreply.github.com> | 2023-06-01 17:44:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-01 17:44:56 +0200 |
commit | 0e3643dc47b8b4730fddeb5414eda0085182a725 (patch) | |
tree | bfe9925d5ed35b40e38fbb6071bb5345bd504e64 | |
parent | e985c95958a722691b72ac89fd0ba6766ae45083 (diff) | |
parent | 241f880e08589cfa91b3da5c45f4f5b8dcb3b9d5 (diff) |
Merge pull request #27252 from vespa-engine/balder/modernize-code-while-reading-1
Modernize and unify code.
6 files changed, 62 insertions, 74 deletions
diff --git a/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.cpp b/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.cpp index 3663d76ad51..d94e8c19981 100644 --- a/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.cpp +++ b/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.cpp @@ -6,21 +6,22 @@ #include <vespa/searchcommon/attribute/iattributecontext.h> #include <vespa/vespalib/util/stringfmt.h> -namespace search { -namespace expression { +namespace search::expression { using vespalib::Serializer; using vespalib::Deserializer; IMPLEMENT_EXPRESSIONNODE(ArrayAtLookup, UnaryFunctionNode); -ArrayAtLookup::ArrayAtLookup() +ArrayAtLookup::ArrayAtLookup() noexcept + : _attributeName(), + _attribute(nullptr), + _docId(0), + _basicAttributeType(BAT_STRING) { } -ArrayAtLookup::~ArrayAtLookup() -{ -} +ArrayAtLookup::~ArrayAtLookup() = default; ArrayAtLookup::ArrayAtLookup(const vespalib::string &attribute, ExpressionNode::UP arg) : UnaryFunctionNode(std::move(arg)), @@ -41,11 +42,9 @@ ArrayAtLookup::ArrayAtLookup(const ArrayAtLookup &rhs) : UnaryFunctionNode(rhs), _attributeName(rhs._attributeName), _attribute(rhs._attribute), - _docId(rhs._docId), + _docId(0), _basicAttributeType(rhs._basicAttributeType) { - // why? - _docId = 0; } ArrayAtLookup & ArrayAtLookup::operator= (const ArrayAtLookup &rhs) @@ -54,7 +53,6 @@ ArrayAtLookup & ArrayAtLookup::operator= (const ArrayAtLookup &rhs) UnaryFunctionNode::operator =(rhs); _attributeName = rhs._attributeName; _attribute = rhs._attribute; - // _docId = rhs._docId; _docId = 0; _basicAttributeType = rhs._basicAttributeType; } @@ -65,13 +63,13 @@ void ArrayAtLookup::onPrepareResult() { if (_attribute->isIntegerType()) { _basicAttributeType = BAT_INT; - setResultType(std::unique_ptr<ResultNode>(new Int64ResultNode())); + setResultType(std::make_unique<Int64ResultNode>()); } else if (_attribute->isFloatingPointType()) { _basicAttributeType = BAT_FLOAT; - setResultType(std::unique_ptr<ResultNode>(new FloatResultNode())); + setResultType(std::make_unique<FloatResultNode>()); } else { _basicAttributeType = BAT_STRING; - setResultType(std::unique_ptr<ResultNode>(new StringResultNode())); + setResultType(std::make_unique<StringResultNode>()); } } @@ -137,7 +135,7 @@ bool ArrayAtLookup::onExecute() const void ArrayAtLookup::wireAttributes(const search::attribute::IAttributeContext & attrCtx) { _attribute = attrCtx.getAttribute(_attributeName); - if (_attribute == NULL) { + if (_attribute == nullptr) { throw std::runtime_error(vespalib::make_string("Failed locating attribute vector '%s'", _attributeName.c_str())); } } @@ -156,5 +154,4 @@ Deserializer & ArrayAtLookup::onDeserialize(Deserializer & is) return is; } -} // namespace expression -} // namespace search +} diff --git a/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.h b/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.h index be3c6ac2b4a..9404ec09b04 100644 --- a/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.h +++ b/searchlib/src/vespa/searchlib/expression/arrayatlookupfunctionnode.h @@ -3,12 +3,12 @@ #include "unaryfunctionnode.h" -namespace search { - namespace attribute { - class IAttributeVector; - class IAttributeContext; - } -namespace expression { +namespace search::attribute { + class IAttributeVector; + class IAttributeContext; +} + +namespace search::expression { class ArrayAtLookup : public UnaryFunctionNode { @@ -16,8 +16,8 @@ public: DECLARE_EXPRESSIONNODE(ArrayAtLookup); DECLARE_NBO_SERIALIZE; - ArrayAtLookup(); - ~ArrayAtLookup(); + ArrayAtLookup() noexcept; + ~ArrayAtLookup() override; ArrayAtLookup(const vespalib::string &attribute, ExpressionNode::UP arg); ArrayAtLookup(const search::attribute::IAttributeVector &attr, ExpressionNode::UP indexArg); ArrayAtLookup(const ArrayAtLookup &rhs); @@ -32,12 +32,10 @@ private: BAT_INT, BAT_FLOAT, BAT_STRING }; - vespalib::string _attributeName = vespalib::string(); - const search::attribute::IAttributeVector * _attribute = 0; - DocId _docId = 0; - BasicAttributeType _basicAttributeType = BAT_STRING; + vespalib::string _attributeName; + const search::attribute::IAttributeVector * _attribute; + DocId _docId; + BasicAttributeType _basicAttributeType; }; } -} - diff --git a/searchlib/src/vespa/searchlib/expression/expressiontree.cpp b/searchlib/src/vespa/searchlib/expression/expressiontree.cpp index e47ba4ebeb0..5592f2f863b 100644 --- a/searchlib/src/vespa/searchlib/expression/expressiontree.cpp +++ b/searchlib/src/vespa/searchlib/expression/expressiontree.cpp @@ -79,7 +79,7 @@ void ExpressionTree::onPrepare(bool preserveAccurateTypes) { (void) preserveAccurateTypes; - if (_root.get() != NULL) { + if (_root) { gather(_attributeNodes).from(*_root); gather(_documentAccessorNodes).from(*_root); gather(_relevanceNodes).from(*_root); @@ -141,18 +141,16 @@ ExpressionTree::swap(ExpressionTree & e) _arrayAtLookupNodes.swap(_arrayAtLookupNodes); } -ExpressionTree::~ExpressionTree() -{ -} +ExpressionTree::~ExpressionTree() = default; bool ExpressionTree::execute(const document::Document & doc, HitRank rank) const { - for(DocumentAccessorNodeList::const_iterator it(_documentAccessorNodes.begin()), mt(_documentAccessorNodes.end()); it != mt; it++) { - (*it)->setDoc(doc); + for(auto * node : _documentAccessorNodes) { + node->setDoc(doc); } - for(RelevanceNodeList::const_iterator it(_relevanceNodes.begin()), mt(_relevanceNodes.end()); it != mt; it++) { - (*it)->setRelevance(rank); + for(auto * node : _relevanceNodes) { + node->setRelevance(rank); } return _root->execute(); } diff --git a/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.cpp b/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.cpp index f220b1d455d..705fba1f75e 100644 --- a/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.cpp +++ b/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.cpp @@ -5,23 +5,20 @@ #include <vespa/searchlib/common/converters.h> #include <vespa/vespalib/util/stringfmt.h> -namespace search { -namespace expression { +namespace search::expression { using vespalib::Serializer; using vespalib::Deserializer; IMPLEMENT_EXPRESSIONNODE(InterpolatedLookup, UnaryFunctionNode); -InterpolatedLookup::InterpolatedLookup() - : _attribute(0), +InterpolatedLookup::InterpolatedLookup() noexcept + : _attribute(nullptr), _docId(0) { } -InterpolatedLookup::~InterpolatedLookup() -{ -} +InterpolatedLookup::~InterpolatedLookup() = default; InterpolatedLookup::InterpolatedLookup(const vespalib::string &attribute, ExpressionNode::UP arg) : UnaryFunctionNode(std::move(arg)), @@ -44,10 +41,8 @@ InterpolatedLookup::InterpolatedLookup(const InterpolatedLookup &rhs) : UnaryFunctionNode(rhs), _attributeName(rhs._attributeName), _attribute(rhs._attribute), - _docId(rhs._docId) + _docId(0) { - // why? - _docId = 0; } InterpolatedLookup & @@ -57,26 +52,27 @@ InterpolatedLookup::operator= (const InterpolatedLookup &rhs) UnaryFunctionNode::operator =(rhs); _attributeName = rhs._attributeName; _attribute = rhs._attribute; - // _docId = rhs._docId; _docId = 0; } return *this; } -void InterpolatedLookup::onPrepareResult() +void +InterpolatedLookup::onPrepareResult() { - setResultType(std::unique_ptr<ResultNode>(new FloatResultNode())); + setResultType(std::make_unique<FloatResultNode>()); } -static double -simpleInterpolate(size_t sz, std::vector<double> v, double lookup) -{ +namespace { + +double +simpleInterpolate(size_t sz, std::vector<double> v, double lookup) { if (sz == 0 || lookup < v[0]) return 0; for (size_t i = 1; i < sz; ++i) { if (lookup < v[i]) { - double total = v[i] - v[i-1]; - double above = lookup - v[i-1]; + double total = v[i] - v[i - 1]; + double above = lookup - v[i - 1]; double result = i - 1; result += (above / total); return result; @@ -85,7 +81,10 @@ simpleInterpolate(size_t sz, std::vector<double> v, double lookup) return sz - 1; } -bool InterpolatedLookup::onExecute() const +} + +bool +InterpolatedLookup::onExecute() const { getArg().execute(); double lookup = getArg().getResult()->getFloat(); @@ -99,27 +98,29 @@ bool InterpolatedLookup::onExecute() const return true; } -void InterpolatedLookup::wireAttributes(const search::attribute::IAttributeContext & attrCtx) +void +InterpolatedLookup::wireAttributes(const search::attribute::IAttributeContext & attrCtx) { _attribute = attrCtx.getAttribute(_attributeName); - if (_attribute == NULL) { + if (_attribute == nullptr) { throw std::runtime_error(vespalib::make_string("Failed locating attribute vector '%s'", _attributeName.c_str())); } } -Serializer & InterpolatedLookup::onSerialize(Serializer & os) const +Serializer & +InterpolatedLookup::onSerialize(Serializer & os) const { UnaryFunctionNode::onSerialize(os); os << _attributeName; return os; } -Deserializer & InterpolatedLookup::onDeserialize(Deserializer & is) +Deserializer & +InterpolatedLookup::onDeserialize(Deserializer & is) { UnaryFunctionNode::onDeserialize(is); is >> _attributeName; return is; } -} // namespace expression -} // namespace search +} diff --git a/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.h b/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.h index 794adf9106d..de2ba5c362c 100644 --- a/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.h +++ b/searchlib/src/vespa/searchlib/expression/interpolatedlookupfunctionnode.h @@ -3,10 +3,9 @@ #include "unaryfunctionnode.h" -namespace search { - namespace attribute { class IAttributeVector; } +namespace search::attribute { class IAttributeVector; } -namespace expression { +namespace search::expression { class InterpolatedLookup : public UnaryFunctionNode { @@ -14,8 +13,8 @@ public: DECLARE_EXPRESSIONNODE(InterpolatedLookup); DECLARE_NBO_SERIALIZE; - InterpolatedLookup(); - ~InterpolatedLookup(); + InterpolatedLookup() noexcept; + ~InterpolatedLookup() override; InterpolatedLookup(const vespalib::string &attribute, ExpressionNode::UP arg); InterpolatedLookup(const search::attribute::IAttributeVector &attr, ExpressionNode::UP lookupArg); InterpolatedLookup(const InterpolatedLookup &rhs); @@ -31,5 +30,3 @@ private: }; } -} - diff --git a/searchlib/src/vespa/searchlib/expression/relevancenode.h b/searchlib/src/vespa/searchlib/expression/relevancenode.h index 05ac3fe6cc4..675ce996ec3 100644 --- a/searchlib/src/vespa/searchlib/expression/relevancenode.h +++ b/searchlib/src/vespa/searchlib/expression/relevancenode.h @@ -3,8 +3,7 @@ #include "floatresultnode.h" -namespace search { -namespace expression { +namespace search::expression { class RelevanceNode : public ExpressionNode { @@ -22,5 +21,3 @@ private: }; } -} - |