FCV.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_FCV_HPP
  14. #define ZT_FCV_HPP
  15. #include "Constants.hpp"
  16. #include <iterator>
  17. #include <algorithm>
  18. #include <memory>
  19. #include <stdexcept>
  20. namespace ZeroTier {
  21. /**
  22. * FCV is a Fixed Capacity Vector
  23. *
  24. * This doesn't implement everything in std::vector, just what we need. It
  25. * also adds a few special things for use in ZT core code.
  26. *
  27. * @tparam T Type to contain
  28. * @tparam C Maximum capacity of vector
  29. */
  30. template< typename T, unsigned int C >
  31. class FCV
  32. {
  33. public:
  34. typedef T *iterator;
  35. typedef const T *const_iterator;
  36. ZT_INLINE FCV() noexcept: _s(0)
  37. {}
  38. ZT_INLINE FCV(const FCV &v) : _s(0)
  39. { *this = v; }
  40. ZT_INLINE FCV(const T *const contents, const unsigned int len) :
  41. _s(len)
  42. {
  43. const unsigned int l = std::min(len, C);
  44. for (unsigned int i = 0; i < l; ++i)
  45. new(reinterpret_cast<T *>(_m) + i) T(contents[i]);
  46. }
  47. template< typename I >
  48. ZT_INLINE FCV(I i, I end) :
  49. _s(0)
  50. {
  51. while (i != end) {
  52. push_back(*i);
  53. ++i;
  54. }
  55. }
  56. ZT_INLINE ~FCV()
  57. { this->clear(); }
  58. ZT_INLINE FCV &operator=(const FCV &v)
  59. {
  60. if (likely(&v != this)) {
  61. this->clear();
  62. const unsigned int s = v._s;
  63. _s = s;
  64. for (unsigned int i = 0; i < s; ++i)
  65. new(reinterpret_cast<T *>(_m) + i) T(*(reinterpret_cast<const T *>(v._m) + i));
  66. }
  67. return *this;
  68. }
  69. /**
  70. * Clear this vector, destroying all content objects
  71. */
  72. ZT_INLINE void clear()
  73. {
  74. const unsigned int s = _s;
  75. _s = 0;
  76. for (unsigned int i = 0; i < s; ++i)
  77. (reinterpret_cast<T *>(_m) + i)->~T();
  78. }
  79. /**
  80. * Move contents from this vector to another and clear this vector.
  81. *
  82. * @param v Target vector
  83. */
  84. ZT_INLINE void unsafeMoveTo(FCV &v) noexcept
  85. {
  86. Utils::copy(v._m, _m, (v._s = _s) * sizeof(T));
  87. _s = 0;
  88. }
  89. ZT_INLINE iterator begin() noexcept
  90. { return reinterpret_cast<T *>(_m); }
  91. ZT_INLINE iterator end() noexcept
  92. { return reinterpret_cast<T *>(_m) + _s; }
  93. ZT_INLINE const_iterator begin() const noexcept
  94. { return reinterpret_cast<const T *>(_m); }
  95. ZT_INLINE const_iterator end() const noexcept
  96. { return reinterpret_cast<const T *>(_m) + _s; }
  97. ZT_INLINE T &operator[](const unsigned int i)
  98. {
  99. if (likely(i < _s))
  100. return reinterpret_cast<T *>(_m)[i];
  101. throw Utils::OutOfRangeException;
  102. }
  103. ZT_INLINE const T &operator[](const unsigned int i) const
  104. {
  105. if (likely(i < _s))
  106. return reinterpret_cast<const T *>(_m)[i];
  107. throw Utils::OutOfRangeException;
  108. }
  109. static constexpr unsigned int capacity() noexcept
  110. { return C; }
  111. ZT_INLINE unsigned int size() const noexcept
  112. { return _s; }
  113. ZT_INLINE bool empty() const noexcept
  114. { return (_s == 0); }
  115. ZT_INLINE T *data() noexcept
  116. { return reinterpret_cast<T *>(_m); }
  117. ZT_INLINE const T *data() const noexcept
  118. { return reinterpret_cast<const T *>(_m); }
  119. /**
  120. * Push a value onto the back of this vector
  121. *
  122. * If the vector is at capacity this silently fails.
  123. *
  124. * @param v Value to push
  125. */
  126. ZT_INLINE void push_back(const T &v)
  127. {
  128. if (likely(_s < C))
  129. new(reinterpret_cast<T *>(_m) + _s++) T(v);
  130. else throw Utils::OutOfRangeException;
  131. }
  132. /**
  133. * Push new default value or return last in vector if full.
  134. *
  135. * @return Reference to new item
  136. */
  137. ZT_INLINE T &push()
  138. {
  139. if (likely(_s < C)) {
  140. return *(new(reinterpret_cast<T *>(_m) + _s++) T());
  141. } else {
  142. return *(reinterpret_cast<T *>(_m) + (C - 1));
  143. }
  144. }
  145. /**
  146. * Push new default value or replace and return last in vector if full.
  147. *
  148. * @return Reference to new item
  149. */
  150. ZT_INLINE T &push(const T &v)
  151. {
  152. if (likely(_s < C)) {
  153. return *(new(reinterpret_cast<T *>(_m) + _s++) T(v));
  154. } else {
  155. T &tmp = *(reinterpret_cast<T *>(_m) + (C - 1));
  156. tmp = v;
  157. return tmp;
  158. }
  159. }
  160. /**
  161. * Remove the last element if this vector is not empty
  162. */
  163. ZT_INLINE void pop_back()
  164. {
  165. if (likely(_s != 0))
  166. (reinterpret_cast<T *>(_m) + --_s)->~T();
  167. }
  168. /**
  169. * Resize vector
  170. *
  171. * @param ns New size (clipped to C if larger than capacity)
  172. */
  173. ZT_INLINE void resize(unsigned int ns)
  174. {
  175. if (unlikely(ns > C))
  176. throw Utils::OutOfRangeException;
  177. unsigned int s = _s;
  178. while (s < ns)
  179. new(reinterpret_cast<T *>(_m) + s++) T();
  180. while (s > ns)
  181. (reinterpret_cast<T *>(_m) + --s)->~T();
  182. _s = s;
  183. }
  184. /**
  185. * Set the size of this vector without otherwise changing anything
  186. *
  187. * @param ns New size
  188. */
  189. ZT_INLINE void unsafeSetSize(unsigned int ns)
  190. { _s = ns; }
  191. /**
  192. * This is a bounds checked auto-resizing variant of the [] operator
  193. *
  194. * If 'i' is out of bounds vs the current size of the vector, the vector is
  195. * resized. If that size would exceed C (capacity), 'i' is clipped to C-1.
  196. *
  197. * @param i Index to obtain as a reference, resizing if needed
  198. * @return Reference to value at this index
  199. */
  200. ZT_INLINE T &at(unsigned int i)
  201. {
  202. if (i >= _s) {
  203. if (unlikely(i >= C))
  204. i = C - 1;
  205. do {
  206. new(reinterpret_cast<T *>(_m) + _s++) T();
  207. } while (i >= _s);
  208. }
  209. return *(reinterpret_cast<T *>(_m) + i);
  210. }
  211. /**
  212. * Assign this vector's contents from a range of pointers or iterators
  213. *
  214. * If the range is larger than C it is truncated at C.
  215. *
  216. * @tparam X Inferred type of interators or pointers
  217. * @param start Starting iterator
  218. * @param end Ending iterator (must be greater than start)
  219. */
  220. template< typename X >
  221. ZT_INLINE void assign(X start, const X &end)
  222. {
  223. const int l = std::min((int)std::distance(start, end), (int)C);
  224. if (l > 0) {
  225. this->resize((unsigned int)l);
  226. for (int i = 0; i < l; ++i)
  227. reinterpret_cast<T *>(_m)[i] = *(start++);
  228. } else {
  229. this->clear();
  230. }
  231. }
  232. ZT_INLINE bool operator==(const FCV &v) const noexcept
  233. {
  234. if (_s == v._s) {
  235. for (unsigned int i = 0; i < _s; ++i) {
  236. if (!(*(reinterpret_cast<const T *>(_m) + i) == *(reinterpret_cast<const T *>(v._m) + i)))
  237. return false;
  238. }
  239. return true;
  240. }
  241. return false;
  242. }
  243. ZT_INLINE bool operator!=(const FCV &v) const noexcept
  244. { return (!(*this == v)); }
  245. ZT_INLINE bool operator<(const FCV &v) const noexcept
  246. { return std::lexicographical_compare(begin(), end(), v.begin(), v.end()); }
  247. ZT_INLINE bool operator>(const FCV &v) const noexcept
  248. { return (v < *this); }
  249. ZT_INLINE bool operator<=(const FCV &v) const noexcept
  250. { return !(v < *this); }
  251. ZT_INLINE bool operator>=(const FCV &v) const noexcept
  252. { return !(*this < v); }
  253. private:
  254. #ifdef _MSC_VER
  255. uint8_t _m[sizeof(T) * C];
  256. #else
  257. __attribute__((aligned(16))) uint8_t _m[sizeof(T) * C];
  258. #endif
  259. unsigned int _s;
  260. };
  261. } // namespace ZeroTier
  262. #endif