carl  24.04
Computer ARithmetic Library
IDPool.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../config.h"
4 #include "../datastructures/Bitset.h"
5 
6 #include <iostream>
7 #include <mutex>
8 
9 namespace carl {
10 
11  class IDPool {
12  private:
14  std::size_t mLargestID = 0;
15 #ifdef THREAD_SAFE
16  mutable std::mutex mMutex;
17 #define IDPOOL_LOCK std::lock_guard<std::mutex> lock(mMutex)
18 #else
19 #define IDPOOL_LOCK
20 #endif
21  public:
22  std::size_t size() const {
24  return mFreeIDs.size();
25  }
26  std::size_t largestID() const {
28  return mLargestID;
29  }
30  std::size_t get() {
32  std::size_t pos = mFreeIDs.find_first();
33  if (pos == Bitset::npos) {
34  pos = mFreeIDs.size();
36  }
37  mFreeIDs.reset(pos);
38  if (pos > mLargestID) mLargestID = pos;
39  return pos;
40  }
41  void free(std::size_t id) {
43  assert(id < mFreeIDs.size());
44  mFreeIDs.set(id);
45  }
46  void clear() {
48  mFreeIDs = Bitset(true);
49  }
50  friend std::ostream& operator<<(std::ostream& os, const IDPool& p) {
51  return os << "Free: " << p.mFreeIDs;
52  }
53 
54 #undef IDPOOL_LOCK
55  };
56 
57 }
#define IDPOOL_LOCK
Definition: IDPool.h:19
carl is the main namespace for the library.
This class is a simple wrapper around boost::dynamic_bitset.
Definition: Bitset.h:20
static constexpr auto bits_per_block
Number of bits in each storage block.
Definition: Bitset.h:29
auto num_blocks() const
Retrieves the number of blocks used to store mData.
Definition: Bitset.h:188
std::size_t find_first() const
Retrieves the index of the first bit that is set to true.
Definition: Bitset.h:200
auto resize(std::size_t num_bits, bool value) const
Resize the Bitset to hold at least num_bits bits. New bits are set to the given value.
Definition: Bitset.h:111
Bitset & reset(std::size_t n)
Resets a bit to false.
Definition: Bitset.h:154
static constexpr auto npos
Sentinel element for iteration.
Definition: Bitset.h:27
Bitset & set(std::size_t n, bool value=true)
Sets the given bit to a value, true by default.
Definition: Bitset.h:140
auto size() const
Retrieves the size of mData.
Definition: Bitset.h:184
std::size_t get()
Definition: IDPool.h:30
void clear()
Definition: IDPool.h:46
friend std::ostream & operator<<(std::ostream &os, const IDPool &p)
Definition: IDPool.h:50
std::size_t largestID() const
Definition: IDPool.h:26
std::size_t size() const
Definition: IDPool.h:22
std::size_t mLargestID
Definition: IDPool.h:14
void free(std::size_t id)
Definition: IDPool.h:41
Bitset mFreeIDs
Definition: IDPool.h:13