pax_global_header00006660000000000000000000000064115625503450014520gustar00rootroot0000000000000052 comment=d984c679c42cbd2311881f7949a1e85cfcd4f081 minisat+-1.0/000077500000000000000000000000001156255034500130775ustar00rootroot00000000000000minisat+-1.0/ADTs/000077500000000000000000000000001156255034500136725ustar00rootroot00000000000000minisat+-1.0/ADTs/FEnv.C000066400000000000000000000055331156255034500146420ustar00rootroot00000000000000/******************************************************************************************[FEnv.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #include "FEnv.h" namespace FEnv { vec nodes; Map uniqueness_table; vec stack; } //================================================================================================= static bool eval(Formula f, AMap& values, CMap& memo) { if (Const_p(f)) return !sign(f); else if (Atom_p(f)) return sign(f) ? !values.at(f) : values.at(f); else{ int ret = memo.at(f); if (ret == -1){ if (Bin_p(f)){ bool l = eval(left(f), values, memo); bool r = eval(left(f), values, memo); if (op(f) == op_And) ret = l & r; else{ assert(op(f) == op_Equiv); ret = (l ^ r) ^ 1; } }else if (ITE_p(f)){ bool sel = eval(cond(f), values, memo); bool tru = eval(tt (f), values, memo); bool fal = eval(ff (f), values, memo); ret = sel ? tru : fal; }else{ assert(FA_p(f)); bool x = eval(FA_x(f), values, memo); bool y = eval(FA_y(f), values, memo); bool c = eval(FA_c(f), values, memo); if (isCarry(f)) ret = ((int)x + (int) y + (int)c) >= 2; else ret = x ^ y ^ c; } memo.set(f, ret); } return sign(f) ? !ret : ret; } } bool eval(Formula f, AMap& values) { CMap memo(-1);; return eval(f, values, memo); } minisat+-1.0/ADTs/FEnv.h000066400000000000000000000335231156255034500147070ustar00rootroot00000000000000/******************************************************************************************[FEnv.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef FEnv_h #define FEnv_h //================================================================================================= #define ENV FEnv #define FML Formula #define UNDEF_INDEX -2 #include "Map.h" #include "VecMaps.h" enum Op { op_And, op_Equiv }; //------------------------------------------------------------------------------------------------- // Layout: 31..4=index, 3=sign, 2=compo, 1..0=unused class FML { unsigned data; public: FML(unsigned d) : data(d) { } FML(bool compo = false, int index = UNDEF_INDEX, bool sign = false) : data( ((unsigned)index << 4) | ((unsigned)sign << 3) | ((unsigned)compo << 2) ) { } operator unsigned(void) const { return data; } }; //------------------------------------------------------------------------------------------------- namespace ENV { struct NodeData { unsigned data0; unsigned data1; unsigned data2; NodeData(unsigned d0, unsigned d1, unsigned d2) : data0(d0), data1(d1), data2(d2) { } // unsigned hash(void) const { return data0 ^ data1 ^ data2; } unsigned hash(void) const { return data0 ^ ((data1 << 16) | (data1 >> 16)) ^ data2; } bool operator == (const NodeData& other) const { return data0 == other.data0 && data1 == other.data1 && data2 == other.data2; } }; extern vec nodes; extern Map uniqueness_table; } //------------------------------------------------------------------------------------------------- macro bool operator == (FML f, FML g) { return (unsigned)f == (unsigned)g; } macro bool operator != (FML f, FML g) { return (unsigned)f != (unsigned)g; } macro bool operator < (FML f, FML g) { return (unsigned)f < (unsigned)g; } template<> struct Hash { unsigned operator () (FML f) const { return (unsigned)f; } }; macro FML neg (FML f) { return FML((unsigned)f ^ 8); } macro FML unsign(FML f) { return FML((unsigned)f & ~8); } macro FML id (FML f, bool sign) { return FML((unsigned)f ^ ((unsigned)sign << 3)); } macro bool sign (FML f) { return ((unsigned)f & 8) != 0; } macro bool compo (FML f) { return ((unsigned)f & 4) != 0; } macro int index (FML f) { return (int)(unsigned)f >> 4; } macro int sindex(FML f) { return (int)(unsigned)f >> 3; } macro FML FormulaC(int index, bool sign = false) { return FML(true , index, sign); } macro FML FormulaA(int index, bool sign = false) { return FML(false, index, sign); } namespace ENV { macro FML Comp_new(int index, bool sign = false) { return FML(true , index, sign); } macro FML Atom_new(int index, bool sign = false) { return FML(false, index, sign); } } //------------------------------------------------------------------------------------------------- #ifndef tag_Atom #define tag_Atom (-1) #endif #define tag_Bin 0 #define tag_ITE 1 #define tag_FA 2 macro int ctag(FML f ) { return (int)(ENV::nodes[index(f)].data0 & 3); } macro int ctag(int index) { return (int)(ENV::nodes[index ].data0 & 3); } macro int tag (FML f ) { return compo(f) ? ctag(f) : tag_Atom; } macro bool Atom_p(FML f) { return !compo(f); } macro bool Bin_p (FML f) { return tag(f) == tag_Bin; } macro bool ITE_p (FML f) { return tag(f) == tag_ITE; } macro bool FA_p (FML f) { return tag(f) == tag_FA; } //------------------------------------------------------------------------------------------------- macro Op op (FML f) { return (Op)(ENV::nodes[index(f)].data1 & 0x3); } macro FML left (FML f) { return (FML)(ENV::nodes[index(f)].data1 & 0xFFFFFFFC); } macro FML right (FML f) { return (FML)(ENV::nodes[index(f)].data2 & 0xFFFFFFFC); } macro FML cond (FML f) { return (FML)(ENV::nodes[index(f)].data0 & 0xFFFFFFFC); } macro FML tt (FML f) { return (FML)(ENV::nodes[index(f)].data1 & 0xFFFFFFFC); } macro FML ff (FML f) { return (FML)(ENV::nodes[index(f)].data2 & 0xFFFFFFFC); } macro bool isCarry(FML f) { return (bool)(ENV::nodes[index(f)].data1 & 0x1); } macro FML FA_x (FML f) { return (FML)(ENV::nodes[index(f)].data0 & 0xFFFFFFFC); } macro FML FA_y (FML f) { return (FML)(ENV::nodes[index(f)].data1 & 0xFFFFFFFC); } macro FML FA_c (FML f) { return (FML)(ENV::nodes[index(f)].data2 & 0xFFFFFFFC); } //------------------------------------------------------------------------------------------------- macro ENV::NodeData Bin_newData(Op op, FML left, FML right) { #ifdef PARANOID assert((unsigned)op < 0x4U); assert(((unsigned)left & 0x3) == 0); assert(((unsigned)right & 0x3) == 0); #endif return ENV::NodeData(tag_Bin, (unsigned)op | (unsigned)left, (unsigned)right); } macro ENV::NodeData ITE_newData(FML cond, FML tt, FML ff) { #ifdef PARANOID assert(((unsigned)cond & 0x3) == 0); assert(((unsigned)tt & 0x3) == 0); assert(((unsigned)ff & 0x3) == 0); #endif return ENV::NodeData(tag_ITE | (unsigned)cond, (unsigned)tt, (unsigned)ff); } macro ENV::NodeData FA_newData(bool isCarry, FML FA_x, FML FA_y, FML FA_c) { #ifdef PARANOID assert((unsigned)isCarry < 0x2U); assert(((unsigned)FA_x & 0x3) == 0); assert(((unsigned)FA_y & 0x3) == 0); assert(((unsigned)FA_c & 0x3) == 0); #endif return ENV::NodeData(tag_FA | (unsigned)FA_x, (unsigned)isCarry | (unsigned)FA_y, (unsigned)FA_c); } namespace ENV { macro FML new_helper(ENV::NodeData node, bool sign) { int index = ENV::nodes.size(); ENV::nodes.push(node); return ENV::Comp_new(index, sign); } macro FML newS_helper(ENV::NodeData node, bool sign) { int index; if (!uniqueness_table.peek(node, index)){ index = ENV::nodes.size(); ENV::nodes.push(node); uniqueness_table.set(node, index); } return ENV::Comp_new(index, sign); } } macro FML Bin_new (Op op, FML left, FML right, bool sign = false, bool sharing = false) { return sharing ? ENV::newS_helper(Bin_newData(op, left, right), sign) : ENV::new_helper (Bin_newData(op, left, right), sign); } macro FML Bin_newS(Op op, FML left, FML right, bool sign = false) { return Bin_new(op, left, right, sign, true); } macro FML ITE_new (FML cond, FML tt, FML ff, bool sign = false, bool sharing = false) { return sharing ? ENV::newS_helper(ITE_newData(cond, tt, ff), sign) : ENV::new_helper (ITE_newData(cond, tt, ff), sign); } macro FML ITE_newS(FML cond, FML tt, FML ff, bool sign = false) { return ITE_new(cond, tt, ff, sign, true); } macro FML FA_new (bool isCarry, FML FA_x, FML FA_y, FML FA_c, bool sign = false, bool sharing = false) { return sharing ? ENV::newS_helper(FA_newData(isCarry, FA_x, FA_y, FA_c), sign) : ENV::new_helper (FA_newData(isCarry, FA_x, FA_y, FA_c), sign); } macro FML FA_newS(bool isCarry, FML FA_x, FML FA_y, FML FA_c, bool sign = false) { return FA_new(isCarry, FA_x, FA_y, FA_c, sign, true); } //------------------------------------------------------------------------------------------------- #define atom_Undef (-2) #define atom_True (-1) const FML _undef_ = ENV::Atom_new(atom_Undef, false); const FML _error_ = ENV::Atom_new(atom_Undef, true ); const FML _1_ = ENV::Atom_new(atom_True , false); const FML _0_ = ENV::Atom_new(atom_True , true ); // Must only be used for atoms: macro bool Exeception_p(FML f) { return index(f) == atom_Undef; } macro bool Exeception_p(int index) { return index == atom_Undef; } macro bool Const_p (FML f) { return index(f) == atom_True; } macro bool Const_p (int index) { return index == atom_True; } macro bool Var_p (FML f) { return index(f) >= 0; } macro bool Var_p (int index) { return index >= 0; } // Use indices >= 0 for variables! // macro Formula var(int index) { return FormulaA(index); } //------------------------------------------------------------------------------------------------- namespace ENV { template struct AtomMap : public VecMap { typedef FML Key; typedef T Datum; T at (FML f) { return VecMap::at(sgn ? sindex(f)+4 : index(f)+2); } void set(FML f, const T& value) { VecMap::set((sgn ? sindex(f)+4 : index(f)+2), value); } }; } namespace ENV { template class CompMap : DeckMap { int offset; public: typedef FML Key; typedef T Datum; CompMap(void) : DeckMap() , offset(ENV::nodes.size()) { } CompMap(T null) : DeckMap(null), offset(ENV::nodes.size()) { } T at (FML f) { return DeckMap::at((sgn ? sindex(f) : ::index(f)) - offset); } void set(FML f, T value) { DeckMap::set((sgn ? sindex(f) : ::index(f)) - offset, value); } }; template class FullMap { AtomMap amap; CompMap cmap; public: typedef FML Key; typedef T Datum; T at (FML f) { return compo(f) ? cmap.at(f) : amap.at(f); } void set(FML f, T value) { compo(f) ? cmap.set(f, value) : amap.set(f, value); } }; } #define AMap FEnv::AtomMap #define CMap FEnv::CompMap #define FMap FEnv::FullMap //------------------------------------------------------------------------------------------------- macro Formula operator ~ (Formula f) { return neg(f); } macro Formula operator & (Formula f, Formula g) { if (f == _0_ || g == _0_) return _0_; else if (f == _1_) return g; else if (g == _1_) return f; else if (f == g ) return f; else if (f == ~g ) return _0_; if (g < f) swp(f, g); return Bin_newS(op_And, f, g); } macro Formula operator | (Formula f, Formula g) { return ~(~f & ~g); } macro Formula operator ^ (Formula f, Formula g) { if (f == _0_) return g; else if (f == _1_) return ~g; else if (g == _0_) return f; else if (g == _1_) return ~f; else if (f == g ) return _0_; else if (f == ~g ) return _1_; if (g < f) swp(f, g); return Bin_newS(op_Equiv, unsign(f), unsign(g), sign(f) == sign(g)); } macro Formula operator &= (Formula& f, Formula g) { return f = f & g; } macro Formula operator |= (Formula& f, Formula g) { return f = f | g; } macro Formula operator ^= (Formula& f, Formula g) { return f = f ^ g; } macro Formula FAs(Formula x, Formula y, Formula c) // XOR of 3 arguments: x # y # c { bool sgn = sign(x) ^ sign(y) ^ sign(c); x = unsign(x); y = unsign(y); c = unsign(c); if (x == _1_) return id(y ^ c, !sgn); if (y == _1_) return id(x ^ c, !sgn); if (c == _1_) return id(x ^ y, !sgn); if (x == y) return id(c, sgn); if (x == c) return id(y, sgn); if (y == c) return id(x, sgn); if (c < y) swp(c, y); if (y < x) swp(y, x); if (c < y) swp(c, y); return FA_newS(false, x, y, c, sgn); } macro Formula FAc(Formula x, Formula y, Formula c) // x + y + c >= 2 { if (x == _0_) return y & c; if (x == _1_) return y | c; if (y == _0_) return x & c; if (y == _1_) return x | c; if (c == _0_) return x & y; if (c == _1_) return x | y; if (x == y) return x; if (x == c) return c; if (y == c) return y; if (x == ~y) return c; if (x == ~c) return y; if (y == ~c) return x; if (c < y) swp(c, y); if (y < x) swp(y, x); if (c < y) swp(c, y); bool s = sign(c); return FA_newS(true, id(x,s), id(y,s), unsign(c), s); } macro Formula ITE(Formula c, Formula t, Formula f) { if (c == _0_) return f; if (c == _1_) return t; if (t == f) return t; if (t == ~f) return c ^ f; if (t == _0_ || t == ~c) return ~c & f; if (t == _1_ || t == c) return c | f; if (f == _0_ || f == c) return c & t; if (f == _1_ || f == ~c) return ~c | t; if (t < f) swp(t, f), c = ~c; return ITE_newS(c, id(t,sign(f)), unsign(f), sign(f)); } //------------------------------------------------------------------------------------------------- bool eval(Formula f, AMap& values); namespace FEnv { extern vec stack; macro void clear() { nodes.clear(); uniqueness_table.clear(); } macro void push() { stack.push(nodes.size()); } macro void pop() { while (nodes.size() > stack.last()) uniqueness_table.remove(nodes.last()), nodes.pop(); } macro void keep() { stack.pop(); } macro int topSize() { return (stack.size() == 0) ? nodes.size() : nodes.size() - stack.last(); } } //------------------------------------------------------------------------------------------------- #undef ENV #undef FML #undef UNDEF_INDEX //================================================================================================= #endif minisat+-1.0/ADTs/File.C000066400000000000000000000127021156255034500146570ustar00rootroot00000000000000/******************************************************************************************[File.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #include "File.h" void File::open(int file_descr, FileMode m, bool own) { if (fd != -1) ::close(fd); fd = file_descr; mode = m; own_fd = own; pos = 0; buf = xmalloc(File_BufSize); if (mode == READ) size = read(fd, buf, File_BufSize); else size = -1; } void File::open(cchar* name, cchar* mode_) { if (fd != -1) ::close(fd); bool has_r = strchr(mode_, 'r') != NULL; bool has_w = strchr(mode_, 'w') != NULL; bool has_a = strchr(mode_, 'a') != NULL; bool has_p = strchr(mode_, '+') != NULL; assert(!(has_r && has_w)); assert(has_r || has_w || has_a); int mask = 0; if (has_p) mask |= O_RDWR; else if (has_r) mask |= O_RDONLY; else mask |= O_WRONLY; if (!has_r) mask |= O_CREAT; if (has_w) mask |= O_TRUNC; fd = open64(name, mask, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); if (fd != -1){ mode = has_r ? READ : WRITE; own_fd = true; pos = 0; if (has_a) lseek64(fd, 0, SEEK_END); buf = xmalloc(File_BufSize); if (mode == READ) size = read(fd, buf, File_BufSize); else size = -1; } } void File::close(void) { if (fd == -1) return; if (mode == WRITE) flush(); xfree(buf); buf = NULL; if (own_fd) ::close(fd); fd = -1; } void File::seek(int64 file_pos, int whence) { if (mode == WRITE){ flush(); pos = 0; lseek64(fd, file_pos, whence); }else{ if (whence == SEEK_CUR) lseek64(fd, file_pos - (size - pos), SEEK_CUR); else lseek64(fd, file_pos, whence); size = read(fd, buf, File_BufSize); pos = 0; } } int64 File::tell(void) { if (mode == WRITE) return lseek64(fd, 0, SEEK_CUR); else return lseek64(fd, 0, SEEK_CUR) - (size - pos); } //================================================================================================= // Marshaling: void putUInt(File& out, uint64 val) { if (val < 0x20000000){ uint v = (uint)val; if (v < 0x80) out.putChar(v); else{ if (v < 0x2000) out.putChar(0x80 | (v >> 8)), out.putChar((uchar)v); else if (v < 0x200000) out.putChar(0xA0 | (v >> 16)), out.putChar((uchar)(v >> 8)), out.putChar((uchar)v); else out.putChar((v >> 24) | 0xC0), out.putChar((uchar)(v >> 16)), out.putChar((uchar)(v >> 8)), out.putChar((uchar)v); } }else out.putChar(0xE0), out.putChar((uchar)(val >> 56)), out.putChar((uchar)(val >> 48)), out.putChar((uchar)(val >> 40)), out.putChar((uchar)(val >> 32)), out.putChar((uchar)(val >> 24)), out.putChar((uchar)(val >> 16)), out.putChar((uchar)(val >> 8)), out.putChar((uchar)val); } uint64 getUInt(File& in) // Returns 0 at end-of-file. { uint byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7; byte0 = in.getChar(); if (byte0 == (uint)EOF) return 0; if (!(byte0 & 0x80)) return byte0; else{ switch ((byte0 & 0x60) >> 5){ case 0: byte1 = in.getChar(); return ((byte0 & 0x1F) << 8) | byte1; case 1: byte1 = in.getChar(); byte2 = in.getChar(); return ((byte0 & 0x1F) << 16) | (byte1 << 8) | byte2; case 2: byte1 = in.getChar(); byte2 = in.getChar(); byte3 = in.getChar(); return ((byte0 & 0x1F) << 24) | (byte1 << 16) | (byte2 << 8) | byte3; case 3: byte0 = in.getChar(); byte1 = in.getChar(); byte2 = in.getChar(); byte3 = in.getChar(); byte4 = in.getChar(); byte5 = in.getChar(); byte6 = in.getChar(); byte7 = in.getChar(); return ((uint64)((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3) << 32) | (uint64)((byte4 << 24) | (byte5 << 16) | (byte6 << 8) | byte7); } assert(false); } } minisat+-1.0/ADTs/File.h000066400000000000000000000127741156255034500147350ustar00rootroot00000000000000/******************************************************************************************[File.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef File_h #define File_h #include #include #include #define lseek64 lseek // }- (disable explicit 64-bit support for FreeBSD...) #define open64 ::open // } //================================================================================================= // A buffered file abstraction with only 'putChar()' and 'getChar()'. #define File_BufSize 1024 // A small buffer seem to work just as fine as a big one (at least under Linux) enum FileMode { READ, WRITE }; // WARNING! This code is not thoroughly tested. May contain bugs! class File { int fd; // Underlying file descriptor. FileMode mode; // Reading or writing. uchar* buf; // Read or write buffer. int size; // Size of buffer (at end of file, less than 'File_BufSize'). int pos; // Current position in buffer bool own_fd; // Do we own the file descriptor? If so, will close file in destructor. public: #define DEFAULTS fd(-1), mode(READ), buf(NULL), size(-1), pos(0), own_fd(true) File(void) : DEFAULTS {} File(int fd, FileMode mode, bool own_fd = true) : DEFAULTS { open(fd, mode, own_fd); } File(cchar* name, cchar* mode) : DEFAULTS { open(name, mode); } #undef DEFAULTS ~File(void) { close(); } void open(int fd, FileMode mode, bool own_fd = true); // Low-level open. If 'own_fd' is FALSE, descriptor will not be closed by destructor. void open(cchar* name, cchar* mode); // FILE* compatible interface. void close(void); bool null(void) { // TRUE if no file is opened. return fd == -1; } int releaseDescriptor(void) { // Don't run UNIX function 'close()' on descriptor in 'File's 'close()'. if (mode == READ) lseek64(fd, pos - size, SEEK_CUR); own_fd = false; return fd; } FileMode getMode(void) { return mode; } void setMode(FileMode m) { if (m == mode) return; if (m == READ){ flush(); size = read(fd, buf, File_BufSize); }else{ lseek64(fd, pos - size, SEEK_CUR); size = -1; } mode = m; pos = 0; } int getCharQ(void) { // Quick version with minimal overhead -- don't call this in the wrong mode! #ifdef PARANOID assert(mode == READ); #endif if (pos < size) return (uchar)buf[pos++]; if (size < File_BufSize) return EOF; size = read(fd, buf, File_BufSize); pos = 0; if (size == 0) return EOF; return (uchar)buf[pos++]; } int putCharQ(int chr) { // Quick version with minimal overhead -- don't call this in the wrong mode! #ifdef PARANOID assert(mode == WRITE); #endif if (pos == File_BufSize) write(fd, buf, File_BufSize), pos = 0; return buf[pos++] = (uchar)chr; } int getChar(void) { if (mode == WRITE) setMode(READ); return getCharQ(); } int putChar(int chr) { if (mode == READ) setMode(WRITE); return putCharQ(chr); } bool eof(void) { assert(mode == READ); if (pos < size) return false; if (size < File_BufSize) return true; size = read(fd, buf, File_BufSize); pos = 0; if (size == 0) return true; return false; } void flush(void) { assert(mode == WRITE); write(fd, buf, pos); pos = 0; } void seek(int64 pos, int whence = SEEK_SET); int64 tell(void); }; //================================================================================================= // Some nice helper functions: void putUInt (File& out, uint64 val); uint64 getUInt (File& in); macro uint64 encode64(int64 val) { return (val >= 0) ? (uint64)val << 1 : (((uint64)(~val) << 1) | 1); } macro int64 decode64(uint64 val) { return ((val & 1) == 0) ? (int64)(val >> 1) : ~(int64)(val >> 1); } macro void putInt (File& out, int64 val) { putUInt(out, encode64(val)); } macro uint64 getInt (File& in) { return decode64(getUInt(in)); } //================================================================================================= #endif minisat+-1.0/ADTs/Global.C000066400000000000000000000051671156255034500152070ustar00rootroot00000000000000/****************************************************************************************[Global.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #include #include #include #include char* vnsprintf(const char* format, va_list args) { static FILE* dummy = fopen("/dev/null", "wb"); unsigned chars_written; char* ret; va_list args_copy; #ifdef __va_copy __va_copy (args_copy, args); #else args_copy = args; #endif chars_written = vfprintf(dummy, format, args); ret = xmalloc(chars_written + 1); ret[chars_written] = 255; args = args_copy; vsprintf(ret, format, args); assert(ret[chars_written] == 0); return ret; } char* nsprintf(const char* format, ...) { va_list args; va_start(args, format); char* ret = vnsprintf(format, args); va_end(args); return ret; } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - macro bool hasChar(cchar* text, int chr) { while (*text != 0) if (*text++ == chr) return true; return false; } void splitString(cchar* text, cchar* seps, vec& out) { while (hasChar(seps, *text)) text++; if (*text == 0) return; cchar* start = text; for(;;){ if (*text == 0 || hasChar(seps, *text)){ out.push(xstrndup(start, text-start)); while (hasChar(seps, *text)) text++; if (*text == 0) return; start = text; }else text++; } } minisat+-1.0/ADTs/Global.h000066400000000000000000000323611156255034500152500ustar00rootroot00000000000000/****************************************************************************************[Global.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ /************************************************************************************************** Contains types, macros, and inline functions generally useful in a C++ program. **************************************************************************************************/ #ifndef Global_h #define Global_h #include #include #include #include #include #include #include //================================================================================================= // Basic Types & Minor Things: #ifdef _MSC_VER typedef INT64 int64; typedef UINT64 uint64; typedef INT_PTR intp; typedef UINT_PTR uintp; #define I64_fmt "I64d" #else typedef long long int64; typedef unsigned long long uint64; typedef __PTRDIFF_TYPE__ intp; typedef unsigned __PTRDIFF_TYPE__ uintp; #define I64_fmt "lld" #endif typedef signed char schar; typedef unsigned char uchar; typedef unsigned int uint; typedef unsigned long ulong; typedef const char cchar; typedef short int16; typedef unsigned short uint16; typedef int int32; typedef unsigned uint32; #ifdef __LP64__ #define LP64 // LP : int=32 long,ptr=64 (unix model -- the windows model is LLP : int,long=32 ptr=64) #endif #define macro static inline template macro T min(T x, T y) { return (x < y) ? x : y; } template macro T max(T x, T y) { return (x > y) ? x : y; } template struct STATIC_ASSERTION_FAILURE; template <> struct STATIC_ASSERTION_FAILURE{}; #define TEMPLATE_FAIL STATIC_ASSERTION_FAILURE() #define PANIC(msg) assert((fprintf(stderr, "%s\n", msg), fflush(stderr), false)) #define Ping (fflush(stdout), fprintf(stderr, "%s %d\n", __FILE__, __LINE__), fflush(stderr)) #define INITIALIZER(tag) struct Initializer_ ## tag { Initializer_ ## tag(); } static Initializer_ ## tag ## _instance; Initializer_ ## tag:: Initializer_ ## tag(void) #define FINALIZER( tag) struct Finalizer_ ## tag { ~Finalizer_ ## tag(); } static Finalizer_ ## tag ## _instance; Finalizer_ ## tag::~Finalizer_ ## tag(void) #define elemsof(x) (sizeof(x)/sizeof(*(x))) template macro void swp(T& x, T& y) { // 'swap' is used by STL T tmp = x; x = y; y = tmp; } // Some GNUC extensions: // #ifdef __GNUC__ #define ___noreturn __attribute__ ((__noreturn__)) #define ___unused __attribute__ ((__unused__)) #define ___const __attribute__ ((__const__)) #define ___format(type,fmt,arg) __attribute__ ((__format__(type, fmt, arg) )) #if (__GNUC__ > 2) || (__GNUC__ >= 2 && __GNUC_MINOR__ > 95) #define ___malloc __attribute__ ((__malloc__)) #else #define ___malloc #endif #else #define ___noreturn #define ___unused #define ___const #define ___format(type,fmt,arg) #define ___malloc #endif // The right 'sprintf()' -- allocating the string itself! char* nsprintf (cchar* format, ...) ___format(printf, 1, 2) ___malloc; char* vnsprintf(const char* format, va_list args) ___malloc; //================================================================================================= // 'malloc()'-style memory allocation -- never returns NULL; aborts instead: template macro T* xmalloc(size_t size) { T* tmp = (T*)malloc(size * sizeof(T)); assert(size == 0 || tmp != NULL); return tmp; } template macro T* xrealloc(T* ptr, size_t size) { T* tmp = (T*)realloc((void*)ptr, size * sizeof(T)); assert(size == 0 || tmp != NULL); return tmp; } template macro void xfree(T *ptr) { if (ptr != NULL) free((void*)ptr); } macro char* Xstrdup(cchar* src); macro char* Xstrdup(cchar* src) { int size = strlen(src)+1; char* tmp = xmalloc(size); memcpy(tmp, src, size); return tmp; } #define xstrdup(s) Xstrdup(s) macro char* xstrndup(cchar* src, int len) ___malloc; macro char* xstrndup(cchar* src, int len) { int size; for (size = 0; size < len && src[size] != '\0'; size++); char* tmp = xmalloc(size+1); memcpy(tmp, src, size); tmp[size] = '\0'; return tmp; } //================================================================================================= // Random numbers: // Returns a random float 0 <= x < 1. Seed must never be 0. macro double drand(double& seed) { seed *= 1389796; int q = (int)(seed / 2147483647); seed -= (double)q * 2147483647; return seed / 2147483647; } // Returns a random integer 0 <= x < size. Seed must never be 0. macro int irand(double& seed, int size) { return (int)(drand(seed) * size); } //================================================================================================= // Time: #ifdef _MSC_VER #include macro double cpuTime(void) { return (double)clock() / CLOCKS_PER_SEC; } #else #include #include macro double cpuTime(void) { struct rusage ru; getrusage(RUSAGE_SELF, &ru); return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000; } #endif //================================================================================================= // 'Pair': template struct Pair { typedef Fst Fst_t; typedef Snd Snd_t; Fst fst; Snd snd; Pair(void) { } Pair(const Fst& x, const Snd& y) : fst(x), snd(y) { } template Pair(const Pair& p) : fst(p.fst), snd(p.snd) { } void split(Fst& out_fst, Snd& out_snd) { out_fst = fst; out_snd = snd; } }; template inline bool operator == (const Pair& x, const Pair& y) { return (x.fst == y.fst) && (x.snd == y.snd); } template inline bool operator < (const Pair& x, const Pair& y) { return x.fst < y.fst || (!(y.fst < x.fst) && x.snd < y.snd); } template inline Pair Pair_new(const Fst& x, const Snd& y) { return Pair(x, y); } //================================================================================================= // 'vec' -- automatically resizable arrays (via 'push()' method): // NOTE! Don't use this vector on datatypes that cannot be re-located in memory (with realloc) template class vec { T* data; int sz; int cap; void init(int size, const T& pad); void grow(int min_cap); public: // Types: typedef int Key; typedef T Datum; // Constructors: vec(void) : data(NULL) , sz(0) , cap(0) { } vec(int size) : data(NULL) , sz(0) , cap(0) { growTo(size); } vec(int size, const T& pad) : data(NULL) , sz(0) , cap(0) { growTo(size, pad); } vec(T* array, int size) : data(array), sz(size), cap(size) { } // (takes ownership of array -- will be deallocated with 'xfree()') ~vec(void) { clear(true); } // Ownership of underlying array: T* release (void) { T* ret = data; data = NULL; sz = 0; cap = 0; return ret; } operator T* (void) { return data; } // (unsafe but convenient) operator const T* (void) const { return data; } // Size operations: int size (void) const { return sz; } void shrink (int nelems) { assert(nelems <= sz); for (int i = 0; i < nelems; i++) sz--, data[sz].~T(); } void shrink_ (int nelems) { assert(nelems <= sz); sz -= nelems; } void pop (void) { sz--, data[sz].~T(); } void growTo (int size); void growTo (int size, const T& pad); void clear (bool dealloc = false); void clear_ (void) { sz = 0; } void capacity (int size) { grow(size); } // Stack interface: void push (void) { if (sz == cap) grow(sz+1); new (&data[sz]) T() ; sz++; } void push (const T& elem) { if (sz == cap) grow(sz+1); new (&data[sz]) T(elem); sz++; } //void push (const T& elem) { if (sz == cap) grow(sz+1); data[sz++] = elem; } void push_ (const T& elem) { data[sz++] = elem; } const T& last (void) const { return data[sz-1]; } T& last (void) { return data[sz-1]; } // Vector interface: #ifdef DEBUG const T& operator [] (int index) const { assert((uint)index < (uint)sz); return data[index]; } T& operator [] (int index) { assert((uint)index < (uint)sz); return data[index]; } #else const T& operator [] (int index) const { return data[index]; } T& operator [] (int index) { return data[index]; } #endif // Don't allow copying (error prone): vec& operator = (vec& other) { TEMPLATE_FAIL; return *this; } vec (vec& other) { TEMPLATE_FAIL; } // Duplicatation (preferred instead): void copyTo(vec& copy) const { copy.clear(); copy.growTo(sz); for (int i = 0; i < sz; i++) new (©[i]) T(data[i]); } void moveTo(vec& dest) { dest.clear(true); dest.data = data; dest.sz = sz; dest.cap = cap; data = NULL; sz = 0; cap = 0; } }; template void vec::grow(int min_cap) { if (min_cap <= cap) return; if (cap == 0) cap = (min_cap >= 2) ? min_cap : 2; else do cap = (cap*3+1) >> 1; while (cap < min_cap); data = xrealloc(data, cap); } template void vec::growTo(int size, const T& pad) { if (sz >= size) return; grow(size); for (int i = sz; i < size; i++) new (&data[i]) T(pad); sz = size; } template void vec::growTo(int size) { if (sz >= size) return; grow(size); for (int i = sz; i < size; i++) new (&data[i]) T(); sz = size; } template void vec::clear(bool dealloc) { if (data != NULL){ for (int i = 0; i < sz; i++) data[i].~T(); sz = 0; if (dealloc) xfree(data), data = NULL, cap = 0; } } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // (for convenience) void splitString(cchar* text, cchar* seps, vec& out); template macro void xfreeAll(vec& ptrs) { for (int i = 0; i < ptrs.size(); i++) xfree(ptrs[i]); } //================================================================================================= // Lifted booleans: class lbool { int value; explicit lbool(int v) : value(v) { } public: lbool() : value(0) { } lbool(bool x) : value((int)x*2-1) { } int toInt(void) const { return value; } bool operator == (const lbool& other) const { return value == other.value; } bool operator != (const lbool& other) const { return value != other.value; } lbool operator ~ (void) const { return lbool(-value); } friend int toInt (lbool l); friend lbool toLbool(int v); friend char name (lbool l); }; inline int toInt (lbool l) { return l.toInt(); } inline lbool toLbool(int v) { return lbool(v); } inline char name (lbool l) { static char name[4] = {'!','0','?','1'}; int x = l.value; x = 2 + ((x >> (sizeof(int)*8-2)) | (x & ~(1 << (sizeof(int)*8-1)))); return name[x]; } const lbool l_True = toLbool( 1); const lbool l_False = toLbool(-1); const lbool l_Undef = toLbool( 0); const lbool l_Error = toLbool(1 << (sizeof(int)*8-1)); //================================================================================================= // Relation operators -- extend definitions from '==' and '<' #ifndef __SGI_STL_INTERNAL_RELOPS // (be aware of SGI's STL implementation...) #define __SGI_STL_INTERNAL_RELOPS template macro bool operator != (const T& x, const T& y) { return !(x == y); } template macro bool operator > (const T& x, const T& y) { return y < x; } template macro bool operator <= (const T& x, const T& y) { return !(y < x); } template macro bool operator >= (const T& x, const T& y) { return !(x < y); } #endif //================================================================================================= #endif minisat+-1.0/ADTs/Hash_standard.h000066400000000000000000000114541156255034500166130ustar00rootroot00000000000000/*********************************************************************************[Hash_standard.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef Hash_standard_h #define Hash_standard_h //================================================================================================= // Some Primes... static int prime_twins[25] = { 31, 73, 151, 313, 643, 1291, 2593, 5233, 10501, 21013, 42073, 84181, 168451, 337219, 674701, 1349473, 2699299, 5398891, 10798093, 21596719, 43193641, 86387383, 172775299, 345550609, 691101253 }; //================================================================================================= // Standard hash parameters: template struct Hash { uint operator () (const K& key) const { return key.hash(); } }; template struct Equal { bool operator () (const K& key1, const K& key2) const { return key1 == key2; } }; template struct Hash_params { static uint hash (K key) { return Hash ()(key); } static bool equal(K key1, K key2) { return Equal()(key1, key2); } }; //================================================================================================= // DEFINE PATTERN MACRO: // // 'code' should use 'key' (hash) or 'key1'/'key2' (equal) to access the elements. The last statement should be a return. // #define DefineHash( type, code) template <> struct Hash { uint operator () (type key) const { code } }; #define DefineEqual(type, code) template <> struct Equal { bool operator () (type key1, type key2) const { code } }; // PAIRS: template struct Hash > { uint operator () (const Pair& key) const { return Hash()(key.fst) ^ Hash()(key.snd); } }; template struct Equal > { bool operator () (const Pair& key1, const Pair& key2) const { return Equal()(key1.fst, key2.fst) && Equal()(key1.snd, key2.snd); } }; // POINTERS: #ifdef LP64 template struct Hash { uint operator () (const K* key) const { uintp tmp = reinterpret_cast(key); return (unsigned)((tmp >> 32) ^ tmp); } }; template struct Hash { uint operator () (K* key) const { uintp tmp = reinterpret_cast(key); return (unsigned)((tmp >> 32) ^ tmp); } }; #else template struct Hash { uint operator () (const K* key) const { return reinterpret_cast(key); } }; template struct Hash { uint operator () (K* key) const { return reinterpret_cast(key); } }; #endif // C-STRINGS: DefineHash (const char*, uint v = 0; for (int i = 0; key[i] != '\0'; i++) v = (v << 3) + key[i]; return v;) DefineEqual(const char*, if (key1 == key2) return true; else return strcmp(key1, key2) == 0;) DefineHash (char*, uint v = 0; for (int i = 0; key[i] != '\0'; i++) v = (v << 3) + key[i]; return v;) DefineEqual(char*, if (key1 == key2) return true; else return strcmp(key1, key2) == 0;) // INTEGER TYPES: DefineHash(char , return (uint)key;) DefineHash(signed char , return (uint)key;) DefineHash(unsigned char , return (uint)key;) DefineHash(short , return (uint)key;) DefineHash(ushort, return (uint)key;) DefineHash(int , return (uint)key;) DefineHash(uint , return (uint)key;) #ifdef LP64 DefineHash(long , return (uint)(((ulong)key >> 32) ^ key);) DefineHash(ulong , return (uint)(((ulong)key >> 32) ^ key);) #else DefineHash(long , return (uint)key;) DefineHash(ulong , return (uint)key;) #endif DefineHash(int64 , return (uint)(((uint64)key >> 32) ^ key);) DefineHash(uint64, return (uint)(((uint64)key >> 32) ^ key);) //================================================================================================= #endif minisat+-1.0/ADTs/Heap.h000066400000000000000000000066701156255034500147310ustar00rootroot00000000000000/******************************************************************************************[Heap.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef __HEAP__ #define __HEAP__ static inline int left (int i) { return i * 2; } static inline int right(int i) { return i * 2 + 1; } static inline int parent(int i) { return i / 2; } template class Heap { public: C comp; vec heap; // heap of ints vec indices; // int -> index in heap inline void percolateUp(int i) { int x = heap[i]; while (parent(i) != 0 && comp(x,heap[parent(i)])){ heap[i] = heap[parent(i)]; indices[heap[i]] = i; i = parent(i); } heap[i] = x; indices[x] = i; } inline void percolateDown(int i) { int x = heap[i]; while (left(i) < heap.size()){ int child = right(i) < heap.size() && comp(heap[right(i)],heap[left(i)]) ? right(i) : left(i); if (!comp(heap[child],x)) break; heap[i] = heap[child]; indices[heap[i]] = i; i = child; } heap[i] = x; indices[x] = i; } bool ok(int n) { return n >= 0 && n < (int)indices.size(); } public: Heap(C c) : comp(c) { heap.push(-1); } void setBounds (int size) { assert(size >= 0); indices.growTo(size,0); } bool inHeap (int n) { assert(ok(n)); return indices[n] != 0; } void increase (int n) { assert(ok(n)); assert(inHeap(n)); percolateUp(indices[n]); } bool empty (void) { return heap.size() == 1; } void insert(int n) { assert(ok(n)); indices[n] = heap.size(); heap.push(n); percolateUp(indices[n]); } int getmin(void) { int r = heap[1]; heap[1] = heap.last(); indices[heap[1]] = 1; indices[r] = 0; heap.pop(); if (heap.size() > 1) percolateDown(1); return r; } int peekmin(void) { return heap[1]; } bool heapProperty(void) { return heapProperty(1); } bool heapProperty(int i) { return (size_t)i >= heap.size() || ((parent(i) == 0 || !comp(heap[i],heap[parent(i)])) && heapProperty(left(i)) && heapProperty(right(i))); } }; #endif minisat+-1.0/ADTs/Int.h000066400000000000000000000245271156255034500146070ustar00rootroot00000000000000/*******************************************************************************************[Int.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef Int_h #define Int_h //================================================================================================= struct Exception_IntOverflow { char* where; Exception_IntOverflow(char* w) : where(w) {} // (takes ownership of string) }; #ifdef NO_GMP //================================================================================================= // Fake bignums using 'int64': //================================================================================================= #define Int_Max__ 9223372036854775807LL #define Int_Min__ (-Int_Max__) #define Int_Undef__ (-Int_Max__ - 1LL) //------------------------------------------------------------------------------------------------- #define A1 assert(data != Int_Undef__); #define A2 assert(data != Int_Undef__), assert(other.data != Int_Undef__); // NOTE! This is not a proper abstraction of big numbers. It just includes the operators // used in 'PbSolver'. It should be easy enough to add the missing operators on demand. // class Int { int64 data; public: Int() : data(Int_Undef__) {} Int(int x) : data(x) {} Int(int64 x) : data(x) {} // "operator =" and copy-constructor "Int(const Int& src)" are default defined to the right thing. uint hash() const {A1 return (uint)data ^ (uint)(data >> 32); } bool operator == (Int other) const {A2 return data == other.data; } bool operator != (Int other) const {A2 return data != other.data; } bool operator < (Int other) const {A2 return data < other.data; } bool operator > (Int other) const {A2 return data > other.data; } bool operator <= (Int other) const {A2 return data <= other.data; } bool operator >= (Int other) const {A2 return data >= other.data; } Int operator & (Int other) const {A2 return Int(data & other.data); } Int& operator >>= (int n) {A1 data >>= n; return *this; } Int operator - () const {A1 return Int(-data); } Int& operator ++ () {A1 ++data; return *this; } Int& operator -= (Int other) {A2 data -= other.data; return *this; } Int& operator += (Int other) {A2 data += other.data; return *this; } Int& operator *= (Int other) {A2 data *= other.data; return *this; } Int& operator /= (Int other) {A2 data /= other.data; return *this; } Int operator + (Int other) const {A2 return Int(data + other.data); } Int operator - (Int other) const {A2 return Int(data - other.data); } Int operator * (Int other) const {A2 return Int(data * other.data); } Int operator / (Int other) const {A2 return Int(data / other.data); } Int operator % (Int other) const {A2 return Int(data % other.data); } friend char* toString(Int num) { char buf[32]; sprintf(buf, "%lld", num.data); return xstrdup(buf); } // Caller must free string. friend int toint (Int num) { if (num > INT_MAX || num < INT_MIN) throw Exception_IntOverflow(xstrdup("toint")); return (int)num.data; } }; #define Int_MAX Int(Int_Max__) #define Int_MIN Int(Int_Min__) #undef A1 #undef A2 #else //================================================================================================= // Real bignums using "GNU Multiple Precision Arithmetic Library" //================================================================================================= #include "gmp.h" //================================================================================================= #define A1 assert(!small()); #define A2 assert(!small()); assert(!other.small()); //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #define Int_MIN Int((mpz_t*)-1) #define Int_MAX Int((mpz_t*)1) class Int { mpz_t* data; // This pointer is meant to contain small integers when bit 0 is set (for efficiency). // Currently the only small integers used are the special values 'Int_MIN' and 'Int_MAX'. bool small() const { return ((intp)data & 1) != 0; } public: // Constructors/Destructor (+assignment operator) // Int(mpz_t* d) : data(d) {} // Low-level constructor -- don't use! Int() { data = xmalloc(1); assert(((intp)data & 1) == 0); mpz_init(*data); } Int(int x) { data = xmalloc(1); assert(((intp)data & 1) == 0); mpz_init_set_si(*data, x); } Int(const Int& src) { if (src.small()) data = src.data; else{ data = xmalloc(1); assert(((intp)data & 1) == 0); mpz_init_set(*data, *src.data); } } ~Int() { if (!small()){ mpz_clear(*data); xfree(data); } data = 0; } Int& operator = (const Int& other) { if (&other != this){ if (other.small()){ this->~Int(); data = other.data; }else{ if (small()){ data = xmalloc(1); assert(((intp)data & 1) == 0); mpz_init_set(*data, *other.data); }else mpz_set(*data, *other.data); } } return *this; } // Operators: // // -- Comparison (supports infinity) // '+oo' and '-oo' are treated as two unique points beyond the integers. For instanse '+oo' is not < than itself, but <= than itself. bool operator == (const Int& other) const { if (small()) return other.small() ? (data == other.data) : false; else return other.small() ? false : mpz_cmp(*data, *other.data) == 0; } bool operator < (const Int& other) const { if (small()){ if (data == Int_MIN.data) return (!other.small() || other.data != Int_MIN.data); else{ assert(data == Int_MAX.data); return false; } }else{ if (other.small()){ if (other.data == Int_MIN.data) return false; else{ assert(other.data == Int_MAX.data); return true; } }else return mpz_cmp(*data, *other.data) < 0; } } bool operator != (const Int& other) const { return !(*this == other); } bool operator >= (const Int& other) const { return !(*this < other); } bool operator > (const Int& other) const { return other < *this; } bool operator <= (const Int& other) const { return !(*this > other); } // -- Arithmetic (not allowed on infinity except for unary '-') Int operator + (const Int& other) const {A2 Int ret; mpz_add (*ret.data, *data, *other.data); return ret; } Int operator - (const Int& other) const {A2 Int ret; mpz_sub (*ret.data, *data, *other.data); return ret; } Int operator * (const Int& other) const {A2 Int ret; mpz_mul (*ret.data, *data, *other.data); return ret; } Int operator / (const Int& other) const {A2 Int ret; mpz_tdiv_q(*ret.data, *data, *other.data); return ret; } Int operator % (const Int& other) const {A2 Int ret; mpz_tdiv_r(*ret.data, *data, *other.data); return ret; } Int& operator += (const Int& other) {A2 mpz_add (*data, *data, *other.data); return *this; } Int& operator -= (const Int& other) {A2 mpz_sub (*data, *data, *other.data); return *this; } Int& operator *= (const Int& other) {A2 mpz_mul (*data, *data, *other.data); return *this; } Int& operator /= (const Int& other) {A2 mpz_tdiv_q(*data, *data, *other.data); return *this; } Int& operator %= (const Int& other) {A2 mpz_tdiv_r(*data, *data, *other.data); return *this; } Int& operator ++ () { return *this += Int(1); } Int& operator -- () { return *this -= Int(1); } Int operator - () const { if (small()) return Int((mpz_t*)(-(intp)data)); else{ Int ret; mpz_neg(*ret.data, *data); return ret; } } // -- Bit operators (incomplete; we don't need more at the moment) Int operator & (const Int& other) const {A2 Int ret; mpz_and(*ret.data, *data, *other.data); return ret; } Int& operator >>= (int n) {A1 mpz_fdiv_q_2exp(*data, *data, n); return *this; } // Methods: // friend char* toString(Int num) { if (num == Int_MIN) return xstrdup("-oo"); else if (num == Int_MAX) return xstrdup("+oo"); assert(!num.small()); char* tmp = xmalloc(mpz_sizeinbase (*num.data, 10) + 2); mpz_get_str(tmp, 10, *num.data); return tmp; } friend int toint (Int num) { if (num.small() || !mpz_fits_sint_p(*num.data)) throw Exception_IntOverflow(xstrdup("toint")); return (int)mpz_get_si(*num.data); } uint hash() const { // primitive hash function -- not good with bit-shifts mp_size_t size = mpz_size(*data); mp_limb_t val = 0; for (mp_size_t i = 0; i < size; i++){ mp_limb_t limb = mpz_getlimbn(*data, i); val ^= limb; } #ifdef LP64 return (uint)val | (uint)(val >> 32); #else return (uint)val; #endif } }; //================================================================================================= #endif #endif minisat+-1.0/ADTs/Map.h000066400000000000000000000210641156255034500145630ustar00rootroot00000000000000/*******************************************************************************************[Map.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ /* Requires copy constructor and assignment operator to be defined for the key and datum types, (plus default constructor unless null value is passed to constructor) */ #ifndef Map_h #define Map_h #include "VecAlloc.h" #include "Hash_standard.h" //================================================================================================= // Map implementation: template > class Map { struct Cell { K key; D datum; Cell* next; }; VecAlloc alloc; Cell** table; int capacity; int nelems; const D D_null; //--------------------------------------------------------------------------------------------- int index(const K& key) const { return Par::hash(key) % capacity; } // (this is done in 'rehash()' as well) int getCapacity(int min_capacity) { int i; for (i = 0; prime_twins[i] < min_capacity; i++); return prime_twins[i]; } void init(int min_capacity) { capacity = getCapacity(min_capacity); nelems = 0; table = xmalloc(capacity); for (int i = 0; i < capacity; i++) table[i] = NULL; } void dispose(void) { for (int i = 0; i < capacity; i++){ for (Cell* p = table[i]; p != NULL;){ Cell* next = p->next; p->key .~K(); p->datum.~D(); alloc.free(p); p = next; } } xfree(table); } void rehash(int min_capacity) { int new_capacity = getCapacity(min_capacity); Cell** new_table = xmalloc(new_capacity); for (int i = 0; i < new_capacity; i++) new_table[i] = NULL; for (int i = 0; i < capacity; i++){ for (Cell* p = table[i]; p != NULL;){ Cell* next = p->next; unsigned j = Par::hash(p->key) % new_capacity; p->next = new_table[j]; new_table[j] = p; p = next; } } xfree(table); table = new_table; capacity = new_capacity; } D& newEntry(int i, const K& key, const D& value) { if (nelems > capacity / 2){ rehash(capacity * 2); i = index(key); } Cell* p = alloc.alloc(); new (&p->key) K(key); new (&p->datum) D(value); p->next = table[i]; table[i] = p; nelems++; return p->datum; } //--------------------------------------------------------------------------------------------- public: // Types: typedef K Key; typedef D Datum; // Constructors: Map(void) : D_null() { init(1); } Map(const D& null) : D_null(null) { init(1); } Map(const D& null, int capacity) : D_null(null) { init(capacity); } ~Map(void) { dispose(); } // Size operations: int size (void) const { return nelems; } void clear (void) { dispose(); init(1); } // Don't allow copying: Map& operator = (Map& other) { TEMPLATE_FAIL; return *this; } Map (Map& other) { TEMPLATE_FAIL; } //--------------------------------------------------------------------------------------------- // Export: void domain(vec& result) const { for (int i = 0; i < capacity; i++) for (Cell* p = table[i]; p != NULL; p = p->next) result.push(p->key); } void range (vec& result) const { for (int i = 0; i < capacity; i++) for (Cell* p = table[i]; p != NULL; p = p->next) result.push(p->datum); } void pairs (vec >& result) const { for (int i = 0; i < capacity; i++) for (Cell* p = table[i]; p != NULL; p = p->next) result.push(Pair_new(p->key, p->datum)); } //--------------------------------------------------------------------------------------------- // Main: // Searches for 'key' in the hash table. Returns the value of 'found' or 'notfound'. // 'found' may use 'Cell* p' refering to the cell that was found; 'notfound' may use // 'unsigned i', the index position in the table. #define SEARCH(found, notfound) \ unsigned i = index(key); \ for (Cell* p = table[i]; p != NULL; p = p->next){ \ if (Par::equal(p->key, key)) \ return found; \ } \ return notfound; // Returns a reference to existing or newly created element. D& ref(const K& key) { SEARCH( p->datum , newEntry(i, key, D_null) );} // Sets 'key' to 'value' and returns 'value'. const D& set(const K& key, const D& value) { SEARCH( p->datum = value , newEntry(i, key, value) );} // Sets 'key' to 'value' IF not already set. Returns the value at 'key' after operation (new or old). const D& weakSet(const K& key, const D& value) { SEARCH( p->datum , newEntry(i, key, value) );} // Same as 'set()' but 'key' must not be set already. const D& add(const K& key, const D& value) { SEARCH( (PANIC("Tried to add an already existing element to the hashtable."), D_null) , newEntry(i, key, value) );} // Same as 'has' but returns through reference a pointer to the element (if it exists). bool peek(const K& key, D*& result) const { SEARCH( (result = &p->datum, true) , false );} // Same as 'has' but returns through reference the element (if it exists). bool peek(const K& key, D& result) const { SEARCH( (result = p->datum, true) , false );} // Returns datum at 'key' (or the null value if none) const D& at(const K& key) { SEARCH( p->datum , D_null );} // Same as 'at' but 'key' must contain a value. const D& find(const K& key) { SEARCH( p->datum , (PANIC("Tried to find non-existing element in hashtable."), D_null) );} // Have 'key' been set? bool has(const K& key) { SEARCH( true , false );} // Get the current equal (but not same) key used for 'key'. const K& findKey(const K& key) { SEARCH( p->key , (PANIC("Tried to find non-existing element in hashtable."), *(K*)NULL) );} // Exclude 'key' if exists. bool exclude(const K& key) { unsigned i = index(key); for (Cell** pp = &table[i]; *pp != NULL; pp = &(*pp)->next){ if (Par::equal((*pp)->key, key)){ Cell* p = *pp; *pp = (*pp)->next; nelems--; p->key .~K(); p->datum.~D(); alloc.free(p); return true; } } return false; } // Same as 'exclude' but 'key' must exist. void remove(const K& key) { if (!exclude(key)) PANIC("Tried to remove non-existing element from hashtable."); } #undef SEARCH }; //================================================================================================= #endif minisat+-1.0/ADTs/Sort.h000066400000000000000000000107671156255034500150050ustar00rootroot00000000000000/******************************************************************************************[Sort.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ /************************************************************************************************** Template based sorting routines: sort, sortUnique (remove duplicates). Can be applied either on 'vec's or on standard C arrays (pointers). **************************************************************************************************/ #ifndef Sort_h #define Sort_h //#include //================================================================================================= template struct LessThan_default { bool operator () (T x, T y) { return x < y; } }; //================================================================================================= template void selectionSort(T* array, int size, LessThan lt) { int i, j, best_i; T tmp; for (i = 0; i < size-1; i++){ best_i = i; for (j = i+1; j < size; j++){ if (lt(array[j], array[best_i])) best_i = j; } tmp = array[i]; array[i] = array[best_i]; array[best_i] = tmp; } } template static inline void selectionSort(T* array, int size) { selectionSort(array, size, LessThan_default()); } template void sort(T* array, int size, LessThan lt, double& seed) { if (size <= 15) selectionSort(array, size, lt); else{ T pivot = array[irand(seed, size)]; T tmp; int i = -1; int j = size; for(;;){ do i++; while(lt(array[i], pivot)); do j--; while(lt(pivot, array[j])); if (i >= j) break; tmp = array[i]; array[i] = array[j]; array[j] = tmp; } sort(array , i , lt, seed); sort(&array[i], size-i, lt, seed); } } template void sort(T* array, int size, LessThan lt) { double seed = 91648253; sort(array, size, lt, seed); } template static inline void sort(T* array, int size) { sort(array, size, LessThan_default()); } template void sortUnique(T* array, int& size, LessThan lt) { int i, j; T last; if (size == 0) return; sort(array, size, lt); i = 1; last = array[0]; for (j = 1; j < size; j++){ if (lt(last, array[j])){ last = array[i] = array[j]; i++; } } size = i; } template static inline void sortUnique(T* array, int& size) { sortUnique(array, size, LessThan_default()); } //================================================================================================= // For 'vec's: template void sort(vec& v, LessThan lt) { sort((T*)v, v.size(), lt); } template void sort(vec& v) { sort(v, LessThan_default()); } template void sortUnique(vec& v, LessThan lt) { int size = v.size(); T* data = v.release(); sortUnique(data, size, lt); v.~vec(); new (&v) vec(data, size); } template void sortUnique(vec& v) { sortUnique(v, LessThan_default()); } //================================================================================================= #endif minisat+-1.0/ADTs/StackAlloc.h000066400000000000000000000061651156255034500160730ustar00rootroot00000000000000/************************************************************************************[StackAlloc.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef StackAlloc_h #define StackAlloc_h //================================================================================================= template struct Allocator { virtual T* alloc(int nwords) = 0; }; // STACK ALLOCATOR // // 'cap' is capacity, 'lim' is malloc limit (use 'malloc()' for larger sizer // than this -- unless enough space left on the current stack). // template class StackAlloc : public Allocator { T* data; StackAlloc* prev; int index; T* alloc_helper(int n); StackAlloc(T* d, StackAlloc* p, int i) : data(d), prev(p), index(i) { } void init(void) { data = xmalloc(cap); index = 0; prev = NULL; } public: StackAlloc(void) { init(); } T* alloc(int n) { if (index + n <= cap) { T* tmp = data+index; index += n; return tmp; } else return alloc_helper(n); } void freeAll(void); void clear (void) { freeAll(); init(); } }; template T* StackAlloc::alloc_helper(int n) { if (n > lim) { StackAlloc* singleton = new StackAlloc(xmalloc(n), prev, -1); prev = singleton; return singleton->data; } else{ StackAlloc* copy = new StackAlloc(data, prev, index); data = xmalloc(cap); index = n; prev = copy; assert(n <= cap); return data; } } // Call this before allocator is destructed if you do not want to keep the data. // template void StackAlloc::freeAll(void) { StackAlloc* tmp,* ptr; for (ptr = this; ptr != NULL; ptr = ptr->prev) xfree(ptr->data); for (ptr = prev; ptr != NULL;) tmp = ptr->prev, delete ptr, ptr = tmp; } //================================================================================================= #endif minisat+-1.0/ADTs/VecAlloc.h000066400000000000000000000034201156255034500155320ustar00rootroot00000000000000#ifndef VecAlloc_h #define VecAlloc_h #include //================================================================================================= template class VecAlloc { union Slot { char data[sizeof(T)]; // (would have liked type 'T' here) Slot* next; }; Slot* table; int index; Slot* recycle; #ifdef DEBUG int nallocs; #endif void newTable(void) { Slot* t = xmalloc(chunk_size); t[0].next = table; table = t; index = 1; } public: VecAlloc(void) { recycle = NULL; table = NULL; #ifdef DEBUG nallocs = 0; #endif newTable(); } ~VecAlloc(void) { #ifdef DEBUG //if (nallocs != 0) fprintf(stderr, "WARNING! VecAlloc detected leak of %d unit(s) of type '%s'.\n", nallocs, typeid(T).name()); if (nallocs != 0) fprintf(stderr, "WARNING! VecAlloc detected leak of %d unit(s) of size %d.\n", nallocs, sizeof(T)); #endif Slot* curr,* next; curr = table; while (curr != NULL) next = curr[0].next, xfree(curr), curr = next; } T* alloc(void) { #ifdef DEBUG nallocs++; #endif if (recycle == NULL){ if (index >= chunk_size) newTable(); return (T*)&table[index++]; }else{ T* tmp = (T*)recycle; recycle = (*recycle).next; return tmp; } } void free(T* ptr) { #ifdef DEBUG nallocs--; #endif ((Slot*)ptr)->next = recycle; recycle = (Slot*)ptr; } }; //================================================================================================= #endif minisat+-1.0/ADTs/VecMaps.h000066400000000000000000000101741156255034500154040ustar00rootroot00000000000000/***************************************************************************************[VecMaps.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef VecMaps_h #define VecMaps_h //================================================================================================= // TODO: Adapt constructors of 'BitMap' and 'DeckMap' to those of VecMap. class BitMap { vec v; bool bool_null; unsigned word(int index) const { return (unsigned)index / (sizeof(int)*8); } unsigned mask(int index) const { return 1 << ((unsigned)index % (sizeof(int)*8)); } public: typedef int Key; typedef bool Datum; BitMap(void) : bool_null() { } BitMap(bool null) : bool_null(null) { } BitMap(bool null, int capacity) : v((capacity+(sizeof(int)*8)-1) / (sizeof(int)*8), -(int)null), bool_null(null) { } bool at(int index) const { if (word(index) >= (unsigned)v.size()) return bool_null; else return v[word(index)] & mask(index); } void set(int index, bool value) { if (word(index) >= (unsigned)v.size()) assert(index >= 0), v.growTo(word(index)+1, -(int)bool_null); if (value == false) v[word(index)] &= ~mask(index); else v[word(index)] |= mask(index); } void clear(void) { v.clear(); } }; template class VecMap { vec v; T T_null; public: typedef int Key; typedef T Datum; VecMap(void) : T_null() { } VecMap(T null) : T_null(null) { } VecMap(T null, int capacity) : v(capacity, null), T_null(null) { } T at(int index) const { if ((unsigned)index >= (unsigned)v.size()) return T_null; else return v[index]; } void set(int index, T value) { if ((unsigned)index >= (unsigned)v.size()) assert(index >= 0), v.growTo(index+1, T_null); v[index] = value; } void clear(void) { v.clear(); } }; template <> struct VecMap : BitMap { VecMap(void) { } VecMap(bool null) : BitMap(null) { } VecMap(bool null, int capacity) : BitMap(null, capacity) { } }; template class DeckMap { VecMap pos, neg; public: typedef int Key; typedef T Datum; DeckMap(void) { } DeckMap(T null) : pos(null) , neg(null) { } DeckMap(T null, int capacity) : pos(null, capacity), neg(null, capacity) { } T at(int index) const { if (index >= 0) return pos.at(index); else return neg.at(~index); } void set(int index, Datum value) { if (index >= 0) pos.set(index, value); else neg.set(~index, value); } void clear(void) { pos.clear(); neg.clear(); } }; //================================================================================================= #endif minisat+-1.0/Debug.C000066400000000000000000000075461156255034500142450ustar00rootroot00000000000000/*****************************************************************************************[Debug.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #include "Debug.h" vec* debug_names = NULL; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void dump(Int num) { if (num == Int_MIN) reportf("-oo"); else if (num == Int_MAX) reportf("+oo"); else { char* tmp = toString(num); reportf("%s", tmp); xfree(tmp); } } void dump(Lit p) { if (debug_names == NULL) reportf("%sx%d", sign(p)?"~":"", var(p)); else{ if (var(p) < debug_names->size()) reportf("%s%s", sign(p)?"~":"", (*debug_names)[var(p)]); else reportf("%s@%d", sign(p)?"~":"", var(p)); } } void dump(Formula f) { if (Atom_p(f)) reportf("%sv%d", sign(f)?"~":"", index(f)); else reportf("%s@%d", sign(f)?"~":"", index(f)); } void dump(const vec& ps, const vec& Cs) { assert(ps.size() == Cs.size()); for (int i = 0; i < ps.size(); i++){ dump(Cs[i]); reportf("*"); dump(ps[i]); if (i+1 < ps.size()) reportf(" "); } } void dump(const vec& ps, const vec& Cs) { assert(ps.size() == Cs.size()); for (int i = 0; i < ps.size(); i++){ dump(Cs[i]); reportf("*"); dump(ps[i]); if (i+1 < ps.size()) reportf(" "); } } void dump(const vec& ps, const vec& Cs, const vec& assigns) { assert(ps.size() == Cs.size()); for (int i = 0; i < ps.size(); i++){ dump(Cs[i]); reportf("*"); dump(ps[i]); if (assigns[var(ps[i])] == toInt(l_Undef)) reportf(":?"); else if ((assigns[var(ps[i])] == toInt(l_True) && !sign(ps[i])) || (assigns[var(ps[i])] == toInt(l_False) && sign(ps[i]))) reportf(":1"); else reportf(":0"); if (i+1 < ps.size()) reportf(" "); } } void dump(const Linear& pb) { for (int i = 0; i < pb.size; i++){ dump(pb(i)); reportf("*"); dump(pb[i]); reportf(" "); } reportf("in ["); dump(pb.lo); reportf(","); dump(pb.hi); reportf("]"); } void dump(const Linear& pb, const vec& assigns) { for (int i = 0; i < pb.size; i++){ dump(pb(i)); reportf("*"); dump(pb[i]); if (assigns[var(pb[i])] == toInt(l_Undef)) reportf(":?"); else if ((assigns[var(pb[i])] == toInt(l_True) && !sign(pb[i])) || (assigns[var(pb[i])] == toInt(l_False) && sign(pb[i]))) reportf(":1"); else reportf(":0"); if (i+1 < pb.size) reportf(" "); } reportf("in ["); dump(pb.lo); reportf(","); dump(pb.hi); reportf("]"); } minisat+-1.0/Debug.h000066400000000000000000000037101156255034500142770ustar00rootroot00000000000000/*****************************************************************************************[Debug.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef Debug_h #define Debug_h #include "SolverTypes.h" #include "PbSolver.h" #include "FEnv.h" //================================================================================================= extern vec* debug_names; void dump(Int num); void dump(Lit p); void dump(Formula f); void dump(const vec& ps, const vec& Cs); void dump(const vec& ps, const vec& Cs, const vec& assigns); void dump(const vec& ps, const vec& Cs); void dump(const Linear& pb, const vec& assigns); void dump(const Linear& pb); macro void dump(Linear* pb) { dump(*pb); } //================================================================================================= #endif minisat+-1.0/Examples/000077500000000000000000000000001156255034500146555ustar00rootroot00000000000000minisat+-1.0/Examples/garden9x9.opb000066400000000000000000000072741156255034500172030ustar00rootroot00000000000000* #variable= 81 #constraint= 81 * converted from file: submitted/sorensson/garden/g9x9.opb min: +1*x1 +1*x2 +1*x3 +1*x4 +1*x5 +1*x6 +1*x7 +1*x8 +1*x9 +1*x10 +1*x11 +1*x12 +1*x13 +1*x14 +1*x15 +1*x16 +1*x17 +1*x18 +1*x19 +1*x20 +1*x21 +1*x22 +1*x23 +1*x24 +1*x25 +1*x26 +1*x27 +1*x28 +1*x29 +1*x30 +1*x31 +1*x32 +1*x33 +1*x34 +1*x35 +1*x36 +1*x37 +1*x38 +1*x39 +1*x40 +1*x41 +1*x42 +1*x43 +1*x44 +1*x45 +1*x46 +1*x47 +1*x48 +1*x49 +1*x50 +1*x51 +1*x52 +1*x53 +1*x54 +1*x55 +1*x56 +1*x57 +1*x58 +1*x59 +1*x60 +1*x61 +1*x62 +1*x63 +1*x64 +1*x65 +1*x66 +1*x67 +1*x68 +1*x69 +1*x70 +1*x71 +1*x72 +1*x73 +1*x74 +1*x75 +1*x76 +1*x77 +1*x78 +1*x79 +1*x80 +1*x81 ; +1*x1 +1*x2 +1*x10 >= +1; +1*x10 +1*x11 +1*x1 +1*x19 >= +1; +1*x19 +1*x20 +1*x10 +1*x28 >= +1; +1*x28 +1*x29 +1*x19 +1*x37 >= +1; +1*x37 +1*x38 +1*x28 +1*x46 >= +1; +1*x46 +1*x47 +1*x37 +1*x55 >= +1; +1*x55 +1*x56 +1*x46 +1*x64 >= +1; +1*x64 +1*x65 +1*x55 +1*x73 >= +1; +1*x73 +1*x74 +1*x64 >= +1; +1*x2 +1*x1 +1*x3 +1*x11 >= +1; +1*x11 +1*x10 +1*x12 +1*x2 +1*x20 >= +1; +1*x20 +1*x19 +1*x21 +1*x11 +1*x29 >= +1; +1*x29 +1*x28 +1*x30 +1*x20 +1*x38 >= +1; +1*x38 +1*x37 +1*x39 +1*x29 +1*x47 >= +1; +1*x47 +1*x46 +1*x48 +1*x38 +1*x56 >= +1; +1*x56 +1*x55 +1*x57 +1*x47 +1*x65 >= +1; +1*x65 +1*x64 +1*x66 +1*x56 +1*x74 >= +1; +1*x74 +1*x73 +1*x75 +1*x65 >= +1; +1*x3 +1*x2 +1*x4 +1*x12 >= +1; +1*x12 +1*x11 +1*x13 +1*x3 +1*x21 >= +1; +1*x21 +1*x20 +1*x22 +1*x12 +1*x30 >= +1; +1*x30 +1*x29 +1*x31 +1*x21 +1*x39 >= +1; +1*x39 +1*x38 +1*x40 +1*x30 +1*x48 >= +1; +1*x48 +1*x47 +1*x49 +1*x39 +1*x57 >= +1; +1*x57 +1*x56 +1*x58 +1*x48 +1*x66 >= +1; +1*x66 +1*x65 +1*x67 +1*x57 +1*x75 >= +1; +1*x75 +1*x74 +1*x76 +1*x66 >= +1; +1*x4 +1*x3 +1*x5 +1*x13 >= +1; +1*x13 +1*x12 +1*x14 +1*x4 +1*x22 >= +1; +1*x22 +1*x21 +1*x23 +1*x13 +1*x31 >= +1; +1*x31 +1*x30 +1*x32 +1*x22 +1*x40 >= +1; +1*x40 +1*x39 +1*x41 +1*x31 +1*x49 >= +1; +1*x49 +1*x48 +1*x50 +1*x40 +1*x58 >= +1; +1*x58 +1*x57 +1*x59 +1*x49 +1*x67 >= +1; +1*x67 +1*x66 +1*x68 +1*x58 +1*x76 >= +1; +1*x76 +1*x75 +1*x77 +1*x67 >= +1; +1*x5 +1*x4 +1*x6 +1*x14 >= +1; +1*x14 +1*x13 +1*x15 +1*x5 +1*x23 >= +1; +1*x23 +1*x22 +1*x24 +1*x14 +1*x32 >= +1; +1*x32 +1*x31 +1*x33 +1*x23 +1*x41 >= +1; +1*x41 +1*x40 +1*x42 +1*x32 +1*x50 >= +1; +1*x50 +1*x49 +1*x51 +1*x41 +1*x59 >= +1; +1*x59 +1*x58 +1*x60 +1*x50 +1*x68 >= +1; +1*x68 +1*x67 +1*x69 +1*x59 +1*x77 >= +1; +1*x77 +1*x76 +1*x78 +1*x68 >= +1; +1*x6 +1*x5 +1*x7 +1*x15 >= +1; +1*x15 +1*x14 +1*x16 +1*x6 +1*x24 >= +1; +1*x24 +1*x23 +1*x25 +1*x15 +1*x33 >= +1; +1*x33 +1*x32 +1*x34 +1*x24 +1*x42 >= +1; +1*x42 +1*x41 +1*x43 +1*x33 +1*x51 >= +1; +1*x51 +1*x50 +1*x52 +1*x42 +1*x60 >= +1; +1*x60 +1*x59 +1*x61 +1*x51 +1*x69 >= +1; +1*x69 +1*x68 +1*x70 +1*x60 +1*x78 >= +1; +1*x78 +1*x77 +1*x79 +1*x69 >= +1; +1*x7 +1*x6 +1*x8 +1*x16 >= +1; +1*x16 +1*x15 +1*x17 +1*x7 +1*x25 >= +1; +1*x25 +1*x24 +1*x26 +1*x16 +1*x34 >= +1; +1*x34 +1*x33 +1*x35 +1*x25 +1*x43 >= +1; +1*x43 +1*x42 +1*x44 +1*x34 +1*x52 >= +1; +1*x52 +1*x51 +1*x53 +1*x43 +1*x61 >= +1; +1*x61 +1*x60 +1*x62 +1*x52 +1*x70 >= +1; +1*x70 +1*x69 +1*x71 +1*x61 +1*x79 >= +1; +1*x79 +1*x78 +1*x80 +1*x70 >= +1; +1*x8 +1*x7 +1*x9 +1*x17 >= +1; +1*x17 +1*x16 +1*x18 +1*x8 +1*x26 >= +1; +1*x26 +1*x25 +1*x27 +1*x17 +1*x35 >= +1; +1*x35 +1*x34 +1*x36 +1*x26 +1*x44 >= +1; +1*x44 +1*x43 +1*x45 +1*x35 +1*x53 >= +1; +1*x53 +1*x52 +1*x54 +1*x44 +1*x62 >= +1; +1*x62 +1*x61 +1*x63 +1*x53 +1*x71 >= +1; +1*x71 +1*x70 +1*x72 +1*x62 +1*x80 >= +1; +1*x80 +1*x79 +1*x81 +1*x71 >= +1; +1*x9 +1*x8 +1*x18 >= +1; +1*x18 +1*x17 +1*x9 +1*x27 >= +1; +1*x27 +1*x26 +1*x18 +1*x36 >= +1; +1*x36 +1*x35 +1*x27 +1*x45 >= +1; +1*x45 +1*x44 +1*x36 +1*x54 >= +1; +1*x54 +1*x53 +1*x45 +1*x63 >= +1; +1*x63 +1*x62 +1*x54 +1*x72 >= +1; +1*x72 +1*x71 +1*x63 +1*x81 >= +1; +1*x81 +1*x80 +1*x72 >= +1; minisat+-1.0/Examples/stein27_bignum.opb000066400000000000000000000374311156255034500202230ustar00rootroot00000000000000* #variable= 27 #constraint= 118 * converted from file: submitted/een/stein27.opb min: +1000000000000000000000000*x0 +1000000000000000000000000*x1 +1000000000000000000000000*x2 +1000000000000000000000000*x3 +1000000000000000000000000*x4 +1000000000000000000000000*x5 +1000000000000000000000000*x6 +1000000000000000000000000*x7 +1000000000000000000000000*x8 +1000000000000000000000000*x9 +1000000000000000000000000*x10 +1000000000000000000000000*x11 +1000000000000000000000000*x12 +1000000000000000000000000*x13 +1000000000000000000000000*x14 +1000000000000000000000000*x15 +1000000000000000000000000*x16 +1000000000000000000000000*x17 +1000000000000000000000000*x18 +1000000000000000000000000*x19 +1000000000000000000000000*x20 +1000000000000000000000000*x21 +1000000000000000000000000*x22 +1000000000000000000000000*x23 +1000000000000000000000000*x24 +1000000000000000000000000*x25 +1000000000000000000000000*x26 ; +1000000000000000000000000*x2 +1000000000000000000000000*x13 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x2 +1000000000000000000000000*x3 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x2 +1000000000000000000000000*x4 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x1 +1000000000000000000000000*x5 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x5 +1000000000000000000000000*x6 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x5 +1000000000000000000000000*x7 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x4 +1000000000000000000000000*x8 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x7 +1000000000000000000000000*x8 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x6 +1000000000000000000000000*x8 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x6 +1000000000000000000000000*x7 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x3 +1000000000000000000000000*x6 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x4 +1000000000000000000000000*x7 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x5 +1000000000000000000000000*x8 >= +1000000000000000000000000; +1000000000000000000000000*x10 +1000000000000000000000000*x11 +1000000000000000000000000*x12 >= +1000000000000000000000000; +1000000000000000000000000*x9 +1000000000000000000000000*x11 +1000000000000000000000000*x13 >= +1000000000000000000000000; +1000000000000000000000000*x9 +1000000000000000000000000*x10 +1000000000000000000000000*x14 >= +1000000000000000000000000; +1000000000000000000000000*x13 +1000000000000000000000000*x14 +1000000000000000000000000*x15 >= +1000000000000000000000000; +1000000000000000000000000*x12 +1000000000000000000000000*x14 +1000000000000000000000000*x16 >= +1000000000000000000000000; +1000000000000000000000000*x12 +1000000000000000000000000*x13 +1000000000000000000000000*x17 >= +1000000000000000000000000; +1000000000000000000000000*x9 +1000000000000000000000000*x16 +1000000000000000000000000*x17 >= +1000000000000000000000000; +1000000000000000000000000*x10 +1000000000000000000000000*x15 +1000000000000000000000000*x17 >= +1000000000000000000000000; +1000000000000000000000000*x11 +1000000000000000000000000*x15 +1000000000000000000000000*x16 >= +1000000000000000000000000; +1000000000000000000000000*x9 +1000000000000000000000000*x12 +1000000000000000000000000*x15 >= +1000000000000000000000000; +1000000000000000000000000*x10 +1000000000000000000000000*x13 +1000000000000000000000000*x16 >= +1000000000000000000000000; +1000000000000000000000000*x11 +1000000000000000000000000*x14 +1000000000000000000000000*x17 >= +1000000000000000000000000; +1000000000000000000000000*x19 +1000000000000000000000000*x20 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x18 +1000000000000000000000000*x20 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x18 +1000000000000000000000000*x19 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x22 +1000000000000000000000000*x23 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x16 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x15 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x14 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x13 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x12 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x11 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x10 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x9 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x20 +1000000000000000000000000*x23 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x19 +1000000000000000000000000*x22 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x18 +1000000000000000000000000*x21 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x20 +1000000000000000000000000*x24 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x19 +1000000000000000000000000*x24 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x18 +1000000000000000000000000*x25 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x21 +1000000000000000000000000*x22 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x17 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x9 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x10 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x11 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x12 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x13 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x14 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x15 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x16 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x1 +1000000000000000000000000*x17 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x9 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x10 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x11 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x12 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x14 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x15 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x16 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x2 +1000000000000000000000000*x17 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x9 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x10 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x11 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x12 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x13 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x14 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x15 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x16 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x3 +1000000000000000000000000*x17 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x9 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x10 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x11 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x12 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x13 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x14 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x15 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x16 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x4 +1000000000000000000000000*x17 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x9 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x10 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x11 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x12 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x13 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x14 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x15 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x21 +1000000000000000000000000*x23 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x16 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x5 +1000000000000000000000000*x17 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x9 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x10 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x11 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x12 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x13 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x14 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x15 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x16 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x6 +1000000000000000000000000*x17 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x9 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x10 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x11 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x17 +1000000000000000000000000*x26 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x16 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x15 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x14 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x13 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x12 +1000000000000000000000000*x22 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x11 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x10 +1000000000000000000000000*x24 >= +1000000000000000000000000; +1000000000000000000000000*x8 +1000000000000000000000000*x9 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x17 +1000000000000000000000000*x18 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x16 +1000000000000000000000000*x25 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x15 +1000000000000000000000000*x20 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x14 +1000000000000000000000000*x21 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x13 +1000000000000000000000000*x19 >= +1000000000000000000000000; +1000000000000000000000000*x7 +1000000000000000000000000*x12 +1000000000000000000000000*x23 >= +1000000000000000000000000; +1000000000000000000000000*x0 +1000000000000000000000000*x1 +1000000000000000000000000*x2 +1000000000000000000000000*x3 +1000000000000000000000000*x4 +1000000000000000000000000*x5 +1000000000000000000000000*x6 +1000000000000000000000000*x7 +1000000000000000000000000*x8 +1000000000000000000000000*x9 +1000000000000000000000000*x10 +1000000000000000000000000*x11 +1000000000000000000000000*x12 +1000000000000000000000000*x13 +1000000000000000000000000*x14 +1000000000000000000000000*x15 +1000000000000000000000000*x16 +1000000000000000000000000*x17 +1000000000000000000000000*x18 +1000000000000000000000000*x19 +1000000000000000000000000*x20 +1000000000000000000000000*x21 +1000000000000000000000000*x22 +1000000000000000000000000*x23 +1000000000000000000000000*x24 +1000000000000000000000000*x25 +1000000000000000000000000*x26 >= +13000000000000000000000000; minisat+-1.0/Examples/unsat.opb000066400000000000000000000001021156255034500165020ustar00rootroot00000000000000+1*v1 +1*v2 -1*v3 -1*v4 >= 1; -1*v1 -1*v2 +1*v3 +1*v4 >= 1; minisat+-1.0/Hardware.h000066400000000000000000000036171156255034500150140ustar00rootroot00000000000000/**************************************************************************************[Hardware.h] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #ifndef Hardware_h #define Hardware_h #include "FEnv.h" #include "PbSolver.h" //================================================================================================= int estimatedAdderCost(const Linear& c); void oddEvenSort(vec& fs); void rippleAdder(const vec& xs, const vec& ys, vec& out); void addPb(const vec& ps, const vec& Cs_, vec& out, int bits); void clausify(Solver& s, const vec& fs, vec& out); void clausify(Solver& s, const vec& fs); //================================================================================================= #endif minisat+-1.0/Hardware_adders.C000066400000000000000000000114301156255034500162610ustar00rootroot00000000000000/*******************************************************************************[Hardware_adders.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #include "Hardware.h" #include "Debug.h" int estimatedAdderCost(const Linear& c) { // (sorry about strange implementation -- copy/paste programming) vec Cs(c.size); Int max_C = -1; for (int i = 0; i < c.size; i++){ Cs[i] = c(i); if (Cs[i] > max_C) max_C = Cs[i]; } int cost = 0; for (; max_C > 0; max_C >>= 1){ for (int i = 0; i < Cs.size(); i++){ if ((Cs[i] & 1) != 0) cost++; Cs[i] >>= 1; } } return cost; } void rippleAdder(const vec& xs, const vec& ys, vec& out) { Formula c = _0_; out.clear(); for (int i = 0; i < max(xs.size(),ys.size()); i++){ Formula x = i < xs.size() ? xs[i] : _0_; Formula y = i < ys.size() ? ys[i] : _0_; out.push(FAs(x,y,c)); c = FAc(x,y,c); } out.push(c); while (out.last() == _0_) out.pop(); } /*_________________________________________________________________________________________________ | | addPb : (ps : const vec&) (Cs_ : const vec&) (out : vec&) (bits : int) | -> [void] | | Description: | Compute 'C[0]*p[0] + C[1]*p[1] + ... + C[n-1]*P[n-1]' and store in 'out'. Sometimes the higher | order bits are un-interesting, so only the first 'bits' bits will be stored, plus one more | "overflow" bit, so "out.size() <= bits + 1". |________________________________________________________________________________________________@*/ void addPb(const vec& ps, const vec& Cs_, vec& out, int bits) { assert(ps.size() == Cs_.size()); vec > pools; vec Cs(Cs_.size()); Int max_C = -1; for (int i = 0; i < Cs_.size(); i++){ Cs[i] = Cs_[i]; if (Cs[i] > max_C) max_C = Cs[i]; } for (; max_C > 0; max_C >>= 1){ pools.push(); for (int i = 0; i < Cs.size(); i++){ if ((Cs[i] & 1) != 0) pools.last().push(ps[i]); Cs[i] >>= 1; } } vec carry; for (int p = 0; p < pools.size(); p++){ vec& pool = pools[p]; carry.clear(); if (p == bits){ Formula overflow = _0_; for (; p < pools.size(); p++) for (int i = 0; i < pools[p].size(); i++) overflow |= pools[p][i]; out.push(overflow); }else if (pool.size() == 0) out.push(_0_); else{ int head = 0; while (pool.size()-head >= 3){ pool .push(FAs(pool[head], pool[head+1], pool[head+2])); carry.push(FAc(pool[head], pool[head+1], pool[head+2])); head += 3; } if (pool.size()-head == 2){ pool .push(FAs(pool[head], pool[head+1], _0_)); carry.push(FAc(pool[head], pool[head+1], _0_)); head += 2; } assert(pool.size()-head == 1); out.push(pool[head]); } if (carry.size() > 0){ if (p+1 == pools.size()) pools.push(); for (int i = 0; i < carry.size(); i++) pools[p+1].push(carry[i]); } } #if 0 //DEBUG for (int p = 0; p < pools.size(); p++){ printf("pool %d:", (1 << p)); for (int i = 0; i < pools[p].size(); i++){ printf(" "); dump(pools[p][i]); } printf("\n"); } #endif } minisat+-1.0/Hardware_clausify.C000066400000000000000000000255011156255034500166420ustar00rootroot00000000000000/*****************************************************************************[Hardware_clausify.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #include "Hardware.h" struct Clausifier { Solver& s; vec tmp_clause; vec tmp_marked; Clausifier(Solver& _s) : s(_s) {} static /*WARNING*/ CMap occ; static /*WARNING*/ CMap vmap; static /*WARNING*/ CMap vmapp; FMap seen; inline void clause(Lit a, Lit b) { tmp_clause.clear(); tmp_clause.push(a); tmp_clause.push(b); s.addClause(tmp_clause); } inline void clause(Lit a, Lit b, Lit c) { tmp_clause.clear(); tmp_clause.push(a); tmp_clause.push(b); tmp_clause.push(c); s.addClause(tmp_clause); } inline void clause(Lit a, Lit b, Lit c, Lit d) { tmp_clause.clear(); tmp_clause.push(a); tmp_clause.push(b); tmp_clause.push(c); tmp_clause.push(d); s.addClause(tmp_clause); } void usage (Formula f); void _collect(Formula f, vec& out); void collect(Formula f, vec& out); Lit basicClausify (Formula f); Lit polarityClausify(Formula f); }; CMap Clausifier::occ (0); CMap Clausifier::vmap (var_Undef); CMap Clausifier::vmapp(lit_Undef); void Clausifier::usage(Formula f) { if (Atom_p(f)) return; occ.set(f,occ.at(f)+1); if (occ.at(f) == 1){ if (Bin_p(f)){ usage(left(f)); usage(right(f)); }else if (ITE_p(f)){ usage(cond(f)); usage(tt(f)); usage(ff(f)); }else{ assert(FA_p(f)); usage(FA_x(f)); usage(FA_y(f)); usage(FA_c(f)); } } } void Clausifier::collect(Formula f, vec& out) { tmp_marked.clear(); _collect(left(f), out); _collect(right(f),out); for (int i = 0; i < tmp_marked.size(); i++) seen.set(tmp_marked[i],false); } void Clausifier::_collect(Formula f, vec& out) { if (!seen.at(f)){ seen.set(f,true); tmp_marked.push(f); if (Bin_p(f) && op(f) == op_And && !sign(f) && occ.at(f) == 1){ _collect(left(f) ,out); _collect(right(f),out); } else out.push(f); } } Lit Clausifier::polarityClausify(Formula f) { Lit result = lit_Undef; if (Atom_p(f)){ #if 0 assert(!Const_p(f)); #else if (Const_p(f)){ Var x = s.newVar(); s.addUnit(Lit(x, (f == _0_))); result = Lit(x); }else #endif result = Lit(index(f),sign(f)); }else if (vmapp.at(f) != lit_Undef && !s.varElimed(var(vmapp.at(f)))){ result = vmapp.at(f); }else{ #if 1 result = vmapp.at(~f) != lit_Undef && !s.varElimed(var(vmapp.at(~f))) ? Lit(var(vmapp.at(~f))) : Lit(s.newVar(!opt_branch_pbvars)); #else result = Lit(s.newVar(!opt_branch_pbvars)); #endif if (Bin_p(f)){ if (op(f) == op_And){ vec conj; collect(f, conj); assert(conj.size() > 1); if (!sign(f)){ for (int i = 0; i < conj.size(); i++) clause(~result,polarityClausify(conj[i])); }else{ vec ls; ls.push(result); for (int i = 0; i < conj.size(); i++) ls.push(polarityClausify(~conj[i])); s.addClause(ls); } //printf("and: %d = ", var(result)); //for (int i = 0; i < conj.size(); i++) // printf("%c%d ", sign(polarityClausify(conj[i])) ? '-' : ' ', // var(polarityClausify(conj[i]))); //printf("\n"); }else{ Lit l = polarityClausify( left (f)); Lit r = polarityClausify( right(f)); Lit nl = polarityClausify(~left (f)); Lit nr = polarityClausify(~right(f)); //printf("equiv:\n"); assert(op(f) == op_Equiv); if (!sign(f)){ clause(~result, nl, r); clause(~result, l, nr); }else{ clause( result, nl, nr); clause( result, l, r); } } }else if (ITE_p(f)){ Lit c = polarityClausify( cond(f)); Lit nc = polarityClausify(~cond(f)); if (!sign(f)){ Lit a = polarityClausify( tt (f)); Lit b = polarityClausify( ff (f)); clause(~result, nc, a); clause(~result, c, b); clause( a, b, ~result); }else{ Lit na = polarityClausify(~tt (f)); Lit nb = polarityClausify(~ff (f)); clause( result, nc, na); clause( result, c, nb); clause(na, nb, result); } }else{ assert(FA_p(f)); if (isCarry(f)){ //printf("carry:\n"); if (!sign(f)){ Lit a = polarityClausify( FA_x(f)); Lit b = polarityClausify( FA_y(f)); Lit c = polarityClausify( FA_c(f)); clause(~result, a, b); clause(~result, c, a); clause(~result, c, b); }else{ Lit na = polarityClausify(~FA_x(f)); Lit nb = polarityClausify(~FA_y(f)); Lit nc = polarityClausify(~FA_c(f)); clause( result, na, nb); clause( result, nc, na); clause( result, nc, nb); } }else{ Lit a = polarityClausify( FA_x(f)); Lit b = polarityClausify( FA_y(f)); Lit c = polarityClausify( FA_c(f)); Lit na = polarityClausify(~FA_x(f)); Lit nb = polarityClausify(~FA_y(f)); Lit nc = polarityClausify(~FA_c(f)); //printf("sum:\n"); if (!sign(f)){ clause(~result, nc, na, b); clause(~result, nc, a, nb); clause(~result, c, na, nb); clause(~result, c, a, b); }else{ clause( result, nc, na, nb); clause( result, nc, a, b); clause( result, c, na, b); clause( result, c, a, nb); } } } result = Lit(var(result),sign(f)); vmapp.set(f,result); } assert(result != lit_Undef); return result; } Lit Clausifier::basicClausify(Formula f) { Var result = var_Undef; if (Atom_p(f)){ assert(!Const_p(f)); result = index(f); }else if (vmap.at(f) != var_Undef && !s.varElimed(vmap.at(f))){ result = vmap.at(f); }else{ result = s.newVar(!opt_branch_pbvars); Lit p = Lit(result); if (Bin_p(f)){ if (op(f) == op_And){ vec conj; collect(f, conj); assert(conj.size() > 1); for (int i = 0; i < conj.size(); i++) clause(~p,basicClausify(conj[i])); tmp_clause.clear(); tmp_clause.push(p); for (int i = 0; i < conj.size(); i++) tmp_clause.push(~basicClausify(conj[i])); s.addClause(tmp_clause); }else{ Lit l = basicClausify(left (f)); Lit r = basicClausify(right(f)); //printf("equiv:\n"); assert(op(f) == op_Equiv); clause(~p, ~l, r); clause(~p, l, ~r); clause( p, ~l, ~r); clause( p, l, r); } }else if (ITE_p(f)){ Lit c = basicClausify(cond(f)); Lit a = basicClausify(tt (f)); Lit b = basicClausify(ff (f)); clause(~p, ~c, a); clause(~p, c, b); clause( p, ~c, ~a); clause( p, c, ~b); // not neccessary !! clause(~a, ~b, p); clause( a, b, ~p); }else{ assert(FA_p(f)); Lit a = basicClausify(FA_x(f)); Lit b = basicClausify(FA_y(f)); Lit c = basicClausify(FA_c(f)); if (isCarry(f)){ //printf("carry:\n"); clause(~p, a, b); clause(~p, c, a); clause(~p, c, b); clause( p, ~c, ~a); clause( p, ~c, ~b); clause( p, ~a, ~b); }else{ //printf("sum:\n"); clause(~p, ~c, ~a, b); clause(~p, ~c, a, ~b); clause(~p, c, ~a, ~b); clause(~p, c, a, b); clause( p, ~c, ~a, ~b); clause( p, ~c, a, b); clause( p, c, ~a, b); clause( p, c, a, ~b); } } vmap.set(f,result); } assert(result != var_Undef); return Lit(result,sign(f)); } void clausify(Solver& s, const vec& fs, vec& out) { Clausifier c(s); for (int i = 0; i < fs.size(); i++) c.usage(fs[i]); if (opt_convert_weak) for (int i = 0; i < fs.size(); i++) out.push(c.polarityClausify(fs[i])); else for (int i = 0; i < fs.size(); i++) out.push(c.basicClausify(fs[i])); } void clausify(Solver& s, const vec& fs) { vec out; clausify(s, fs, out); for (int i = 0; i < out.size(); i++) s.addUnit(out[i]); } minisat+-1.0/Hardware_sorters.C000066400000000000000000000071461156255034500165310ustar00rootroot00000000000000/******************************************************************************[Hardware_sorters.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ #include "Hardware.h" //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - macro Formula operator && (Formula f, Formula g) { if (f == _0_ || g == _0_) return _0_; else if (f == _1_) return g; else if (g == _1_) return f; else if (f == g ) return f; else if (f == ~g ) return _0_; if (g < f) swp(f, g); return Bin_new(op_And, f, g); } macro Formula operator || (Formula f, Formula g) { return ~(~f && ~g); } //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static inline void cmp2(vec& fs, int begin) { Formula a = fs[begin]; Formula b = fs[begin + 1]; #if 1 fs[begin] = a | b; fs[begin + 1] = a & b; #else fs[begin] = a || b; fs[begin + 1] = a && b; #endif } static void riffle(vec& fs) { vec tmp; fs.copyTo(tmp); for (int i = 0; i < fs.size() / 2; i++){ fs[i*2] = tmp[i]; fs[i*2+1] = tmp[i+fs.size() / 2]; } } static void unriffle(vec& fs) { vec tmp; fs.copyTo(tmp); for (int i = 0; i < fs.size() / 2; i++){ fs[i] = tmp[i*2]; fs[i+fs.size() / 2] = tmp[i*2+1]; } } static void oddEvenMerge(vec& fs, int begin, int end) { assert(end - begin > 1); if (end - begin == 2) cmp2(fs,begin); else { int mid = (end - begin) / 2; vec tmp; for (int i = 0; i < end - begin; i++) tmp.push(fs[begin+i]); unriffle(tmp); oddEvenMerge(tmp,0,mid); oddEvenMerge(tmp,mid,tmp.size()); riffle(tmp); for (int i = 1; i < tmp.size() - 1; i += 2) cmp2(tmp,i); for (int i = 0; i < tmp.size(); i++) fs[i + begin] = tmp[i]; } } // Inputs to the circuit is the formulas in fs, which is overwritten // by the resulting outputs of the circuit. // NOTE: The number of comparisons is bounded by: n * log n * (log n + 1) void oddEvenSort(vec& fs) { int orig_sz = fs.size(); int sz; for (sz = 1; sz < fs.size(); sz *= 2); fs.growTo(sz,_0_); for (int i = 1; i < fs.size(); i *= 2) for (int j = 0; j + 2*i <= fs.size(); j += 2*i) oddEvenMerge(fs,j,j+2*i); fs.shrink(sz - orig_sz); } minisat+-1.0/INSTALL000066400000000000000000000010431156255034500141260ustar00rootroot00000000000000~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MiniSat v1.13+ and SatElite v1.0+ -- (C) Niklas Een, Niklas Sörensson, 2005 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ NOTE! You need GNU make and GCC to compile this software. To make the 64-bit version, type make rx To make the BigNum version, type make rs To use them together (trying the faster 64-bit version first), use the script minisat+_script Precompiled binaries for Linux are included. // Niklas minisat+-1.0/LICENSE000066400000000000000000000021021156255034500140770ustar00rootroot00000000000000MiniSat+ -- Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. minisat+-1.0/Main.C000066400000000000000000000331631156255034500140750ustar00rootroot00000000000000/******************************************************************************************[Main.C] Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **************************************************************************************************/ /************************************************************************************************** Read a DIMACS file and apply the SAT-solver to it. **************************************************************************************************/ #include #include #include #include "MiniSat.h" #include "PbSolver.h" #include "PbParser.h" //================================================================================================= // Command line options: bool opt_satlive = true; bool opt_ansi = true; char* opt_cnf = NULL; int opt_verbosity = 1; bool opt_try = false; // (hidden option -- if set, then "try" to parse, but don't output "s UNKNOWN" if you fail, instead exit with error code 5) SolverT opt_solver = st_MiniSat; ConvertT opt_convert = ct_Mixed; ConvertT opt_convert_goal = ct_Undef; bool opt_convert_weak = true; double opt_bdd_thres = 3; double opt_sort_thres = 20; double opt_goal_bias = 3; Int opt_goal = Int_MAX; Command opt_command = cmd_Minimize; bool opt_branch_pbvars = false; int opt_polarity_sug = 1; bool opt_old_format = false; char* opt_input = NULL; char* opt_result = NULL; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - cchar* doc = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" "MiniSat+ 1.0, based on MiniSat v1.13 -- (C) Niklas Een, Niklas Sorensson, 2005\n" "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" "USAGE: minisat+ [] [-