Import minisat2-070721
[cl-satwrap.git] / backends / minisat / mtl / Map.h
blobb6d76a31c2deff1acf097301ca297aa1fddb9ae8
1 /*******************************************************************************************[Map.h]
2 MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 associated documentation files (the "Software"), to deal in the Software without restriction,
6 including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
10 The above copyright notice and this permission notice shall be included in all copies or
11 substantial portions of the Software.
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 **************************************************************************************************/
20 #ifndef Map_h
21 #define Map_h
23 #include <stdint.h>
25 #include "Vec.h"
27 //=================================================================================================
28 // Default hash/equals functions
31 template<class K> struct Hash { uint32_t operator()(const K& k) const { return hash(k); } };
32 template<class K> struct Equal { bool operator()(const K& k1, const K& k2) const { return k1 == k2; } };
34 template<class K> struct DeepHash { uint32_t operator()(const K* k) const { return hash(*k); } };
35 template<class K> struct DeepEqual { bool operator()(const K* k1, const K* k2) const { return *k1 == *k2; } };
37 //=================================================================================================
38 // Some primes
41 static const int nprimes = 25;
42 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 };
44 //=================================================================================================
45 // Hash table implementation of Maps
48 template<class K, class D, class H = Hash<K>, class E = Equal<K> >
49 class Map {
50 struct Pair { K key; D data; };
52 H hash;
53 E equals;
55 vec<Pair>* table;
56 int cap;
57 int size;
59 // Don't allow copying (error prone):
60 Map<K,D,H,E>& operator = (Map<K,D,H,E>& other) { assert(0); }
61 Map (Map<K,D,H,E>& other) { assert(0); }
63 int32_t index (const K& k) const { return hash(k) % cap; }
64 void _insert (const K& k, const D& d) { table[index(k)].push(); table[index(k)].last().key = k; table[index(k)].last().data = d; }
65 void rehash () {
66 const vec<Pair>* old = table;
68 int newsize = primes[0];
69 for (int i = 1; newsize <= cap && i < nprimes; i++)
70 newsize = primes[i];
72 table = new vec<Pair>[newsize];
74 for (int i = 0; i < cap; i++){
75 for (int j = 0; j < old[i].size(); j++){
76 _insert(old[i][j].key, old[i][j].data); }}
78 delete [] old;
80 cap = newsize;
84 public:
86 Map () : table(NULL), cap(0), size(0) {}
87 Map (const H& h, const E& e) : Map(), hash(h), equals(e) {}
88 ~Map () { delete [] table; }
90 void insert (const K& k, const D& d) { if (size+1 > cap / 2) rehash(); _insert(k, d); size++; }
91 bool peek (const K& k, D& d) {
92 if (size == 0) return false;
93 const vec<Pair>& ps = table[index(k)];
94 for (int i = 0; i < ps.size(); i++)
95 if (equals(ps[i].key, k)){
96 d = ps[i].data;
97 return true; }
98 return false;
101 void remove (const K& k) {
102 assert(table != NULL);
103 vec<Pair>& ps = table[index(k)];
104 int j = 0;
105 for (; j < ps.size() && !equals(ps[j].key, k); j++);
106 assert(j < ps.size());
107 ps[j] = ps.last();
108 ps.pop();
111 void clear () {
112 cap = size = 0;
113 delete [] table;
114 table = NULL;
118 #endif