carl  24.04
Computer ARithmetic Library
MonomialPool.h
Go to the documentation of this file.
1 /**
2  * @file MonomialPool.h
3  * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
4  */
5 
6 #pragma once
7 
8 #include <carl-common/config.h>
12 #include "Monomial.h"
13 
14 #include <boost/intrusive/unordered_set.hpp>
15 #include <memory>
16 
17 namespace carl {
18 
19 inline std::size_t hash_value(const carl::Monomial& monomial) {
20  return monomial.hash();
21 }
22 
23 class MonomialPool : public Singleton<MonomialPool> {
24  friend class Singleton<MonomialPool>;
25  friend std::ostream& operator<<(std::ostream& os, const MonomialPool& mp);
26 
27  struct content_equal {
28  bool operator()(const Monomial::Content& content, const Monomial& monomial) const {
29  return content == monomial.mExponents;
30  }
31 
32  bool operator()(const Monomial& monomial, const Monomial::Content& content) const {
33  return content == monomial.mExponents;
34  }
35  };
36 
37  struct content_hash {
38  std::size_t operator()(const Monomial::Content& content) const {
40  }
41  };
42 
43 private:
44  // Members:
45  /// id allocator
47  //size_t mIdAllocator;
49  using underlying_set = boost::intrusive::unordered_set<Monomial>;
50  std::unique_ptr<underlying_set::bucket_type[]> mPoolBuckets;
51  /// The pool.
53  /// Mutex to avoid multiple access to the pool
54  mutable std::recursive_mutex mMutex;
55 
56  #ifdef THREAD_SAFE
57  #define MONOMIAL_POOL_LOCK_GUARD std::lock_guard<std::recursive_mutex> lock(mMutex);
58  #define MONOMIAL_POOL_LOCK mMutex.lock();
59  #define MONOMIAL_POOL_UNLOCK mMutex.unlock();
60  #else
61  #define MONOMIAL_POOL_LOCK_GUARD
62  #define MONOMIAL_POOL_LOCK
63  #define MONOMIAL_POOL_UNLOCK
64  #endif
65 
66 protected:
67  /**
68  * Constructor of the pool.
69  * @param _capacity Expected necessary capacity of the pool.
70  */
71  explicit MonomialPool(std::size_t _capacity = 1000)
72  : mPoolBuckets(new underlying_set::bucket_type[mRehashPolicy.numBucketsFor(_capacity)]),
73  mPool(underlying_set::bucket_traits(mPoolBuckets.get(), mRehashPolicy.numBucketsFor(_capacity))) {
74  mIDs.get();
75  assert(mIDs.largestID() == 0);
77  CARL_LOG_DEBUG("carl.pool", "Monomialpool constructed");
78  }
79 
81  // for (const auto& m : mPool) m.mId = 0; // hacky fix for singletons not being destroyed in proper order
82  // CARL_LOG_DEBUG("carl.pool", "Monomialpool destructed");
83  }
84 
85  Monomial::Arg add(Monomial::Content&& c, exponent totalDegree = 0);
86 
87  void check_rehash() {
88  auto rehash = mRehashPolicy.needRehash(mPool.bucket_count(), mPool.size());
89  if (rehash.first) {
90  auto new_buckets = new underlying_set::bucket_type[rehash.second];
91  mPool.rehash(underlying_set::bucket_traits(new_buckets, rehash.second));
92  mPoolBuckets.reset(new_buckets);
93  }
94  }
95 
96 public:
97  /**
98  * Creates a monomial from a variable and an exponent.
99  */
101 
102  /**
103  * Creates a monomial from a variable and an exponent.
104  */
105  template<typename Number>
106  Monomial::Arg create(Variable _var, Number&& _exp) {
107  return create(_var, static_cast<exponent>(std::forward<Number>(_exp)));
108  }
109 
110  /**
111  * Creates a monomial from a list of variables and their exponents.
112  *
113  * Note that the input is required to be sorted.
114  *
115  * @param _exponents Sorted list of variables and exponents.
116  * @param _totalDegree Total degree.
117  */
118  Monomial::Arg create(std::vector<std::pair<Variable, exponent>>&& _exponents, exponent _totalDegree);
119 
120  /**
121  * Creates a Monomial.
122  *
123  * @param _exponents Possibly unsorted list of variables and epxonents.
124  */
125  Monomial::Arg create(const std::initializer_list<std::pair<Variable, exponent>>& _exponents);
126 
127  /**
128  * Creates a monomial from a list of variables and their exponents.
129  *
130  * Note that the input is required to be sorted.
131  *
132  * @param Sorted list of variables and exponents.
133  */
134  Monomial::Arg create(std::vector<std::pair<Variable, exponent>>&& _exponents);
135 
136  void free(const Monomial* m) {
137  if (m == nullptr) return;
138  if (m->id() == 0) return;
139  CARL_LOG_TRACE("carl.core.monomial", "Freeing " << m);
141  auto it = mPool.find(*m);
142  if (it != mPool.end()) {
143  CARL_LOG_TRACE("carl.core.monomial", "Found " << m->id());
144  mIDs.free(m->id());
145  mPool.erase(it);
146  } else {
147  CARL_LOG_TRACE("carl.core.monomial", "Not found in pool.");
148  }
149  }
150 
151  std::size_t size() const {
152  return mPool.size();
153  }
154  std::size_t largestID() const {
155  return mIDs.largestID();
156  }
157 };
158 
159 inline std::ostream& operator<<(std::ostream& os, const MonomialPool& mp) {
160  os << "MonomialPool of size " << mp.size() << std::endl;
161  for (const auto& entry : mp.mPool) {
162  os << "\t" << entry << std::endl;
163  }
164  return os;
165 }
166 
167 template<typename... T>
168 inline Monomial::Arg createMonomial(T&&... t) {
169  return MonomialPool::getInstance().create(std::forward<T>(t)...);
170 }
171 
172 } // namespace carl
#define MONOMIAL_POOL_LOCK_GUARD
Definition: MonomialPool.h:61
#define CARL_LOG_TRACE(channel, msg)
Definition: carl-logging.h:44
#define CARL_LOG_DEBUG(channel, msg)
Definition: carl-logging.h:43
carl is the main namespace for the library.
Coeff content(const UnivariatePolynomial< Coeff > &p)
The content of a polynomial is the gcd of the coefficients of the normal part of a polynomial.
Definition: Content.h:22
Monomial::Arg createMonomial(T &&... t)
Definition: MonomialPool.h:168
std::size_t exponent
Type of an exponent.
Definition: Monomial.h:29
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
std::size_t hash_value(const carl::Monomial &monomial)
Definition: MonomialPool.h:19
auto & get(const std::string &name)
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
The general-purpose monomials.
Definition: Monomial.h:59
std::shared_ptr< const Monomial > Arg
Definition: Monomial.h:62
Content mExponents
A vector of variable exponent pairs (v_i^e_i) with nonzero exponents.
Definition: Monomial.h:74
std::size_t hash() const
Returns the hash of this monomial.
Definition: Monomial.h:169
std::vector< std::pair< Variable, std::size_t > > Content
Definition: Monomial.h:63
std::size_t id() const
Return the id of this monomial.
Definition: Monomial.h:177
static std::size_t hashContent(const Monomial::Content &c)
Calculate the hash of a monomial based on its content.
Definition: Monomial.h:466
Monomial::Arg create(Variable _var, Number &&_exp)
Creates a monomial from a variable and an exponent.
Definition: MonomialPool.h:106
boost::intrusive::unordered_set< Monomial > underlying_set
Definition: MonomialPool.h:49
std::unique_ptr< underlying_set::bucket_type[]> mPoolBuckets
Definition: MonomialPool.h:50
underlying_set mPool
The pool.
Definition: MonomialPool.h:52
std::size_t largestID() const
Definition: MonomialPool.h:154
friend std::ostream & operator<<(std::ostream &os, const MonomialPool &mp)
Definition: MonomialPool.h:159
Monomial::Arg create(Variable _var, exponent _exp)
Creates a monomial from a variable and an exponent.
std::size_t size() const
Definition: MonomialPool.h:151
void free(const Monomial *m)
Definition: MonomialPool.h:136
std::recursive_mutex mMutex
Mutex to avoid multiple access to the pool.
Definition: MonomialPool.h:54
MonomialPool(std::size_t _capacity=1000)
Constructor of the pool.
Definition: MonomialPool.h:71
IDPool mIDs
id allocator
Definition: MonomialPool.h:46
pool::RehashPolicy mRehashPolicy
Definition: MonomialPool.h:48
Monomial::Arg add(Monomial::Content &&c, exponent totalDegree=0)
bool operator()(const Monomial &monomial, const Monomial::Content &content) const
Definition: MonomialPool.h:32
bool operator()(const Monomial::Content &content, const Monomial &monomial) const
Definition: MonomialPool.h:28
std::size_t operator()(const Monomial::Content &content) const
Definition: MonomialPool.h:38
std::size_t get()
Definition: IDPool.h:30
std::size_t largestID() const
Definition: IDPool.h:26
void free(std::size_t id)
Definition: IDPool.h:41
Mimics stdlibs default rehash policy for hashtables.
Definition: PoolHelper.h:15
std::pair< bool, std::size_t > needRehash(std::size_t numBuckets, std::size_t numElements) const
Definition: PoolHelper.cpp:14
Base class that implements a singleton.
Definition: Singleton.h:24
static VariablePool & getInstance()
Returns the single instance of this class by reference.
Definition: Singleton.h:45