diff --git a/include/loki/common/collections.hpp b/include/loki/common/collections.hpp index 7ff26cad..100ca236 100644 --- a/include/loki/common/collections.hpp +++ b/include/loki/common/collections.hpp @@ -31,8 +31,8 @@ namespace loki { /// @param vec /// @return template -std::vector get_sorted_vector(const Collection& collection) { - std::vector result(collection.begin(), collection.end()); +boost::container::small_vector get_sorted_vector(const Collection& collection) { + boost::container::small_vector result(collection.begin(), collection.end()); std::sort(result.begin(), result.end()); return result; } diff --git a/include/loki/common/pddl/garbage_collected_factory.hpp b/include/loki/common/pddl/garbage_collected_factory.hpp index 5aca59ed..6f82196e 100644 --- a/include/loki/common/pddl/garbage_collected_factory.hpp +++ b/include/loki/common/pddl/garbage_collected_factory.hpp @@ -75,7 +75,7 @@ class GarbageCollectedFactory { auto& t_cache = std::get>(m_cache->data); int identifier = m_cache->count; auto key = T(identifier, args...); - const auto& [it, inserted] = t_cache.uniqueness.insert(key); + const auto [it, inserted] = t_cache.uniqueness.insert(key); if (!inserted) { assert(t_cache.identifier_to_object.count(it->get_identifier())); return t_cache.identifier_to_object.at(it->get_identifier()).lock(); diff --git a/include/loki/common/pddl/persistent_factory.hpp b/include/loki/common/pddl/persistent_factory.hpp index 78810cf0..c82482db 100644 --- a/include/loki/common/pddl/persistent_factory.hpp +++ b/include/loki/common/pddl/persistent_factory.hpp @@ -68,7 +68,7 @@ class PersistentFactory { /// @brief Returns a pointer to an existing object /// or creates it before if it does not exist.v template - [[nodiscard]] HolderType const* get_or_create(Args... args) { + [[nodiscard]] HolderType const* get_or_create(Args&&... args) { std::lock_guard hold(m_mutex); /* Construct and insert the element in persistent memory. */ size_t identifier = m_count; @@ -76,26 +76,18 @@ class PersistentFactory { assert((identifier == (m_persistent_vector.size()-1)) || (identifier == m_persistent_vector.size())); // Explicitly call the constructor of T to give exclusive access to the factory. - auto element = HolderType(std::move(SubType(identifier, args...))); + auto element = HolderType(std::move(SubType(identifier, std::forward(args)...))); bool overwrite_last_element = (identifier == m_persistent_vector.size() - 1); // The pointer to the location in persistent memory. - const auto* element_ptr = static_cast(nullptr); - if (overwrite_last_element) { - const auto& persistent_element = m_persistent_vector[identifier] = std::move(element); - element_ptr = &persistent_element; - } else { - const auto& persistent_element = m_persistent_vector.push_back(std::move(element)); - element_ptr = &persistent_element; - } - if (!element_ptr) { - throw std::runtime_error("element_ptr is nullptr"); - } + const auto* element_ptr = overwrite_last_element + ? &(m_persistent_vector[identifier] = std::move(element)) + : &(m_persistent_vector.push_back(std::move(element))); assert(element_ptr); /* Test for uniqueness */ auto it = m_uniqueness_set.find(element_ptr); if (it == m_uniqueness_set.end()) { // Element is unique! - m_uniqueness_set.insert(element_ptr); + m_uniqueness_set.emplace(element_ptr); // Validate the element by increasing the identifier to the next free position ++m_count; assert(m_uniqueness_set.size() == m_count); diff --git a/include/loki/domain/pddl/declarations.hpp b/include/loki/domain/pddl/declarations.hpp index ad71d5b6..1ac23faa 100644 --- a/include/loki/domain/pddl/declarations.hpp +++ b/include/loki/domain/pddl/declarations.hpp @@ -33,8 +33,8 @@ namespace loki::pddl { class TypeImpl; using Type = const TypeImpl*; - //using TypeList = boost::container::small_vector; // often single type - using TypeList = std::vector; + using TypeList = boost::container::small_vector; // often single type + //using TypeList = std::vector; class ObjectImpl; using Object = const ObjectImpl*; @@ -49,8 +49,8 @@ namespace loki::pddl { class TermVariableImpl; using TermImpl = std::variant; using Term = const TermImpl*; - //using TermList = boost::container::small_vector; // often unary and binary predicates - using TermList = std::vector; + using TermList = boost::container::small_vector; // often unary and binary predicates + //using TermList = std::vector; class AtomImpl; using Atom = const AtomImpl*; @@ -58,8 +58,8 @@ namespace loki::pddl { class ParameterImpl; using Parameter = const ParameterImpl*; - //using ParameterList = boost::container::small_vector; // often actions, quantifiers with few parameters - using ParameterList = std::vector; + using ParameterList = boost::container::small_vector; // often actions, quantifiers with few parameters + //using ParameterList = std::vector; using ParameterAssignment = std::unordered_map; class PredicateImpl; diff --git a/include/loki/domain/pddl/object.hpp b/include/loki/domain/pddl/object.hpp index d9fb6598..9db15e17 100644 --- a/include/loki/domain/pddl/object.hpp +++ b/include/loki/domain/pddl/object.hpp @@ -37,7 +37,7 @@ class ObjectImpl : public Base { std::string m_name; TypeList m_types; - ObjectImpl(int identifier, std::string name, TypeList types); + ObjectImpl(int identifier, std::string name, TypeList types={}); // Give access to the constructor. template diff --git a/include/loki/domain/pddl/type.hpp b/include/loki/domain/pddl/type.hpp index 3da5e7bd..b4eda99b 100644 --- a/include/loki/domain/pddl/type.hpp +++ b/include/loki/domain/pddl/type.hpp @@ -37,7 +37,7 @@ class TypeImpl : public Base { std::string m_name; TypeList m_bases; - TypeImpl(int identifier, std::string name, TypeList bases); + TypeImpl(int identifier, std::string name, TypeList bases = {}); // Give access to the constructor. template diff --git a/src/domain/pddl/parser/common.cpp b/src/domain/pddl/parser/common.cpp index 693cd241..e9e1b6a9 100644 --- a/src/domain/pddl/parser/common.cpp +++ b/src/domain/pddl/parser/common.cpp @@ -54,7 +54,7 @@ pddl::Term TermDeclarationTermVisitor::operator()(const ast::Name& node) const { } // Constant are not tracked and hence must not be untracked. // Construct Term and return it - const auto& [constant, _position, _error_handler] = binding.value(); + const auto [constant, _position, _error_handler] = binding.value(); const auto term = context.factories.terms.get_or_create(constant); // Add position of PDDL object context.positions.push_back(term, node); @@ -68,7 +68,7 @@ pddl::Term TermDeclarationTermVisitor::operator()(const ast::Variable& node) con const auto binding = context.scopes.get(variable->get_name()); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); - const auto& [_constant, position, error_handler] = binding.value(); + const auto [_constant, position, error_handler] = binding.value(); assert(position.has_value()); const auto message_2 = error_handler(position.value(), "First defined here:"); throw MultiDefinitionVariableError(variable->get_name(), message_1 + message_2); @@ -94,7 +94,7 @@ pddl::Term TermReferenceTermVisitor::operator()(const ast::Name& node) const { throw UndefinedConstantError(object_name, context.scopes.get_error_handler()(node, "")); } // Construct Term and return it - const auto& [object, _position, _error_handler] = binding.value(); + const auto [object, _position, _error_handler] = binding.value(); context.references.untrack(object); const auto term = context.factories.terms.get_or_create(object); // Add position of PDDL object diff --git a/src/domain/pddl/parser/constants.cpp b/src/domain/pddl/parser/constants.cpp index 70fa95a8..982b64d9 100644 --- a/src/domain/pddl/parser/constants.cpp +++ b/src/domain/pddl/parser/constants.cpp @@ -33,7 +33,7 @@ static void test_multiple_definition(const pddl::Object& constant, const domain: if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); auto message_2 = std::string(""); - const auto& [_object, position, error_handler] = binding.value(); + const auto [_object, position, error_handler] = binding.value(); if (position.has_value()) { message_2 = error_handler(position.value(), "First defined here:"); } @@ -78,8 +78,7 @@ pddl::ObjectList ConstantListVisitor::operator()(const std::vector& n // std::vector has single base type "object" assert(context.scopes.get("object").has_value()); const auto [type, _position, _error_handler] = context.scopes.get("object").value(); - const auto type_list = pddl::TypeList{type}; - return parse_constant_definitions(name_nodes, type_list, context); + return parse_constant_definitions(name_nodes, pddl::TypeList{type}, context); } pddl::ObjectList ConstantListVisitor::operator()(const ast::TypedListOfNamesRecursively& typed_list_of_names_recursively_node) { diff --git a/src/domain/pddl/parser/effects.cpp b/src/domain/pddl/parser/effects.cpp index b79c2062..d9ad2b6d 100644 --- a/src/domain/pddl/parser/effects.cpp +++ b/src/domain/pddl/parser/effects.cpp @@ -85,7 +85,7 @@ pddl::Effect parse(const domain::ast::EffectProductionNumericFluentTotalCost& no if (!binding.has_value()) { throw UndefinedFunctionSkeletonError(function_name, context.scopes.get_error_handler()(node.function_symbol_total_cost, "")); } - const auto& [function_skeleton, _position, _error_handler] = binding.value(); + const auto [function_skeleton, _position, _error_handler] = binding.value(); const auto function = context.factories.functions.get_or_create(function_skeleton, pddl::TermList{}); context.references.untrack(function->get_function_skeleton()); const auto function_expression = boost::apply_visitor(FunctionExpressionVisitor(context), node.numeric_term); diff --git a/src/domain/pddl/parser/functions.cpp b/src/domain/pddl/parser/functions.cpp index 9a14466d..e06d0f99 100644 --- a/src/domain/pddl/parser/functions.cpp +++ b/src/domain/pddl/parser/functions.cpp @@ -121,7 +121,7 @@ pddl::FunctionSkeleton parse_function_skeleton_reference(const domain::ast::Func if (!binding.has_value()) { throw UndefinedFunctionSkeletonError(function_name, context.scopes.get_error_handler()(node, "")); } - const auto& [function_skeleton, _position, _error_handler] = binding.value(); + const auto [function_skeleton, _position, _error_handler] = binding.value(); context.references.untrack(function_skeleton); return function_skeleton; } @@ -133,7 +133,7 @@ static void test_multiple_definition(const pddl::FunctionSkeleton& function_skel if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); auto message_2 = std::string(""); - const auto& [_function_skeleton, position, error_handler] = binding.value(); + const auto [_function_skeleton, position, error_handler] = binding.value(); if (position.has_value()) { message_2 = error_handler(position.value(), "First defined here:"); } @@ -163,7 +163,7 @@ pddl::FunctionSkeleton parse(const domain::ast::AtomicFunctionSkeletonTotalCost& } assert(context.scopes.get("number").has_value()); - const auto& [type, _position, _error_handler] = context.scopes.get("number").value(); + const auto [type, _position, _error_handler] = context.scopes.get("number").value(); auto function_name = parse(node.function_symbol.name); auto function_skeleton = context.factories.function_skeletons.get_or_create(function_name, pddl::ParameterList{}, type); @@ -184,7 +184,7 @@ pddl::FunctionSkeleton parse(const domain::ast::AtomicFunctionSkeletonGeneral& n context.scopes.close_scope(); assert(context.scopes.get("number").has_value()); - const auto& [type, _position, _error_handler] = context.scopes.get("number").value(); + const auto [type, _position, _error_handler] = context.scopes.get("number").value(); auto function_name = parse(node.function_symbol.name); auto function_skeleton = context.factories.function_skeletons.get_or_create(function_name, function_parameters, type); diff --git a/src/domain/pddl/parser/literal.cpp b/src/domain/pddl/parser/literal.cpp index c8530ce5..683af764 100644 --- a/src/domain/pddl/parser/literal.cpp +++ b/src/domain/pddl/parser/literal.cpp @@ -33,7 +33,7 @@ pddl::Atom parse(const domain::ast::AtomicFormulaOfTermsPredicate& node, Context for (const auto& term_node : node.terms) { term_list.push_back(boost::apply_visitor(TermReferenceTermVisitor(context), term_node)); } - const auto& [predicate, _position, _error_handler] = binding.value(); + const auto [predicate, _position, _error_handler] = binding.value(); if (predicate->get_parameters().size() != term_list.size()) { throw MismatchedPredicateTermListError(predicate, term_list, context.scopes.get_error_handler()(node, "")); } @@ -50,7 +50,7 @@ pddl::Atom parse(const domain::ast::AtomicFormulaOfTermsEquality& node, Context& } context.references.untrack(pddl::RequirementEnum::EQUALITY); assert(context.scopes.get("=").has_value()); - const auto& [equal_predicate, _position, _error_handler] = context.scopes.get("=").value(); + const auto [equal_predicate, _position, _error_handler] = context.scopes.get("=").value(); auto left_term = boost::apply_visitor(TermReferenceTermVisitor(context), node.term_left); auto right_term = boost::apply_visitor(TermReferenceTermVisitor(context), node.term_right); const auto atom = context.factories.atoms.get_or_create( diff --git a/src/domain/pddl/parser/parameters.cpp b/src/domain/pddl/parser/parameters.cpp index 3a2ee9c9..402cdb96 100644 --- a/src/domain/pddl/parser/parameters.cpp +++ b/src/domain/pddl/parser/parameters.cpp @@ -33,7 +33,7 @@ static void test_multiple_definition(const pddl::Variable& variable, const ast:: if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); auto message_2 = std::string(""); - const auto& [_variable, position, error_handler] = binding.value(); + const auto [_variable, position, error_handler] = binding.value(); if (position.has_value()) { message_2 = error_handler(position.value(), "First defined here:"); } diff --git a/src/domain/pddl/parser/predicates.cpp b/src/domain/pddl/parser/predicates.cpp index cceea1a5..989d04f1 100644 --- a/src/domain/pddl/parser/predicates.cpp +++ b/src/domain/pddl/parser/predicates.cpp @@ -30,7 +30,7 @@ static void test_multiple_definition(const pddl::Predicate& predicate, const dom if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); auto message_2 = std::string(""); - const auto& [_predicate, position, error_handler] = binding.value(); + const auto [_predicate, position, error_handler] = binding.value(); if (position.has_value()) { message_2 = error_handler(position.value(), "First defined here:"); } diff --git a/src/domain/pddl/parser/reference_utils.cpp b/src/domain/pddl/parser/reference_utils.cpp index 19a9c5a4..b68936c6 100644 --- a/src/domain/pddl/parser/reference_utils.cpp +++ b/src/domain/pddl/parser/reference_utils.cpp @@ -31,7 +31,7 @@ void track_variable_references(const pddl::ParameterList& parameter_list, Contex void test_variable_references(const pddl::ParameterList& parameter_list, const Context& context) { for (const auto& parameter : parameter_list) { if (context.references.exists(parameter->get_variable())) { - const auto& [variable, position, error_handler] = context.scopes.get(parameter->get_variable()->get_name()).value(); + const auto [variable, position, error_handler] = context.scopes.get(parameter->get_variable()->get_name()).value(); throw UnusedVariableError(variable->get_name(), error_handler(position.value(), "")); } } @@ -47,7 +47,7 @@ void track_predicate_references(const pddl::PredicateList& predicate_list, Conte void test_predicate_references(const pddl::PredicateList& predicate_list, const Context& context) { for (const auto& predicate : predicate_list) { if (context.references.exists(predicate)) { - const auto& [_predicate, position, error_handler] = context.scopes.get(predicate->get_name()).value(); + const auto [_predicate, position, error_handler] = context.scopes.get(predicate->get_name()).value(); throw UnusedPredicateError(predicate->get_name(), error_handler(position.value(), "")); } } @@ -63,7 +63,7 @@ void track_function_skeleton_references(const pddl::FunctionSkeletonList& functi void test_function_skeleton_references(const pddl::FunctionSkeletonList& function_skeleton_list, const Context& context) { for (const auto& function_skeleton : function_skeleton_list) { if (context.references.exists(function_skeleton)) { - const auto& [_function_skeleton, position, error_handler] = context.scopes.get(function_skeleton->get_name()).value(); + const auto [_function_skeleton, position, error_handler] = context.scopes.get(function_skeleton->get_name()).value(); throw UnusedFunctionSkeletonError(function_skeleton->get_name(), error_handler(position.value(), "")); } } diff --git a/src/domain/pddl/parser/types.cpp b/src/domain/pddl/parser/types.cpp index 8f8ac0ba..c1a5c8fb 100644 --- a/src/domain/pddl/parser/types.cpp +++ b/src/domain/pddl/parser/types.cpp @@ -68,14 +68,14 @@ TypeReferenceTypeVisitor::TypeReferenceTypeVisitor(const Context& context_) pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::TypeObject&) { const auto binding = context.scopes.get("object"); assert(binding.has_value()); - const auto& [type, _position, _error_handler] = binding.value(); + const auto [type, _position, _error_handler] = binding.value(); return {type}; } pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::TypeNumber&) { const auto binding = context.scopes.get("number"); assert(binding.has_value()); - const auto& [type, _position, _error_handler] = binding.value(); + const auto [type, _position, _error_handler] = binding.value(); return {type}; } @@ -85,7 +85,7 @@ pddl::TypeList TypeReferenceTypeVisitor::operator()(const domain::ast::Name& nod if (!binding.has_value()) { throw UndefinedTypeError(name, context.scopes.get_error_handler()(node, "")); } - const auto& [type, _position, _error_handler] = binding.value(); + const auto [type, _position, _error_handler] = binding.value(); context.positions.push_back(type, node); return { type }; } @@ -107,7 +107,7 @@ static void test_multiple_definition(const pddl::Type& type, const domain::ast:: if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); auto message_2 = std::string(""); - const auto& [_type, position, error_handler] = binding.value(); + const auto [_type, position, error_handler] = binding.value(); if (position.has_value()) { message_2 = error_handler(position.value(), "First defined here:"); } @@ -159,9 +159,8 @@ TypeDeclarationTypedListOfNamesVisitor::TypeDeclarationTypedListOfNamesVisitor(C pddl::TypeList TypeDeclarationTypedListOfNamesVisitor::operator()(const std::vector& name_nodes) { // std::vector has single base type "object" assert(context.scopes.get("object").has_value()); - const auto& [type_object, _position, _error_handler] = context.scopes.get("object").value(); - const auto base_types = pddl::TypeList{type_object}; - const auto type_list = parse_type_definitions(name_nodes, base_types, context); + const auto [type_object, _position, _error_handler] = context.scopes.get("object").value(); + const auto type_list = parse_type_definitions(name_nodes, pddl::TypeList{type_object}, context); return type_list; } diff --git a/src/problem/pddl/parser/literal.cpp b/src/problem/pddl/parser/literal.cpp index 3cbc4fca..ef7084c7 100644 --- a/src/problem/pddl/parser/literal.cpp +++ b/src/problem/pddl/parser/literal.cpp @@ -38,7 +38,7 @@ pddl::GroundAtom parse(const problem::ast::AtomicFormulaOfNamesPredicate& node, for (const auto& name_node : node.names) { object_list.push_back(parse_object_reference(name_node, context)); } - const auto& [predicate, _position, _error_handler] = binding.value(); + const auto [predicate, _position, _error_handler] = binding.value(); if (predicate->get_parameters().size() != object_list.size()) { throw MismatchedPredicateObjectListError(predicate, object_list, context.scopes.get_error_handler()(node, "")); } @@ -52,7 +52,7 @@ pddl::GroundAtom parse(const problem::ast::AtomicFormulaOfNamesEquality& node, C throw UndefinedRequirementError(pddl::RequirementEnum::EQUALITY, context.scopes.get_error_handler()(node, "")); } assert(context.scopes.get("=").has_value()); - const auto& [equal_predicate, _position, _error_handler] = context.scopes.get("=").value(); + const auto [equal_predicate, _position, _error_handler] = context.scopes.get("=").value(); const auto object_left = parse_object_reference(node.name_left, context); const auto object_right = parse_object_reference(node.name_right, context); const auto atom = context.factories.ground_atoms.get_or_create(equal_predicate, pddl::ObjectList{object_left, object_right}); diff --git a/src/problem/pddl/parser/objects.cpp b/src/problem/pddl/parser/objects.cpp index e928090f..3303f624 100644 --- a/src/problem/pddl/parser/objects.cpp +++ b/src/problem/pddl/parser/objects.cpp @@ -40,7 +40,7 @@ pddl::Object parse_object_reference(const domain::ast::Name& name_node, Context& if (!binding.has_value()) { throw UndefinedObjectError(name, context.scopes.get_error_handler()(name_node, "")); } - const auto& [object, _position, _error_handler] = binding.value(); + const auto [object, _position, _error_handler] = binding.value(); context.positions.push_back(object, name_node); context.references.untrack(object); return object; @@ -52,7 +52,7 @@ static void test_multiple_definition_object(const std::string& object_name, cons if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(name_node, "Defined here:"); auto message_2 = std::string(""); - const auto& [_object, position, error_handler] = binding.value(); + const auto [_object, position, error_handler] = binding.value(); if (position.has_value()) { message_2 = error_handler(position.value(), "First defined here:"); } @@ -83,7 +83,7 @@ static pddl::ObjectList parse_object_definitions(const std::vector& name_nodes) { // std::vector has single base type "object" assert(context.scopes.get("object").has_value()); - const auto& [type, _position, _error_handler] = context.scopes.get("object").value(); + const auto [type, _position, _error_handler] = context.scopes.get("object").value(); auto object_list = parse_object_definitions(name_nodes, pddl::TypeList{type}, context); return object_list; } diff --git a/src/problem/pddl/parser/reference_utils.cpp b/src/problem/pddl/parser/reference_utils.cpp index a10c4415..a0cc761f 100644 --- a/src/problem/pddl/parser/reference_utils.cpp +++ b/src/problem/pddl/parser/reference_utils.cpp @@ -31,7 +31,7 @@ void track_object_references(const pddl::ObjectList& object_list, Context& conte void test_object_references(const pddl::ObjectList& object_list, const Context& context) { for (const auto& object : object_list) { if (context.references.exists(object)) { - const auto& [_object, position, error_handler] = context.scopes.get(object->get_name()).value(); + const auto [_object, position, error_handler] = context.scopes.get(object->get_name()).value(); throw UnusedObjectError(object->get_name(), error_handler(position.value(), "")); } }