ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1#ifndef _util_hpp_INCLUDED
2#define _util_hpp_INCLUDED
3
4#include "global.h"
5
6#include <cassert>
7#include <cstdint>
8#include <vector>
9
11
12namespace CaDiCaL {
13
14using namespace std;
15
16// Common simple utility functions independent from 'Internal'.
17
18/*------------------------------------------------------------------------*/
19
20inline double relative (double a, double b) { return b ? a / b : 0; }
21inline double percent (double a, double b) { return relative (100 * a, b); }
22inline int sign (int lit) { return (lit > 0) - (lit < 0); }
23inline unsigned bign (int lit) { return 1 + (lit < 0); }
24
25/*------------------------------------------------------------------------*/
26
27bool has_suffix (const char *str, const char *suffix);
28bool has_prefix (const char *str, const char *prefix);
29
30/*------------------------------------------------------------------------*/
31
32// Parse integer string in the form of
33//
34// true
35// false
36// [-]<mantissa>[e<exponent>]
37//
38// and in the latter case '<val>' has to be within [-INT_MAX,INT_MAX].
39//
40// The function returns true if parsing is successful and then also sets
41// the second argument to the parsed value.
42
43bool parse_int_str (const char *str, int &);
44
45/*------------------------------------------------------------------------*/
46
47inline bool is_power_of_two (unsigned n) { return n && !(n & (n - 1)); }
48
49inline bool contained (int64_t c, int64_t l, int64_t u) {
50 return l <= c && c <= u;
51}
52
53inline bool aligned (size_t n, size_t alignment) {
54 CADICAL_assert (is_power_of_two (alignment));
55 const size_t mask = alignment - 1;
56 return !(n & mask);
57}
58
59inline size_t align (size_t n, size_t alignment) {
60 size_t res = n;
61 CADICAL_assert (is_power_of_two (alignment));
62 const size_t mask = alignment - 1;
63 if (res & mask)
64 res = (res | mask) + 1;
65 CADICAL_assert (aligned (res, alignment));
66 return res;
67}
68
69/*------------------------------------------------------------------------*/
70
71inline bool parity (unsigned a) {
72 CADICAL_assert (sizeof a == 4);
73 unsigned tmp = a;
74 tmp ^= (tmp >> 16);
75 tmp ^= (tmp >> 8);
76 tmp ^= (tmp >> 4);
77 tmp ^= (tmp >> 2);
78 tmp ^= (tmp >> 1);
79 return tmp & 1;
80}
81
82/*------------------------------------------------------------------------*/
83
84// The standard 'Effective STL' way (though not guaranteed) to clear a
85// vector and reduce its capacity to zero, thus deallocating all its
86// internal memory. This is quite important for keeping the actual
87// allocated size of watched and occurrence lists small particularly during
88// bounded variable elimination where many clauses are added and removed.
89
90template <class T> void erase_vector (std::vector<T> &v) {
91 if (v.capacity ()) {
92 std::vector<T> ().swap (v);
93 }
94 CADICAL_assert (!v.capacity ()); // not guaranteed though
95}
96
97// The standard 'Effective STL' way (though not guaranteed) to shrink the
98// capacity of a vector to its size thus kind of releasing all the internal
99// excess memory not needed at the moment any more.
100
101template <class T> void shrink_vector (std::vector<T> &v) {
102 if (v.capacity () > v.size ()) {
103 std::vector<T> (v).swap (v);
104 }
105 CADICAL_assert (v.capacity () == v.size ()); // not guaranteed though
106}
107
108template <class T>
109static void enlarge_init (vector<T> &v, size_t N, const T &i) {
110 if (v.size () < N)
111 v.resize (N, i);
112}
113
114template <class T> static void enlarge_only (vector<T> &v, size_t N) {
115 if (v.size () < N)
116 v.resize (N, T ());
117}
118
119template <class T> static void enlarge_zero (vector<T> &v, size_t N) {
120 enlarge_init (v, N, (const T &) 0);
121}
122
123// Clean-up class for bad_alloc error safety.
124
125template <typename T> struct DeferDeleteArray {
127 DeferDeleteArray (T *t) : data (t) {}
128 ~DeferDeleteArray () { delete[] data; }
129 void release () { data = nullptr; }
130 void free () {
131 delete[] data;
132 data = nullptr;
133 }
134};
135
136template <typename T> struct DeferDeletePtr {
138 DeferDeletePtr (T *t) : data (t) {}
139 ~DeferDeletePtr () { delete data; }
140 void release () { data = nullptr; }
141 void free () {
142 delete data;
143 data = nullptr;
144 }
145};
146
147/*------------------------------------------------------------------------*/
148
149template <class T> inline void clear_n (T *base, size_t n) {
150 memset (base, 0, sizeof (T) * n);
151}
152
153/*------------------------------------------------------------------------*/
154
155// These are options both to 'cadical' and 'mobical'. After wasting some
156// on not remembering the spelling (British vs American), nor singular vs
157// plural and then wanted to use '--color=false', and '--colours=0' too, I
158// just factored this out into these two utility functions.
159
160bool is_color_option (const char *arg); // --color, --colour, ...
161bool is_no_color_option (const char *arg); // --no-color, --no-colour, ...
162
163/*------------------------------------------------------------------------*/
164
165uint64_t hash_string (const char *str);
166
167/*------------------------------------------------------------------------*/
168
169} // namespace CaDiCaL
170
172
173#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
#define CADICAL_assert(ignore)
Definition global.h:14
unsigned bign(int lit)
Definition util.hpp:23
bool has_suffix(const char *str, const char *suffix)
int sign(int lit)
Definition util.hpp:22
bool is_color_option(const char *arg)
bool is_power_of_two(unsigned n)
Definition util.hpp:47
double relative(double a, double b)
Definition util.hpp:20
void shrink_vector(std::vector< T > &v)
Definition util.hpp:101
bool aligned(size_t n, size_t alignment)
Definition util.hpp:53
bool parse_int_str(const char *val_str, int &val)
bool has_prefix(const char *str, const char *prefix)
size_t align(size_t n, size_t alignment)
Definition util.hpp:59
void clear_n(T *base, size_t n)
Definition util.hpp:149
bool contained(int64_t c, int64_t l, int64_t u)
Definition util.hpp:49
bool parity(unsigned a)
Definition util.hpp:71
uint64_t hash_string(const char *str)
void erase_vector(std::vector< T > &v)
Definition util.hpp:90
bool is_no_color_option(const char *arg)
double percent(double a, double b)
Definition util.hpp:21
int lit
Definition satVec.h:130
unsigned size
Definition vector.h:35
char * memset()
struct vector vector
Definition vector.h:24