#ifndef YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_ #define YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_ #include #include #include #include #include "TinySet.h" #include "Util.h" namespace horus { 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: friend std::ostream& operator<< (std::ostream&, const Symbol&); unsigned id_; }; class LogVar { public: LogVar (void) : id_(util::maxUnsigned()) { } LogVar (unsigned id) : id_(id) { } operator unsigned (void) const { return id_; } LogVar& operator++ (void); bool valid (void) const; private: friend std::ostream& operator<< (std::ostream&, const LogVar&); unsigned id_; }; inline LogVar& LogVar::operator++ (void) { assert (valid()); id_ ++; return *this; } inline bool LogVar::valid (void) const { return id_ != util::maxUnsigned(); } } // namespace horus namespace std { template <> struct hash { size_t operator() (const horus::Symbol& s) const { return std::hash() (s); }}; template <> struct hash { size_t operator() (const horus::LogVar& X) const { return std::hash() (X); }}; } // namespace std namespace horus { typedef std::vector Symbols; typedef std::vector Tuple; typedef std::vector Tuples; typedef std::vector LogVars; typedef TinySet SymbolSet; typedef TinySet LogVarSet; typedef TinySet TupleSet; std::ostream& operator<< (std::ostream&, const Tuple&); namespace LiftedUtils { Symbol getSymbol (const std::string&); void printSymbolDictionary (void); } 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_; } size_t arity (void) const { return args_.size(); } bool isAtom (void) const { return args_.empty(); } private: friend std::ostream& operator<< (std::ostream&, const Ground&); Symbol functor_; Symbols args_; }; typedef std::vector Grounds; class Substitution { public: void add (LogVar X_old, LogVar X_new); void rename (LogVar X_old, LogVar X_new); LogVar newNameFor (LogVar X) const; bool containsReplacementFor (LogVar X) const; size_t nrReplacements (void) const; LogVars getDiscardedLogVars (void) const; private: friend std::ostream& operator<< ( std::ostream&, const Substitution&); std::unordered_map subs_; }; inline void Substitution::add (LogVar X_old, LogVar X_new) { assert (util::contains (subs_, X_old) == false); 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 { std::unordered_map::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(); } } // namespace horus #endif // YAP_PACKAGES_CLPBN_HORUS_LIFTEDUTILS_H_