ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
Map.h
Go to the documentation of this file.
1/*******************************************************************************************[Map.h]
2Copyright (c) 2006-2010, Niklas Sorensson
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5associated documentation files (the "Software"), to deal in the Software without restriction,
6including without limitation the rights to use, copy, modify, merge, publish, distribute,
7sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or
11substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18**************************************************************************************************/
19
20#ifndef Minisat_Map_h
21#define Minisat_Map_h
22
23#include "sat/bsat2/IntTypes.h"
24#include "sat/bsat2/Vec.h"
25
27
28namespace Minisat {
29
30//=================================================================================================
31// Default hash/equals functions
32//
33
34template<class K> struct Hash { uint32_t operator()(const K& k) const { return hash(k); } };
35template<class K> struct Equal { bool operator()(const K& k1, const K& k2) const { return k1 == k2; } };
36
37template<class K> struct DeepHash { uint32_t operator()(const K* k) const { return hash(*k); } };
38template<class K> struct DeepEqual { bool operator()(const K* k1, const K* k2) const { return *k1 == *k2; } };
39
40static inline uint32_t hash(uint32_t x){ return x; }
41static inline uint32_t hash(uint64_t x){ return (uint32_t)x; }
42static inline uint32_t hash(int32_t x) { return (uint32_t)x; }
43static inline uint32_t hash(int64_t x) { return (uint32_t)x; }
44
45
46//=================================================================================================
47// Some primes
48//
49
50static const int nprimes = 25;
51static 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 };
52
53//=================================================================================================
54// Hash table implementation of Maps
55//
56
57template<class K, class D, class H = Hash<K>, class E = Equal<K> >
58class Map {
59 public:
60 struct Pair { K key; D data; };
61
62 private:
63 H hash;
64 E equals;
65
66 vec<Pair>* table;
67 int cap;
68 int size;
69
70 // Don't allow copying (error prone):
71 Map<K,D,H,E>& operator = (Map<K,D,H,E>& other) { assert(0); }
72 Map (Map<K,D,H,E>& other) { assert(0); }
73
74 bool checkCap(int new_size) const { return new_size > cap; }
75
76 int32_t index (const K& k) const { return hash(k) % cap; }
77 void _insert (const K& k, const D& d) {
78 vec<Pair>& ps = table[index(k)];
79 ps.push(); ps.last().key = k; ps.last().data = d; }
80
81 void rehash () {
82 const vec<Pair>* old = table;
83
84 int old_cap = cap;
85 int newsize = primes[0];
86 for (int i = 1; newsize <= cap && i < nprimes; i++)
87 newsize = primes[i];
88
89 table = new vec<Pair>[newsize];
90 cap = newsize;
91
92 for (int i = 0; i < old_cap; i++){
93 for (int j = 0; j < old[i].size(); j++){
94 _insert(old[i][j].key, old[i][j].data); }}
95
96 delete [] old;
97
98 // printf(" --- rehashing, old-cap=%d, new-cap=%d\n", cap, newsize);
99 }
100
101
102 public:
103
104 Map () : table(NULL), cap(0), size(0) {}
105 Map (const H& h, const E& e) : hash(h), equals(e), table(NULL), cap(0), size(0){}
106 ~Map () { delete [] table; }
107
108 // PRECONDITION: the key must already exist in the map.
109 const D& operator [] (const K& k) const
110 {
111 assert(size != 0);
112 const D* res = NULL;
113 const vec<Pair>& ps = table[index(k)];
114 for (int i = 0; i < ps.size(); i++)
115 if (equals(ps[i].key, k))
116 res = &ps[i].data;
117 assert(res != NULL);
118 return *res;
119 }
120
121 // PRECONDITION: the key must already exist in the map.
122 D& operator [] (const K& k)
123 {
124 assert(size != 0);
125 D* res = NULL;
126 vec<Pair>& ps = table[index(k)];
127 for (int i = 0; i < ps.size(); i++)
128 if (equals(ps[i].key, k))
129 res = &ps[i].data;
130 assert(res != NULL);
131 return *res;
132 }
133
134 // PRECONDITION: the key must *NOT* exist in the map.
135 void insert (const K& k, const D& d) { if (checkCap(size+1)) rehash(); _insert(k, d); size++; }
136 bool peek (const K& k, D& d) const {
137 if (size == 0) return false;
138 const vec<Pair>& ps = table[index(k)];
139 for (int i = 0; i < ps.size(); i++)
140 if (equals(ps[i].key, k)){
141 d = ps[i].data;
142 return true; }
143 return false;
144 }
145
146 bool has (const K& k) const {
147 if (size == 0) return false;
148 const vec<Pair>& ps = table[index(k)];
149 for (int i = 0; i < ps.size(); i++)
150 if (equals(ps[i].key, k))
151 return true;
152 return false;
153 }
154
155 // PRECONDITION: the key must exist in the map.
156 void remove(const K& k) {
157 assert(table != NULL);
158 vec<Pair>& ps = table[index(k)];
159 int j = 0;
160 for (; j < ps.size() && !equals(ps[j].key, k); j++);
161 assert(j < ps.size());
162 ps[j] = ps.last();
163 ps.pop();
164 size--;
165 }
166
167 void clear () {
168 cap = size = 0;
169 delete [] table;
170 table = NULL;
171 }
172
173 int elems() const { return size; }
174 int bucket_count() const { return cap; }
175
176 // NOTE: the hash and equality objects are not moved by this method:
177 void moveTo(Map& other){
178 delete [] other.table;
179
180 other.table = table;
181 other.cap = cap;
182 other.size = size;
183
184 table = NULL;
185 size = cap = 0;
186 }
187
188 // NOTE: given a bit more time, I could make a more C++-style iterator out of this:
189 const vec<Pair>& bucket(int i) const { return table[i]; }
190};
191
192//=================================================================================================
193}
194
196
197#endif
unsigned int uint32_t
Definition Fxch.h:32
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
void insert(const K &k, const D &d)
Definition Map.h:135
const D & operator[](const K &k) const
Definition Map.h:109
void moveTo(Map &other)
Definition Map.h:177
const vec< Pair > & bucket(int i) const
Definition Map.h:189
int bucket_count() const
Definition Map.h:174
Map(const H &h, const E &e)
Definition Map.h:105
void remove(const K &k)
Definition Map.h:156
int elems() const
Definition Map.h:173
bool peek(const K &k, D &d) const
Definition Map.h:136
bool has(const K &k) const
Definition Map.h:146
void clear()
Definition Map.h:167
int size(void) const
Definition Vec.h:65
const T & last(void) const
Definition Vec.h:84
void pop(void)
Definition Vec.h:78
enum keys key
Definition main.h:25
Definition Alg.h:28
bool operator()(const K *k1, const K *k2) const
Definition Map.h:38
uint32_t operator()(const K *k) const
Definition Map.h:37
bool operator()(const K &k1, const K &k2) const
Definition Map.h:35
uint32_t operator()(const K &k) const
Definition Map.h:34
#define assert(ex)
Definition util_old.h:213