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 **************************************************************************************************/
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 //=================================================================================================
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
> >
50 struct Pair
{ K key
; D data
; };
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
; }
66 const vec
<Pair
>* old
= table
;
68 int newsize
= primes
[0];
69 for (int i
= 1; newsize
<= cap
&& i
< nprimes
; 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
); }}
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
)){
101 void remove (const K
& k
) {
102 assert(table
!= NULL
);
103 vec
<Pair
>& ps
= table
[index(k
)];
105 for (; j
< ps
.size() && !equals(ps
[j
].key
, k
); j
++);
106 assert(j
< ps
.size());