blob: d5e7014b0f21cf1e55b37446a1974ece97401d82 [file] [edit]
#ifndef _util_hpp_INCLUDED
#define _util_hpp_INCLUDED
#include <cassert>
#include <cstdint>
#include <unordered_map>
#include <vector>
#include "hashmap.hpp"
namespace CaDiCaL {
using namespace std;
// Common simple utility functions independent from 'Internal'.
/*------------------------------------------------------------------------*/
inline double relative (double a, double b) { return b ? a / b : 0; }
inline double percent (double a, double b) { return relative (100 * a, b); }
inline signed char sign (int lit) { return (lit > 0) - (lit < 0); }
inline unsigned bign (int lit) { return 1 + (lit < 0); }
/*------------------------------------------------------------------------*/
bool has_suffix (const char *str, const char *suffix);
bool has_prefix (const char *str, const char *prefix);
/*------------------------------------------------------------------------*/
// Parse integer string in the form of
//
// true
// false
// [-]<mantissa>[e<exponent>]
//
// and in the latter case '<val>' has to be within [-INT_MAX,INT_MAX].
//
// The function returns true if parsing is successful and then also sets
// the second argument to the parsed value.
bool parse_int_str (const char *str, int &, bool cut_to_fit = false);
bool parse_int64_str (const char *str, int64_t &);
/*------------------------------------------------------------------------*/
inline bool is_power_of_two (size_t n) {
return n > 0 && n && !(n & (n - 1));
}
inline bool contained (int64_t c, int64_t l, int64_t u) {
return l <= c && c <= u;
}
inline bool aligned (size_t n, size_t alignment) {
assert (is_power_of_two (alignment));
const size_t mask = alignment - 1;
return !(n & mask);
}
inline size_t align (size_t n, size_t alignment) {
size_t res = n;
assert (is_power_of_two (alignment));
const size_t mask = alignment - 1;
if (res & mask)
res = (res | mask) + 1;
assert (aligned (res, alignment));
return res;
}
/*------------------------------------------------------------------------*/
inline bool parity (unsigned a) {
assert (sizeof a == 4);
unsigned tmp = a;
tmp ^= (tmp >> 16);
tmp ^= (tmp >> 8);
tmp ^= (tmp >> 4);
tmp ^= (tmp >> 2);
tmp ^= (tmp >> 1);
return tmp & 1;
}
/*------------------------------------------------------------------------*/
// The standard 'Effective STL' way (though not guaranteed) to clear a
// vector and reduce its capacity to zero, thus deallocating all its
// internal memory. This is quite important for keeping the actual
// allocated size of watched and occurrence lists small particularly during
// bounded variable elimination where many clauses are added and removed.
template <class T> void erase_vector (std::vector<T> &v) {
if (v.capacity ()) {
std::vector<T> ().swap (v);
}
assert (!v.capacity ()); // not guaranteed though
}
// The standard 'Effective STL' way (though not guaranteed) to shrink the
// capacity of a vector to its size thus kind of hoping to release all the
// internal excess memory not needed at the moment any more. We had earlier
// the classical solution using 'swap' which might not be the best way.
template <class T> void shrink_vector (std::vector<T> &v) {
v.shrink_to_fit ();
}
template <class T>
static void enlarge_init (vector<T> &v, size_t N, const T &i) {
if (v.size () < N)
v.resize (N, i);
}
template <class T> static void enlarge_only (vector<T> &v, size_t N) {
if (v.size () < N)
v.resize (N, T ());
}
template <class T> static void enlarge_zero (vector<T> &v, size_t N) {
enlarge_init (v, N, (const T &) 0);
}
// double the capacity until it fits. This is different from reserve
// that will allocate exactly the size requested, meaning that the
// amortized complexity is lost.
template <class T> static void reserve_at_least (vector<T> &v, size_t N) {
if (N < v.capacity ())
return;
size_t new_size = v.size ();
if (!new_size)
new_size = N;
while (new_size < N)
new_size *= 2;
v.reserve (new_size);
}
template <class K, class E>
static K find_or_default (const std::unordered_map<K, E> &map, K key,
E default_el) {
auto it = map.find (key);
if (it == map.end ())
return default_el;
return it->second;
}
template <class Key, class Element, class FirstHash, class SecondHash,
class Tumb, class KeyEqual>
static Key find_or_default (
const hashmap<Key, Element, FirstHash, SecondHash, Tumb, KeyEqual> &map,
Key key, Element default_el) {
auto it = map.find (key);
if (it.first == Tumb () ().first)
return default_el;
return it.second;
}
// Clean-up class for bad_alloc error safety.
template <typename T> struct DeferDeleteArray {
T *data;
DeferDeleteArray (T *t) : data (t) {}
~DeferDeleteArray () { delete[] data; }
void release () { data = nullptr; }
void free () {
delete[] data;
data = nullptr;
}
};
template <typename T> struct DeferCloseFile {
T *data;
DeferCloseFile (T *t) : data (t) {}
~DeferCloseFile () {
if (data)
fclose (data);
}
void release () { data = nullptr; }
void free () {
if (data)
fclose (data);
data = nullptr;
}
};
template <typename T> struct DeferDeletePtr {
T *data;
DeferDeletePtr (T *t) : data (t) {}
~DeferDeletePtr () { delete data; }
void release () { data = nullptr; }
void free () {
delete data;
data = nullptr;
}
};
template <typename T, typename F = void (*) (T *&)> struct DeferDeleteFunc {
T *data;
F func;
DeferDeleteFunc (T *t, F f) : data (t), func (f) {}
~DeferDeleteFunc () { (*func) (data); }
void release () { data = nullptr; }
void free () {
(*func) (data);
data = nullptr;
}
};
/*------------------------------------------------------------------------*/
template <class T> inline void clear_n (T *base, size_t n) {
memset (base, 0, sizeof (T) * n);
}
/*------------------------------------------------------------------------*/
// These are options both to 'cadical' and 'mobical'. After wasting some
// on not remembering the spelling (British vs American), nor singular vs
// plural and then wanted to use '--color=false', and '--colours=0' too, I
// just factored this out into these two utility functions.
bool is_color_option (const char *arg); // --color, --colour, ...
bool is_no_color_option (const char *arg); // --no-color, --no-colour, ...
/*------------------------------------------------------------------------*/
uint64_t hash_string (const char *str);
/*------------------------------------------------------------------------*/
static inline int64_t cache_lines (size_t bytes) {
return (bytes + 127) / 128;
}
static inline int64_t cache_lines (size_t n, size_t bytes) {
return cache_lines (n * bytes);
}
template <class C>
static inline int64_t cache_lines (const std::vector<C> &c) {
return cache_lines (c.size (), sizeof (C));
}
/*------------------------------------------------------------------------*/
} // namespace CaDiCaL
#endif