1 /****************************************************************************************[IntMap.h]
2 Copyright (c) 2011, Niklas Sorensson
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4 associated documentation files (the "Software"), to deal in the Software without restriction,
5 including without limitation the rights to use, copy, modify, merge, publish, distribute,
6 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7 furnished to do so, subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in all copies or
10 substantial portions of the Software.
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
13 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
16 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 **************************************************************************************************/
19 #ifndef Minisat_IntMap_h
20 #define Minisat_IntMap_h
26 template<class T
> struct MkIndexDefault
{
27 typename vec
<T
>::Size
operator()(T t
) const { return (typename vec
<T
>::Size
)t
; }
30 template<class K
, class V
, class MkIndex
= MkIndexDefault
<K
> >
35 explicit IntMap(MkIndex _index
= MkIndex()) : index(_index
){}
37 bool has (K k
) const { return index(k
) < map
.size(); }
39 const V
& operator[](K k
) const { assert(has(k
)); return map
[index(k
)]; }
40 V
& operator[](K k
) { assert(has(k
)); return map
[index(k
)]; }
42 const V
* begin () const { return &map
[0]; }
43 const V
* end () const { return &map
[map
.size()]; }
44 V
* begin () { return &map
[0]; }
45 V
* end () { return &map
[map
.size()]; }
47 void reserve(K key
, V pad
) { map
.growTo(index(key
)+1, pad
); }
48 void reserve(K key
) { map
.growTo(index(key
)+1); }
49 void insert (K key
, V val
, V pad
){ reserve(key
, pad
); operator[](key
) = val
; }
50 void insert (K key
, V val
) { reserve(key
); operator[](key
) = val
; }
52 void clear (bool dispose
= false) { map
.clear(dispose
); }
53 void moveTo (IntMap
& to
) { map
.moveTo(to
.map
); to
.index
= index
; }
54 void copyTo (IntMap
& to
) const { map
.copyTo(to
.map
); to
.index
= index
; }
58 template<class K
, class MkIndex
= MkIndexDefault
<K
> >
61 IntMap
<K
, char, MkIndex
> in_set
;
66 int size (void) const { return xs
.size(); }
67 void clear (bool free
= false){
71 for (int i
= 0; i
< xs
.size(); i
++)
76 // Allow inspecting the internal vector:
78 toVec () const { return xs
; }
81 K
operator [] (int index
) const { return xs
[index
]; }
84 void insert (K k
) { in_set
.reserve(k
, 0); if (!in_set
[k
]) { in_set
[k
] = 1; xs
.push(k
); } }
85 bool has (K k
) { in_set
.reserve(k
, 0); return in_set
[k
]; }
89 template<class K
, class V
, V nil
, class MkIndex
= MkIndexDefault
<K
> >
99 const V
& operator[](K k
) const;
104 //=================================================================================================
105 } // namespace Minisat