FCV.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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: 2024-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 <cstring>
  20. #include <cstdlib>
  21. namespace ZeroTier {
  22. /**
  23. * FCV is a Fixed Capacity Vector
  24. *
  25. * Attempts to resize, push, or access this vector beyond its capacity will
  26. * silently fail. The [] operator is NOT bounds checked!
  27. *
  28. * This doesn't implement everything in std::vector, just what we need. It
  29. * also adds a few special things for use in ZT core code.
  30. *
  31. * Note that an FCV will be TriviallyCopyable IF and only if its contained
  32. * type is TriviallyCopyable. There's a const static checker for this.
  33. *
  34. * @tparam T Type to contain
  35. * @tparam C Maximum capacity of vector
  36. */
  37. template<typename T,unsigned int C>
  38. class FCV
  39. {
  40. public:
  41. typedef T * iterator;
  42. typedef const T * const_iterator;
  43. /**
  44. * @return True if this FCV is trivially copyable, which means its type is also.
  45. */
  46. static constexpr bool isTriviallyCopyable() noexcept { return isTriviallyCopyable(reinterpret_cast<const T *>(0)); }
  47. ZT_INLINE FCV() noexcept : _s(0) {}
  48. ZT_INLINE FCV(const FCV &v) : _s(0) { *this = v; }
  49. ZT_INLINE ~FCV() { this->clear(); }
  50. ZT_INLINE FCV &operator=(const FCV &v)
  51. {
  52. if (&v != this) {
  53. this->clear();
  54. const unsigned int s = v._s;
  55. _s = s;
  56. for (unsigned int i=0;i<s;++i)
  57. new(reinterpret_cast<T *>(_m) + i) T(*(reinterpret_cast<const T *>(v._m) + i));
  58. }
  59. return *this;
  60. }
  61. /**
  62. * Clear this vector, destroying all content objects
  63. */
  64. ZT_INLINE void clear()
  65. {
  66. const unsigned int s = _s;
  67. _s = 0;
  68. for(unsigned int i=0;i<s;++i)
  69. (reinterpret_cast<T *>(_m) + i)->~T();
  70. }
  71. /**
  72. * Clear without calling destructors (same as unsafeResize(0))
  73. */
  74. ZT_INLINE void unsafeClear() noexcept { _s = 0; }
  75. /**
  76. * This does a straight copy of one vector's data to another
  77. *
  78. * @tparam C2 Inferred capacity of other vector
  79. * @param v Other vector to copy to this one
  80. */
  81. template<unsigned int C2>
  82. ZT_INLINE void unsafeAssign(const FCV<T,C2> &v) noexcept
  83. {
  84. _s = ((C2 > C)&&(v._s > C)) ? C : v._s;
  85. Utils::copy(_m,v._m,_s * sizeof(T));
  86. }
  87. /**
  88. * Move contents from this vector to another and clear this vector
  89. *
  90. * @param v Target vector
  91. */
  92. ZT_INLINE void unsafeMoveTo(FCV &v) noexcept
  93. {
  94. Utils::copy(v._m,_m,(v._s = _s) * sizeof(T));
  95. _s = 0;
  96. }
  97. ZT_INLINE iterator begin() noexcept { return reinterpret_cast<T *>(_m); }
  98. ZT_INLINE const_iterator begin() const noexcept { return reinterpret_cast<const T *>(_m); }
  99. ZT_INLINE iterator end() noexcept { return reinterpret_cast<T *>(_m) + _s; }
  100. ZT_INLINE const_iterator end() const noexcept { return reinterpret_cast<const T *>(_m) + _s; }
  101. ZT_INLINE T &operator[](const unsigned int i) noexcept { return reinterpret_cast<T *>(_m)[i]; }
  102. ZT_INLINE const T &operator[](const unsigned int i) const noexcept { return reinterpret_cast<T *>(_m)[i]; }
  103. ZT_INLINE unsigned int size() const noexcept { return _s; }
  104. ZT_INLINE bool empty() const noexcept { return (_s == 0); }
  105. static constexpr unsigned int capacity() noexcept { return C; }
  106. /**
  107. * Push a value onto the back of this vector
  108. *
  109. * If the vector is at capacity this silently fails.
  110. *
  111. * @param v Value to push
  112. */
  113. ZT_INLINE void push_back(const T &v)
  114. {
  115. if (_s < C)
  116. new (reinterpret_cast<T *>(_m) + _s++) T(v);
  117. }
  118. /**
  119. * Push a new value onto the vector and return it, or return last item if capacity is reached
  120. *
  121. * @return Reference to new item
  122. */
  123. ZT_INLINE T &push()
  124. {
  125. if (_s < C) {
  126. return *(new(reinterpret_cast<T *>(_m) + _s++) T());
  127. } else {
  128. return *(reinterpret_cast<T *>(_m) + (C - 1));
  129. }
  130. }
  131. /**
  132. * Push a new value onto the vector and return it, or return last item if capacity is reached
  133. *
  134. * @return Reference to new item
  135. */
  136. ZT_INLINE T &push(const T &v)
  137. {
  138. if (_s < C) {
  139. return *(new(reinterpret_cast<T *>(_m) + _s++) T(v));
  140. } else {
  141. T &tmp = *(reinterpret_cast<T *>(_m) + (C - 1));
  142. tmp = v;
  143. return tmp;
  144. }
  145. }
  146. /**
  147. * Remove the last element if this vector is not empty
  148. */
  149. ZT_INLINE void pop_back()
  150. {
  151. if (_s != 0)
  152. (reinterpret_cast<T *>(_m) + --_s)->~T();
  153. }
  154. /**
  155. * Resize vector
  156. *
  157. * @param ns New size (clipped to C if larger than capacity)
  158. */
  159. ZT_INLINE void resize(unsigned int ns)
  160. {
  161. if (ns > C)
  162. ns = C;
  163. unsigned int s = _s;
  164. while (s < ns)
  165. new(reinterpret_cast<T *>(_m) + s++) T();
  166. while (s > ns)
  167. (reinterpret_cast<T *>(_m) + --s)->~T();
  168. _s = s;
  169. }
  170. /**
  171. * Resize without calling any constructors or destructors on T
  172. *
  173. * This must only be called if T is a primitive type or is TriviallyCopyable and
  174. * safe to initialize from undefined contents.
  175. *
  176. * @param ns New size (clipped to C if larger than capacity)
  177. */
  178. ZT_INLINE void unsafeResize(const unsigned int ns) noexcept { _s = (ns > C) ? C : ns; }
  179. /**
  180. * This is a bounds checked auto-resizing variant of the [] operator
  181. *
  182. * If 'i' is out of bounds vs the current size of the vector, the vector is
  183. * resized. If that size would exceed C (capacity), 'i' is clipped to C-1.
  184. *
  185. * @param i Index to obtain as a reference, resizing if needed
  186. * @return Reference to value at this index
  187. */
  188. ZT_INLINE T &at(unsigned int i)
  189. {
  190. if (i >= _s) {
  191. if (unlikely(i >= C))
  192. i = C - 1;
  193. do {
  194. new(reinterpret_cast<T *>(_m) + _s++) T();
  195. } while (i >= _s);
  196. }
  197. return *(reinterpret_cast<T *>(_m) + i);
  198. }
  199. /**
  200. * Assign this vector's contents from a range of pointers or iterators
  201. *
  202. * If the range is larger than C it is truncated at C.
  203. *
  204. * @tparam X Inferred type of interators or pointers
  205. * @param start Starting iterator
  206. * @param end Ending iterator (must be greater than start)
  207. */
  208. template<typename X>
  209. ZT_INLINE void assign(X start,const X &end)
  210. {
  211. const int l = std::min((int)std::distance(start,end),(int)C);
  212. if (l > 0) {
  213. this->resize((unsigned int)l);
  214. for(int i=0;i<l;++i)
  215. reinterpret_cast<T *>(_m)[i] = *(start++);
  216. } else {
  217. this->clear();
  218. }
  219. }
  220. ZT_INLINE bool operator==(const FCV &v) const noexcept
  221. {
  222. if (_s == v._s) {
  223. for(unsigned int i=0;i<_s;++i) {
  224. if (!(*(reinterpret_cast<const T *>(_m) + i) == *(reinterpret_cast<const T *>(v._m) + i)))
  225. return false;
  226. }
  227. return true;
  228. }
  229. return false;
  230. }
  231. ZT_INLINE bool operator!=(const FCV &v) const noexcept { return (!(*this == v)); }
  232. ZT_INLINE bool operator<(const FCV &v) const noexcept { return std::lexicographical_compare(begin(),end(),v.begin(),v.end()); }
  233. ZT_INLINE bool operator>(const FCV &v) const noexcept { return (v < *this); }
  234. ZT_INLINE bool operator<=(const FCV &v) const noexcept { return !(v < *this); }
  235. ZT_INLINE bool operator>=(const FCV &v) const noexcept { return !(*this < v); }
  236. private:
  237. unsigned int _s;
  238. uint8_t _m[sizeof(T) * C];
  239. };
  240. } // namespace ZeroTier
  241. #endif