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-06 22:58:42 +00:00
#ifndef PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H
#define PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H
2012-05-23 14:56:01 +01:00
#include <string>
2012-12-27 12:54:58 +00:00
2012-05-23 14:56:01 +01:00
#include <vector>
#include <unordered_map>
#include "TinySet.h"
#include "Util.h"
class Symbol
{
public:
Symbol (void) : id_(Util::maxUnsigned()) { }
Symbol (unsigned id) : id_(id) { }
operator unsigned (void) const { return id_; }
bool valid (void) const { return id_ != Util::maxUnsigned(); }
static Symbol invalid (void) { return Symbol(); }
private:
unsigned id_;
2013-02-07 13:37:15 +00:00
friend std::ostream& operator<< (std::ostream &os, const Symbol& s);
2012-05-23 14:56:01 +01:00
};
class LogVar
{
public:
LogVar (void) : id_(Util::maxUnsigned()) { }
LogVar (unsigned id) : id_(id) { }
operator unsigned (void) const { return id_; }
LogVar& operator++ (void);
2012-05-23 14:56:01 +01:00
bool valid (void) const;
2012-05-23 14:56:01 +01:00
private:
unsigned id_;
2013-02-07 13:37:15 +00:00
friend std::ostream& operator<< (std::ostream &os, const LogVar& X);
2012-05-23 14:56:01 +01:00
};
inline LogVar&
LogVar::operator++ (void)
{
assert (valid());
id_ ++;
return *this;
}
inline bool
LogVar::valid (void) const
{
return id_ != Util::maxUnsigned();
}
2012-05-23 14:56:01 +01:00
namespace std {
2012-05-23 14:56:01 +01:00
template <> struct hash<Symbol> {
size_t operator() (const Symbol& s) const {
return std::hash<unsigned>() (s);
}
};
2012-05-23 14:56:01 +01:00
template <> struct hash<LogVar> {
size_t operator() (const LogVar& X) const {
return std::hash<unsigned>() (X);
}
};
2012-05-23 14:56:01 +01:00
};
2013-02-07 13:37:15 +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
2013-02-07 13:37:15 +00:00
std::ostream& operator<< (std::ostream &os, const Tuple& t);
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 (void);
2012-05-23 14:56:01 +01:00
}
class Ground
{
public:
Ground (Symbol f) : functor_(f) { }
Ground (Symbol f, const Symbols& args) : functor_(f), args_(args) { }
Symbol functor (void) const { return functor_; }
Symbols args (void) const { return args_; }
2012-05-24 22:55:20 +01:00
size_t arity (void) const { return args_.size(); }
2012-05-23 14:56:01 +01:00
2012-12-27 12:54:58 +00:00
bool isAtom (void) const { return args_.empty(); }
2012-05-23 14:56:01 +01:00
private:
Symbol functor_;
Symbols args_;
2013-02-07 13:37:15 +00:00
friend std::ostream& operator<< (std::ostream &os, const Ground& gr);
2012-05-23 14:56:01 +01:00
};
2013-02-07 13:37:15 +00:00
typedef std::vector<Ground> Grounds;
2012-05-23 14:56:01 +01:00
class Substitution
{
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 (void) const;
LogVars getDiscardedLogVars (void) const;
2012-05-23 14:56:01 +01:00
private:
2013-02-07 13:37:15 +00:00
std::unordered_map<LogVar, LogVar> subs_;
2012-05-23 14:56:01 +01:00
2013-02-07 13:37:15 +00:00
friend std::ostream& operator<< (
std::ostream &os, const Substitution& theta);
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 (void) const
{
return subs_.size();
}
2013-02-06 22:58:42 +00:00
#endif // PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H
2012-05-23 14:56:01 +01:00