#ifndef __TermSharing__
#define __TermSharing__
#include "Lib/Set.hpp"
#include "Lib/DHSet.hpp"
#include "Kernel/Term.hpp"
using namespace Lib;
using namespace Kernel;
namespace Indexing {
class TermSharing
{
public:
TermSharing();
~TermSharing();
void computeAndSetSharedTermData(Term*);
void computeAndSetSharedSortData(AtomicSort*);
void computeAndSetSharedLiteralData(Literal*);
void computeAndSetSharedVarEqData(Literal*, TermList eqSort);
Literal* tryGetOpposite(Literal* l);
void setPoly();
inline static unsigned hash(const Literal* l)
{ return l->hash(); }
inline static unsigned hash(const Term* t)
{ return t->hash(); }
static bool equals(const Term* t1,const Term* t2);
template<bool opposite = false>
static bool equals(const Literal* l1, const Literal* l2)
{ return Literal::literalEquals(l1, l2->functor(), l2->polarity() ^ opposite,
[&](auto i){ return *l2->nthArgument(i); },
l2->arity(), someIf(l2->isTwoVarEquality(), [&](){ return l2->twoVarEqSort(); })); }
DHSet<TermList>* getArraySorts(){
return &_arraySorts;
}
void resetEqualityArgumentOrders();
struct OpLitWrapper {
OpLitWrapper(Literal* l) : l(l) {}
Literal* l;
};
inline static unsigned hash(const OpLitWrapper& w)
{ return w.l->hash<true>(); }
static bool equals(const Literal* l1,const OpLitWrapper& w) {
return equals<true>(l1, w.l);
}
class WellSortednessCheckingLocalDisabler {
TermSharing* _tsInstance;
bool _valueToRestore;
public:
WellSortednessCheckingLocalDisabler(TermSharing* tsInstance) {
_tsInstance = tsInstance;
_valueToRestore = _tsInstance->_wellSortednessCheckingDisabled;
_tsInstance->_wellSortednessCheckingDisabled = true;
}
~WellSortednessCheckingLocalDisabler() {
_tsInstance->_wellSortednessCheckingDisabled = _valueToRestore;
}
};
bool isWellSortednessCheckingDisabled() const { return _wellSortednessCheckingDisabled; }
private:
friend class Kernel::Term;
friend class Kernel::Literal;
friend class Kernel::AtomicSort;
static bool argNormGt(TermList t1, TermList t2);
Set<Term*,TermSharing> _terms;
Set<Literal*,TermSharing> _literals;
Set<AtomicSort*,TermSharing> _sorts;
DHSet<TermList> _arraySorts;
bool _poly;
bool _wellSortednessCheckingDisabled;
};
}
#endif