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