sat-solver/Map.h
00001 /*******************************************************************************************[Map.h]
00002 MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
00005 associated documentation files (the "Software"), to deal in the Software without restriction,
00006 including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008 furnished to do so, subject to the following conditions:
00009 
00010 The above copyright notice and this permission notice shall be included in all copies or
00011 substantial portions of the Software.
00012 
00013 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
00014 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
00017 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018 **************************************************************************************************/
00019 
00020 #ifndef Map_h
00021 #define Map_h
00022 
00023 #include <stdint.h>
00024 
00025 #include "Vec.h"
00026 
00027 //=================================================================================================
00028 // Default hash/equals functions
00029 //
00030 
00031 template<class K> struct Hash  { uint32_t operator()(const K& k)               const { return hash(k);  } };
00032 template<class K> struct Equal { bool     operator()(const K& k1, const K& k2) const { return k1 == k2; } };
00033 
00034 template<class K> struct DeepHash  { uint32_t operator()(const K* k)               const { return hash(*k);  } };
00035 template<class K> struct DeepEqual { bool     operator()(const K* k1, const K* k2) const { return *k1 == *k2; } };
00036 
00037 //=================================================================================================
00038 // Some primes
00039 //
00040 
00041 static const int nprimes          = 25;
00042 static const int primes [nprimes] = { 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 };
00043 
00044 //=================================================================================================
00045 // Hash table implementation of Maps
00046 //
00047 
00048 template<class K, class D, class H = Hash<K>, class E = Equal<K> >
00049 class Map {
00050     struct Pair { K key; D data; };
00051 
00052     H          hash;
00053     E          equals;
00054 
00055     vec<Pair>* table;
00056     int        cap;
00057     int        size;
00058 
00059     // Don't allow copying (error prone):
00060     Map<K,D,H,E>&  operator = (Map<K,D,H,E>& other) { assert(0); }
00061                    Map        (Map<K,D,H,E>& other) { assert(0); }
00062 
00063     int32_t index  (const K& k) const { return hash(k) % cap; }
00064     void   _insert (const K& k, const D& d) { table[index(k)].push(); table[index(k)].last().key = k; table[index(k)].last().data = d; }
00065     void    rehash () {
00066         const vec<Pair>* old = table;
00067 
00068         int newsize = primes[0];
00069         for (int i = 1; newsize <= cap && i < nprimes; i++)
00070            newsize = primes[i];
00071 
00072         table = new vec<Pair>[newsize];
00073 
00074         for (int i = 0; i < cap; i++){
00075             for (int j = 0; j < old[i].size(); j++){
00076                 _insert(old[i][j].key, old[i][j].data); }}
00077 
00078         delete [] old;
00079 
00080         cap = newsize;
00081     }
00082 
00083     
00084     public:
00085 
00086      Map () : table(NULL), cap(0), size(0) {}
00087      Map (const H& h, const E& e) : Map(), hash(h), equals(e) {}
00088     ~Map () { delete [] table; }
00089 
00090     void insert (const K& k, const D& d) { if (size+1 > cap / 2) rehash(); _insert(k, d); size++; }
00091     bool peek   (const K& k, D& d) {
00092         if (size == 0) return false;
00093         const vec<Pair>& ps = table[index(k)];
00094         for (int i = 0; i < ps.size(); i++)
00095             if (equals(ps[i].key, k)){
00096                 d = ps[i].data;
00097                 return true; } 
00098         return false;
00099     }
00100 
00101     void remove (const K& k) {
00102         assert(table != NULL);
00103         vec<Pair>& ps = table[index(k)];
00104         int j = 0;
00105         for (; j < ps.size() && !equals(ps[j].key, k); j++);
00106         assert(j < ps.size());
00107         ps[j] = ps.last();
00108         ps.pop();
00109     }
00110 
00111     void clear  () {
00112         cap = size = 0;
00113         delete [] table;
00114         table = NULL;
00115     }
00116 };
00117 
00118 #endif