Mungojerrie  1.1
Mungojerrie
Util.hh
Go to the documentation of this file.
1 #ifndef UTIL_H_
2 #define UTIL_H_
3 
46 #include <vector>
47 #include <set>
48 #include <string>
49 #include <iostream>
50 #include <sstream>
51 #include <random>
52 #include <boost/random.hpp>
53 #include <boost/functional/hash.hpp>
54 #include <chrono>
55 #include <functional>
56 #include "cuddObj.hh"
57 #include "State.hh"
58 
62 namespace Util {
64  void replace(std::string & str, std::string const & from,
65  std::string const & to);
67  void replaceAll(std::string & str, std::string const & from,
68  std::string const & to);
70  char ** cStringArrayAlloc(std::vector<std::string> const & sv);
72  void cStringArrayFree(char ** sa, size_t n);
73 
75  template<typename T> std::string to_string(T const & num)
76  {
77  std::ostringstream oss;
78  oss << num;
79  return oss.str();
80  }
81 
89  class BddCompare {
90  public:
91  bool operator()(BDD const & bdd1, BDD const & bdd2) const
92  {
93  return bdd1.getNode() < bdd2.getNode();
94  }
95  };
96 
100  template<typename T>
102  public:
103  using pwbdd = std::pair<T,BDD>;
104  bool operator()(pwbdd const & p1, pwbdd const & p2) const
105  {
106  return std::tuple<T,DdNode *>(p1.first, p1.second.getNode()) <
107  std::tuple<T,DdNode *>(p2.first, p2.second.getNode());
108  }
109  };
110 
114  template<typename T1, typename T2>
115  class PairHash {
116  public:
117  size_t operator()(std::pair<T1,T2> const & p) const
118  {
119  size_t seed = std::hash<T1>()(p.first);
120  boost::hash_combine(seed, std::hash<T2>()(p.second));
121  return seed;
122  }
123  };
124 
126  template<typename T, typename Comp>
127  std::vector< std::set<T, Comp> > mapToPartition(
128  std::map<T,unsigned,Comp> const & m)
129  {
130  if (m.size() == 0) {
131  return std::vector< std::set<T, Comp> >{};
132  }
133  unsigned maxindex = 0;
134  for (auto const & e : m) {
135  if (e.second > maxindex)
136  maxindex = e.second;
137  }
138  std::vector< std::set<T, Comp> > partition(maxindex+1);
139  for (auto const & e : m) {
140  partition[e.second].insert(e.first);
141  }
142  return partition;
143  }
144 
145  using RandomEngine = std::mt19937;
146 
148  RandomEngine & urng();
149 
151  void seed_urng();
152 
154  void seed_urng(unsigned seed);
155 
157  unsigned int get_urng_seed();
158 
160  template <typename Iterator>
161  typename std::iterator_traits<Iterator>::value_type
162  sample_discrete_distribution(Iterator first, Iterator last)
163  {
164  using Distribution = boost::random::discrete_distribution<>;
165  using Parameter = Distribution::param_type;
166  thread_local static Distribution dist{};
167  return dist(urng(), Parameter{first, last});
168  }
169 
171  int sample_discrete_distribution(std::vector<double> const & weights);
172 
174  template <typename IntType>
175  IntType sample_uniform_distribution(IntType low, IntType hi)
176  {
177  using Distribution = boost::random::uniform_int_distribution<IntType>;
178  using Parameter = typename Distribution::param_type;
179  thread_local static Distribution dist{};
180  return dist(urng(), Parameter{low, hi});
181  }
182 
184  std::chrono::time_point<std::chrono::system_clock> stopwatch();
185 
187  void printElapsedTag(std::ostream & os = std::cout);
188 
190  double getElapsedTime();
191 
193  char const * to_decimal(int number);
194 
196  bool is_prefix(std::string const & str, std::string const & pre);
197 
198 }
199 
200 #endif
Util::to_decimal
const char * to_decimal(int number)
Converts an int to an array of characters in base 10.
Definition: Util.cc:164
Util::PairWithBddCompare
Class that defines a comparison operator for pairs with BDDs.
Definition: Util.hh:101
Util::PairHash
Class that defines a hash function for pairs.
Definition: Util.hh:115
Util::BddCompare
Class that defines a comparison operator for the BDD class.
Definition: Util.hh:89
Util::sample_uniform_distribution
IntType sample_uniform_distribution(IntType low, IntType hi)
Returns a sample from a uniform integer distribution.
Definition: Util.hh:175
Util::urng
RandomEngine & urng()
Returns reference to uniform random number generator.
Definition: Util.cc:96
State.hh
Basic type definitions.
Util
Namespace of utilities.
Definition: Util.cc:50
Util::seed_urng
void seed_urng()
Seeds the uniform random number generator.
Definition: Util.cc:102
Util::to_string
std::string to_string(T const &num)
Replacement for std::to_string since cygwin does not have it.
Definition: Util.hh:75
Util::mapToPartition
std::vector< std::set< T, Comp > > mapToPartition(std::map< T, unsigned, Comp > const &m)
Converts a partition map to a vector of sets.
Definition: Util.hh:127
Util::cStringArrayFree
void cStringArrayFree(char **sa, size_t n)
Dispose of an array of C strings.
Definition: Util.cc:89
Util::stopwatch
chrono::time_point< chrono::system_clock > stopwatch()
Records a reference time.
Definition: Util.cc:127
Util::getElapsedTime
double getElapsedTime()
Returns the elapsed time in seconds.
Definition: Util.cc:140
Util::get_urng_seed
unsigned int get_urng_seed()
Return the seed for the uniform random number generator.
Definition: Util.cc:116