1 /*******************************************************************************************[Map.h]
2 Copyright (c) 2006-2010, 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 **************************************************************************************************/
23 #include "libs/minisat/IntTypes.h"
24 #include "libs/minisat/Vec.h"
28 //=================================================================================================
29 // Default hash/equals functions
32 template<class K
> struct Hash
{ uint32_t operator()(const K
& k
) const { return hash(k
); } };
33 template<class K
> struct Equal
{ bool operator()(const K
& k1
, const K
& k2
) const { return k1
== k2
; } };
35 template<class K
> struct DeepHash
{ uint32_t operator()(const K
* k
) const { return hash(*k
); } };
36 template<class K
> struct DeepEqual
{ bool operator()(const K
* k1
, const K
* k2
) const { return *k1
== *k2
; } };
38 static inline uint32_t hash(uint32_t x
){ return x
; }
39 static inline uint32_t hash(uint64_t x
){ return (uint32_t)x
; }
40 static inline uint32_t hash(int32_t x
) { return (uint32_t)x
; }
41 static inline uint32_t hash(int64_t x
) { return (uint32_t)x
; }
44 //=================================================================================================
48 static const int nprimes
= 25;
49 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 };
51 //=================================================================================================
52 // Hash table implementation of Maps
55 template<class K
, class D
, class H
= Hash
<K
>, class E
= Equal
<K
> >
58 struct Pair
{ K key
; D data
; };
68 // Don't allow copying (error prone):
69 Map
<K
,D
,H
,E
>& operator = (Map
<K
,D
,H
,E
>& other
);
70 Map (Map
<K
,D
,H
,E
>& other
);
72 bool checkCap(int new_size
) const { return new_size
> cap
; }
74 int32_t index (const K
& k
) const { return hash(k
) % cap
; }
75 void _insert (const K
& k
, const D
& d
) {
76 vec
<Pair
>& ps
= table
[index(k
)];
77 ps
.push(); ps
.last().key
= k
; ps
.last().data
= d
; }
80 const vec
<Pair
>* old
= table
;
83 int newsize
= primes
[0];
84 for (int i
= 1; newsize
<= cap
&& i
< nprimes
; i
++)
87 table
= new vec
<Pair
>[newsize
];
90 for (int i
= 0; i
< old_cap
; i
++){
91 for (int j
= 0; j
< old
[i
].size(); j
++){
92 _insert(old
[i
][j
].key
, old
[i
][j
].data
); }}
96 // printf(" --- rehashing, old-cap=%d, new-cap=%d\n", cap, newsize);
102 Map () : table(NULL
), cap(0), size(0) {}
103 Map (const H
& h
, const E
& e
) : hash(h
), equals(e
), table(NULL
), cap(0), size(0){}
104 ~Map () { delete [] table
; }
106 // PRECONDITION: the key must already exist in the map.
107 const D
& operator [] (const K
& k
) const
111 const vec
<Pair
>& ps
= table
[index(k
)];
112 for (int i
= 0; i
< ps
.size(); i
++)
113 if (equals(ps
[i
].key
, k
))
119 // PRECONDITION: the key must already exist in the map.
120 D
& operator [] (const K
& k
)
124 vec
<Pair
>& ps
= table
[index(k
)];
125 for (int i
= 0; i
< ps
.size(); i
++)
126 if (equals(ps
[i
].key
, k
))
132 // PRECONDITION: the key must *NOT* exist in the map.
133 void insert (const K
& k
, const D
& d
) { if (checkCap(size
+1)) rehash(); _insert(k
, d
); size
++; }
134 bool peek (const K
& k
, D
& d
) const {
135 if (size
== 0) return false;
136 const vec
<Pair
>& ps
= table
[index(k
)];
137 for (int i
= 0; i
< ps
.size(); i
++)
138 if (equals(ps
[i
].key
, k
)){
144 bool has (const K
& k
) const {
145 if (size
== 0) return false;
146 const vec
<Pair
>& ps
= table
[index(k
)];
147 for (int i
= 0; i
< ps
.size(); i
++)
148 if (equals(ps
[i
].key
, k
))
153 // PRECONDITION: the key must exist in the map.
154 void remove(const K
& k
) {
155 assert(table
!= NULL
);
156 vec
<Pair
>& ps
= table
[index(k
)];
158 for (; j
< ps
.size() && !equals(ps
[j
].key
, k
); j
++);
159 assert(j
< ps
.size());
171 int elems() const { return size
; }
172 int bucket_count() const { return cap
; }
174 // NOTE: the hash and equality objects are not moved by this method:
175 void moveTo(Map
& other
){
176 delete [] other
.table
;
186 // NOTE: given a bit more time, I could make a more C++-style iterator out of this:
187 const vec
<Pair
>& bucket(int i
) const { return table
[i
]; }
190 //=================================================================================================