yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
IntMap.h
Go to the documentation of this file.
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:
8 
9 The above copyright notice and this permission notice shall be included in all copies or
10 substantial portions of the Software.
11 
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 **************************************************************************************************/
18 
19 #ifndef Minisat_IntMap_h
20 #define Minisat_IntMap_h
21 
22 #include "Vec.h"
23 
24 namespace Minisat {
25 
26  template<class T> struct MkIndexDefault {
27  typename vec<T>::Size operator()(T t) const { return (typename vec<T>::Size)t; }
28  };
29 
30  template<class K, class V, class MkIndex = MkIndexDefault<K> >
31  class IntMap {
33  MkIndex index;
34  public:
35  explicit IntMap(MkIndex _index = MkIndex()) : index(_index){}
36 
37  bool has (K k) const { return index(k) < map.size(); }
38 
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)]; }
41 
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()]; }
46 
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; }
51 
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; }
55  };
56 
57 
58  template<class K, class MkIndex = MkIndexDefault<K> >
59  class IntSet
60  {
63 
64  public:
65  // Size operations:
66  int size (void) const { return xs.size(); }
67  void clear (bool free = false){
68  if (free)
69  in_set.clear(true);
70  else
71  for (int i = 0; i < xs.size(); i++)
72  in_set[xs[i]] = 0;
73  xs.clear(free);
74  }
75 
76  // Allow inspecting the internal vector:
77  const vec<K>&
78  toVec () const { return xs; }
79 
80  // Vector interface:
81  K operator [] (int index) const { return xs[index]; }
82 
83 
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]; }
86  };
87 
88  #if 0
89  template<class K, class V, V nil, class MkIndex = MkIndexDefault<K> >
90  class IntMapNil {
91  vec<V> map;
92  V nil;
93 
94  public:
95  IntMap(){}
96 
97  void reserve(K);
98  V& find (K);
99  const V& operator[](K k) const;
100 
101  };
102  #endif
103 
104 //=================================================================================================
105 } // namespace Minisat
106 #endif
vec< V > map
Definition: IntMap.h:32
void insert(K key, V val, V pad)
Definition: IntMap.h:49
bool has(K k) const
Definition: IntMap.h:37
void free(void *)
void insert(K k)
Definition: IntMap.h:84
void clear(bool dispose=false)
Definition: IntMap.h:52
const V * end() const
Definition: IntMap.h:43
void clear(bool free=false)
Definition: IntMap.h:67
bool has(K k)
Definition: IntMap.h:85
void copyTo(vec< T > &copy) const
Definition: Vec.h:92
void growTo(Size size)
Definition: Vec.h:117
void reserve(K key)
Definition: IntMap.h:48
void insert(K key, V val)
Definition: IntMap.h:50
void reserve(K key, V pad)
Definition: IntMap.h:47
IntMap(MkIndex _index=MkIndex())
Definition: IntMap.h:35
K operator[](int index) const
Definition: IntMap.h:81
vec< K > xs
Definition: IntMap.h:62
void push(void)
Definition: Vec.h:74
const vec< K > & toVec() const
Definition: IntMap.h:78
Size size(void) const
Definition: Vec.h:64
const V & operator[](K k) const
Definition: IntMap.h:39
V * begin()
Definition: IntMap.h:44
V * end()
Definition: IntMap.h:45
void clear(bool dealloc=false)
Definition: Vec.h:125
void moveTo(IntMap &to)
Definition: IntMap.h:53
vec< T >::Size operator()(T t) const
Definition: IntMap.h:27
int size(void) const
Definition: IntMap.h:66
void copyTo(IntMap &to) const
Definition: IntMap.h:54
static bool find(V &ts, const T &t)
Definition: Alg.h:47
IntMap< K, char, MkIndex > in_set
Definition: IntMap.h:61
V & operator[](K k)
Definition: IntMap.h:40
void moveTo(vec< T > &dest)
Definition: Vec.h:93
MkIndex index
Definition: IntMap.h:33
const V * begin() const
Definition: IntMap.h:42