Skip to content

Commit

Permalink
switched to small_vector in some cases, improved generality of other …
Browse files Browse the repository at this point in the history
…code to make dropin replacement possible
  • Loading branch information
drexlerd committed Jan 1, 2024
1 parent b44976f commit d72d763
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 49 deletions.
28 changes: 8 additions & 20 deletions include/common/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,27 @@ inline size_t hash_combine(const Types&... args)
return seed;
}

template<class T>
inline std::size_t hash_vector(const std::vector<T>& vector)
template<class Container>
inline std::size_t hash_container(const Container& container)
{
using T = typename Container::value_type;
const auto hash_function = std::hash<T>();
std::size_t aggregated_hash = 0;
for (const auto& item : vector)
for (const auto& item : container)
{
const auto item_hash = hash_function(item);
hash_combine(aggregated_hash, item_hash);
}
return aggregated_hash;
}

template<typename T>
struct hash_vector_type {
size_t operator()(const std::vector<T>& vector) const {
return hash_vector(vector);
template<typename Container>
struct hash_container_type {
size_t operator()(const Container& container) const {
return hash_container(container);
}
};

template<class T>
inline std::size_t hash_set(const std::set<T>& vector)
{
const auto hash_function = std::hash<T>();
std::size_t aggregated_hash = 0;
for (const auto& item : vector)
{
const auto item_hash = hash_function(item);
hash_combine(aggregated_hash, item_hash);
}
return aggregated_hash;
}

}


Expand Down
8 changes: 5 additions & 3 deletions include/domain/pddl/declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "../../common/pddl/declarations.hpp"

#include <boost/container/small_vector.hpp>

#include <memory>
#include <string>
#include <unordered_map>
Expand All @@ -35,7 +37,7 @@ namespace loki::pddl {

class TypeImpl;
using Type = const TypeImpl*;
using TypeList = std::vector<Type>;
using TypeList = boost::container::small_vector<Type,1>;
using TypeSet = std::set<Type>;

class ObjectImpl;
Expand All @@ -52,15 +54,15 @@ namespace loki::pddl {
class TermVariableImpl;
using TermImpl = std::variant<TermObjectImpl, TermVariableImpl>;
using Term = const TermImpl*;
using TermList = std::vector<Term>;
using TermList = boost::container::small_vector<Term, 3>;

class AtomImpl;
using Atom = const AtomImpl*;
using AtomList = std::vector<Atom>;

class ParameterImpl;
using Parameter = const ParameterImpl*;
using ParameterList = std::vector<Parameter>;
using ParameterList = boost::container::small_vector<Parameter, 4>;
using ParameterAssignment = std::unordered_map<Parameter, Object>;

class PredicateImpl;
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool ActionImpl::are_equal_impl(const ActionImpl& other) const {
size_t ActionImpl::hash_impl() const {
return hash_combine(
m_name,
hash_vector(m_parameters),
hash_container(m_parameters),
*m_condition,
*m_effect);
}
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool AtomImpl::are_equal_impl(const AtomImpl& other) const {
}

size_t AtomImpl::hash_impl() const {
return hash_combine(m_predicate, hash_vector(m_terms));
return hash_combine(m_predicate, hash_container(m_terms));
}


Expand Down
8 changes: 4 additions & 4 deletions src/domain/pddl/conditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bool ConditionAndImpl::are_equal_impl(const ConditionAndImpl& other) const {
}

size_t ConditionAndImpl::hash_impl() const {
return hash_vector(get_sorted_vector(m_conditions));
return hash_container(get_sorted_vector(m_conditions));
}

void ConditionAndImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down Expand Up @@ -94,7 +94,7 @@ bool ConditionOrImpl::are_equal_impl(const ConditionOrImpl& other) const {
}

size_t ConditionOrImpl::hash_impl() const {
return hash_vector(get_sorted_vector(m_conditions));
return hash_container(get_sorted_vector(m_conditions));
}

void ConditionOrImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down Expand Up @@ -184,7 +184,7 @@ bool ConditionExistsImpl::are_equal_impl(const ConditionExistsImpl& other) const
}

size_t ConditionExistsImpl::hash_impl() const {
return hash_combine(hash_vector(m_parameters), m_condition);
return hash_combine(hash_container(m_parameters), m_condition);
}

void ConditionExistsImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down Expand Up @@ -220,7 +220,7 @@ bool ConditionForallImpl::are_equal_impl(const ConditionForallImpl& other) const
}

size_t ConditionForallImpl::hash_impl() const {
return hash_combine(hash_vector(m_parameters), m_condition);
return hash_combine(hash_container(m_parameters), m_condition);
}

void ConditionForallImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/derived_predicate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool DerivedPredicateImpl::are_equal_impl(const DerivedPredicateImpl& other) con
}

size_t DerivedPredicateImpl::hash_impl() const {
return hash_combine(hash_vector(get_sorted_vector(m_parameters)), m_condition);
return hash_combine(hash_container(get_sorted_vector(m_parameters)), m_condition);
}

void DerivedPredicateImpl::str_impl(std::ostringstream& out, const FormattingOptions& /*options*/) const {
Expand Down
14 changes: 7 additions & 7 deletions src/domain/pddl/domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ size_t DomainImpl::hash_impl() const {
return hash_combine(
m_name,
m_requirements,
hash_vector(get_sorted_vector(m_types)),
hash_vector(get_sorted_vector(m_constants)),
hash_vector(get_sorted_vector(m_predicates)),
hash_vector(get_sorted_vector(m_functions)),
hash_vector(get_sorted_vector(m_actions)));
hash_container(get_sorted_vector(m_types)),
hash_container(get_sorted_vector(m_constants)),
hash_container(get_sorted_vector(m_predicates)),
hash_container(get_sorted_vector(m_functions)),
hash_container(get_sorted_vector(m_actions)));
}

void DomainImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand All @@ -79,7 +79,7 @@ void DomainImpl::str_impl(std::ostringstream& out, const FormattingOptions& opti
}
if (!m_types.empty()) {
out << string(nested_options.indent, ' ') << "(:types ";
std::unordered_map<std::vector<pddl::Type>, std::vector<pddl::Type>, hash_vector_type<pddl::Type>> subtypes_by_parent_types;
std::unordered_map<pddl::TypeList, pddl::TypeList, hash_container_type<pddl::TypeList>> subtypes_by_parent_types;
for (const auto& type : m_types) {
subtypes_by_parent_types[type->get_bases()].push_back(type);
}
Expand All @@ -103,7 +103,7 @@ void DomainImpl::str_impl(std::ostringstream& out, const FormattingOptions& opti
}
if (!m_constants.empty()) {
out << string(nested_options.indent, ' ') << "(:constants ";
std::unordered_map<std::vector<pddl::Type>, std::vector<pddl::Object>, hash_vector_type<pddl::Type>> constants_by_types;
std::unordered_map<pddl::TypeList, pddl::ObjectList, hash_container_type<pddl::TypeList>> constants_by_types;
for (const auto& constant : m_constants) {
constants_by_types[constant->get_bases()].push_back(constant);
}
Expand Down
4 changes: 2 additions & 2 deletions src/domain/pddl/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool EffectAndImpl::are_equal_impl(const EffectAndImpl& other) const {
}

size_t EffectAndImpl::hash_impl() const {
return hash_vector(get_sorted_vector(m_effects));
return hash_container(get_sorted_vector(m_effects));
}

void EffectAndImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down Expand Up @@ -150,7 +150,7 @@ bool EffectConditionalForallImpl::are_equal_impl(const EffectConditionalForallIm
}

size_t EffectConditionalForallImpl::hash_impl() const {
return hash_combine(hash_vector(m_parameters), m_effect);
return hash_combine(hash_container(m_parameters), m_effect);
}

void EffectConditionalForallImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool FunctionImpl::are_equal_impl(const FunctionImpl& other) const {
}

size_t FunctionImpl::hash_impl() const {
return hash_combine(m_function_skeleton, hash_vector(m_terms));
return hash_combine(m_function_skeleton, hash_container(m_terms));
}

void FunctionImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/function_expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ bool FunctionExpressionMultiOperatorImpl::are_equal_impl(const FunctionExpressio
}

size_t FunctionExpressionMultiOperatorImpl::hash_impl() const {
return hash_combine(m_multi_operator, hash_vector(get_sorted_vector(m_function_expressions)));
return hash_combine(m_multi_operator, hash_container(get_sorted_vector(m_function_expressions)));
}

void FunctionExpressionMultiOperatorImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/function_skeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool FunctionSkeletonImpl::are_equal_impl(const FunctionSkeletonImpl& other) con
}

size_t FunctionSkeletonImpl::hash_impl() const {
return hash_combine(m_name, hash_vector(m_parameters), m_type);
return hash_combine(m_name, hash_container(m_parameters), m_type);
}

void FunctionSkeletonImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool ObjectImpl::are_equal_impl(const ObjectImpl& other) const {
}

size_t ObjectImpl::hash_impl() const {
return hash_combine(m_name, hash_vector(get_sorted_vector(m_types)));
return hash_combine(m_name, hash_container(get_sorted_vector(m_types)));
}

void ObjectImpl::str_impl(std::ostringstream& out, const FormattingOptions& /*options*/) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool ParameterImpl::are_equal_impl(const ParameterImpl& other) const {
}

size_t ParameterImpl::hash_impl() const {
return hash_combine(m_variable, hash_vector(get_sorted_vector(m_types)));
return hash_combine(m_variable, hash_container(get_sorted_vector(m_types)));
}

void ParameterImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/predicate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bool PredicateImpl::are_equal_impl(const PredicateImpl& other) const {
}

size_t PredicateImpl::hash_impl() const {
return hash_combine(m_name, hash_vector(m_parameters));
return hash_combine(m_name, hash_container(m_parameters));
}

void PredicateImpl::str_impl(std::ostringstream& out, const FormattingOptions& options) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/requirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool RequirementsImpl::are_equal_impl(const RequirementsImpl& other) const {
}

size_t RequirementsImpl::hash_impl() const {
return hash_set(m_requirements);
return hash_container(m_requirements);
}

void RequirementsImpl::str_impl(std::ostringstream& out, const FormattingOptions& /*options*/) const {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/pddl/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool TypeImpl::are_equal_impl(const TypeImpl& other) const {
}

size_t TypeImpl::hash_impl() const {
return hash_combine(m_name, hash_vector(get_sorted_vector(m_bases)));
return hash_combine(m_name, hash_container(get_sorted_vector(m_bases)));
}

void TypeImpl::str_impl(std::ostringstream& out, const FormattingOptions& /*options*/) const {
Expand Down
4 changes: 2 additions & 2 deletions src/problem/pddl/problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ size_t ProblemImpl::hash_impl() const {
m_domain,
m_name,
m_requirements,
hash_vector(get_sorted_vector(m_objects)),
hash_vector(get_sorted_vector(m_initial_literals)),
hash_container(get_sorted_vector(m_objects)),
hash_container(get_sorted_vector(m_initial_literals)),
m_goal_condition,
optimization_hash);
}
Expand Down

0 comments on commit d72d763

Please sign in to comment.