ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
rrrUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <iomanip>
5#include <sstream>
6#include <vector>
7#include <set>
8#include <string>
9#include <functional>
10#include <limits>
11#include <type_traits>
12#include <cassert>
13
14#include "rrrTypes.h"
15
17
18namespace rrr {
19
20 /* {{{ Invocable */
21
22#if defined(__cpp_lib_is_invocable)
23 template <typename Fn, typename... Args>
24 using is_invokable = std::is_invocable<Fn, Args...>;
25#else
26 template <typename Fn, typename... Args>
27 struct is_invokable: std::is_constructible<std::function<void(Args...)>, std::reference_wrapper<typename std::remove_reference<Fn>::type>> {};
28#endif
29
30 /* }}} */
31
32 /* {{{ Int size */
33
34 template <template <typename...> typename Container, typename... Ts>
35 static inline int int_size(Container<Ts...> const &c) {
36 assert(c.size() <= (typename Container<Ts...>::size_type)std::numeric_limits<int>::max());
37 return c.size();
38 }
39
40 template <template <typename...> typename Container, typename... Ts>
41 static inline bool check_int_size(Container<Ts...> const &c) {
42 return c.size() <= (typename Container<Ts...>::size_type)std::numeric_limits<int>::max();
43 }
44
45 static inline bool check_int_max(int i) {
46 return i == std::numeric_limits<int>::max();
47 }
48
49 template <typename Iterator>
50 static inline int int_distance(Iterator begin, Iterator it) {
51 typename std::iterator_traits<Iterator>::difference_type d = std::distance(begin, it);
52 assert(d <= (typename std::iterator_traits<Iterator>::difference_type)std::numeric_limits<int>::max());
53 assert(d >= (typename std::iterator_traits<Iterator>::difference_type)std::numeric_limits<int>::lowest());
54 return d;
55 }
56
57 /* }}} */
58
59 /* {{{ Print containers */
60
61 template <typename T>
62 std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
63 std::string delim;
64 os << "[";
65 for(T const &e: v) {
66 os << delim << e;
67 delim = ", ";
68 }
69 os << "]";
70 return os;
71 }
72
73 template <typename T>
74 std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
75 std::string delim;
76 os << "{";
77 for(T const &e: s) {
78 os << delim << e;
79 delim = ", ";
80 }
81 os << "}";
82 return os;
83 }
84
85 void PrintComplementedEdges(std::function<void(std::function<void(int, bool)> const &)> const &ForEachEdge) {
86 std::string delim;
87 std::cout << "[";
88 ForEachEdge([&] (int id, bool c) {
89 std::cout << delim << (c? "!": "") << id;
90 delim = ", ";
91 });
92 std::cout << "]";
93 }
94
95 /* }}} */
96
97 /* {{{ Print next */
98
99 struct SW {
100 int width = 0;
101 bool left = false;
102 };
103
104 struct NS {}; // no space
105
106 template <typename T>
107 void PrintNext(std::ostream &os, T t);
108 template <typename T, typename... Args>
109 void PrintNext(std::ostream &os, T t, Args... args);
110
111 static inline void PrintNext(std::ostream &os, int t) {
112 os << std::setw(4) << t;
113 }
114
115 template <typename... Args>
116 static inline void PrintNext(std::ostream &os, int t, Args... args) {
117 os << std::setw(4) << t << " ";
118 PrintNext(os, args...);
119 }
120
121 static inline void PrintNext(std::ostream &os, bool arg) {
122 os << arg;
123 }
124
125 template <typename... Args>
126 static inline void PrintNext(std::ostream &os, bool arg, Args... args) {
127 if(arg) {
128 os << "!";
129 } else {
130 os << " ";
131 }
132 PrintNext(os, args...);
133 }
134
135 static inline void PrintNext(std::ostream &os, double t) {
136 os << std::fixed << std::setprecision(2) << std::setw(8) << t;
137 }
138
139 template <typename... Args>
140 static inline void PrintNext(std::ostream &os, double t, Args... args) {
141 os << std::fixed << std::setprecision(2) << std::setw(8) << t << " ";
142 PrintNext(os, args...);
143 }
144
145 template <typename T>
146 static inline void PrintNext(std::ostream &os, SW sw, T arg) {
147 if(sw.left) {
148 os << std::left;
149 }
150 os << std::setw(sw.width) << arg;
151 if(sw.left) {
152 os << std::right;
153 }
154 }
155
156 template <typename T, typename... Args>
157 static inline void PrintNext(std::ostream &os, SW sw, T arg, Args... args) {
158 if(sw.left) {
159 os << std::left;
160 }
161 os << std::setw(sw.width) << arg << " ";
162 if(sw.left) {
163 os << std::right;
164 }
165 PrintNext(os, args...);
166 }
167
168 template <typename T, typename... Args>
169 static inline void PrintNext(std::ostream &os, NS ns, T arg, Args... args) {
170 (void)ns;
171 os << arg;
172 PrintNext(os, args...);
173 }
174
175 template <typename T>
176 static inline void PrintNext(std::ostream& os, std::vector<T> const &arg) {
177 os << "[ ";
178 for(T const &e: arg) {
179 PrintNext(os, e);
180 os << " ";
181 }
182 os << "]";
183 }
184
185 template <typename T, typename... Args>
186 static inline void PrintNext(std::ostream& os, std::vector<T> const &arg, Args... args) {
187 os << "[ ";
188 for(T const &e: arg) {
189 PrintNext(os, e);
190 os << " ";
191 }
192 os << "] ";
193 PrintNext(os, args...);
194 }
195
196 template <typename T>
197 static inline void PrintNext(std::ostream& os, std::set<T> const &arg) {
198 os << "{ ";
199 for(T const &e: arg) {
200 PrintNext(os, e);
201 os << " ";
202 }
203 os << "}";
204 }
205
206 template <typename T, typename... Args>
207 static inline void PrintNext(std::ostream& os, std::set<T> const &arg, Args... args) {
208 os << "{ ";
209 for(T const &e: arg) {
210 PrintNext(os, e);
211 os << " ";
212 }
213 os << "} ";
214 PrintNext(os, args...);
215 }
216
217 template <typename T>
218 void PrintNext(std::ostream &os, T t) {
219 os << t;
220 }
221
222 template <typename T, typename... Args>
223 void PrintNext(std::ostream &os, T t, Args... args) {
224 os << t << " ";
225 PrintNext(os, args...);
226 }
227
228 /* }}} */
229
230 /* {{{ Print others */
231
232 static inline void PrintWarning(std::string message) {
233 std::cerr << "[w] " << message << std::endl;
234 }
235
236 /* }}} */
237
238 /* {{{ Combination */
239
240 bool ForEachCombinationStopRec(std::vector<int> &v, int n, int k, std::function<bool(std::vector<int> const &)> const &func) {
241 if(k == 0) {
242 return func(v);
243 }
244 for(int i = v.back() + 1; i < n - k + 1; i++) {
245 v.push_back(i);
246 if(ForEachCombinationStopRec(v, n, k-1, func)) {
247 return true;
248 }
249 v.pop_back();
250 }
251 return false;
252 }
253
254 static inline void ForEachCombinationStop(int n, int k, std::function<bool(std::vector<int> const &)> const &func) {
255 std::vector<int> v;
256 v.reserve(k);
257 ForEachCombinationStopRec(v, n, k, func);
258 }
259
260 /* }}} */
261
262 /* {{{ VarValue functions */
263
264 static inline VarValue DecideVarValue(VarValue x) {
265 switch(x) {
266 case UNDEF:
267 assert(0);
268 case TRUE:
269 return TRUE;
270 case FALSE:
271 return FALSE;
272 case TEMP_TRUE:
273 return TRUE;
274 case TEMP_FALSE:
275 return FALSE;
276 default:
277 assert(0);
278 }
279 return UNDEF;
280 }
281
282 static inline char GetVarValueChar(VarValue x) {
283 switch(x) {
284 case UNDEF:
285 return 'x';
286 case TRUE:
287 return '1';
288 case FALSE:
289 return '0';
290 case TEMP_TRUE:
291 return 't';
292 case TEMP_FALSE:
293 return 'f';
294 default:
295 assert(0);
296 }
297 return 'X';
298 }
299
300 /* }}} */
301
302 /* {{{ Action functions */
303
304 static inline char const *GetActionTypeCstr(Action const &action) {
305 switch(action.type) {
306 case REMOVE_FANIN:
307 return "remove fanin";
308 case REMOVE_UNUSED:
309 return "remove unused";
310 case REMOVE_BUFFER:
311 return "remove buffer";
312 case REMOVE_CONST:
313 return "remove const";
314 case ADD_FANIN:
315 return "add fanin";
316 case TRIVIAL_COLLAPSE:
317 return "trivial collapse";
319 return "trivial decompose";
320 case SORT_FANINS:
321 return "sort fanins";
322 case READ:
323 return "read";
324 case SAVE:
325 return "save";
326 case LOAD:
327 return "load";
328 case POP_BACK:
329 return "pop back";
330 case INSERT:
331 return "insert";
332 default:
333 assert(0);
334 }
335 return "";
336 }
337
338 static inline std::stringstream GetActionDescription(Action const &action) {
339 std::stringstream ss;
340 ss << GetActionTypeCstr(action);
341 std::string delim = " : ";
342 if(action.id != -1) {
343 ss << delim;
344 PrintNext(ss, "node", action.id);
345 delim = " , ";
346 }
347 if(action.fi != -1) {
348 ss << delim;
349 PrintNext(ss, "fanin", (bool)action.c, action.fi);
350 delim = " , ";
351 }
352 if(action.idx != -1) {
353 ss << delim;
354 PrintNext(ss, "index", action.idx);
355 }
356 if(action.fNew) {
357 ss << " new";
358 }
359 ss << std::endl;
360 if(!action.vFanins.empty()) {
361 ss << "fanins : ";
362 PrintNext(ss, action.vFanins);
363 }
364 if(!action.vIndices.empty()) {
365 ss << "indices : ";
366 PrintNext(ss, action.vIndices);
367 }
368 if(!action.vFanouts.empty()) {
369 ss << "fanouts : ";
370 PrintNext(ss, action.vFanouts);
371 }
372 return ss;
373 }
374
375 /* }}} */
376
377 /* {{{ Time functions */
378
379 static inline time_point GetCurrentTime() {
380 return clock_type::now();
381 }
382
383 static inline seconds DurationInSeconds(time_point start, time_point end) {
384 seconds t = (std::chrono::duration_cast<std::chrono::seconds>(end - start)).count();
385 assert(t >= 0);
386 return t;
387 }
388
389 static inline double Duration(time_point start, time_point end) {
390 double t = (std::chrono::duration<double>(end - start)).count();
391 assert(t >= 0);
392 return t;
393 }
394
395 /* }}} */
396
397}
398
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
Definition rrr.h:16
int64_t seconds
Definition rrrTypes.h:61
void PrintComplementedEdges(std::function< void(std::function< void(int, bool)> const &)> const &ForEachEdge)
Definition rrrUtils.h:85
VarValue
Definition rrrTypes.h:24
@ UNDEF
Definition rrrTypes.h:25
@ TEMP_FALSE
Definition rrrTypes.h:29
@ TRUE
Definition rrrTypes.h:26
@ FALSE
Definition rrrTypes.h:27
@ TEMP_TRUE
Definition rrrTypes.h:28
@ POP_BACK
Definition rrrTypes.h:45
@ REMOVE_BUFFER
Definition rrrTypes.h:36
@ REMOVE_UNUSED
Definition rrrTypes.h:35
@ TRIVIAL_DECOMPOSE
Definition rrrTypes.h:40
@ TRIVIAL_COLLAPSE
Definition rrrTypes.h:39
@ INSERT
Definition rrrTypes.h:46
@ SAVE
Definition rrrTypes.h:43
@ REMOVE_CONST
Definition rrrTypes.h:37
@ LOAD
Definition rrrTypes.h:44
@ SORT_FANINS
Definition rrrTypes.h:41
@ REMOVE_FANIN
Definition rrrTypes.h:34
@ ADD_FANIN
Definition rrrTypes.h:38
@ READ
Definition rrrTypes.h:42
void PrintNext(std::ostream &os, T t)
Definition rrrUtils.h:218
bool ForEachCombinationStopRec(std::vector< int > &v, int n, int k, std::function< bool(std::vector< int > const &)> const &func)
Definition rrrUtils.h:240
std::chrono::time_point< clock_type > time_point
Definition rrrTypes.h:63
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
Definition rrrUtils.h:62
int width
Definition rrrUtils.h:100
bool left
Definition rrrUtils.h:101
#define assert(ex)
Definition util_old.h:213