1 /*************************************************************
2 MiniSat       --- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 CryptoMiniSat --- Copyright (c) 2014, Mate Soos
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 ***************************************************************/
23 
24 #ifndef __WATCHARRAY_H__
25 #define __WATCHARRAY_H__
26 
27 #include "watched.h"
28 #include "Vec.h"
29 #include <vector>
30 
31 namespace CMSat {
32 using std::vector;
33 
34 typedef vec<Watched>& watch_subarray;
35 typedef const vec<Watched>& watch_subarray_const;
36 
37 class watch_array
38 {
39 public:
40     vec<vec<Watched> > watches;
41     vector<Lit> smudged_list;
42     vector<char> smudged;
43 
smudge(const Lit lit)44     void smudge(const Lit lit) {
45         if (!smudged[lit.toInt()]) {
46             smudged_list.push_back(lit);
47             smudged[lit.toInt()] = true;
48         }
49     }
50 
get_smudged_list()51     const vector<Lit>& get_smudged_list() const {
52         return smudged_list;
53     }
54 
clear_smudged()55     void clear_smudged()
56     {
57         for(const Lit lit: smudged_list) {
58             assert(smudged[lit.toInt()]);
59             smudged[lit.toInt()] = false;
60         }
61         smudged_list.clear();
62     }
63 
64     watch_subarray operator[](Lit pos)
65     {
66         return watches[pos.toInt()];
67     }
68 
at(size_t pos)69     watch_subarray at(size_t pos)
70     {
71         assert(watches.size() > pos);
72         return watches[pos];
73     }
74 
75     watch_subarray_const operator[](Lit at) const
76     {
77         return watches[at.toInt()];
78     }
79 
at(size_t pos)80     watch_subarray_const at(size_t pos) const
81     {
82         assert(watches.size() > pos);
83         return watches[pos];
84     }
85 
resize(const size_t new_size)86     void resize(const size_t new_size)
87     {
88         assert(smudged_list.empty());
89         if (watches.size() < new_size) {
90             watches.growTo(new_size);
91         } else {
92             watches.shrink(watches.size()-new_size);
93         }
94         smudged.resize(new_size, false);
95     }
96 
insert(uint32_t num)97     void insert(uint32_t num)
98     {
99         smudged.insert(smudged.end(), num, false);
100         watches.insert(num);
101     }
102 
mem_used()103     size_t mem_used() const
104     {
105         double mem = watches.capacity()*sizeof(vec<Watched>);
106         for(size_t i = 0; i < watches.size(); i++) {
107             //1.2 is overhead
108             mem += (double)watches[i].capacity()*(double)sizeof(Watched)*1.2;
109         }
110         mem += smudged.capacity()*sizeof(char);
111         mem += smudged_list.capacity()*sizeof(Lit);
112         return mem;
113     }
114 
size()115     size_t size() const
116     {
117         return watches.size();
118     }
119 
prefetch(const size_t at)120     void prefetch(const size_t at) const
121     {
122         __builtin_prefetch(watches[at].data);
123     }
124     typedef vec<Watched>* iterator;
125     typedef const vec<Watched>* const_iterator;
126 
begin()127     iterator begin()
128     {
129         return watches.begin();
130     }
131 
end()132     iterator end()
133     {
134         return watches.end();
135     }
136 
begin()137     const_iterator begin() const
138     {
139         return watches.begin();
140     }
141 
end()142     const_iterator end() const
143     {
144         return watches.end();
145     }
146 
consolidate()147     void consolidate()
148     {
149         /*for(auto& ws: watches) {
150             ws.shrink_to_fit();
151         }*/
152         watches.shrink_to_fit();
153     }
154 
full_consolidate()155     void full_consolidate()
156     {
157         for(auto& ws: watches) {
158             ws.shrink_to_fit();
159         }
160         watches.shrink_to_fit();
161     }
162 
print_stat()163     void print_stat()
164     {
165     }
166 
mem_used_alloc()167     size_t mem_used_alloc() const
168     {
169         size_t mem = 0;
170         for(auto& ws: watches) {
171             mem += ws.capacity()*sizeof(Watched);
172         }
173 
174         return mem;
175     }
176 
mem_used_array()177     size_t mem_used_array() const
178     {
179         size_t mem = 0;
180         mem += watches.capacity()*sizeof(vector<Watched>);
181         mem += sizeof(watch_array);
182         return mem;
183     }
184 };
185 
swap(watch_subarray a,watch_subarray b)186 inline void swap(watch_subarray a, watch_subarray b)
187 {
188     a.swap(b);
189 }
190 
191 
192 } //End of namespace
193 
194 #endif //__WATCHARRAY_H__
195