7 #if !defined(MQTT_VALUE_ALLOCATOR_HPP)
8 #define MQTT_VALUE_ALLOCATOR_HPP
15 #include <boost/multi_index_container.hpp>
16 #include <boost/multi_index/ordered_index.hpp>
17 #include <boost/multi_index/identity.hpp>
23 namespace mi = boost::multi_index;
29 class value_interval {
31 explicit value_interval(value_type v) : low_{v}, high_{v} {}
32 value_interval(value_type l, value_type h) : low_{l}, high_{h} {}
56 friend bool operator<(value_interval
const& lhs, value_interval
const& rhs) {
58 (lhs.low_ < rhs.low_) &&
59 (lhs.high_ < rhs.low_);
61 value_type low()
const {
64 value_type high()
const {
68 friend std::ostream&
operator<<(std::ostream& o, value_interval
const v) {
69 o <<
'[' << v.low() <<
',' << v.high() <<
']';
86 :lowest_{lowest}, highest_{highest} {
88 BOOST_ASSERT(std::numeric_limits<value_type>::min() <= lowest);
89 BOOST_ASSERT(highest <= std::numeric_limits<value_type>::max());
91 pool_.emplace(lowest_, highest_);
99 if (pool_.empty())
return nullopt;
102 auto it = pool_.begin();
103 value_type value = it->low();
105 if (it->low() + 1 <= it->high()) {
110 BOOST_ASSERT(it->low() < highest_);
111 e = value_interval{value_type(it->low() + 1) , it->high()};
127 if (pool_.empty())
return nullopt;
130 auto it = pool_.begin();
139 BOOST_ASSERT(lowest_ <= value && value <= highest_);
140 auto itr = pool_.upper_bound(value_interval{value});
141 if (itr == pool_.end()) {
145 if (itr == pool_.begin()) {
147 pool_.emplace(value);
153 if (itl->high() + 1 == value) {
158 e = value_interval{itl->low(), value};
164 pool_.emplace(value);
167 else if (itr == pool_.begin()) {
171 if (value + 1 == itr->low()) {
176 e = value_interval{value, itr->high()};
182 pool_.emplace(value);
191 if (itl->high() + 1 == value) {
192 if (value + 1 == itr->low()) {
194 auto right = itr->high();
199 e = value_interval{itl->low(), right};
209 e = value_interval{itl->low(), value};
215 if (value + 1 == itr->low()) {
220 e = value_interval{value, itr->high()};
226 pool_.emplace(value);
237 bool use(value_type value) {
238 auto it = pool_.find(value_interval{value});
239 if (it == pool_.end())
return false;
241 value_interval iv = *it;
243 if (iv.low() < value) {
244 pool_.emplace(iv.low(), value - 1);
246 if (value + 1 <= iv.high()) {
247 pool_.emplace(value + 1, iv.high());
257 pool_.emplace(lowest_, highest_);
264 std::ostream&
dump(std::ostream& o) {
265 for (
auto const& e : pool_) {
271 using mi_value_interval = mi::multi_index_container<
275 mi::identity<value_interval>
279 mi_value_interval pool_;
Definition: value_allocator.hpp:26
std::size_t interval_count() const
Definition: value_allocator.hpp:260
void deallocate(value_type value)
Dellocate one value.
Definition: value_allocator.hpp:138
value_allocator(value_type lowest, value_type highest)
Create value_allocator The allocator has [lowest, highest] values.
Definition: value_allocator.hpp:85
optional< value_type > allocate()
Allocate one value.
Definition: value_allocator.hpp:98
optional< value_type > first_vacant() const
Get the first vacant value.
Definition: value_allocator.hpp:126
void clear()
Clear all allocated or used values.
Definition: value_allocator.hpp:255
bool use(value_type value)
Declare the value as used.
Definition: value_allocator.hpp:237
std::ostream & dump(std::ostream &o)
Definition: value_allocator.hpp:264
std::ostream & operator<<(std::ostream &os, connect_return_code val)
Definition: connect_return_code.hpp:43
bool operator<(share_name_topic_filter const &lhs, share_name_topic_filter const &rhs)
Definition: shared_subscriptions.hpp:36