carl  24.04
Computer ARithmetic Library
Pool.h
Go to the documentation of this file.
1 /**
2  * @file Pool.h
3  *
4  * @author Andreas Krueger <andreas.krueger@rwth-aachen.de>
5  * @author Florian Corzilius<corzilius@cs.rwth-aachen.de>
6  */
7 
8 #pragma once
9 
12 
13 #include <mutex>
14 #include <iostream>
15 
16 namespace carl
17 {
18 
19  template<typename Element>
20  class Pool
21  {
22  // friend Singleton<Pool>;
23 
24  using ElementPtr = Element*;
25  using ConstElementPtr = const Element*;
26 
27  private:
28 
29  // Members:
30  /// id allocator
31  unsigned mIdAllocator = 1;
32  /// The formula pool.
34  /// Mutex to avoid multiple access to the pool
35  mutable std::mutex mMutexPool;
36 
37 #define POOL_LOCK_GUARD std::lock_guard<std::mutex> lock( mMutexPool );
38 #define POOL_LOCK mMutexPool.lock();
39 #define POOL_UNLOCK mMutexPool.unlock();
40 
41  protected:
42 
43  /**
44  * Constructor of the pool.
45  * @param _capacity Expected necessary capacity of the pool.
46  */
47  explicit Pool(unsigned _capacity = 10000) {
48  mPool.reserve(_capacity);
49  }
50 
52  {
53  while(!mPool.empty()) {
54  ConstElementPtr element = *mPool.begin();
55  mPool.erase(mPool.begin());
56  delete element;
57  }
58  }
59 
60  /**
61  * Assigns a unique id to the generated element.
62  * Note that this method serves as a callback for subclasses.
63  * The actual assignment of the id is done there.
64  * @param _element The element for which to add the id.
65  * @param _id A unique id.
66  */
67  virtual void assignId(ElementPtr /* _element */, std::size_t /* _id */)
68  { }
69 
70  public:
71 
72  void print() const
73  {
74  std::cout << "Pool contains:" << std::endl;
75  for(const auto& ele : mPool) {
76  std::cout << "- " << *ele << std::endl;
77  }
78  std::cout << std::endl;
79  }
80 
81  /**
82  * Inserts the given element into the pool, if it does not yet occur in there.
83  * @param _element The element to add to the pool.
84  * @param _assertFreshness When true, an assertion fails if the element is not fresh
85  * (i.e., if it already occurs in the pool).
86  * @return The position of the given element in the pool and true, if it did not yet occur in the pool;
87  * The position of the equivalent element in the pool and false, otherwise.
88  */
89  std::pair<typename FastPointerSet<Element>::iterator, bool> insert(ElementPtr _element, bool _assertFreshness = false)
90  {
92  auto iterBoolPair = mPool.insert(_element);
93  assert(iterBoolPair.second || !_assertFreshness);
94 
95  if(iterBoolPair.second) { // Element has just been inserted
96  // Assign a new id
97  assignId(_element, mIdAllocator++); // id should be set here to avoid conflicts when multi-threading
98  } else {
99  // The argument can be deleted, return the already existent instance
100  delete _element;
101  }
102 
103  return iterBoolPair;
104  }
105 
106  /**
107  * Adds the given element to the pool, if it does not yet occur in there.
108  * Note, that this method uses the allocator which is locked before calling.
109  * @param _element The element to add to the pool.
110  * @return The given element, if it did not yet occur in the pool;
111  * The equivalent element already occurring in the pool, otherwise.
112  */
114  {
115  return *(insert(_element).first);
116  }
117  };
118 } // namespace carl
carl is the main namespace for the library.
std::unordered_set< const T *, pointerHash< T >, pointerEqual< T > > FastPointerSet
unsigned mIdAllocator
id allocator
Definition: Pool.h:31
std::mutex mMutexPool
Mutex to avoid multiple access to the pool.
Definition: Pool.h:35
virtual void assignId(ElementPtr, std::size_t)
Assigns a unique id to the generated element.
Definition: Pool.h:67
Element * ElementPtr
Definition: Pool.h:24
std::pair< typename FastPointerSet< Element >::iterator, bool > insert(ElementPtr _element, bool _assertFreshness=false)
Inserts the given element into the pool, if it does not yet occur in there.
Definition: Pool.h:89
Pool(unsigned _capacity=10000)
Constructor of the pool.
Definition: Pool.h:47
FastPointerSet< Element > mPool
The formula pool.
Definition: Pool.h:33
const Element * ConstElementPtr
Definition: Pool.h:25
void print() const
Definition: Pool.h:72
~Pool()
Definition: Pool.h:51
ConstElementPtr add(ElementPtr _element)
Adds the given element to the pool, if it does not yet occur in there.
Definition: Pool.h:113
#define POOL_LOCK_GUARD
Definition: Pool.h:37