00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef CLASP_LITERAL_H_INCLUDED
00021 #define CLASP_LITERAL_H_INCLUDED
00022
00023 #ifdef _MSC_VER
00024 #pragma once
00025 #pragma warning (disable : 4996)
00026 #endif
00027 #include <clasp/util/platform.h>
00028 #include <clasp/pod_vector.h>
00029 #include <algorithm>
00030 #include <limits>
00035 namespace Clasp {
00040
00042 typedef uint32 Var;
00043
00045 const Var varMax = (Var(1) << 30);
00046
00048 const Var sentVar = 0;
00049
00051 const uint32 idMax = UINT32_MAX;
00052
00054 struct Var_t {
00055 enum Type { atom_var = 1, body_var = 2, atom_body_var = atom_var | body_var};
00056 static bool isBody(Type t) {
00057 return (static_cast<uint32>(t) & static_cast<uint32>(body_var)) != 0;
00058 }
00059 static bool isAtom(Type t) {
00060 return (static_cast<uint32>(t) & static_cast<uint32>(atom_var)) != 0;
00061 }
00062 };
00063 typedef Var_t::Type VarType;
00064
00066 typedef int32 weight_t;
00068 typedef int64 wsum_t;
00069
00071
00080 class Literal {
00081 public:
00083 Literal() : rep_(0) { }
00084
00086
00091 Literal(Var var, bool sign) : rep_( (var<<2) + (uint32(sign)<<1) ) {
00092 assert( var < varMax );
00093 }
00094
00096
00099 uint32 index() const { return rep_ >> 1; }
00100
00102
00105 static Literal fromIndex(uint32 idx) {
00106 assert( idx < (uint32(1)<<31) );
00107 return Literal(idx<<1);
00108 }
00109
00111 static Literal fromRep(uint32 rep) { return Literal(rep); }
00112
00113 uint32& asUint() { return rep_; }
00114 uint32 asUint() const { return rep_; }
00115
00117 Var var() const { return rep_ >> 2; }
00118
00120
00123 bool sign() const { return test_bit(rep_, 1); }
00124
00125 void swap(Literal& other) { std::swap(rep_, other.rep_); }
00126
00128 void watch() { store_set_bit(rep_, 0); }
00129
00131 void clearWatch() { store_clear_bit(rep_, 0); }
00132
00134 bool watched() const { return test_bit(rep_, 0); }
00135
00137
00141 inline Literal operator~() const {
00142 return Literal( (rep_ ^ 2) & ~static_cast<uint32>(1u) );
00143 }
00144
00146
00152 inline bool operator==(const Literal& rhs) const {
00153 return index() == rhs.index();
00154 }
00155 private:
00156 Literal(uint32 rep) : rep_(rep) {}
00157 uint32 rep_;
00158 };
00159 inline Literal operator^(Literal lhs, bool sign) { return Literal::fromIndex( lhs.index() ^ uint32(sign) ); }
00160 inline Literal operator^(bool sign, Literal rhs) { return rhs ^ sign; }
00162
00164
00166 inline Literal negLit(Var v) { return Literal(v, true);}
00168 inline Literal posLit(Var v) { return Literal(v, false);}
00170
00173 inline uint32 index(Var v) { return v << 1; }
00174
00176 inline bool isSentinel(Literal p) { return p.var() == sentVar; }
00177
00179 inline bool operator<(const Literal& lhs, const Literal& rhs) {
00180 return lhs.index() < rhs.index();
00181 }
00182
00184 inline bool operator!=(const Literal& lhs, const Literal& rhs) {
00185 return ! (lhs == rhs);
00186 }
00187
00188 inline void swap(Literal& l, Literal& r) {
00189 l.swap(r);
00190 }
00191
00192 typedef PodVector<Var>::type VarVec;
00193 typedef PodVector<Literal>::type LitVec;
00194 typedef PodVector<weight_t>::type WeightVec;
00195 typedef PodVector<wsum_t>::type SumVec;
00197 typedef std::pair<Literal, weight_t> WeightLiteral;
00198 typedef PodVector<WeightLiteral>::type WeightLitVec;
00200
00201
00203 typedef uint8 ValueRep;
00204 const ValueRep value_true = 1;
00205 const ValueRep value_false = 2;
00206 const ValueRep value_free = 0;
00207 typedef PodVector<ValueRep>::type ValueVec;
00208
00210
00217 inline ValueRep trueValue(const Literal& lit) { return 1 + lit.sign(); }
00218
00220
00227 inline ValueRep falseValue(const Literal& lit) { return 1 + !lit.sign(); }
00228
00230
00235 inline bool valSign(ValueRep v) { return v != value_true; }
00237
00239
00249 class SymbolTable {
00250 private:
00251 struct String {
00252 String(const char* n) : str(n) {}
00253 bool empty() const { return str == 0 || !*str; }
00254 const char* c_str() const { return str; }
00255 char operator[](uint32 i) const { return str[i]; }
00256 private: const char* str;
00257 };
00258 public:
00259 typedef uint32 key_type;
00260 typedef String data_type;
00261 struct symbol_type {
00262 symbol_type(Literal x = negLit(0), data_type d = 0)
00263 : lit(x), name(d) {}
00264 mutable Literal lit;
00265 data_type name;
00266 };
00267 typedef std::pair<key_type, symbol_type> value_type;
00268 typedef PodVector<value_type>::type map_type;
00269 typedef map_type::const_iterator const_iterator;
00270 enum MapType { map_direct, map_indirect };
00271
00273 SymbolTable() : lastSort_(0), lastStart_(0), end_(varMax), type_(map_indirect) { }
00274 ~SymbolTable() { clear(); }
00275 void copyTo(SymbolTable& o) {
00276 o.clear();
00277 o.map_.reserve(map_.size());
00278 for (const_iterator it = map_.begin(), end = map_.end(); it != end; ++it) {
00279 o.map_.push_back(value_type(it->first, symbol_type(it->second.lit, dupName(it->second.name.c_str()))));
00280 }
00281 o.lastSort_ = lastSort_;
00282 o.lastStart_= lastStart_;
00283 }
00285
00290 MapType type() const { return type_; }
00292 uint32 size() const { return type() == map_indirect ? (uint32)map_.size() : end_; }
00294 const_iterator begin() const { return map_.begin(); }
00296 const_iterator curBegin() const { return map_.begin() + lastStart_; }
00298 const_iterator end() const { return map_.begin() + lastSort_; }
00300 const symbol_type* find(key_type i) const {
00301 const_iterator it = std::lower_bound(begin(), end(), i, LessKey());
00302 return it != end() && it->first == i ? &it->second : 0;
00303 }
00304 const_iterator lower_bound(const_iterator start, key_type i) const {
00305 return std::lower_bound(start, end(), i, LessKey());
00306 }
00308
00311 const symbol_type& operator[](key_type id) const {
00312 const symbol_type* a = find(id); assert(a);
00313 return *a;
00314 }
00316 void clear() {
00317 for (const_iterator it = map_.begin(), end = map_.end(); it != end; ++it) {
00318 freeName(it->second.name.c_str());
00319 }
00320 map_.clear();
00321 lastSort_ = 0;
00322 lastStart_= 0;
00323 }
00325 void startInit() {
00326 lastStart_ = map_.size();
00327 lastSort_ = lastStart_;
00328 }
00330
00335 symbol_type& addUnique(key_type id, const char* name) {
00336 assert((lastSort_ == 0 || map_[lastSort_-1].first < id) && "Symbol table: Invalid id in incremental step!");
00337 map_.push_back(value_type(id, symbol_type(negLit(0), dupName(name))));
00338 return map_.back().second;
00339 }
00341
00342
00343
00344
00345 void endInit(MapType type = map_indirect, Var end = varMax) {
00346 std::sort(map_.begin()+lastSort_, map_.end(), LessKey());
00347 assert(unique() && "Symbol table: Duplicate atoms are not allowed\n");
00348 lastSort_ = map_.size();
00349 end_ = varMax;
00350 if ((type_=type) == map_direct) {
00351 end_ = end;
00352 }
00353 }
00354 private:
00355 SymbolTable(const SymbolTable&);
00356 SymbolTable& operator=(const SymbolTable&);
00357 const char* dupName(const char* n) const {
00358 if (!n) return 0;
00359 std::size_t len = std::strlen(n);
00360 char* dest= new char[len+1];
00361 std::strncpy(dest, n, len+1);
00362 return dest;
00363 }
00364 void freeName(const char* n) const { delete [] n; }
00365 bool unique() const {
00366 for (const_iterator it = map_.begin() + lastSort_, end = map_.end(), n = it + (it != end); n != end; it = n++) {
00367 if (it->first == n->first) { return false; }
00368 }
00369 return true;
00370 }
00371 struct LessKey {
00372 bool operator()(const value_type& lhs, const value_type& rhs) const { return lhs.first < rhs.first; }
00373 bool operator()(const value_type& lhs, key_type i) const { return lhs.first < i; }
00374 bool operator()(key_type i, const value_type& rhs) const { return i < rhs.first; }
00375 };
00376 map_type map_;
00377 map_type::size_type lastSort_;
00378 map_type::size_type lastStart_;
00379 uint32 end_;
00380 MapType type_;
00381 };
00382
00383 class ClaspError : public std::runtime_error {
00384 public:
00385 explicit ClaspError(const std::string& msg)
00386 : std::runtime_error(msg){}
00387 };
00388
00389 }
00390 #endif