diff --git a/include/loki/details/pddl/action.hpp b/include/loki/details/pddl/action.hpp index a6b81ca..0b86695 100644 --- a/include/loki/details/pddl/action.hpp +++ b/include/loki/details/pddl/action.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_ACTION_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include #include @@ -62,6 +64,18 @@ class ActionImpl const std::optional& get_effect() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ActionImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ActionImpl* l, const ActionImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const ActionImpl& element); } diff --git a/include/loki/details/pddl/atom.hpp b/include/loki/details/pddl/atom.hpp index 3226dcb..9f4259b 100644 --- a/include/loki/details/pddl/atom.hpp +++ b/include/loki/details/pddl/atom.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_ATOM_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -49,6 +51,18 @@ class AtomImpl const TermList& get_terms() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const AtomImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const AtomImpl* l, const AtomImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const AtomImpl& element); } diff --git a/include/loki/details/pddl/axiom.hpp b/include/loki/details/pddl/axiom.hpp index e8e5748..57cf390 100644 --- a/include/loki/details/pddl/axiom.hpp +++ b/include/loki/details/pddl/axiom.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_DERIVED_PREDICATE_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -53,6 +55,18 @@ class AxiomImpl size_t get_num_parameters_to_ground_head() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const AxiomImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const AxiomImpl* l, const AxiomImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const AxiomImpl& element); } diff --git a/include/loki/details/pddl/conditions.hpp b/include/loki/details/pddl/conditions.hpp index 72bca4e..c2b1690 100644 --- a/include/loki/details/pddl/conditions.hpp +++ b/include/loki/details/pddl/conditions.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_CONDITIONS_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -199,6 +201,102 @@ class ConditionForallImpl const Condition& get_condition() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionLiteralImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionAndImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionOrImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionNotImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionImplyImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionExistsImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionForallImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ConditionImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionLiteralImpl& l, const ConditionLiteralImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionAndImpl& l, const ConditionAndImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionOrImpl& l, const ConditionOrImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionNotImpl& l, const ConditionNotImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionImplyImpl& l, const ConditionImplyImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionExistsImpl& l, const ConditionExistsImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionForallImpl& l, const ConditionForallImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ConditionImpl* l, const ConditionImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const ConditionLiteralImpl& element); extern std::ostream& operator<<(std::ostream& out, const ConditionAndImpl& element); extern std::ostream& operator<<(std::ostream& out, const ConditionOrImpl& element); diff --git a/include/loki/details/pddl/domain.hpp b/include/loki/details/pddl/domain.hpp index b9bad81..c16c6ea 100644 --- a/include/loki/details/pddl/domain.hpp +++ b/include/loki/details/pddl/domain.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_DOMAIN_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include "loki/details/utils/filesystem.hpp" #include @@ -75,6 +77,18 @@ class DomainImpl const AxiomList& get_axioms() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const DomainImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const DomainImpl* l, const DomainImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const DomainImpl& element); } diff --git a/include/loki/details/pddl/effects.hpp b/include/loki/details/pddl/effects.hpp index d019a7e..dd3515a 100644 --- a/include/loki/details/pddl/effects.hpp +++ b/include/loki/details/pddl/effects.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_EFFECTS_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -164,6 +166,78 @@ class EffectConditionalWhenImpl const Effect& get_effect() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const EffectLiteralImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const EffectAndImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const EffectNumericImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const EffectConditionalForallImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const EffectConditionalWhenImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const EffectImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const EffectLiteralImpl& l, const EffectLiteralImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const EffectAndImpl& l, const EffectAndImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const EffectNumericImpl& l, const EffectNumericImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const EffectConditionalForallImpl& l, const EffectConditionalForallImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const EffectConditionalWhenImpl& l, const EffectConditionalWhenImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const EffectImpl* l, const EffectImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const EffectLiteralImpl& element); extern std::ostream& operator<<(std::ostream& out, const EffectAndImpl& element); extern std::ostream& operator<<(std::ostream& out, const EffectNumericImpl& element); diff --git a/include/loki/details/pddl/equal_to.hpp b/include/loki/details/pddl/equal_to.hpp new file mode 100644 index 0000000..fd82788 --- /dev/null +++ b/include/loki/details/pddl/equal_to.hpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 Dominik Drexler and Simon Stahlberg + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *< + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LOKI_INCLUDE_LOKI_PDDL_EQUAL_TO_HPP_ +#define LOKI_INCLUDE_LOKI_PDDL_EQUAL_TO_HPP_ + +#include "loki/details/pddl/declarations.hpp" +#include "loki/details/utils/concepts.hpp" + +#include +#include + +namespace loki +{ + +/// @brief `UniquePDDLEqualTo` is a comparator that should be implemented to work in the context where +/// all objects of a custom type `T` are uniquely created and uncopieable. +/// This ensures that pointers can simply be compared and do not need to be dereferenced. +/// @tparam T the type of the object to compare. +template +struct UniquePDDLEqualTo +{ + bool operator()(const T& l, const T& r) const { return std::equal_to()(l, r); } +}; + +/// Spezialization for std::variant. +template +struct UniquePDDLEqualTo> +{ + bool operator()(const std::variant& l, const std::variant& r) const + { + if (l.index() != r.index()) + { + return false; // Different types held + } + // Compare the held values, but only if they are of the same type + return std::visit( + [](const auto& lhs, const auto& rhs) -> bool + { + if constexpr (std::is_same_v) + { + return UniquePDDLEqualTo()(lhs, rhs); + } + else + { + return false; // Different types can't be equal + } + }, + l, + r); + } +}; + +} + +#endif diff --git a/include/loki/details/pddl/factories.hpp b/include/loki/details/pddl/factories.hpp index 1b6c46e..23fa4b3 100644 --- a/include/loki/details/pddl/factories.hpp +++ b/include/loki/details/pddl/factories.hpp @@ -25,9 +25,11 @@ #include "loki/details/pddl/declarations.hpp" #include "loki/details/pddl/domain.hpp" #include "loki/details/pddl/effects.hpp" +#include "loki/details/pddl/equal_to.hpp" #include "loki/details/pddl/function.hpp" #include "loki/details/pddl/function_expressions.hpp" #include "loki/details/pddl/function_skeleton.hpp" +#include "loki/details/pddl/hash.hpp" #include "loki/details/pddl/literal.hpp" #include "loki/details/pddl/metric.hpp" #include "loki/details/pddl/numeric_fluent.hpp" @@ -46,26 +48,29 @@ namespace loki { -using RequirementsFactory = UniqueFactory, EqualTo>; -using TypeFactory = UniqueFactory, EqualTo>; -using VariableFactory = UniqueFactory, EqualTo>; -using TermFactory = UniqueFactory, EqualTo>; -using ObjectFactory = UniqueFactory, EqualTo>; -using AtomFactory = UniqueFactory, EqualTo>; -using LiteralFactory = UniqueFactory, EqualTo>; -using ParameterFactory = UniqueFactory, EqualTo>; -using PredicateFactory = UniqueFactory, EqualTo>; -using FunctionExpressionFactory = UniqueFactory, EqualTo>; -using FunctionFactory = UniqueFactory, EqualTo>; -using FunctionSkeletonFactory = UniqueFactory, EqualTo>; -using ConditionFactory = UniqueFactory, EqualTo>; -using EffectFactory = UniqueFactory, EqualTo>; -using ActionFactory = UniqueFactory, EqualTo>; -using AxiomFactory = UniqueFactory, EqualTo>; -using OptimizationMetricFactory = UniqueFactory, EqualTo>; -using NumericFluentFactory = UniqueFactory, EqualTo>; -using DomainFactory = UniqueFactory, EqualTo>; -using ProblemFactory = UniqueFactory, EqualTo>; +using RequirementsFactory = UniqueFactory, UniquePDDLEqualTo>; +using TypeFactory = UniqueFactory, UniquePDDLEqualTo>; +using VariableFactory = UniqueFactory, UniquePDDLEqualTo>; +using TermFactory = UniqueFactory, UniquePDDLEqualTo>; +using ObjectFactory = UniqueFactory, UniquePDDLEqualTo>; +using AtomFactory = UniqueFactory, UniquePDDLEqualTo>; +using LiteralFactory = UniqueFactory, UniquePDDLEqualTo>; +using ParameterFactory = UniqueFactory, UniquePDDLEqualTo>; +using PredicateFactory = UniqueFactory, UniquePDDLEqualTo>; +using FunctionExpressionFactory = + UniqueFactory, UniquePDDLEqualTo>; +using FunctionFactory = UniqueFactory, UniquePDDLEqualTo>; +using FunctionSkeletonFactory = + UniqueFactory, UniquePDDLEqualTo>; +using ConditionFactory = UniqueFactory, UniquePDDLEqualTo>; +using EffectFactory = UniqueFactory, UniquePDDLEqualTo>; +using ActionFactory = UniqueFactory, UniquePDDLEqualTo>; +using AxiomFactory = UniqueFactory, UniquePDDLEqualTo>; +using OptimizationMetricFactory = + UniqueFactory, UniquePDDLEqualTo>; +using NumericFluentFactory = UniqueFactory, UniquePDDLEqualTo>; +using DomainFactory = UniqueFactory, UniquePDDLEqualTo>; +using ProblemFactory = UniqueFactory, UniquePDDLEqualTo>; using VariadicPDDLConstructorFactory = VariadicContainer @@ -49,6 +51,18 @@ class FunctionImpl const TermList& get_terms() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionImpl* l, const FunctionImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const FunctionImpl& element); } diff --git a/include/loki/details/pddl/function_expressions.hpp b/include/loki/details/pddl/function_expressions.hpp index fdc28ea..a110b07 100644 --- a/include/loki/details/pddl/function_expressions.hpp +++ b/include/loki/details/pddl/function_expressions.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_FUNCTION_EXPRESSIONS_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include #include @@ -174,6 +176,78 @@ class FunctionExpressionFunctionImpl const Function& get_function() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionExpressionNumberImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionExpressionBinaryOperatorImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionExpressionMultiOperatorImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionExpressionMinusImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionExpressionFunctionImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionExpressionImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionExpressionNumberImpl& l, const FunctionExpressionNumberImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionExpressionBinaryOperatorImpl& l, const FunctionExpressionBinaryOperatorImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionExpressionMultiOperatorImpl& l, const FunctionExpressionMultiOperatorImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionExpressionMinusImpl& l, const FunctionExpressionMinusImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionExpressionFunctionImpl& l, const FunctionExpressionFunctionImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionExpressionImpl* l, const FunctionExpressionImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionNumberImpl& element); extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionBinaryOperatorImpl& element); extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionMultiOperatorImpl& element); diff --git a/include/loki/details/pddl/function_skeleton.hpp b/include/loki/details/pddl/function_skeleton.hpp index 36cb382..f07dc37 100644 --- a/include/loki/details/pddl/function_skeleton.hpp +++ b/include/loki/details/pddl/function_skeleton.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_FUNCTION_SKELETON_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -51,6 +53,18 @@ class FunctionSkeletonImpl const Type& get_type() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const FunctionSkeletonImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const FunctionSkeletonImpl* l, const FunctionSkeletonImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const FunctionSkeletonImpl& element); } diff --git a/include/loki/details/pddl/hash.hpp b/include/loki/details/pddl/hash.hpp new file mode 100644 index 0000000..a73a76a --- /dev/null +++ b/include/loki/details/pddl/hash.hpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2023 Dominik Drexler and Simon Stahlberg + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *< + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LOKI_INCLUDE_LOKI_PDDL_HASH_HPP_ +#define LOKI_INCLUDE_LOKI_PDDL_HASH_HPP_ + +#include "loki/details/pddl/declarations.hpp" +#include "loki/details/utils/concepts.hpp" +#include "loki/details/utils/unique_factory.hpp" + +#include +#include +#include +#include +#include +#include + +namespace loki +{ + +/// @brief `UniquePDDLHasher` is a hasher that should be implemented to work in the context where +/// all objects of a custom type `T` are uniquely created and uncopieable. +/// This ensures that pointers can simply be hashed and do not need to be dereferenced. +/// @tparam T the type of the object to hash. +template +struct UniquePDDLHasher +{ + size_t operator()(const T& element) const { return std::hash()(element); } +}; + +struct UniquePDDLHashCombiner +{ +public: + template + void operator()(size_t& seed, const T& value) const + { + seed ^= UniquePDDLHasher()(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } + + void operator()(size_t& seed, const std::size_t& value) const { seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2); } + + template + size_t operator()(const Types&... args) const + { + size_t seed = 0; + ((*this)(seed, args), ...); + return seed; + } +}; + +/// Spezialization for std::ranges::forward_range. +template +requires std::ranges::forward_range +struct UniquePDDLHasher +{ + size_t operator()(const ForwardRange& range) const + { + std::size_t aggregated_hash = 0; + for (const auto& item : range) + { + UniquePDDLHashCombiner()(aggregated_hash, item); + } + return aggregated_hash; + } +}; + +/// Spezialization for std::variant. +template +requires IsVariant +struct UniquePDDLHasher +{ + size_t operator()(const Variant& variant) const + { + return std::visit([](const auto& arg) { return UniquePDDLHasher()(arg); }, variant); + } +}; + +} + +#endif diff --git a/include/loki/details/pddl/literal.hpp b/include/loki/details/pddl/literal.hpp index 3bdd64f..2beb5d2 100644 --- a/include/loki/details/pddl/literal.hpp +++ b/include/loki/details/pddl/literal.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_LITERAL_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -49,6 +51,18 @@ class LiteralImpl const Atom& get_atom() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const LiteralImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const LiteralImpl* l, const LiteralImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const LiteralImpl& element); } diff --git a/include/loki/details/pddl/metric.hpp b/include/loki/details/pddl/metric.hpp index 57d613f..f71a2ec 100644 --- a/include/loki/details/pddl/metric.hpp +++ b/include/loki/details/pddl/metric.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_METRIC_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -58,6 +60,18 @@ class OptimizationMetricImpl const FunctionExpression& get_function_expression() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const OptimizationMetricImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const OptimizationMetricImpl* l, const OptimizationMetricImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const OptimizationMetricImpl& element); } diff --git a/include/loki/details/pddl/numeric_fluent.hpp b/include/loki/details/pddl/numeric_fluent.hpp index 6a13437..3445fac 100644 --- a/include/loki/details/pddl/numeric_fluent.hpp +++ b/include/loki/details/pddl/numeric_fluent.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_NUMERIC_FLUENT_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -49,6 +51,18 @@ class NumericFluentImpl double get_number() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const NumericFluentImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const NumericFluentImpl* l, const NumericFluentImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const NumericFluentImpl& element); } diff --git a/include/loki/details/pddl/object.hpp b/include/loki/details/pddl/object.hpp index 10ff1bd..97365a8 100644 --- a/include/loki/details/pddl/object.hpp +++ b/include/loki/details/pddl/object.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_OBJECT_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -49,6 +51,20 @@ class ObjectImpl const TypeList& get_bases() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ObjectImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ObjectImpl* l, const ObjectImpl* r) const; +}; + +extern std::ostream& operator<<(std::ostream& out, const ObjectImpl& element); + } #endif diff --git a/include/loki/details/pddl/parameter.hpp b/include/loki/details/pddl/parameter.hpp index e8af150..c66e62d 100644 --- a/include/loki/details/pddl/parameter.hpp +++ b/include/loki/details/pddl/parameter.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_PARAMETER_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -54,6 +56,18 @@ class ParameterImpl /// by checking whether the types are compatible extern bool is_specialized_parameter(const Parameter& specialized_parameter, const Parameter& generalized_parameter); +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ParameterImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ParameterImpl* l, const ParameterImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const ParameterImpl& element); } diff --git a/include/loki/details/pddl/predicate.hpp b/include/loki/details/pddl/predicate.hpp index 6bc7546..33a3459 100644 --- a/include/loki/details/pddl/predicate.hpp +++ b/include/loki/details/pddl/predicate.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_PREDICATE_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -51,6 +53,18 @@ class PredicateImpl const ParameterList& get_parameters() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const PredicateImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const PredicateImpl* l, const PredicateImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const PredicateImpl& element); } diff --git a/include/loki/details/pddl/problem.hpp b/include/loki/details/pddl/problem.hpp index 861ab0c..5b4538d 100644 --- a/include/loki/details/pddl/problem.hpp +++ b/include/loki/details/pddl/problem.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_PROBLEM_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include "loki/details/utils/filesystem.hpp" #include @@ -80,6 +82,18 @@ class ProblemImpl const AxiomList& get_axioms() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const ProblemImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const ProblemImpl* l, const ProblemImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const ProblemImpl& element); } diff --git a/include/loki/details/pddl/requirements.hpp b/include/loki/details/pddl/requirements.hpp index 6dbd89b..4759271 100644 --- a/include/loki/details/pddl/requirements.hpp +++ b/include/loki/details/pddl/requirements.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_REQUIREMENTS_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include #include @@ -80,6 +82,18 @@ class RequirementsImpl const RequirementEnumSet& get_requirements() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const RequirementsImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const RequirementsImpl* l, const RequirementsImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const RequirementsImpl& element); } diff --git a/include/loki/details/pddl/term.hpp b/include/loki/details/pddl/term.hpp index 3237a1c..2c1827f 100644 --- a/include/loki/details/pddl/term.hpp +++ b/include/loki/details/pddl/term.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_TERM_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -75,6 +77,42 @@ class TermVariableImpl const Variable& get_variable() const; }; +template<> +struct UniquePDDLHasher +{ + size_t operator()(const TermObjectImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const TermVariableImpl& e) const; +}; + +template<> +struct UniquePDDLHasher +{ + size_t operator()(const TermImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const TermObjectImpl& l, const TermObjectImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const TermVariableImpl& l, const TermVariableImpl& r) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const TermImpl* l, const TermImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const TermObjectImpl& element); extern std::ostream& operator<<(std::ostream& out, const TermVariableImpl& element); extern std::ostream& operator<<(std::ostream& out, const TermImpl& element); diff --git a/include/loki/details/pddl/type.hpp b/include/loki/details/pddl/type.hpp index a6cf295..275d7fd 100644 --- a/include/loki/details/pddl/type.hpp +++ b/include/loki/details/pddl/type.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_TYPE_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -55,6 +57,18 @@ extern TypeSet collect_types_from_hierarchy(const TypeList& types); /// @brief Return true iff type is a subtype of or equal to one of the types in parent_types extern bool is_subtype_or_equal(const Type& type, const TypeList& parent_types); +template<> +struct UniquePDDLHasher +{ + size_t operator()(const TypeImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const TypeImpl* l, const TypeImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const TypeImpl& element); } diff --git a/include/loki/details/pddl/variable.hpp b/include/loki/details/pddl/variable.hpp index 1386490..8788ca8 100644 --- a/include/loki/details/pddl/variable.hpp +++ b/include/loki/details/pddl/variable.hpp @@ -19,6 +19,8 @@ #define LOKI_INCLUDE_LOKI_PDDL_VARIABLE_HPP_ #include "loki/details/pddl/declarations.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include @@ -49,6 +51,18 @@ class VariableImpl extern VariableSet collect_free_variables(const loki::ConditionImpl& condition); +template<> +struct UniquePDDLHasher +{ + size_t operator()(const VariableImpl* e) const; +}; + +template<> +struct UniquePDDLEqualTo +{ + bool operator()(const VariableImpl* l, const VariableImpl* r) const; +}; + extern std::ostream& operator<<(std::ostream& out, const VariableImpl& element); } diff --git a/include/loki/details/utils/equal_to.hpp b/include/loki/details/utils/equal_to.hpp deleted file mode 100644 index ec07538..0000000 --- a/include/loki/details/utils/equal_to.hpp +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (C) 2023 Dominik Drexler and Simon Stahlberg - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - *< - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef LOKI_INCLUDE_LOKI_UTILS_EQUAL_TO_HPP_ -#define LOKI_INCLUDE_LOKI_UTILS_EQUAL_TO_HPP_ - -#include "loki/details/pddl/declarations.hpp" -#include "loki/details/utils/concepts.hpp" - -#include - -namespace loki -{ - -/// @brief `EqualTo` is a comparator that should be implemented to work in the context where -/// all objects of a custom type `T` are uniquely created and uncopieable. -/// This ensures that pointers can simply be compared and do not need to be dereferenced. -/// @tparam T the type of the object to compare. -template -struct EqualTo -{ - bool operator()(const T& l, const T& r) const { return std::equal_to()(l, r); } -}; - -/// Spezialization for std::variant. -template -struct EqualTo> -{ - bool operator()(const std::variant& l, const std::variant& r) const - { - if (l.index() != r.index()) - { - return false; // Different types held - } - // Compare the held values, but only if they are of the same type - return std::visit( - [](const auto& lhs, const auto& rhs) -> bool - { - if constexpr (std::is_same_v) - { - return EqualTo()(lhs, rhs); - } - else - { - return false; // Different types can't be equal - } - }, - l, - r); - } -}; - -/** - * Specializations for PDDL - */ - -template<> -struct EqualTo -{ - bool operator()(const ActionImpl* l, const ActionImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const AtomImpl* l, const AtomImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const AxiomImpl* l, const AxiomImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionLiteralImpl& l, const ConditionLiteralImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionAndImpl& l, const ConditionAndImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionOrImpl& l, const ConditionOrImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionNotImpl& l, const ConditionNotImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionImplyImpl& l, const ConditionImplyImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionExistsImpl& l, const ConditionExistsImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionForallImpl& l, const ConditionForallImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ConditionImpl* l, const ConditionImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const DomainImpl* l, const DomainImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const EffectLiteralImpl& l, const EffectLiteralImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const EffectAndImpl& l, const EffectAndImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const EffectNumericImpl& l, const EffectNumericImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const EffectConditionalForallImpl& l, const EffectConditionalForallImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const EffectConditionalWhenImpl& l, const EffectConditionalWhenImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const EffectImpl* l, const EffectImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const FunctionExpressionNumberImpl& l, const FunctionExpressionNumberImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const FunctionExpressionBinaryOperatorImpl& l, const FunctionExpressionBinaryOperatorImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const FunctionExpressionMultiOperatorImpl& l, const FunctionExpressionMultiOperatorImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const FunctionExpressionMinusImpl& l, const FunctionExpressionMinusImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const FunctionExpressionFunctionImpl& l, const FunctionExpressionFunctionImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const FunctionExpressionImpl* l, const FunctionExpressionImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const FunctionImpl* l, const FunctionImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const LiteralImpl* l, const LiteralImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const OptimizationMetricImpl* l, const OptimizationMetricImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const NumericFluentImpl* l, const NumericFluentImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ObjectImpl* l, const ObjectImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ParameterImpl* l, const ParameterImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const PredicateImpl* l, const PredicateImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const ProblemImpl* l, const ProblemImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const RequirementsImpl* l, const RequirementsImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const TermObjectImpl& l, const TermObjectImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const TermVariableImpl& l, const TermVariableImpl& r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const TermImpl* l, const TermImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const TypeImpl* l, const TypeImpl* r) const; -}; - -template<> -struct EqualTo -{ - bool operator()(const VariableImpl* l, const VariableImpl* r) const; -}; -} - -#endif diff --git a/include/loki/details/utils/hash.hpp b/include/loki/details/utils/hash.hpp deleted file mode 100644 index d9ec29c..0000000 --- a/include/loki/details/utils/hash.hpp +++ /dev/null @@ -1,331 +0,0 @@ -/* - * Copyright (C) 2023 Dominik Drexler and Simon Stahlberg - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - *< - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef LOKI_INCLUDE_LOKI_UTILS_HASH_HPP_ -#define LOKI_INCLUDE_LOKI_UTILS_HASH_HPP_ - -#include "loki/details/pddl/declarations.hpp" -#include "loki/details/utils/concepts.hpp" - -#include -#include -#include -#include -#include -#include - -namespace loki -{ - -/// @brief `Hasher` is a hasher that should be implemented to work in the context where -/// all objects of a custom type `T` are uniquely created and uncopieable. -/// This ensures that pointers can simply be hashed and do not need to be dereferenced. -/// @tparam T the type of the object to hash. -template -struct Hasher -{ - size_t operator()(const T& element) const { return std::hash()(element); } -}; - -struct HashCombiner -{ -public: - template - void operator()(size_t& seed, const T& value) const - { - seed ^= Hasher()(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2); - } - - void operator()(size_t& seed, const std::size_t& value) const { seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2); } - - template - size_t operator()(const Types&... args) const - { - size_t seed = 0; - ((*this)(seed, args), ...); - return seed; - } -}; - -/// Spezialization for std::ranges::forward_range. -template -requires std::ranges::forward_range -struct Hasher -{ - size_t operator()(const ForwardRange& range) const - { - std::size_t aggregated_hash = 0; - for (const auto& item : range) - { - HashCombiner()(aggregated_hash, item); - } - return aggregated_hash; - } -}; - -/// Spezialization for std::variant. -template -requires IsVariant -struct Hasher -{ - size_t operator()(const Variant& variant) const - { - return std::visit([](const auto& arg) { return Hasher()(arg); }, variant); - } -}; - -/** - * Specializations for PDDL - */ - -template<> -struct Hasher -{ - size_t operator()(const ActionImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const AtomImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const AxiomImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionLiteralImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionAndImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionOrImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionNotImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionImplyImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionExistsImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionForallImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ConditionImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const DomainImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const EffectLiteralImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const EffectAndImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const EffectNumericImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const EffectConditionalForallImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const EffectConditionalWhenImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const EffectImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionExpressionNumberImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionExpressionBinaryOperatorImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionExpressionMultiOperatorImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionExpressionMinusImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionExpressionFunctionImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionExpressionImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionSkeletonImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const FunctionImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const LiteralImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const OptimizationMetricImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const NumericFluentImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ObjectImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ParameterImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const PredicateImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const ProblemImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const RequirementsImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const TermObjectImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const TermVariableImpl& e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const TermImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const TypeImpl* e) const; -}; - -template<> -struct Hasher -{ - size_t operator()(const VariableImpl* e) const; -}; - -} - -#endif diff --git a/include/loki/details/utils/unique_factory.hpp b/include/loki/details/utils/unique_factory.hpp index efec3c8..0c1bc57 100644 --- a/include/loki/details/utils/unique_factory.hpp +++ b/include/loki/details/utils/unique_factory.hpp @@ -18,8 +18,6 @@ #ifndef LOKI_INCLUDE_LOKI_UTILS_UNIQUE_VALUE_FACTORY_HPP_ #define LOKI_INCLUDE_LOKI_UTILS_UNIQUE_VALUE_FACTORY_HPP_ -#include "loki/details/utils/equal_to.hpp" -#include "loki/details/utils/hash.hpp" #include "loki/details/utils/segmented_vector.hpp" #include @@ -36,9 +34,9 @@ namespace loki /// uniqueness checks and SegmentedVector for continuous and cache-efficient storage of value types. /// @tparam HolderType is the holder value type which can be an std::variant. /// Note that using a base class value type will result in object slicing. -/// @tparam Hash the hash function, default uses `Hasher` hasher. -/// @tparam KeyEqual the comparison function, default uses `EqualTo` comparator. -template, typename KeyEqual = EqualTo> +/// @tparam Hash the hash function. +/// @tparam KeyEqual the comparison function. +template class UniqueFactory { private: @@ -68,12 +66,6 @@ class UniqueFactory UniqueFactory& operator=(UniqueFactory&& other) = default; /// @brief Returns a pointer to an existing object or creates it before if it does not exist. - /// The `SubType` is not allowed to be copieable because `Hasher` and `EqualTo` - /// is implemented to hash and compare simply the pointers. - /// The `HolderType` is allowed to be copieable but `Hasher` and `EqualTo` - /// will have to take this into account by hashing and comparing the nested `SubType`. - /// For example, if `HolderType` is a std::variant, then `Hasher` and `ShallowEqual` - /// will hash or compare the nested `SubType` object. template requires(!std::is_copy_constructible_v) HolderType const* get_or_create(Args&&... args) { diff --git a/include/loki/loki.hpp b/include/loki/loki.hpp index 6a68e8d..b52f1c7 100644 --- a/include/loki/loki.hpp +++ b/include/loki/loki.hpp @@ -57,9 +57,7 @@ */ #include "loki/details/utils/collections.hpp" -#include "loki/details/utils/equal_to.hpp" #include "loki/details/utils/filesystem.hpp" -#include "loki/details/utils/hash.hpp" #include "loki/details/utils/memory.hpp" #include "loki/details/utils/segmented_vector.hpp" #include "loki/details/utils/unique_factory.hpp" diff --git a/src/pddl/action.cpp b/src/pddl/action.cpp index a0a0a62..f4ce641 100644 --- a/src/pddl/action.cpp +++ b/src/pddl/action.cpp @@ -21,6 +21,7 @@ #include "loki/details/pddl/conditions.hpp" #include "loki/details/pddl/effects.hpp" #include "loki/details/pddl/parameter.hpp" +#include "loki/details/utils/collections.hpp" namespace loki { @@ -51,6 +52,21 @@ const std::optional& ActionImpl::get_condition() const { return m_con const std::optional& ActionImpl::get_effect() const { return m_effect; } +size_t UniquePDDLHasher::operator()(const ActionImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_name(), get_sorted_vector(e->get_parameters()), e->get_condition(), e->get_effect()); +} + +bool UniquePDDLEqualTo::operator()(const ActionImpl* l, const ActionImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())) + && (l->get_condition() == r->get_condition()) && (l->get_effect() == r->get_effect()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const ActionImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/atom.cpp b/src/pddl/atom.cpp index 8d0b835..cfca0aa 100644 --- a/src/pddl/atom.cpp +++ b/src/pddl/atom.cpp @@ -31,6 +31,17 @@ const Predicate& AtomImpl::get_predicate() const { return m_predicate; } const TermList& AtomImpl::get_terms() const { return m_terms; } +size_t UniquePDDLHasher::operator()(const AtomImpl* e) const { return UniquePDDLHashCombiner()(e->get_predicate(), e->get_terms()); } + +bool UniquePDDLEqualTo::operator()(const AtomImpl* l, const AtomImpl* r) const +{ + if (&l != &r) + { + return (l->get_predicate() == r->get_predicate()) && (l->get_terms() == r->get_terms()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const AtomImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/axiom.cpp b/src/pddl/axiom.cpp index 4a1ce61..178ed34 100644 --- a/src/pddl/axiom.cpp +++ b/src/pddl/axiom.cpp @@ -23,6 +23,7 @@ #include "loki/details/pddl/literal.hpp" #include "loki/details/pddl/parameter.hpp" #include "loki/details/pddl/predicate.hpp" +#include "loki/details/utils/collections.hpp" namespace loki { @@ -45,6 +46,21 @@ const ParameterList& AxiomImpl::get_parameters() const { return m_parameters; } size_t AxiomImpl::get_num_parameters_to_ground_head() const { return m_num_parameters_to_ground_head; } +size_t UniquePDDLHasher::operator()(const AxiomImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_derived_predicate_name(), get_sorted_vector(e->get_parameters()), e->get_condition()); +} + +bool UniquePDDLEqualTo::operator()(const AxiomImpl* l, const AxiomImpl* r) const +{ + if (&l != &r) + { + return (l->get_derived_predicate_name() == r->get_derived_predicate_name()) + && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())) && (l->get_condition() == r->get_condition()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const AxiomImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/conditions.cpp b/src/pddl/conditions.cpp index 2489b9b..a269345 100644 --- a/src/pddl/conditions.cpp +++ b/src/pddl/conditions.cpp @@ -20,6 +20,7 @@ #include "formatter.hpp" #include "loki/details/pddl/literal.hpp" #include "loki/details/pddl/parameter.hpp" +#include "loki/details/utils/collections.hpp" namespace loki { @@ -94,6 +95,108 @@ const ParameterList& ConditionForallImpl::get_parameters() const { return m_para const Condition& ConditionForallImpl::get_condition() const { return m_condition; } +size_t UniquePDDLHasher::operator()(const ConditionLiteralImpl& e) const { return UniquePDDLHashCombiner()(e.get_literal()); } + +size_t UniquePDDLHasher::operator()(const ConditionAndImpl& e) const +{ + return UniquePDDLHashCombiner()(get_sorted_vector(e.get_conditions())); +} + +size_t UniquePDDLHasher::operator()(const ConditionOrImpl& e) const +{ + return UniquePDDLHashCombiner()(get_sorted_vector(e.get_conditions())); +} + +size_t UniquePDDLHasher::operator()(const ConditionNotImpl& e) const { return UniquePDDLHashCombiner()(e.get_condition()); } + +size_t UniquePDDLHasher::operator()(const ConditionImplyImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_condition_left(), e.get_condition_right()); +} + +size_t UniquePDDLHasher::operator()(const ConditionExistsImpl& e) const +{ + return UniquePDDLHashCombiner()(get_sorted_vector(e.get_parameters()), e.get_condition()); +} + +size_t UniquePDDLHasher::operator()(const ConditionForallImpl& e) const +{ + return UniquePDDLHashCombiner()(get_sorted_vector(e.get_parameters()), e.get_condition()); +} + +size_t UniquePDDLHasher::operator()(const ConditionImpl* e) const +{ + return std::visit([](const auto& arg) { return UniquePDDLHasher()(arg); }, *e); +} + +bool UniquePDDLEqualTo::operator()(const ConditionLiteralImpl& l, const ConditionLiteralImpl& r) const +{ + if (&l != &r) + { + return (l.get_literal() == r.get_literal()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const ConditionAndImpl& l, const ConditionAndImpl& r) const +{ + if (&l != &r) + { + return (get_sorted_vector(l.get_conditions()) == get_sorted_vector(r.get_conditions())); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const ConditionOrImpl& l, const ConditionOrImpl& r) const +{ + if (&l != &r) + { + return (get_sorted_vector(l.get_conditions()) == get_sorted_vector(r.get_conditions())); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const ConditionNotImpl& l, const ConditionNotImpl& r) const +{ + if (&l != &r) + { + return (l.get_condition() == r.get_condition()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const ConditionImplyImpl& l, const ConditionImplyImpl& r) const +{ + if (&l != &r) + { + return (l.get_condition_left() == r.get_condition_left()) && (l.get_condition_left() == r.get_condition_left()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const ConditionExistsImpl& l, const ConditionExistsImpl& r) const +{ + if (&l != &r) + { + return (l.get_condition() == r.get_condition()) && (get_sorted_vector(l.get_parameters()) == get_sorted_vector(r.get_parameters())); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const ConditionForallImpl& l, const ConditionForallImpl& r) const +{ + if (&l != &r) + { + return (l.get_condition() == r.get_condition()) && (get_sorted_vector(l.get_parameters()) == get_sorted_vector(r.get_parameters())); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const ConditionImpl* l, const ConditionImpl* r) const +{ + return UniquePDDLEqualTo()(*l, *r); +} + std::ostream& operator<<(std::ostream& out, const ConditionLiteralImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/domain.cpp b/src/pddl/domain.cpp index dfb66cf..5467561 100644 --- a/src/pddl/domain.cpp +++ b/src/pddl/domain.cpp @@ -20,13 +20,14 @@ #include "formatter.hpp" #include "loki/details/pddl/action.hpp" #include "loki/details/pddl/axiom.hpp" +#include "loki/details/pddl/equal_to.hpp" #include "loki/details/pddl/function_skeleton.hpp" +#include "loki/details/pddl/hash.hpp" #include "loki/details/pddl/object.hpp" #include "loki/details/pddl/predicate.hpp" #include "loki/details/pddl/requirements.hpp" #include "loki/details/pddl/type.hpp" -#include "loki/details/utils/equal_to.hpp" -#include "loki/details/utils/hash.hpp" +#include "loki/details/utils/collections.hpp" #include @@ -77,6 +78,33 @@ const ActionList& DomainImpl::get_actions() const { return m_actions; } const AxiomList& DomainImpl::get_axioms() const { return m_axioms; } +size_t UniquePDDLHasher::operator()(const DomainImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_name(), + e->get_requirements(), + get_sorted_vector(e->get_types()), + get_sorted_vector(e->get_constants()), + get_sorted_vector(e->get_predicates()), + get_sorted_vector(e->get_functions()), + get_sorted_vector(e->get_actions()), + get_sorted_vector(e->get_axioms())); +} + +bool UniquePDDLEqualTo::operator()(const DomainImpl* l, const DomainImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()) && (l->get_requirements() == r->get_requirements()) + && (get_sorted_vector(l->get_types()) == get_sorted_vector(r->get_types())) + && (get_sorted_vector(l->get_constants()) == get_sorted_vector(r->get_constants())) + && (get_sorted_vector(l->get_predicates()) == get_sorted_vector(r->get_predicates())) + && (get_sorted_vector(l->get_functions()) == get_sorted_vector(r->get_functions())) + && (get_sorted_vector(l->get_actions()) == get_sorted_vector(r->get_actions())) + && (get_sorted_vector(l->get_axioms()) == get_sorted_vector(r->get_axioms())); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const DomainImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/effects.cpp b/src/pddl/effects.cpp index a9d8397..9ad029b 100644 --- a/src/pddl/effects.cpp +++ b/src/pddl/effects.cpp @@ -23,6 +23,7 @@ #include "loki/details/pddl/function_expressions.hpp" #include "loki/details/pddl/literal.hpp" #include "loki/details/pddl/parameter.hpp" +#include "loki/details/utils/collections.hpp" #include @@ -99,6 +100,78 @@ const Condition& EffectConditionalWhenImpl::get_condition() const { return m_con const Effect& EffectConditionalWhenImpl::get_effect() const { return m_effect; } +size_t UniquePDDLHasher::operator()(const EffectLiteralImpl& e) const { return UniquePDDLHashCombiner()(e.get_literal()); } + +size_t UniquePDDLHasher::operator()(const EffectAndImpl& e) const { return UniquePDDLHashCombiner()(get_sorted_vector(e.get_effects())); } + +size_t UniquePDDLHasher::operator()(const EffectNumericImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_assign_operator(), e.get_function(), e.get_function_expression()); +} + +size_t UniquePDDLHasher::operator()(const EffectConditionalForallImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_effect(), get_sorted_vector(e.get_parameters())); +} + +size_t UniquePDDLHasher::operator()(const EffectConditionalWhenImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_condition(), e.get_effect()); +} + +size_t UniquePDDLHasher::operator()(const EffectImpl* e) const +{ + return std::visit([](const auto& arg) { return UniquePDDLHasher()(arg); }, *e); +} + +bool UniquePDDLEqualTo::operator()(const EffectLiteralImpl& l, const EffectLiteralImpl& r) const +{ + if (&l != &r) + { + return (l.get_literal() == r.get_literal()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const EffectAndImpl& l, const EffectAndImpl& r) const +{ + if (&l != &r) + { + return (get_sorted_vector(l.get_effects()) == get_sorted_vector(r.get_effects())); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const EffectNumericImpl& l, const EffectNumericImpl& r) const +{ + if (&l != &r) + { + return (l.get_assign_operator() == r.get_assign_operator()) && (l.get_function() == r.get_function()) + && (l.get_function_expression() == r.get_function_expression()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const EffectConditionalForallImpl& l, const EffectConditionalForallImpl& r) const +{ + if (&l != &r) + { + return (l.get_effect() == r.get_effect()) && (get_sorted_vector(l.get_parameters()) == get_sorted_vector(r.get_parameters())); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const EffectConditionalWhenImpl& l, const EffectConditionalWhenImpl& r) const +{ + if (&l != &r) + { + return (l.get_condition() == r.get_condition()) && (l.get_effect() == r.get_effect()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const EffectImpl* l, const EffectImpl* r) const { return UniquePDDLEqualTo()(*l, *r); } + std::ostream& operator<<(std::ostream& out, const EffectLiteralImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/formatter.cpp b/src/pddl/formatter.cpp index e3b4e56..e514ead 100644 --- a/src/pddl/formatter.cpp +++ b/src/pddl/formatter.cpp @@ -17,8 +17,8 @@ #include "formatter.hpp" -#include "loki/details/utils/equal_to.hpp" -#include "loki/details/utils/hash.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include #include @@ -180,7 +180,7 @@ void PDDLFormatter::write(const DomainImpl& element, std::ostream& out) if (!element.get_types().empty()) { out << std::string(m_indent, ' ') << "(:types "; - std::unordered_map> subtypes_by_parent_types; + std::unordered_map> subtypes_by_parent_types; for (const auto& type : element.get_types()) { // We do not want to print root type "object" @@ -223,7 +223,7 @@ void PDDLFormatter::write(const DomainImpl& element, std::ostream& out) if (!element.get_constants().empty()) { out << std::string(m_indent, ' ') << "(:constants "; - std::unordered_map> constants_by_types; + std::unordered_map> constants_by_types; for (const auto& constant : element.get_constants()) { constants_by_types[constant->get_bases()].push_back(constant); @@ -514,7 +514,7 @@ void PDDLFormatter::write(const ProblemImpl& element, std::ostream& out) if (!element.get_objects().empty()) { out << std::string(m_indent, ' ') << "(:objects "; - std::unordered_map> objects_by_types; + std::unordered_map> objects_by_types; for (const auto& object : element.get_objects()) { objects_by_types[object->get_bases()].push_back(object); diff --git a/src/pddl/formatter.hpp b/src/pddl/formatter.hpp index 56d1e55..9d65313 100644 --- a/src/pddl/formatter.hpp +++ b/src/pddl/formatter.hpp @@ -15,8 +15,8 @@ * along with this program. If not, see . */ -#ifndef LOKI_INCLUDE_LOKI_PDDL_FORMATTER_HPP_ -#define LOKI_INCLUDE_LOKI_PDDL_FORMATTER_HPP_ +#ifndef LOKI_SRC_LOKI_PDDL_FORMATTER_HPP_ +#define LOKI_SRC_LOKI_PDDL_FORMATTER_HPP_ #include "loki/details/pddl/action.hpp" #include "loki/details/pddl/atom.hpp" diff --git a/src/pddl/function.cpp b/src/pddl/function.cpp index 16b4c0e..1152519 100644 --- a/src/pddl/function.cpp +++ b/src/pddl/function.cpp @@ -36,6 +36,20 @@ const FunctionSkeleton& FunctionImpl::get_function_skeleton() const { return m_f const TermList& FunctionImpl::get_terms() const { return m_terms; } +size_t UniquePDDLHasher::operator()(const FunctionImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_function_skeleton(), e->get_terms()); +} + +bool UniquePDDLEqualTo::operator()(const FunctionImpl* l, const FunctionImpl* r) const +{ + if (&l != &r) + { + return (l->get_function_skeleton() == r->get_function_skeleton()) && (l->get_terms() == r->get_terms()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const FunctionImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/function_expressions.cpp b/src/pddl/function_expressions.cpp index 62eab3e..ce069ad 100644 --- a/src/pddl/function_expressions.cpp +++ b/src/pddl/function_expressions.cpp @@ -19,6 +19,7 @@ #include "formatter.hpp" #include "loki/details/pddl/function.hpp" +#include "loki/details/utils/collections.hpp" #include @@ -110,6 +111,91 @@ size_t FunctionExpressionFunctionImpl::get_index() const { return m_index; } const Function& FunctionExpressionFunctionImpl::get_function() const { return m_function; } +size_t UniquePDDLHasher::operator()(const FunctionExpressionNumberImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_number()); +} + +size_t UniquePDDLHasher::operator()(const FunctionExpressionBinaryOperatorImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_binary_operator(), e.get_left_function_expression(), e.get_right_function_expression()); +} + +size_t UniquePDDLHasher::operator()(const FunctionExpressionMultiOperatorImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_multi_operator(), get_sorted_vector(e.get_function_expressions())); +} + +size_t UniquePDDLHasher::operator()(const FunctionExpressionMinusImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_function_expression()); +} + +size_t UniquePDDLHasher::operator()(const FunctionExpressionFunctionImpl& e) const +{ + return UniquePDDLHashCombiner()(e.get_function()); +} + +size_t UniquePDDLHasher::operator()(const FunctionExpressionImpl* e) const +{ + return std::visit([](const auto& arg) { return UniquePDDLHasher()(arg); }, *e); +} + +bool UniquePDDLEqualTo::operator()(const FunctionExpressionNumberImpl& l, const FunctionExpressionNumberImpl& r) const +{ + if (&l != &r) + { + return (l.get_number() == r.get_number()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const FunctionExpressionBinaryOperatorImpl& l, + const FunctionExpressionBinaryOperatorImpl& r) const +{ + if (&l != &r) + { + return (l.get_binary_operator() == r.get_binary_operator()) && (l.get_left_function_expression() == r.get_left_function_expression()) + && (l.get_right_function_expression() == r.get_right_function_expression()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const FunctionExpressionMultiOperatorImpl& l, + const FunctionExpressionMultiOperatorImpl& r) const +{ + if (&l != &r) + { + return (l.get_multi_operator() == r.get_multi_operator()) + && (get_sorted_vector(l.get_function_expressions()) == get_sorted_vector(r.get_function_expressions())); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const FunctionExpressionMinusImpl& l, const FunctionExpressionMinusImpl& r) const +{ + if (&l != &r) + { + return (l.get_function_expression() == r.get_function_expression()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const FunctionExpressionFunctionImpl& l, + const FunctionExpressionFunctionImpl& r) const +{ + if (&l != &r) + { + return (l.get_function() == r.get_function()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const FunctionExpressionImpl* l, const FunctionExpressionImpl* r) const +{ + return UniquePDDLEqualTo()(*l, *r); +} + std::ostream& operator<<(std::ostream& out, const FunctionExpressionNumberImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/function_skeleton.cpp b/src/pddl/function_skeleton.cpp index b662d58..73f4768 100644 --- a/src/pddl/function_skeleton.cpp +++ b/src/pddl/function_skeleton.cpp @@ -19,6 +19,7 @@ #include "formatter.hpp" #include "loki/details/pddl/parameter.hpp" +#include "loki/details/utils/collections.hpp" namespace loki { @@ -38,6 +39,21 @@ const ParameterList& FunctionSkeletonImpl::get_parameters() const { return m_par const Type& FunctionSkeletonImpl::get_type() const { return m_type; } +size_t UniquePDDLHasher::operator()(const FunctionSkeletonImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_name(), e->get_type(), get_sorted_vector(e->get_parameters())); +} + +bool UniquePDDLEqualTo::operator()(const FunctionSkeletonImpl* l, const FunctionSkeletonImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()) && (l->get_type() == r->get_type()) + && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const FunctionSkeletonImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/literal.cpp b/src/pddl/literal.cpp index b536e91..46d3968 100644 --- a/src/pddl/literal.cpp +++ b/src/pddl/literal.cpp @@ -30,6 +30,17 @@ bool LiteralImpl::is_negated() const { return m_is_negated; } const Atom& LiteralImpl::get_atom() const { return m_atom; } +size_t UniquePDDLHasher::operator()(const LiteralImpl* e) const { return UniquePDDLHashCombiner()(e->is_negated(), e->get_atom()); } + +bool UniquePDDLEqualTo::operator()(const LiteralImpl* l, const LiteralImpl* r) const +{ + if (&l != &r) + { + return (l->is_negated() == r->is_negated()) && (l->get_atom() == r->get_atom()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const LiteralImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/metric.cpp b/src/pddl/metric.cpp index e4f0d8b..61944a3 100644 --- a/src/pddl/metric.cpp +++ b/src/pddl/metric.cpp @@ -50,6 +50,20 @@ OptimizationMetricEnum OptimizationMetricImpl::get_optimization_metric() const { const FunctionExpression& OptimizationMetricImpl::get_function_expression() const { return m_function_expression; } +size_t UniquePDDLHasher::operator()(const OptimizationMetricImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_optimization_metric(), e->get_function_expression()); +} + +bool UniquePDDLEqualTo::operator()(const OptimizationMetricImpl* l, const OptimizationMetricImpl* r) const +{ + if (&l != &r) + { + return (l->get_optimization_metric() == r->get_optimization_metric()) && (l->get_function_expression() == r->get_function_expression()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const OptimizationMetricImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/numeric_fluent.cpp b/src/pddl/numeric_fluent.cpp index 89d9e58..e1ea651 100644 --- a/src/pddl/numeric_fluent.cpp +++ b/src/pddl/numeric_fluent.cpp @@ -30,6 +30,20 @@ const Function& NumericFluentImpl::get_function() const { return m_function; } double NumericFluentImpl::get_number() const { return m_number; } +size_t UniquePDDLHasher::operator()(const NumericFluentImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_number(), e->get_function()); +} + +bool UniquePDDLEqualTo::operator()(const NumericFluentImpl* l, const NumericFluentImpl* r) const +{ + if (&l != &r) + { + return (l->get_number() == r->get_number()) && (l->get_function() == r->get_function()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const NumericFluentImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/object.cpp b/src/pddl/object.cpp index 8e61fb5..f5eb651 100644 --- a/src/pddl/object.cpp +++ b/src/pddl/object.cpp @@ -19,6 +19,7 @@ #include "formatter.hpp" #include "loki/details/pddl/type.hpp" +#include "loki/details/utils/collections.hpp" namespace loki { @@ -30,4 +31,25 @@ const std::string& ObjectImpl::get_name() const { return m_name; } const TypeList& ObjectImpl::get_bases() const { return m_types; } +size_t UniquePDDLHasher::operator()(const ObjectImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_name(), get_sorted_vector(e->get_bases())); +} + +bool UniquePDDLEqualTo::operator()(const ObjectImpl* l, const ObjectImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_bases()) == get_sorted_vector(r->get_bases())); + } + return true; +} + +std::ostream& operator<<(std::ostream& out, const ObjectImpl& element) +{ + auto formatter = PDDLFormatter(); + formatter.write(element, out); + return out; +} + } diff --git a/src/pddl/parameter.cpp b/src/pddl/parameter.cpp index 7ef63de..856b541 100644 --- a/src/pddl/parameter.cpp +++ b/src/pddl/parameter.cpp @@ -20,6 +20,7 @@ #include "formatter.hpp" #include "loki/details/pddl/type.hpp" #include "loki/details/pddl/variable.hpp" +#include "loki/details/utils/collections.hpp" #include @@ -45,6 +46,20 @@ bool is_specialized_parameter(const Parameter& specialized_parameter, const Para return false; } +size_t UniquePDDLHasher::operator()(const ParameterImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_variable(), get_sorted_vector(e->get_bases())); +} + +bool UniquePDDLEqualTo::operator()(const ParameterImpl* l, const ParameterImpl* r) const +{ + if (&l != &r) + { + return (l->get_variable() == r->get_variable()) && (get_sorted_vector(l->get_bases()) == get_sorted_vector(r->get_bases())); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const ParameterImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/predicate.cpp b/src/pddl/predicate.cpp index bf02f4d..187da6a 100644 --- a/src/pddl/predicate.cpp +++ b/src/pddl/predicate.cpp @@ -21,6 +21,7 @@ #include "loki/details/pddl/parameter.hpp" #include "loki/details/pddl/type.hpp" #include "loki/details/pddl/variable.hpp" +#include "loki/details/utils/collections.hpp" #include @@ -39,6 +40,20 @@ const std::string& PredicateImpl::get_name() const { return m_name; } const ParameterList& PredicateImpl::get_parameters() const { return m_parameters; } +size_t UniquePDDLHasher::operator()(const PredicateImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_name(), get_sorted_vector(e->get_parameters())); +} + +bool UniquePDDLEqualTo::operator()(const PredicateImpl* l, const PredicateImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const PredicateImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/problem.cpp b/src/pddl/problem.cpp index 7b8a459..6f27994 100644 --- a/src/pddl/problem.cpp +++ b/src/pddl/problem.cpp @@ -21,6 +21,8 @@ #include "loki/details/pddl/axiom.hpp" #include "loki/details/pddl/conditions.hpp" #include "loki/details/pddl/domain.hpp" +#include "loki/details/pddl/equal_to.hpp" +#include "loki/details/pddl/hash.hpp" #include "loki/details/pddl/literal.hpp" #include "loki/details/pddl/metric.hpp" #include "loki/details/pddl/numeric_fluent.hpp" @@ -28,8 +30,7 @@ #include "loki/details/pddl/predicate.hpp" #include "loki/details/pddl/requirements.hpp" #include "loki/details/pddl/type.hpp" -#include "loki/details/utils/equal_to.hpp" -#include "loki/details/utils/hash.hpp" +#include "loki/details/utils/collections.hpp" #include @@ -88,6 +89,32 @@ const std::optional& ProblemImpl::get_optimization_metric() const AxiomList& ProblemImpl::get_axioms() const { return m_axioms; } +size_t UniquePDDLHasher::operator()(const ProblemImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_name(), + e->get_domain(), + get_sorted_vector(e->get_objects()), + get_sorted_vector(e->get_derived_predicates()), + get_sorted_vector(e->get_initial_literals()), + get_sorted_vector(e->get_numeric_fluents()), + e->get_goal_condition(), + e->get_optimization_metric()); +} + +bool UniquePDDLEqualTo::operator()(const ProblemImpl* l, const ProblemImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()) && (l->get_domain() == r->get_domain()) + && (get_sorted_vector(l->get_objects()) == get_sorted_vector(r->get_objects())) + && (get_sorted_vector(l->get_derived_predicates()) == get_sorted_vector(r->get_derived_predicates())) + && (get_sorted_vector(l->get_initial_literals()) == get_sorted_vector(r->get_initial_literals())) + && (get_sorted_vector(l->get_numeric_fluents()) == get_sorted_vector(r->get_numeric_fluents())) + && (l->get_goal_condition() == r->get_goal_condition()) && (l->get_optimization_metric() == r->get_optimization_metric()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const ProblemImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/requirements.cpp b/src/pddl/requirements.cpp index 9513cb2..c181c0b 100644 --- a/src/pddl/requirements.cpp +++ b/src/pddl/requirements.cpp @@ -58,6 +58,17 @@ bool RequirementsImpl::test(RequirementEnum requirement) const { return m_requir const RequirementEnumSet& RequirementsImpl::get_requirements() const { return m_requirements; } +size_t UniquePDDLHasher::operator()(const RequirementsImpl* e) const { return UniquePDDLHashCombiner()(e->get_requirements()); } + +bool UniquePDDLEqualTo::operator()(const RequirementsImpl* l, const RequirementsImpl* r) const +{ + if (&l != &r) + { + return (l->get_requirements() == r->get_requirements()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const RequirementsImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/term.cpp b/src/pddl/term.cpp index ef35be7..fd89e4d 100644 --- a/src/pddl/term.cpp +++ b/src/pddl/term.cpp @@ -38,6 +38,35 @@ size_t TermVariableImpl::get_index() const { return m_index; } const Variable& TermVariableImpl::get_variable() const { return m_variable; } +size_t UniquePDDLHasher::operator()(const TermObjectImpl& e) const { return UniquePDDLHashCombiner()(e.get_object()); } + +size_t UniquePDDLHasher::operator()(const TermVariableImpl& e) const { return UniquePDDLHashCombiner()(e.get_variable()); } + +size_t UniquePDDLHasher::operator()(const TermImpl* e) const +{ + return std::visit([](const auto& arg) { return UniquePDDLHasher()(arg); }, *e); +} + +bool UniquePDDLEqualTo::operator()(const TermObjectImpl& l, const TermObjectImpl& r) const +{ + if (&l != &r) + { + return (l.get_object() == r.get_object()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const TermVariableImpl& l, const TermVariableImpl& r) const +{ + if (&l != &r) + { + return (l.get_variable() == r.get_variable()); + } + return true; +} + +bool UniquePDDLEqualTo::operator()(const TermImpl* l, const TermImpl* r) const { return UniquePDDLEqualTo()(*l, *r); } + std::ostream& operator<<(std::ostream& out, const TermObjectImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/type.cpp b/src/pddl/type.cpp index cd28b73..fb46158 100644 --- a/src/pddl/type.cpp +++ b/src/pddl/type.cpp @@ -17,6 +17,7 @@ #include "loki/details/pddl/type.hpp" #include "formatter.hpp" +#include "loki/details/utils/collections.hpp" namespace loki { @@ -58,6 +59,20 @@ bool is_subtype_or_equal(const Type& type, const TypeList& parent_types) return false; } +size_t UniquePDDLHasher::operator()(const TypeImpl* e) const +{ + return UniquePDDLHashCombiner()(e->get_name(), get_sorted_vector(e->get_bases())); +} + +bool UniquePDDLEqualTo::operator()(const TypeImpl* l, const TypeImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_bases()) == get_sorted_vector(r->get_bases())); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const TypeImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/pddl/variable.cpp b/src/pddl/variable.cpp index 0cc0b03..69a3aff 100644 --- a/src/pddl/variable.cpp +++ b/src/pddl/variable.cpp @@ -98,6 +98,17 @@ VariableSet collect_free_variables(const ConditionImpl& condition) return free_variables; } +size_t UniquePDDLHasher::operator()(const VariableImpl* e) const { return UniquePDDLHashCombiner()(e->get_name()); } + +bool UniquePDDLEqualTo::operator()(const VariableImpl* l, const VariableImpl* r) const +{ + if (&l != &r) + { + return (l->get_name() == r->get_name()); + } + return true; +} + std::ostream& operator<<(std::ostream& out, const VariableImpl& element) { auto formatter = PDDLFormatter(); diff --git a/src/utils/equal_to.cpp b/src/utils/equal_to.cpp deleted file mode 100644 index cf536ba..0000000 --- a/src/utils/equal_to.cpp +++ /dev/null @@ -1,396 +0,0 @@ -/* - * Copyright (C) 2023 Dominik Drexler and Simon Stahlberg - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "loki/details/utils/equal_to.hpp" - -#include "loki/details/pddl/action.hpp" -#include "loki/details/pddl/atom.hpp" -#include "loki/details/pddl/axiom.hpp" -#include "loki/details/pddl/conditions.hpp" -#include "loki/details/pddl/declarations.hpp" -#include "loki/details/pddl/domain.hpp" -#include "loki/details/pddl/effects.hpp" -#include "loki/details/pddl/function.hpp" -#include "loki/details/pddl/function_expressions.hpp" -#include "loki/details/pddl/function_skeleton.hpp" -#include "loki/details/pddl/literal.hpp" -#include "loki/details/pddl/metric.hpp" -#include "loki/details/pddl/numeric_fluent.hpp" -#include "loki/details/pddl/object.hpp" -#include "loki/details/pddl/parameter.hpp" -#include "loki/details/pddl/position.hpp" -#include "loki/details/pddl/predicate.hpp" -#include "loki/details/pddl/problem.hpp" -#include "loki/details/pddl/requirements.hpp" -#include "loki/details/pddl/term.hpp" -#include "loki/details/pddl/type.hpp" -#include "loki/details/pddl/variable.hpp" -#include "loki/details/utils/collections.hpp" - -namespace loki -{ -bool EqualTo::operator()(const ActionImpl* l, const ActionImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())) - && (l->get_condition() == r->get_condition()) && (l->get_effect() == r->get_effect()); - } - return true; -} - -bool EqualTo::operator()(const AtomImpl* l, const AtomImpl* r) const -{ - if (&l != &r) - { - return (l->get_predicate() == r->get_predicate()) && (l->get_terms() == r->get_terms()); - } - return true; -} - -bool EqualTo::operator()(const AxiomImpl* l, const AxiomImpl* r) const -{ - if (&l != &r) - { - return (l->get_derived_predicate_name() == r->get_derived_predicate_name()) - && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())) && (l->get_condition() == r->get_condition()); - } - return true; -} - -bool EqualTo::operator()(const ConditionLiteralImpl& l, const ConditionLiteralImpl& r) const -{ - if (&l != &r) - { - return (l.get_literal() == r.get_literal()); - } - return true; -} - -bool EqualTo::operator()(const ConditionAndImpl& l, const ConditionAndImpl& r) const -{ - if (&l != &r) - { - return (get_sorted_vector(l.get_conditions()) == get_sorted_vector(r.get_conditions())); - } - return true; -} - -bool EqualTo::operator()(const ConditionOrImpl& l, const ConditionOrImpl& r) const -{ - if (&l != &r) - { - return (get_sorted_vector(l.get_conditions()) == get_sorted_vector(r.get_conditions())); - } - return true; -} - -bool EqualTo::operator()(const ConditionNotImpl& l, const ConditionNotImpl& r) const -{ - if (&l != &r) - { - return (l.get_condition() == r.get_condition()); - } - return true; -} - -bool EqualTo::operator()(const ConditionImplyImpl& l, const ConditionImplyImpl& r) const -{ - if (&l != &r) - { - return (l.get_condition_left() == r.get_condition_left()) && (l.get_condition_left() == r.get_condition_left()); - } - return true; -} - -bool EqualTo::operator()(const ConditionExistsImpl& l, const ConditionExistsImpl& r) const -{ - if (&l != &r) - { - return (l.get_condition() == r.get_condition()) && (get_sorted_vector(l.get_parameters()) == get_sorted_vector(r.get_parameters())); - } - return true; -} - -bool EqualTo::operator()(const ConditionForallImpl& l, const ConditionForallImpl& r) const -{ - if (&l != &r) - { - return (l.get_condition() == r.get_condition()) && (get_sorted_vector(l.get_parameters()) == get_sorted_vector(r.get_parameters())); - } - return true; -} - -bool EqualTo::operator()(const ConditionImpl* l, const ConditionImpl* r) const { return EqualTo()(*l, *r); } - -bool EqualTo::operator()(const DomainImpl* l, const DomainImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()) && (l->get_requirements() == r->get_requirements()) - && (get_sorted_vector(l->get_types()) == get_sorted_vector(r->get_types())) - && (get_sorted_vector(l->get_constants()) == get_sorted_vector(r->get_constants())) - && (get_sorted_vector(l->get_predicates()) == get_sorted_vector(r->get_predicates())) - && (get_sorted_vector(l->get_functions()) == get_sorted_vector(r->get_functions())) - && (get_sorted_vector(l->get_actions()) == get_sorted_vector(r->get_actions())) - && (get_sorted_vector(l->get_axioms()) == get_sorted_vector(r->get_axioms())); - } - return true; -} - -bool EqualTo::operator()(const EffectLiteralImpl& l, const EffectLiteralImpl& r) const -{ - if (&l != &r) - { - return (l.get_literal() == r.get_literal()); - } - return true; -} - -bool EqualTo::operator()(const EffectAndImpl& l, const EffectAndImpl& r) const -{ - if (&l != &r) - { - return (get_sorted_vector(l.get_effects()) == get_sorted_vector(r.get_effects())); - } - return true; -} - -bool EqualTo::operator()(const EffectNumericImpl& l, const EffectNumericImpl& r) const -{ - if (&l != &r) - { - return (l.get_assign_operator() == r.get_assign_operator()) && (l.get_function() == r.get_function()) - && (l.get_function_expression() == r.get_function_expression()); - } - return true; -} - -bool EqualTo::operator()(const EffectConditionalForallImpl& l, const EffectConditionalForallImpl& r) const -{ - if (&l != &r) - { - return (l.get_effect() == r.get_effect()) && (get_sorted_vector(l.get_parameters()) == get_sorted_vector(r.get_parameters())); - } - return true; -} - -bool EqualTo::operator()(const EffectConditionalWhenImpl& l, const EffectConditionalWhenImpl& r) const -{ - if (&l != &r) - { - return (l.get_condition() == r.get_condition()) && (l.get_effect() == r.get_effect()); - } - return true; -} - -bool EqualTo::operator()(const EffectImpl* l, const EffectImpl* r) const { return EqualTo()(*l, *r); } - -bool EqualTo::operator()(const FunctionExpressionNumberImpl& l, const FunctionExpressionNumberImpl& r) const -{ - if (&l != &r) - { - return (l.get_number() == r.get_number()); - } - return true; -} - -bool EqualTo::operator()(const FunctionExpressionBinaryOperatorImpl& l, - const FunctionExpressionBinaryOperatorImpl& r) const -{ - if (&l != &r) - { - return (l.get_binary_operator() == r.get_binary_operator()) && (l.get_left_function_expression() == r.get_left_function_expression()) - && (l.get_right_function_expression() == r.get_right_function_expression()); - } - return true; -} - -bool EqualTo::operator()(const FunctionExpressionMultiOperatorImpl& l, - const FunctionExpressionMultiOperatorImpl& r) const -{ - if (&l != &r) - { - return (l.get_multi_operator() == r.get_multi_operator()) - && (get_sorted_vector(l.get_function_expressions()) == get_sorted_vector(r.get_function_expressions())); - } - return true; -} - -bool EqualTo::operator()(const FunctionExpressionMinusImpl& l, const FunctionExpressionMinusImpl& r) const -{ - if (&l != &r) - { - return (l.get_function_expression() == r.get_function_expression()); - } - return true; -} - -bool EqualTo::operator()(const FunctionExpressionFunctionImpl& l, const FunctionExpressionFunctionImpl& r) const -{ - if (&l != &r) - { - return (l.get_function() == r.get_function()); - } - return true; -} - -bool EqualTo::operator()(const FunctionExpressionImpl* l, const FunctionExpressionImpl* r) const -{ - return EqualTo()(*l, *r); -} - -template<> -struct EqualTo -{ - bool operator()(const FunctionSkeletonImpl* l, const FunctionSkeletonImpl* r) const; -}; - -bool EqualTo::operator()(const FunctionSkeletonImpl* l, const FunctionSkeletonImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()) && (l->get_type() == r->get_type()) - && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())); - } - return true; -} - -bool EqualTo::operator()(const FunctionImpl* l, const FunctionImpl* r) const -{ - if (&l != &r) - { - return (l->get_function_skeleton() == r->get_function_skeleton()) && (l->get_terms() == r->get_terms()); - } - return true; -} - -bool EqualTo::operator()(const LiteralImpl* l, const LiteralImpl* r) const -{ - if (&l != &r) - { - return (l->is_negated() == r->is_negated()) && (l->get_atom() == r->get_atom()); - } - return true; -} - -bool EqualTo::operator()(const OptimizationMetricImpl* l, const OptimizationMetricImpl* r) const -{ - if (&l != &r) - { - return (l->get_optimization_metric() == r->get_optimization_metric()) && (l->get_function_expression() == r->get_function_expression()); - } - return true; -} - -bool EqualTo::operator()(const NumericFluentImpl* l, const NumericFluentImpl* r) const -{ - if (&l != &r) - { - return (l->get_number() == r->get_number()) && (l->get_function() == r->get_function()); - } - return true; -} - -bool EqualTo::operator()(const ObjectImpl* l, const ObjectImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_bases()) == get_sorted_vector(r->get_bases())); - } - return true; -} - -bool EqualTo::operator()(const ParameterImpl* l, const ParameterImpl* r) const -{ - if (&l != &r) - { - return (l->get_variable() == r->get_variable()) && (get_sorted_vector(l->get_bases()) == get_sorted_vector(r->get_bases())); - } - return true; -} - -bool EqualTo::operator()(const PredicateImpl* l, const PredicateImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_parameters()) == get_sorted_vector(r->get_parameters())); - } - return true; -} - -bool EqualTo::operator()(const ProblemImpl* l, const ProblemImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()) && (l->get_domain() == r->get_domain()) - && (get_sorted_vector(l->get_objects()) == get_sorted_vector(r->get_objects())) - && (get_sorted_vector(l->get_derived_predicates()) == get_sorted_vector(r->get_derived_predicates())) - && (get_sorted_vector(l->get_initial_literals()) == get_sorted_vector(r->get_initial_literals())) - && (get_sorted_vector(l->get_numeric_fluents()) == get_sorted_vector(r->get_numeric_fluents())) - && (l->get_goal_condition() == r->get_goal_condition()) && (l->get_optimization_metric() == r->get_optimization_metric()); - } - return true; -} - -bool EqualTo::operator()(const RequirementsImpl* l, const RequirementsImpl* r) const -{ - if (&l != &r) - { - return (l->get_requirements() == r->get_requirements()); - } - return true; -} - -bool EqualTo::operator()(const TermObjectImpl& l, const TermObjectImpl& r) const -{ - if (&l != &r) - { - return (l.get_object() == r.get_object()); - } - return true; -} - -bool EqualTo::operator()(const TermVariableImpl& l, const TermVariableImpl& r) const -{ - if (&l != &r) - { - return (l.get_variable() == r.get_variable()); - } - return true; -} - -bool EqualTo::operator()(const TermImpl* l, const TermImpl* r) const { return EqualTo()(*l, *r); } - -bool EqualTo::operator()(const TypeImpl* l, const TypeImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()) && (get_sorted_vector(l->get_bases()) == get_sorted_vector(r->get_bases())); - } - return true; -} - -bool EqualTo::operator()(const VariableImpl* l, const VariableImpl* r) const -{ - if (&l != &r) - { - return (l->get_name() == r->get_name()); - } - return true; -} -} diff --git a/src/utils/hash.cpp b/src/utils/hash.cpp deleted file mode 100644 index 638e134..0000000 --- a/src/utils/hash.cpp +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright (C) 2023 Dominik Drexler and Simon Stahlberg - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "loki/details/utils/hash.hpp" - -#include "loki/details/pddl/action.hpp" -#include "loki/details/pddl/atom.hpp" -#include "loki/details/pddl/axiom.hpp" -#include "loki/details/pddl/conditions.hpp" -#include "loki/details/pddl/declarations.hpp" -#include "loki/details/pddl/domain.hpp" -#include "loki/details/pddl/effects.hpp" -#include "loki/details/pddl/function.hpp" -#include "loki/details/pddl/function_expressions.hpp" -#include "loki/details/pddl/function_skeleton.hpp" -#include "loki/details/pddl/literal.hpp" -#include "loki/details/pddl/metric.hpp" -#include "loki/details/pddl/numeric_fluent.hpp" -#include "loki/details/pddl/object.hpp" -#include "loki/details/pddl/parameter.hpp" -#include "loki/details/pddl/position.hpp" -#include "loki/details/pddl/predicate.hpp" -#include "loki/details/pddl/problem.hpp" -#include "loki/details/pddl/requirements.hpp" -#include "loki/details/pddl/term.hpp" -#include "loki/details/pddl/type.hpp" -#include "loki/details/pddl/variable.hpp" -#include "loki/details/utils/collections.hpp" - -namespace loki -{ -size_t Hasher::operator()(const ActionImpl* e) const -{ - return HashCombiner()(e->get_name(), get_sorted_vector(e->get_parameters()), e->get_condition(), e->get_effect()); -} - -size_t Hasher::operator()(const AtomImpl* e) const { return HashCombiner()(e->get_predicate(), e->get_terms()); } - -size_t Hasher::operator()(const AxiomImpl* e) const -{ - return HashCombiner()(e->get_derived_predicate_name(), get_sorted_vector(e->get_parameters()), e->get_condition()); -} - -size_t Hasher::operator()(const ConditionLiteralImpl& e) const { return HashCombiner()(e.get_literal()); } - -size_t Hasher::operator()(const ConditionAndImpl& e) const { return HashCombiner()(get_sorted_vector(e.get_conditions())); } - -size_t Hasher::operator()(const ConditionOrImpl& e) const { return HashCombiner()(get_sorted_vector(e.get_conditions())); } - -size_t Hasher::operator()(const ConditionNotImpl& e) const { return HashCombiner()(e.get_condition()); } - -size_t Hasher::operator()(const ConditionImplyImpl& e) const -{ - return HashCombiner()(e.get_condition_left(), e.get_condition_right()); -} - -size_t Hasher::operator()(const ConditionExistsImpl& e) const -{ - return HashCombiner()(get_sorted_vector(e.get_parameters()), e.get_condition()); -} - -size_t Hasher::operator()(const ConditionForallImpl& e) const -{ - return HashCombiner()(get_sorted_vector(e.get_parameters()), e.get_condition()); -} - -size_t Hasher::operator()(const ConditionImpl* e) const -{ - return std::visit([](const auto& arg) { return Hasher()(arg); }, *e); -} - -size_t Hasher::operator()(const DomainImpl* e) const -{ - return HashCombiner()(e->get_name(), - e->get_requirements(), - get_sorted_vector(e->get_types()), - get_sorted_vector(e->get_constants()), - get_sorted_vector(e->get_predicates()), - get_sorted_vector(e->get_functions()), - get_sorted_vector(e->get_actions()), - get_sorted_vector(e->get_axioms())); -} - -size_t Hasher::operator()(const EffectLiteralImpl& e) const { return HashCombiner()(e.get_literal()); } - -size_t Hasher::operator()(const EffectAndImpl& e) const { return HashCombiner()(get_sorted_vector(e.get_effects())); } - -size_t Hasher::operator()(const EffectNumericImpl& e) const -{ - return HashCombiner()(e.get_assign_operator(), e.get_function(), e.get_function_expression()); -} - -size_t Hasher::operator()(const EffectConditionalForallImpl& e) const -{ - return HashCombiner()(e.get_effect(), get_sorted_vector(e.get_parameters())); -} - -size_t Hasher::operator()(const EffectConditionalWhenImpl& e) const -{ - return HashCombiner()(e.get_condition(), e.get_effect()); -} - -size_t Hasher::operator()(const EffectImpl* e) const -{ - return std::visit([](const auto& arg) { return Hasher()(arg); }, *e); -} - -size_t Hasher::operator()(const FunctionExpressionNumberImpl& e) const { return HashCombiner()(e.get_number()); } - -size_t Hasher::operator()(const FunctionExpressionBinaryOperatorImpl& e) const -{ - return HashCombiner()(e.get_binary_operator(), e.get_left_function_expression(), e.get_right_function_expression()); -} - -size_t Hasher::operator()(const FunctionExpressionMultiOperatorImpl& e) const -{ - return HashCombiner()(e.get_multi_operator(), get_sorted_vector(e.get_function_expressions())); -} - -size_t Hasher::operator()(const FunctionExpressionMinusImpl& e) const -{ - return HashCombiner()(e.get_function_expression()); -} - -size_t Hasher::operator()(const FunctionExpressionFunctionImpl& e) const { return HashCombiner()(e.get_function()); } - -size_t Hasher::operator()(const FunctionExpressionImpl* e) const -{ - return std::visit([](const auto& arg) { return Hasher()(arg); }, *e); -} - -size_t Hasher::operator()(const FunctionSkeletonImpl* e) const -{ - return HashCombiner()(e->get_name(), e->get_type(), get_sorted_vector(e->get_parameters())); -} - -size_t Hasher::operator()(const FunctionImpl* e) const { return HashCombiner()(e->get_function_skeleton(), e->get_terms()); } - -size_t Hasher::operator()(const LiteralImpl* e) const { return HashCombiner()(e->is_negated(), e->get_atom()); } - -size_t Hasher::operator()(const OptimizationMetricImpl* e) const -{ - return HashCombiner()(e->get_optimization_metric(), e->get_function_expression()); -} - -size_t Hasher::operator()(const NumericFluentImpl* e) const { return HashCombiner()(e->get_number(), e->get_function()); } - -size_t Hasher::operator()(const ObjectImpl* e) const { return HashCombiner()(e->get_name(), get_sorted_vector(e->get_bases())); } - -size_t Hasher::operator()(const ParameterImpl* e) const { return HashCombiner()(e->get_variable(), get_sorted_vector(e->get_bases())); } - -size_t Hasher::operator()(const PredicateImpl* e) const { return HashCombiner()(e->get_name(), get_sorted_vector(e->get_parameters())); } - -size_t Hasher::operator()(const ProblemImpl* e) const -{ - return HashCombiner()(e->get_name(), - e->get_domain(), - get_sorted_vector(e->get_objects()), - get_sorted_vector(e->get_derived_predicates()), - get_sorted_vector(e->get_initial_literals()), - get_sorted_vector(e->get_numeric_fluents()), - e->get_goal_condition(), - e->get_optimization_metric()); -} - -size_t Hasher::operator()(const RequirementsImpl* e) const { return HashCombiner()(e->get_requirements()); } - -size_t Hasher::operator()(const TermObjectImpl& e) const { return HashCombiner()(e.get_object()); } - -size_t Hasher::operator()(const TermVariableImpl& e) const { return HashCombiner()(e.get_variable()); } - -size_t Hasher::operator()(const TermImpl* e) const -{ - return std::visit([](const auto& arg) { return Hasher()(arg); }, *e); -} - -size_t Hasher::operator()(const TypeImpl* e) const { return HashCombiner()(e->get_name(), get_sorted_vector(e->get_bases())); } - -size_t Hasher::operator()(const VariableImpl* e) const { return HashCombiner()(e->get_name()); } -} diff --git a/tests/unit/pddl/reference.cpp b/tests/unit/pddl/reference.cpp index 084f2e4..d2a01b6 100644 --- a/tests/unit/pddl/reference.cpp +++ b/tests/unit/pddl/reference.cpp @@ -16,6 +16,8 @@ */ #include +#include +#include #include #include #include @@ -25,7 +27,7 @@ namespace loki::domain::tests TEST(LokiTests, PddlReferenceTest) { - UniqueFactory factory(2); + UniqueFactory, UniquePDDLEqualTo> factory(2); const auto object_0 = factory.get_or_create("object_0", TypeList()); const auto object_1 = factory.get_or_create("object_1", TypeList()); diff --git a/tests/unit/utils/value_type_factory.cpp b/tests/unit/utils/value_type_factory.cpp index 4bab0fb..db655bd 100644 --- a/tests/unit/utils/value_type_factory.cpp +++ b/tests/unit/utils/value_type_factory.cpp @@ -16,6 +16,8 @@ */ #include +#include +#include #include #include #include @@ -27,7 +29,7 @@ namespace loki::domain::tests TEST(LokiTests, UtilsUniqueFactoryIteratorTest) { - UniqueFactory factory(2); + UniqueFactory, UniquePDDLEqualTo> factory(2); const auto object_0 = factory.get_or_create("object_0", TypeList()); const auto object_1 = factory.get_or_create("object_1", TypeList()); const auto object_2 = factory.get_or_create("object_2", TypeList()); @@ -46,7 +48,7 @@ TEST(LokiTests, UtilsUniqueFactoryIteratorTest) TEST(LokiTests, UtilsUniqueFactoryIteratorEmptyTest) { - UniqueFactory factory(4); + UniqueFactory, UniquePDDLEqualTo> factory(4); auto objects = ObjectList {}; for (const auto& object : factory) @@ -59,8 +61,8 @@ TEST(LokiTests, UtilsUniqueFactoryIteratorEmptyTest) TEST(LokiTests, UtilsUniqueFactoryVariantTest) { - UniqueFactory objects(2); - UniqueFactory terms(2); + UniqueFactory, UniquePDDLEqualTo> objects(2); + UniqueFactory, UniquePDDLEqualTo> terms(2); const auto object_0 = objects.get_or_create("object_0", TypeList()); const auto object_1 = objects.get_or_create("object_1", TypeList()); @@ -74,7 +76,7 @@ TEST(LokiTests, UtilsUniqueFactoryVariantTest) TEST(LokiTests, UtilsUniqueFactoryTest) { - UniqueFactory factory(2); + UniqueFactory, UniquePDDLEqualTo> factory(2); EXPECT_EQ(factory.size(), 0); // Test uniqueness: insert the same element twice @@ -86,16 +88,16 @@ TEST(LokiTests, UtilsUniqueFactoryTest) const auto object_0_1 = factory.get_or_create("object_0", TypeList()); EXPECT_EQ(factory.size(), 1); EXPECT_EQ(object_0_0, object_0_1); - EXPECT_EQ(Hasher()(object_0_0), Hasher()(object_0_1)); - EXPECT_TRUE(EqualTo()(object_0_0, object_0_1)); + EXPECT_EQ(UniquePDDLHasher()(object_0_0), UniquePDDLHasher()(object_0_1)); + EXPECT_TRUE(UniquePDDLEqualTo()(object_0_0, object_0_1)); const auto object_1 = factory.get_or_create("object_1", TypeList()); EXPECT_EQ(factory.size(), 2); EXPECT_NE(object_0_0, object_1); EXPECT_EQ(object_1->get_index(), 1); EXPECT_EQ(object_1->get_name(), "object_1"); - EXPECT_NE(Hasher()(object_0_0), Hasher()(object_1)); - EXPECT_FALSE(EqualTo()(object_0_0, object_1)); + EXPECT_NE(UniquePDDLHasher()(object_0_0), UniquePDDLHasher()(object_1)); + EXPECT_FALSE(UniquePDDLEqualTo()(object_0_0, object_1)); } }