value_allocator.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright Takatoshi Kondo 2020
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(MQTT_VALUE_ALLOCATOR_HPP)
  7. #define MQTT_VALUE_ALLOCATOR_HPP
  8. #include <mqtt/config.hpp> // should be top to configure variant limit
  9. #include <ostream>
  10. #include <limits>
  11. #include <boost/multi_index_container.hpp>
  12. #include <boost/multi_index/ordered_index.hpp>
  13. #include <boost/multi_index/identity.hpp>
  14. #include <mqtt/optional.hpp>
  15. namespace MQTT_NS {
  16. namespace mi = boost::multi_index;
  17. template <typename T>
  18. class value_allocator {
  19. using value_type = T;
  20. class value_interval {
  21. public:
  22. explicit value_interval(value_type v) : low_{v}, high_{v} {}
  23. value_interval(value_type l, value_type h) : low_{l}, high_{h} {}
  24. // true
  25. // | lhs | | rhs |
  26. //
  27. // false
  28. // | lhs |
  29. // | rhs |
  30. //
  31. // false
  32. // | lhs |
  33. // | rhs |
  34. //
  35. // false
  36. // | lhs |
  37. // | rhs |
  38. //
  39. // false
  40. // | lhs |
  41. // | rhs |
  42. //
  43. // false
  44. // | rhs | | lhs |
  45. //
  46. friend bool operator<(value_interval const& lhs, value_interval const& rhs) {
  47. return
  48. (lhs.low_ < rhs.low_) &&
  49. (lhs.high_ < rhs.low_);
  50. }
  51. value_type low() const {
  52. return low_;
  53. }
  54. value_type high() const {
  55. return high_;
  56. }
  57. friend std::ostream& operator<<(std::ostream& o, value_interval const v) {
  58. o << '[' << v.low() << ',' << v.high() << ']';
  59. return o;
  60. }
  61. private:
  62. value_type low_;
  63. value_type high_;
  64. };
  65. public:
  66. /**
  67. * @brief Create value_allocator
  68. * The allocator has [lowest, highest] values.
  69. * @param lowest The lowest value
  70. * @param highest The highest value.
  71. */
  72. value_allocator(value_type lowest, value_type highest)
  73. :lowest_{lowest}, highest_{highest} {
  74. BOOST_ASSERT(std::numeric_limits<value_type>::min() <= lowest);
  75. BOOST_ASSERT(highest <= std::numeric_limits<value_type>::max());
  76. // create one interval that contains whole values
  77. pool_.emplace(lowest_, highest_);
  78. }
  79. /**
  80. * @brief Allocate one value.
  81. * @return If allocator has at least one value, then returns lowest value, otherwise return nullopt.
  82. */
  83. optional<value_type> allocate() {
  84. if (pool_.empty()) return nullopt;
  85. // The smallest interval is the target.
  86. auto it = pool_.begin();
  87. value_type value = it->low();
  88. if (it->low() + 1 <= it->high()) {
  89. // If the interval contains other value, then update the interval.
  90. pool_.modify(
  91. it,
  92. [&](auto& e) {
  93. BOOST_ASSERT(it->low() < highest_);
  94. e = value_interval{value_type(it->low() + 1) , it->high()};
  95. }
  96. );
  97. }
  98. else {
  99. pool_.erase(it);
  100. }
  101. return value;
  102. }
  103. /**
  104. * @brief Get the first vacant value.
  105. * @return If allocator has at least one vacant value, then returns lowest value, otherwise return nullopt.
  106. */
  107. optional<value_type> first_vacant() const {
  108. if (pool_.empty()) return nullopt;
  109. // The smallest interval is the target.
  110. auto it = pool_.begin();
  111. return it->low();
  112. }
  113. /**
  114. * @brief Dellocate one value.
  115. * @param value value to deallocate. The value must be gotten by allocate() or declared by use().
  116. */
  117. void deallocate(value_type value) {
  118. BOOST_ASSERT(lowest_ <= value && value <= highest_);
  119. auto itr = pool_.upper_bound(value_interval{value});
  120. if (itr == pool_.end()) {
  121. // ..... v
  122. if (itr == pool_.begin()) {
  123. // value is fully allocated
  124. pool_.emplace(value);
  125. return;
  126. }
  127. auto itl = itr;
  128. --itl;
  129. if (itl->high() + 1 == value) { // Can concat to the left interval
  130. // Concat left
  131. pool_.modify(
  132. itl,
  133. [&](auto& e) {
  134. e = value_interval{itl->low(), value};
  135. }
  136. );
  137. }
  138. else {
  139. // No concat
  140. pool_.emplace(value);
  141. }
  142. }
  143. else if (itr == pool_.begin()) {
  144. // v .....
  145. if (value + 1 == itr->low()) { // Can concat to the right interval
  146. // Concat right
  147. pool_.modify(
  148. itr,
  149. [&](auto& e) {
  150. e = value_interval{value, itr->high()};
  151. }
  152. );
  153. }
  154. else {
  155. // No concat
  156. pool_.emplace(value);
  157. }
  158. }
  159. else {
  160. // .. v ..
  161. auto itl = itr;
  162. --itl;
  163. if (itl->high() + 1 == value) { // Can concat to the left interval
  164. if (value + 1 == itr->low()) { // Can concat to the right interval
  165. // Concat both
  166. auto right = itr->high();
  167. pool_.erase(itr);
  168. pool_.modify(
  169. itl,
  170. [&](auto& e) {
  171. e = value_interval{itl->low(), right};
  172. }
  173. );
  174. }
  175. else {
  176. // Concat left
  177. pool_.modify(
  178. itl,
  179. [&](auto& e) {
  180. e = value_interval{itl->low(), value};
  181. }
  182. );
  183. }
  184. }
  185. else {
  186. if (value + 1 == itr->low()) { // Can concat to the right interval
  187. // Concat right
  188. pool_.modify(
  189. itr,
  190. [&](auto& e) {
  191. e = value_interval{value, itr->high()};
  192. }
  193. );
  194. }
  195. else {
  196. // No concat
  197. pool_.emplace(value);
  198. }
  199. }
  200. }
  201. }
  202. /**
  203. * @brief Declare the value as used.
  204. * @param value The value to declare using
  205. * @return If value is not used or allocated then true, otherwise false
  206. */
  207. bool use(value_type value) {
  208. auto it = pool_.find(value_interval{value});
  209. if (it == pool_.end()) return false;
  210. value_interval iv = *it;
  211. pool_.erase (it);
  212. if (iv.low() < value) {
  213. pool_.emplace(iv.low(), value - 1);
  214. }
  215. if (value + 1 <= iv.high()) {
  216. pool_.emplace(value + 1, iv.high());
  217. }
  218. return true;
  219. }
  220. /**
  221. * @brief Clear all allocated or used values.
  222. */
  223. void clear() {
  224. pool_.clear();
  225. pool_.emplace(lowest_, highest_);
  226. }
  227. std::size_t interval_count() const {
  228. return pool_.size();
  229. }
  230. std::ostream& dump(std::ostream& o) {
  231. for (auto const& e : pool_) {
  232. o << e;
  233. }
  234. return o;
  235. }
  236. private:
  237. using mi_value_interval = mi::multi_index_container<
  238. value_interval,
  239. mi::indexed_by<
  240. mi::ordered_unique<
  241. mi::identity<value_interval>
  242. >
  243. >
  244. >;
  245. mi_value_interval pool_;
  246. value_type lowest_;
  247. value_type highest_;
  248. };
  249. } // namespace MQTT_NS
  250. #endif // MQTT_VALUE_ALLOCATOR_HPP