00001 00029 #ifndef ABA_NONDUPLPOOL_INC 00030 #define ABA_NONDUPLPOOL_INC 00031 00032 #include "abacus/nonduplpool.h" 00033 #include "abacus/cutbuffer.h" 00034 #include "abacus/master.h" 00035 #include "abacus/poolslot.h" 00036 #include "abacus/constraint.h" 00037 #include "abacus/variable.h" 00038 #include "abacus/sub.h" 00039 #include "abacus/bheap.h" 00040 00041 #include <iostream> 00042 using namespace std; 00043 00044 template<class BaseType, class CoType> 00045 inline ABA_NONDUPLPOOL<BaseType, CoType>::ABA_NONDUPLPOOL(ABA_MASTER *master, 00046 int size, 00047 bool autoRealloc) 00048 : 00049 ABA_STANDARDPOOL<BaseType, CoType>(master, size, autoRealloc), 00050 hash_(ABA_POOL<BaseType, CoType>::master_, size), 00051 nDuplications_(0) 00052 { } 00053 00054 template<class BaseType, class CoType> 00055 ABA_NONDUPLPOOL<BaseType, CoType>::~ABA_NONDUPLPOOL() 00056 { 00057 #ifdef ABACUSSAFE 00058 ABA_POOL<BaseType, CoType>::master_->out() << "Number of duplicated constraints: " << 00059 nDuplications_ << endl; 00060 #endif 00061 } 00062 00063 template<class BaseType, class CoType> 00064 ABA_POOLSLOT<BaseType, CoType> * ABA_NONDUPLPOOL<BaseType, CoType>::insert( 00065 BaseType *cv) 00066 { 00067 00068 ABA_POOLSLOT<BaseType, CoType>* slot; 00069 00070 slot = present(cv); 00071 if (slot == 0) { 00072 slot = ABA_STANDARDPOOL<BaseType, CoType>::insert(cv); 00073 if (slot) 00074 hash_.insert(cv->hashKey(), slot); 00075 } 00076 else { 00077 delete cv; 00078 nDuplications_++; 00079 } 00080 return slot; 00081 } 00082 00083 template<class BaseType, class CoType> 00084 ABA_POOLSLOT<BaseType, CoType> *ABA_NONDUPLPOOL<BaseType, CoType>::present( 00085 BaseType *cv) 00086 { 00087 int key = cv->hashKey(); 00088 00089 ABA_POOLSLOT<BaseType, CoType> **cand = hash_.initializeIteration(key); 00090 while(cand) { 00091 if (cv->equal((*cand)->conVar())) { 00092 return *cand; 00093 }cand = hash_.next(key); 00094 } 00095 return 0; 00096 } 00097 00098 template<class BaseType, class CoType> 00099 void ABA_NONDUPLPOOL<BaseType, CoType>::increase(int size) 00100 { 00101 ABA_STANDARDPOOL<BaseType, CoType>::increase(size); 00102 hash_.resize(size); 00103 } 00104 00105 template<class BaseType, class CoType> 00106 int ABA_NONDUPLPOOL<BaseType, CoType>::softDeleteConVar(ABA_POOLSLOT<BaseType, CoType> *slot) 00107 { 00108 int key = slot->conVar()->hashKey(); 00109 00110 if (ABA_POOL<BaseType, CoType>::softDeleteConVar(slot) == 0) { 00111 if (hash_.remove(key, slot)) { 00112 ABA_POOL<BaseType, CoType>::master_->err() << "ABA_NONDUPLPOOL::softDeleteCon(): slot not "; 00113 ABA_POOL<BaseType, CoType>::master_->err() << "found in hash table." << endl; 00114 exit(ABA_ABACUSROOT::Fatal); 00115 } 00116 return 0; 00117 } 00118 return 1; 00119 } 00120 00121 template<class BaseType, class CoType> 00122 void ABA_NONDUPLPOOL<BaseType, CoType>::hardDeleteConVar( 00123 ABA_POOLSLOT<BaseType, CoType> *slot) 00124 { 00125 if (hash_.remove(slot->conVar()->hashKey(), slot)) { 00126 ABA_POOL<BaseType, CoType>::master_->err() << "ABA_NONDUPLPOOL::hardDeleteConVar(): constraint "; 00127 ABA_POOL<BaseType, CoType>::master_->err() << "not found in hash table." << endl; 00128 exit(ABA_ABACUSROOT::Fatal); 00129 } 00130 ABA_POOL<BaseType, CoType>::hardDeleteConVar(slot); 00131 } 00132 00133 template<class BaseType, class CoType> 00134 void ABA_NONDUPLPOOL<BaseType, CoType>::statistics(int &nDuplications, 00135 int &nCollisions) const 00136 { 00137 nDuplications = nDuplications_; 00138 nCollisions = hash_.nCollisions(); 00139 } 00140 00141 00142 #endif // ABA_NONDUPLPOOL_INC