This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/horus/LiftedUtils.h

211 lines
3.5 KiB
C
Raw Normal View History

2013-02-07 17:50:02 +00:00
#ifndef YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_
#define YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_
2012-05-23 14:56:01 +01:00
#include <vector>
#include <unordered_map>
2013-02-07 20:09:10 +00:00
#include <string>
#include <ostream>
2012-05-23 14:56:01 +01:00
#include "TinySet.h"
#include "Util.h"
namespace Horus {
2013-02-07 23:53:13 +00:00
class Symbol {
2012-05-23 14:56:01 +01:00
public:
Symbol() : id_(Util::maxUnsigned()) { }
2012-05-23 14:56:01 +01:00
Symbol (unsigned id) : id_(id) { }
operator unsigned() const { return id_; }
2012-05-23 14:56:01 +01:00
bool valid() const { return id_ != Util::maxUnsigned(); }
2012-05-23 14:56:01 +01:00
static Symbol invalid() { return Symbol(); }
2012-05-23 14:56:01 +01:00
private:
friend std::ostream& operator<< (std::ostream&, const Symbol&);
unsigned id_;
2012-05-23 14:56:01 +01:00
};
class LogVar {
2012-05-23 14:56:01 +01:00
public:
LogVar() : id_(Util::maxUnsigned()) { }
2012-05-23 14:56:01 +01:00
LogVar (unsigned id) : id_(id) { }
operator unsigned() const { return id_; }
2012-05-23 14:56:01 +01:00
LogVar& operator++();
2012-05-23 14:56:01 +01:00
bool valid() const;
2012-05-23 14:56:01 +01:00
private:
friend std::ostream& operator<< (std::ostream&, const LogVar&);
unsigned id_;
2012-05-23 14:56:01 +01:00
};
inline LogVar&
LogVar::operator++()
{
assert (valid());
id_ ++;
return *this;
}
inline bool
LogVar::valid() const
{
return id_ != Util::maxUnsigned();
}
} // namespace Horus
2012-05-23 14:56:01 +01:00
namespace std {
template <> struct hash<Horus::Symbol> {
size_t operator() (const Horus::Symbol& s) const {
2012-05-23 14:56:01 +01:00
return std::hash<unsigned>() (s);
2013-02-08 00:56:42 +00:00
}};
2012-05-23 14:56:01 +01:00
template <> struct hash<Horus::LogVar> {
size_t operator() (const Horus::LogVar& X) const {
2012-05-23 14:56:01 +01:00
return std::hash<unsigned>() (X);
2013-02-08 00:56:42 +00:00
}};
2013-02-07 23:53:13 +00:00
} // namespace std
2012-05-23 14:56:01 +01:00
namespace Horus {
2013-02-07 23:53:13 +00:00
typedef std::vector<Symbol> Symbols;
typedef std::vector<Symbol> Tuple;
typedef std::vector<Tuple> Tuples;
typedef std::vector<LogVar> LogVars;
typedef TinySet<Symbol> SymbolSet;
typedef TinySet<LogVar> LogVarSet;
typedef TinySet<Tuple> TupleSet;
2012-05-23 14:56:01 +01:00
std::ostream& operator<< (std::ostream&, const Tuple&);
2012-05-23 14:56:01 +01:00
namespace LiftedUtils {
2013-02-07 13:37:15 +00:00
Symbol getSymbol (const std::string&);
void printSymbolDictionary();
2012-05-23 14:56:01 +01:00
}
class Ground {
2012-05-23 14:56:01 +01:00
public:
Ground (Symbol f) : functor_(f) { }
Ground (Symbol f, const Symbols& args)
: functor_(f), args_(args) { }
2012-05-23 14:56:01 +01:00
Symbol functor() const { return functor_; }
2012-05-23 14:56:01 +01:00
Symbols args() const { return args_; }
2012-05-23 14:56:01 +01:00
size_t arity() const { return args_.size(); }
2012-05-23 14:56:01 +01:00
bool isAtom() const { return args_.empty(); }
2012-05-23 14:56:01 +01:00
private:
friend std::ostream& operator<< (std::ostream&, const Ground&);
2012-05-23 14:56:01 +01:00
Symbol functor_;
Symbols args_;
};
2013-02-07 13:37:15 +00:00
typedef std::vector<Ground> Grounds;
2012-05-23 14:56:01 +01:00
class Substitution {
2012-05-23 14:56:01 +01:00
public:
void add (LogVar X_old, LogVar X_new);
2012-05-23 14:56:01 +01:00
void rename (LogVar X_old, LogVar X_new);
2012-05-23 14:56:01 +01:00
LogVar newNameFor (LogVar X) const;
bool containsReplacementFor (LogVar X) const;
size_t nrReplacements() const;
LogVars getDiscardedLogVars() const;
2012-05-23 14:56:01 +01:00
private:
2013-02-07 13:37:15 +00:00
friend std::ostream& operator<< (
std::ostream&, const Substitution&);
std::unordered_map<LogVar, LogVar> subs_;
2012-05-23 14:56:01 +01:00
};
inline void
Substitution::add (LogVar X_old, LogVar X_new)
{
assert (Util::contains (subs_, X_old) == false);
2013-02-07 13:37:15 +00:00
subs_.insert (std::make_pair (X_old, X_new));
}
inline void
Substitution::rename (LogVar X_old, LogVar X_new)
{
assert (Util::contains (subs_, X_old));
subs_.find (X_old)->second = X_new;
}
inline LogVar
Substitution::newNameFor (LogVar X) const
{
2013-02-07 13:37:15 +00:00
std::unordered_map<LogVar, LogVar>::const_iterator it;
it = subs_.find (X);
if (it != subs_.end()) {
return subs_.find (X)->second;
}
return X;
}
inline bool
Substitution::containsReplacementFor (LogVar X) const
{
return Util::contains (subs_, X);
}
inline size_t
Substitution::nrReplacements() const
{
return subs_.size();
}
} // namespace Horus
2013-02-07 23:53:13 +00:00
2013-02-08 00:20:01 +00:00
#endif // YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_
2012-05-23 14:56:01 +01:00