Skip to content

Commit

Permalink
removed TermVariableImpl and TermObjectImpl by directly storing Varia…
Browse files Browse the repository at this point in the history
…ble and Object in TermImpl.
  • Loading branch information
drexlerd committed Nov 25, 2024
1 parent 1efa111 commit 80c035e
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 134 deletions.
6 changes: 1 addition & 5 deletions include/loki/details/pddl/declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ using VariableList = std::vector<Variable>;
using VariableSet = std::unordered_set<Variable>;
using VariableAssignment = std::unordered_map<Variable, Object>;

class TermObjectImpl;
using TermObject = const TermObjectImpl*;
class TermVariableImpl;
using TermVariable = const TermVariableImpl*;
using TermImpl = std::variant<TermObjectImpl, TermVariableImpl>;
class TermImpl;
using Term = const TermImpl*;
using TermList = std::vector<Term>;

Expand Down
12 changes: 0 additions & 12 deletions include/loki/details/pddl/equal_to.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,6 @@ struct UniquePDDLEqualTo<const RequirementsImpl*>
bool operator()(const RequirementsImpl* l, const RequirementsImpl* r) const;
};

template<>
struct UniquePDDLEqualTo<const TermObjectImpl&>
{
bool operator()(const TermObjectImpl& l, const TermObjectImpl& r) const;
};

template<>
struct UniquePDDLEqualTo<const TermVariableImpl&>
{
bool operator()(const TermVariableImpl& l, const TermVariableImpl& r) const;
};

template<>
struct UniquePDDLEqualTo<const TermImpl*>
{
Expand Down
20 changes: 7 additions & 13 deletions include/loki/details/pddl/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ struct UniquePDDLHasher<std::variant<Ts...>>
{
size_t operator()(const std::variant<Ts...>& variant) const
{
return std::visit([](const auto& arg) { return UniquePDDLHasher<decltype(arg)>()(arg); }, variant);
return std::visit(
[](const auto& arg)
{
using DecayedType = std::decay_t<decltype(arg)>;
return UniquePDDLHasher<DecayedType>()(arg);
},
variant);
}
};

Expand Down Expand Up @@ -298,18 +304,6 @@ struct UniquePDDLHasher<const RequirementsImpl*>
size_t operator()(const RequirementsImpl* e) const;
};

template<>
struct UniquePDDLHasher<const TermObjectImpl&>
{
size_t operator()(const TermObjectImpl& e) const;
};

template<>
struct UniquePDDLHasher<const TermVariableImpl&>
{
size_t operator()(const TermVariableImpl& e) const;
};

template<>
struct UniquePDDLHasher<const TermImpl*>
{
Expand Down
45 changes: 9 additions & 36 deletions include/loki/details/pddl/term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,31 @@
namespace loki
{

/* TermObjectImpl */
/* TermImpl */

class TermObjectImpl
class TermImpl
{
private:
size_t m_index;
Object m_object;
std::variant<Object, Variable> m_object_or_variable;

TermObjectImpl(size_t index, Object object);
TermImpl(size_t index, std::variant<Object, Variable> object_or_variable);

// Give access to the constructor.
template<typename HolderType, typename Hash, typename EqualTo>
friend class SegmentedRepository;

public:
// moveable but not copyable
TermObjectImpl(const TermObjectImpl& other) = delete;
TermObjectImpl& operator=(const TermObjectImpl& other) = delete;
TermObjectImpl(TermObjectImpl&& other) = default;
TermObjectImpl& operator=(TermObjectImpl&& other) = default;
TermImpl(const TermImpl& other) = delete;
TermImpl& operator=(const TermImpl& other) = delete;
TermImpl(TermImpl&& other) = default;
TermImpl& operator=(TermImpl&& other) = default;

size_t get_index() const;
const Object& get_object() const;
const std::variant<Object, Variable>& get_object_or_variable() const;
};

/* TermVariableImpl */

class TermVariableImpl
{
private:
size_t m_index;
Variable m_variable;

TermVariableImpl(size_t index, Variable variable);

// Give access to the constructor.
template<typename HolderType, typename Hash, typename EqualTo>
friend class SegmentedRepository;

public:
// moveable but not copyable
TermVariableImpl(const TermVariableImpl& other) = delete;
TermVariableImpl& operator=(const TermVariableImpl& other) = delete;
TermVariableImpl(TermVariableImpl&& other) = default;
TermVariableImpl& operator=(TermVariableImpl&& other) = default;

size_t get_index() const;
const Variable& get_variable() 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);

}
Expand Down
19 changes: 2 additions & 17 deletions src/pddl/equal_to.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,26 +363,11 @@ bool UniquePDDLEqualTo<const RequirementsImpl*>::operator()(const RequirementsIm
return true;
}

bool UniquePDDLEqualTo<const TermObjectImpl&>::operator()(const TermObjectImpl& l, const TermObjectImpl& r) const
bool UniquePDDLEqualTo<const TermImpl*>::operator()(const TermImpl* l, const TermImpl* r) const
{
if (&l != &r)
{
return (l.get_object() == r.get_object());
}
return true;
}

bool UniquePDDLEqualTo<const TermVariableImpl&>::operator()(const TermVariableImpl& l, const TermVariableImpl& r) const
{
if (&l != &r)
{
return (l.get_variable() == r.get_variable());
}
return true;
return l->get_object_or_variable() == r->get_object_or_variable();
}

bool UniquePDDLEqualTo<const TermImpl*>::operator()(const TermImpl* l, const TermImpl* r) const { return UniquePDDLEqualTo<TermImpl>()(*l, *r); }

bool UniquePDDLEqualTo<const TypeImpl*>::operator()(const TypeImpl* l, const TypeImpl* r) const
{
if (&l != &r)
Expand Down
6 changes: 1 addition & 5 deletions src/pddl/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,9 @@ void PDDLFormatter::write(const RequirementsImpl& element, std::ostream& out)
out << ")";
}

void PDDLFormatter::write(const TermObjectImpl& element, std::ostream& out) { out << element.get_object()->get_name(); }

void PDDLFormatter::write(const TermVariableImpl& element, std::ostream& out) { out << element.get_variable()->get_name(); }

void PDDLFormatter::write(const TermImpl& element, std::ostream& out)
{
std::visit([this, &out](const auto& arg) { this->write(arg, out); }, element);
std::visit([this, &out](const auto& arg) { this->write(*arg, out); }, element.get_object_or_variable());
}

void PDDLFormatter::write(const TypeImpl& element, std::ostream& out)
Expand Down
2 changes: 0 additions & 2 deletions src/pddl/formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class PDDLFormatter
void write(const PredicateImpl& element, std::ostream& out);
void write(const ProblemImpl& element, std::ostream& out);
void write(const RequirementsImpl& element, std::ostream& out);
void write(const TermObjectImpl& element, std::ostream& out);
void write(const TermVariableImpl& element, std::ostream& out);
void write(const TermImpl& element, std::ostream& out);
void write(const TypeImpl& element, std::ostream& out);
void write(const VariableImpl& element, std::ostream& out);
Expand Down
9 changes: 1 addition & 8 deletions src/pddl/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,7 @@ size_t UniquePDDLHasher<const ProblemImpl*>::operator()(const ProblemImpl* e) co

size_t UniquePDDLHasher<const RequirementsImpl*>::operator()(const RequirementsImpl* e) const { return UniquePDDLHashCombiner()(e->get_requirements()); }

size_t UniquePDDLHasher<const TermObjectImpl&>::operator()(const TermObjectImpl& e) const { return UniquePDDLHashCombiner()(e.get_object()); }

size_t UniquePDDLHasher<const TermVariableImpl&>::operator()(const TermVariableImpl& e) const { return UniquePDDLHashCombiner()(e.get_variable()); }

size_t UniquePDDLHasher<const TermImpl*>::operator()(const TermImpl* e) const
{
return std::visit([](const auto& arg) { return UniquePDDLHasher<decltype(arg)>()(arg); }, *e);
}
size_t UniquePDDLHasher<const TermImpl*>::operator()(const TermImpl* e) const { return UniquePDDLHashCombiner()(e->get_object_or_variable()); }

size_t UniquePDDLHasher<const TypeImpl*>::operator()(const TypeImpl* e) const
{
Expand Down
6 changes: 3 additions & 3 deletions src/pddl/parser/error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void test_nonnegative_number(double number, const Position& position, const Cont
* Test assignment
*/

static void test_incompatible_grounding(const Parameter& parameter, const Object& object, const Position& position, const Context& context)
static void test_incompatible_grounding_helper(Parameter parameter, Object object, const Position& position, const Context& context)
{
// Object type must match any of those types.
const auto& parameter_types = TypeSet(parameter->get_bases().begin(), parameter->get_bases().end());
Expand All @@ -277,9 +277,9 @@ void test_incompatible_grounding(const ParameterList& parameters, const TermList

for (size_t i = 0; i < parameters.size(); ++i)
{
if (const auto term_object = std::get_if<TermObjectImpl>(terms[i]))
if (const auto object = std::get_if<Object>(&terms[i]->get_object_or_variable()))
{
test_incompatible_grounding(parameters[i], term_object->get_object(), positions[i], context);
test_incompatible_grounding_helper(parameters[i], *object, positions[i], context);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pddl/repositories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ Variable PDDLRepositories::get_or_create_variable(std::string name)

Term PDDLRepositories::get_or_create_term_variable(Variable variable)
{
return boost::hana::at_key(m_repositories, boost::hana::type<TermImpl> {}).template get_or_create<TermVariableImpl>(std::move(variable));
return boost::hana::at_key(m_repositories, boost::hana::type<TermImpl> {}).template get_or_create<TermImpl>(variable);
}

Term PDDLRepositories::get_or_create_term_object(Object object)
{
return boost::hana::at_key(m_repositories, boost::hana::type<TermImpl> {}).template get_or_create<TermObjectImpl>(std::move(object));
return boost::hana::at_key(m_repositories, boost::hana::type<TermImpl> {}).template get_or_create<TermImpl>(object);
}

Object PDDLRepositories::get_or_create_object(std::string name, TypeList types)
Expand Down
29 changes: 4 additions & 25 deletions src/pddl/term.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,12 @@
namespace loki
{

/* TermObjectImpl */
TermObjectImpl::TermObjectImpl(size_t index, Object object) : m_index(index), m_object(std::move(object)) {}
/* TermImpl */
TermImpl::TermImpl(size_t index, std::variant<Object, Variable> object_or_variable) : m_index(index), m_object_or_variable(std::move(object_or_variable)) {}

size_t TermObjectImpl::get_index() const { return m_index; }
size_t TermImpl::get_index() const { return m_index; }

const Object& TermObjectImpl::get_object() const { return m_object; }

/* TermVariableImpl */
TermVariableImpl::TermVariableImpl(size_t index, Variable variable) : m_index(index), m_variable(std::move(variable)) {}

size_t TermVariableImpl::get_index() const { return m_index; }

const Variable& TermVariableImpl::get_variable() const { return m_variable; }

std::ostream& operator<<(std::ostream& out, const TermObjectImpl& element)
{
auto formatter = PDDLFormatter();
formatter.write(element, out);
return out;
}

std::ostream& operator<<(std::ostream& out, const TermVariableImpl& element)
{
auto formatter = PDDLFormatter();
formatter.write(element, out);
return out;
}
const std::variant<Object, Variable>& TermImpl::get_object_or_variable() const { return m_object_or_variable; }

std::ostream& operator<<(std::ostream& out, const TermImpl& element)
{
Expand Down
6 changes: 3 additions & 3 deletions src/pddl/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ static void collect_free_variables_recursively(const loki::ConditionImpl& condit
{
for (const auto& term : condition_literal->get_literal()->get_atom()->get_terms())
{
if (const auto term_variable = std::get_if<loki::TermVariableImpl>(term))
if (const auto variable = std::get_if<Variable>(&term->get_object_or_variable()))
{
if (!ref_quantified_variables.count(term_variable->get_variable()))
if (!ref_quantified_variables.count(*variable))
{
ref_free_variables.insert(term_variable->get_variable());
ref_free_variables.insert(*variable);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/utils/segmented_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ TEST(LokiTests, UtilsSegmentedRepositoryVariantTest)
const auto object_0 = objects.get_or_create<ObjectImpl>("object_0", TypeList());
const auto object_1 = objects.get_or_create<ObjectImpl>("object_1", TypeList());

const auto term_0_object_0 = terms.get_or_create<TermObjectImpl>(object_0);
const auto term_1_object_0 = terms.get_or_create<TermObjectImpl>(object_0);
const auto term_2_object_1 = terms.get_or_create<TermObjectImpl>(object_1);
const auto term_0_object_0 = terms.get_or_create<TermImpl>(object_0);
const auto term_1_object_0 = terms.get_or_create<TermImpl>(object_0);
const auto term_2_object_1 = terms.get_or_create<TermImpl>(object_1);

EXPECT_EQ(term_0_object_0, term_1_object_0);
EXPECT_NE(term_0_object_0, term_2_object_1);
Expand Down

0 comments on commit 80c035e

Please sign in to comment.