range.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #pragma once
  2. #include <library/cpp/yt/assert/assert.h>
  3. #include <library/cpp/yt/misc/hash.h>
  4. #include <array>
  5. #include <initializer_list>
  6. #include <optional>
  7. #include <span>
  8. #include <vector>
  9. #include <type_traits>
  10. // For size_t.
  11. #include <stddef.h>
  12. namespace google::protobuf {
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // Forward declarations
  15. template <class T>
  16. class RepeatedField;
  17. template <class T>
  18. class RepeatedPtrField;
  19. ////////////////////////////////////////////////////////////////////////////////
  20. } // namespace google::protobuf
  21. namespace NYT {
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // Forward declarations
  24. template <class T, size_t N>
  25. class TCompactVector;
  26. ////////////////////////////////////////////////////////////////////////////////
  27. //! TRange (inspired by TArrayRef from LLVM)
  28. /*!
  29. * Represents a constant reference to an array (zero or more elements
  30. * consecutively in memory), i. e. a start pointer and a length. It allows
  31. * various APIs to take consecutive elements easily and conveniently.
  32. *
  33. * This class does not own the underlying data, it is expected to be used in
  34. * situations where the data resides in some other buffer, whose lifetime
  35. * extends past that of the TRange. For this reason, it is not in general
  36. * safe to store an TRange.
  37. *
  38. * This is intended to be trivially copyable, so it should be passed by
  39. * value.
  40. */
  41. template <class T>
  42. class TRange
  43. {
  44. public:
  45. using iterator = const T*;
  46. using const_iterator = const T*;
  47. using size_type = size_t;
  48. static constexpr bool CAnyRepeatedField = requires {
  49. { google::protobuf::RepeatedField<T>{} }
  50. ->
  51. std::same_as<google::protobuf::RepeatedField<T>>;
  52. };
  53. //! Constructs a null TRange.
  54. TRange()
  55. : Data_(nullptr)
  56. , Length_(0)
  57. { }
  58. //! Constructs a TRange from a pointer and length.
  59. TRange(const T* data, size_t length)
  60. : Data_(data)
  61. , Length_(length)
  62. { }
  63. //! Constructs a TRange from a range.
  64. TRange(const T* begin, const T* end)
  65. : Data_(begin)
  66. , Length_(end - begin)
  67. { }
  68. //! Constructs a TRange from a TCompactVector.
  69. template <size_t N>
  70. TRange(const TCompactVector<T, N>& elements)
  71. : Data_(elements.data())
  72. , Length_(elements.size())
  73. { }
  74. //! Constructs a TRange from an std::vector.
  75. template <class A>
  76. TRange(const std::vector<T, A>& elements)
  77. : Data_(elements.data())
  78. , Length_(elements.size())
  79. { }
  80. //! Constructs a TRange from an std::span.
  81. TRange(const std::span<T>& elements)
  82. : Data_(elements.data())
  83. , Length_(elements.size())
  84. { }
  85. //! Constructs a TRange from a C array.
  86. template <size_t N>
  87. TRange(const T (&elements)[N])
  88. : Data_(elements)
  89. , Length_(N)
  90. { }
  91. //! Constructs a TRange from std::initializer_list.
  92. TRange(std::initializer_list<T> elements)
  93. : Data_(elements.begin())
  94. , Length_(elements.size())
  95. { }
  96. //! Constructs a TRange from std::array.
  97. template <size_t N>
  98. TRange(const std::array<T, N>& elements)
  99. : Data_(elements.data())
  100. , Length_(N)
  101. { }
  102. //! Constructs a TRange from std::optional.
  103. //! Range will contain 0-1 elements.
  104. explicit TRange(const std::optional<T>& element)
  105. : Data_(element ? &*element : nullptr)
  106. , Length_(element ? 1 : 0)
  107. { }
  108. //! Constructs a TRange from RepeatedField.
  109. template<std::same_as<T> U = T>
  110. requires CAnyRepeatedField
  111. TRange(const google::protobuf::RepeatedField<U>& elements)
  112. : Data_(elements.data())
  113. , Length_(elements.size())
  114. { }
  115. //! Constructs a TRange from RepeatedPtrField.
  116. template<class TPointed>
  117. TRange(const google::protobuf::RepeatedPtrField<TPointed>& elements)
  118. : Data_(elements.data())
  119. , Length_(elements.size())
  120. { }
  121. const_iterator Begin() const
  122. {
  123. return Data_;
  124. }
  125. // STL interop, for gcc.
  126. const_iterator begin() const
  127. {
  128. return Begin();
  129. }
  130. const_iterator End() const
  131. {
  132. return Data_ + Length_;
  133. }
  134. // STL interop, for gcc.
  135. const_iterator end() const
  136. {
  137. return End();
  138. }
  139. bool Empty() const
  140. {
  141. return Length_ == 0;
  142. }
  143. bool empty() const
  144. {
  145. return Empty();
  146. }
  147. explicit operator bool() const
  148. {
  149. return Data_ != nullptr;
  150. }
  151. size_t Size() const
  152. {
  153. return Length_;
  154. }
  155. size_t size() const
  156. {
  157. return Size();
  158. }
  159. const T* Data() const
  160. {
  161. return Data_;
  162. }
  163. const T* data() const
  164. {
  165. return Data();
  166. }
  167. const T& operator[](size_t index) const
  168. {
  169. YT_ASSERT(index < Size());
  170. return Data_[index];
  171. }
  172. const T& Front() const
  173. {
  174. YT_ASSERT(Length_ > 0);
  175. return Data_[0];
  176. }
  177. const T& Back() const
  178. {
  179. YT_ASSERT(Length_ > 0);
  180. return Data_[Length_ - 1];
  181. }
  182. TRange<T> Slice(size_t startOffset, size_t endOffset) const
  183. {
  184. YT_ASSERT(startOffset <= endOffset && endOffset <= Size());
  185. return TRange<T>(Begin() + startOffset, endOffset - startOffset);
  186. }
  187. std::vector<T> ToVector() const
  188. {
  189. return std::vector<T>(Data_, Data_ + Length_);
  190. }
  191. protected:
  192. //! The start of the array, in an external buffer.
  193. const T* Data_;
  194. //! The number of elements.
  195. size_t Length_;
  196. };
  197. // STL interop.
  198. template <class T>
  199. typename TRange<T>::const_iterator begin(TRange<T> ref)
  200. {
  201. return ref.Begin();
  202. }
  203. template <class T>
  204. typename TRange<T>::const_iterator end(TRange<T> ref)
  205. {
  206. return ref.End();
  207. }
  208. // Deduction guide for the constructor from RepeatedPtrField.
  209. template<class TPointed>
  210. TRange(const google::protobuf::RepeatedPtrField<TPointed>& elements) -> TRange<const TPointed*>;
  211. ////////////////////////////////////////////////////////////////////////////////
  212. template <class U, class T>
  213. TRange<U> ReinterpretCastRange(TRange<T> range)
  214. {
  215. static_assert(sizeof(T) == sizeof(U), "T and U must have equal sizes.");
  216. return TRange<U>(reinterpret_cast<const U*>(range.Begin()), range.Size());
  217. }
  218. ////////////////////////////////////////////////////////////////////////////////
  219. // TMutableRange (inspired by TMutableArrayRef from LLVM)
  220. /*
  221. * Represents a mutable reference to an array (zero or more elements
  222. * consecutively in memory), i. e. a start pointer and a length.
  223. * It allows various APIs to take and modify consecutive elements easily and
  224. * conveniently.
  225. *
  226. * This class does not own the underlying data, it is expected to be used in
  227. * situations where the data resides in some other buffer, whose lifetime
  228. * extends past that of the TMutableRange. For this reason, it is not in
  229. * general safe to store a TMutableRange.
  230. *
  231. * This is intended to be trivially copyable, so it should be passed by value.
  232. */
  233. template <class T>
  234. class TMutableRange
  235. : public TRange<T>
  236. {
  237. public:
  238. using iterator = T*;
  239. //! Constructs a null TMutableRange.
  240. TMutableRange()
  241. { }
  242. //! Constructs a TMutableRange from a pointer and length.
  243. TMutableRange(T* data, size_t length)
  244. : TRange<T>(data, length)
  245. { }
  246. //! Constructs a TMutableRange from a range.
  247. TMutableRange(T* begin, T* end)
  248. : TRange<T>(begin, end)
  249. { }
  250. //! Constructs a TMutableRange from a TCompactVector.
  251. template <size_t N>
  252. TMutableRange(TCompactVector<T, N>& elements)
  253. : TRange<T>(elements)
  254. { }
  255. //! Constructs a TMutableRange from an std::vector.
  256. TMutableRange(std::vector<T>& elements)
  257. : TRange<T>(elements)
  258. { }
  259. //! Constructs a TMutableRange from an std::span.
  260. TMutableRange(std::span<T>& elements)
  261. : TRange<T>(elements)
  262. { }
  263. //! Constructs a TMutableRange from std::array.
  264. template <size_t N>
  265. TMutableRange(std::array<T, N>& elements)
  266. : TRange<T>(elements.data(), N)
  267. { }
  268. //! Construct a TMutableRange from an std::optional
  269. //! Range will contain 0-1 elements.
  270. explicit TMutableRange(std::optional<T>& optional)
  271. : TRange<T>(optional)
  272. { }
  273. //! Constructs a TMutableRange from a C array.
  274. template <size_t N>
  275. TMutableRange(T (& elements)[N])
  276. : TRange<T>(elements)
  277. { }
  278. //! Constructs a TMutableRange from RepeatedField.
  279. template<std::same_as<T> U = T>
  280. requires TRange<T>::CAnyRepeatedField
  281. TMutableRange(google::protobuf::RepeatedField<U>& elements)
  282. : TRange<T>(elements)
  283. { }
  284. //! Constructs a TMutableRange from RepeatedPtrField.
  285. template<class TPointed>
  286. TMutableRange(google::protobuf::RepeatedPtrField<TPointed>& elements)
  287. : TRange<T>(elements)
  288. { }
  289. using TRange<T>::Begin;
  290. using TRange<T>::End;
  291. using TRange<T>::Front;
  292. using TRange<T>::Back;
  293. using TRange<T>::operator[];
  294. iterator Begin() const
  295. {
  296. return const_cast<T*>(this->Data_);
  297. }
  298. // STL interop, for gcc.
  299. iterator begin() const
  300. {
  301. return Begin();
  302. }
  303. iterator End() const
  304. {
  305. return this->Begin() + this->Size();
  306. }
  307. // STL interop, for gcc.
  308. iterator end() const
  309. {
  310. return End();
  311. }
  312. T& operator[](size_t index)
  313. {
  314. YT_ASSERT(index <= this->Size());
  315. return Begin()[index];
  316. }
  317. T& Front()
  318. {
  319. YT_ASSERT(this->Length_ > 0);
  320. return Begin()[0];
  321. }
  322. T& Back()
  323. {
  324. YT_ASSERT(this->Length_ > 0);
  325. return Begin()[this->Length_ - 1];
  326. }
  327. TMutableRange<T> Slice(size_t startOffset, size_t endOffset) const
  328. {
  329. YT_ASSERT(startOffset <= endOffset && endOffset <= this->Size());
  330. return TMutableRange<T>(Begin() + startOffset, endOffset - startOffset);
  331. }
  332. TMutableRange<T> Slice(T* begin, T* end) const
  333. {
  334. YT_ASSERT(begin >= Begin());
  335. YT_ASSERT(end <= End());
  336. return TMutableRange<T>(begin, end);
  337. }
  338. };
  339. // STL interop.
  340. template <class T>
  341. typename TMutableRange<T>::iterator begin(TMutableRange<T> ref)
  342. {
  343. return ref.Begin();
  344. }
  345. template <class T>
  346. typename TMutableRange<T>::iterator end(TMutableRange<T> ref)
  347. {
  348. return ref.End();
  349. }
  350. // Deduction guide for the constructor from RepeatedPtrField.
  351. template<class TPointed>
  352. TMutableRange(google::protobuf::RepeatedPtrField<TPointed>& elements) -> TMutableRange<TPointed*>;
  353. ////////////////////////////////////////////////////////////////////////////////
  354. template <class U, class T>
  355. TMutableRange<U> ReinterpretCastMutableRange(TMutableRange<T> range)
  356. {
  357. static_assert(sizeof(T) == sizeof(U), "T and U must have equal sizes.");
  358. return TMutableRange<U>(reinterpret_cast<U*>(range.Begin()), range.Size());
  359. }
  360. ////////////////////////////////////////////////////////////////////////////////
  361. // Mark TMutableRange and TMutableRange as PODs.
  362. namespace NMpl {
  363. template <class T>
  364. struct TIsPod;
  365. template <class T>
  366. struct TIsPod<TRange<T>>
  367. {
  368. static const bool Value = true;
  369. };
  370. template <class T>
  371. struct TIsPod<TMutableRange<T>>
  372. {
  373. static const bool Value = true;
  374. };
  375. } // namespace NMpl
  376. ////////////////////////////////////////////////////////////////////////////////
  377. } // namespace NYT
  378. template <class T>
  379. struct hash<NYT::TRange<T>>
  380. {
  381. size_t operator()(const NYT::TRange<T>& range) const
  382. {
  383. size_t result = 0;
  384. for (const auto& element : range) {
  385. NYT::HashCombine(result, element);
  386. }
  387. return result;
  388. }
  389. };
  390. template <class T>
  391. struct hash<NYT::TMutableRange<T>>
  392. {
  393. size_t operator()(const NYT::TMutableRange<T>& range) const
  394. {
  395. size_t result = 0;
  396. for (const auto& element : range) {
  397. NYT::HashCombine(result, element);
  398. }
  399. return result;
  400. }
  401. };